Tracking the variation of the distance between optimization variable and the optimal solution over the iteration time

71 views (last 30 days)
I have written a code as:
function x_opt = locao2(h, C, BS_positions, alpha,x0)
objective = @(x) -calculatePDF(x(1), x(2), h, C, BS_positions, alpha);
% Constraints: x and y must be within [xb, xf] and [yb, yf] range
lb = [0, 0]; % Adjust based on the actual range
ub = [500, 500]; % Adjust based on the actual range
% Using genetic algorithm for optimization
x_opt = ga(objective,2, [], [], [], [], lb, ub, []);
end
objective and calculatePDF are functions selfmade. I want to track the variation of the distance between optimization variable and the optimum x0 over the iteration time. So who can provide me a total code, thanks very much!

Answers (1)

Matt J
Matt J on 4 Apr 2024 at 15:50
Edited: Matt J on 4 Apr 2024 at 15:53
function x_opt = locao2(h, C, BS_positions, alpha,x0)
objective = @(x) -calculatePDF(x(1), x(2), h, C, BS_positions, alpha);
% Constraints: x and y must be within [xb, xf] and [yb, yf] range
lb = [0, 0]; % Adjust based on the actual range
ub = [500, 500]; % Adjust based on the actual range
% Using genetic algorithm for optimization
options = optimoptions('ga', 'PlotFcn', {@plotdistx0});
x_opt = ga(objective,2, [], [], [], [], lb, ub, [], options);
function state = plotdistx0(options, state, flag)
if(strcmp(flag,'init')) % Set up the plot
xlim([1,options.MaxGenerations]);
axx = gca;
axx.YScale = 'log';
hold on;
xlabel Generation
title('Distance to x0')
end
plot(state.Generation,norm(state.Best(:)-x0(:)),'xr');
end
end
  16 Comments
Matt J
Matt J on 6 Apr 2024 at 13:52
Change the plot function to,
function state = plotdistx0(options, state, flag)
if(strcmp(flag,'init')) % Set up the plot
xlim([1,options.MaxGenerations]);
axx = gca;
axx.YScale = 'log';
hold on;
xlabel Generation
title('Distance to x0')
end
[~,imin]=min(state.Score);
xbest=state.Population(imin,:);
dist= norm( xbest(:) - x0(:);
plot(state.Generation, dist,'xr');
end

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!