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)
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=1485-minin;
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
to
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' ;
if isfolder(savefolder)==0; mkdir(savefolder); end
Score_gen=state.Score;
Population_gen=state.Population;
Generation_gen=state.Generation;
optchanged=false;
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!