how to plot a fitness or objective function with optimum values

13 vues (au cours des 30 derniers jours)
manish kumar
manish kumar le 11 Sep 2019
Commenté : Fabio Freschi le 11 Sep 2019
We want to minimize a simple fitness function of two variables x1 and x2
min f(x) = 100 * (x1^2 - x2) ^2 + (1 - x1)^2;
x
such that the following two nonlinear constraints and bounds are satisfied
x1*x2 + x1 - x2 + 1.5 <=0, (nonlinear constraint)
10 - x1*x2 <=0, (nonlinear constraint)
0 <= x1 <= 1, and (bound)
0 <= x2 <= 13 (bound)
it can be solved by
ObjectiveFunction = @simple_fitness;
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [1 13]; % Upper bound
ConstraintFunction = @simple_constraint;
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ...
ConstraintFunction)
how to plot the objective function with both variables simultaniously with marking the optimim values?

Réponse acceptée

Fabio Freschi
Fabio Freschi le 11 Sep 2019
Modifié(e) : Fabio Freschi le 11 Sep 2019
objFun = @(x)100.*(x(:,1).^2 - x(:,2)).^2 + (1 - x(:,1)).^2;
conFun = @(x)[x(:,1).*x(:,2)+x(:,1)-x(:,2)+1.5, 10-x(:,1).*x(:,2)];
LB = [0 0]; % Lower bound
UB = [1 13]; % Upper bound
% resolution
n = 1000;
% regular grid
x1 = linspace(LB(1),UB(1),n);
x2 = linspace(LB(2),UB(2),n);
[X1,X2] = meshgrid(x1,x2);
x = [X1(:) X2(:)];
% evaluate the fitness
Y = objFun(x);
% correct with constraints
Y(any(conFun(x) > 0,2)) = NaN;
% plot
figure;
hold on
s = surf(X1,X2,reshape(Y,size(X1)));
% correct the boundary values
% maybe remove the grid
s.EdgeColor = 'none';
% run GA
% up to you
% plot the best solution
% plot3(x(1),x(2),fval);
  1 commentaire
Fabio Freschi
Fabio Freschi le 11 Sep 2019
Edit to make the contraint calculation compatible with ga

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 11 Sep 2019
Modifié(e) : Matt J le 11 Sep 2019
Sounds like you want this?
options = optimoptions('ga','PlotFcn',{'gaplotbestf','gaplotbestindiv'});
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ...
ConstraintFunction,[],options);
Or, you can write your own custom plot functions as described here,

Catégories

En savoir plus sur Least Squares dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by