MATLAB Answers

feval distorted in ga after using nonlinearconstr

18 views (last 30 days)
Tsuwei Tan
Tsuwei Tan on 25 Feb 2021
Commented: Tsuwei Tan on 28 Mar 2021 at 16:59
I am using Genetic Algoritm
[x,fval,exitflag,output,population,scores]=ga(@(x)cost_fun(x),...
nvar,A,b,[],[],lb,ub,@(x)ssp_constraint(x,U,ssp),IntCon,options);
to find the global min with nvar=6. and the options includes a nonlinear constraint function called ssp_constraint. U and ssp are passing parameters.
function [c, ceq]=ssp_constraint(x,U,ssp)
% c is inequality and ceq is equality
U1=U(:,1);U2=U(:,2);U3=U(:,3);U4=U(:,4);U5=U(:,5);U6=U(:,6);
a1=x(1);a2=x(2);a3=x(3);a4=x(4);a5=x(5);a6=x(6);
SSP_r=ssp+a1*U1+a2*U2+a3*U3+a4*U4+a5*U5+a6*U6;
[minin, I]=min(SSP_r);
% c=[I-40;1485-minin];
c=1485-minin;
% True of False
TF1=islocalmin(SSP_restored_1st15day);
ceq=sum(TF1)-1;
end
The issue is if I have two inequality constraints instead of one in this ssp_constraint function, in my case , change
c=1485-minin;
to
c=[I-40;
1485-minin];
then the fval from ga is skewed to a negative value and contiune to search; my objective function cost_fun is written to spit out a positive value, and latter generation should approach from some positve vale to zero.
I am using a High Performace Computer to conduct parallell computation, the options in ga is
options = optimoptions('ga','UseParallel', true, 'UseVectorized', false,...
'MaxTime',3600*12,'MaxGenerations',600,'MaxStallGenerations',600,'PopulationSize',63,...
'PlotFcn',@gaplotbestf,'OutputFcns',@ga_save_each_gen);
I write a fucntion ga_save_each_gen to save each generation
function [state,options,optchanged]=ga_save_each_gen(options,state,flag)
savefolder='result' ; % Save to the subfolder
if isfolder(savefolder)==0; mkdir(savefolder); end
Score_gen=state.Score;
Population_gen=state.Population;
Generation_gen=state.Generation;
optchanged=false;
%ga does not accept changes in options, and ignores optchanged.
save(['./' savefolder '/gen_' num2str(Generation_gen,'%.4d') '.mat'],'Score_gen','Population_gen','Generation_gen')
end
So I should see the saved variable Score_gen which is a vector with the size (63,1) at seach saved gen_xxxx.mat
Please advise what I did wrong? Thank you!
  1 Comment
Tsuwei Tan
Tsuwei Tan on 27 Feb 2021
It seems to me that
[mini, Index ]=min(any_input);
that Index output is not allowed inside the non linear constraint function.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 27 Feb 2021
The main thing I see that looks out of place is that is the role of SSP_restored_1st15day. If it is a variable, I cannot see where it is defined, but regardless it certainly doesn't depend on the input unknown vector x. If the constraints are not a function of x, that can have crazy effects.
Incidentally also, the constraint
min(SSP_r)>=1485
is linear. ga() would deal with linear constraints more effectively if you used the A,b,Aeq,beq inputs to handle them.
  1 Comment
Tsuwei Tan
Tsuwei Tan on 28 Mar 2021 at 16:59
When IntCon is nonempty, Aeq and beq must be an empty entry ([]), and nonlcon must return empty for ceq. For more information on integer programming, see Mixed Integer ga Optimization.
Finally figure out why.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!