How can I write GA output function?

1 view (last 30 days)
b0ka
b0ka on 23 May 2014
Edited: Yaser Khojah on 18 May 2018
Hi,
I need to optimize simple function using GA. The function is y= 140x(1) + 80x(2), with constraints x(1) <= 6000, x(2)<= 5000, 3*x(1) + 2*x(2)<=24 000, 1.5 * x(1) + 5*x(2)<=35000. x(1) & x(2)>0.
My fitness function is:
function y = fitness( x )
y = 140 * x(1) + 80 * x(2);
end
Constraints:
function [c, ceq] = con(x)
c = [3 * x(1) + 2*x(2) - 24000;
1.5 * x(1) + 5*x(2) - 35000;
x(1) - 6000;
x(2) - 5000];
ceq = [];
end
I got good result but I need a graphic result too. I need to show, on graph, my fitness function with constraints and population. I read that custom output function is needed but I don't know how to write it. I found the syntax but I don't understand it, I don't use matlab often.
Thanks!

Answers (1)

Alan Weiss
Alan Weiss on 23 May 2014
You can use a built-in plot function such as @gaplotbestf to plot the fitness as a function of iteration. This will also plot the mean fitness of the population at the same time. And @gaplotmaxconstr plots the maximum constraint violation as a function of iteration.
You can include both of these plot functions by setting options like this:
options = gaoptimset('PlotFcns',{@gaplotbestf,@gaplotmaxconstr});
And if you want to write your own plot function, do it along the lines documented. I suggest that you start by editing an existing plot function.
That said, because you have nonlinear constraints you will see very few iterations, perhaps 5, before ga converges. That is because the nonlinear constraint solver does a huge amount of work for each iteration. Sorry, but there is nothing to do about this. I mean, you will see that your plots have very few points.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Yaser Khojah
Yaser Khojah on 18 May 2018
Edited: Yaser Khojah on 18 May 2018
Dear Alan,
I have been struggling to get the x and fval of each iteration and generation. I have created my own output function but I still do not get what I want. Here is my output function. I'm not sure what is missing and I have been reading all the stuff related to this part but nothing has been working.
if true
% code
function [state,options,optchanged] = gaoutfun(options,state,flag)
persistent history_pop history_Best history_Score
history_pop = [];
history_Best = [];
history_Score = [];
optchanged = false;
switch flag
case 'init'
history_pop = [history_pop;state.Population];
assignin('base','gapopulationhistory',history_pop);
history_Best = [history_Best;state.Best];
assignin('base','gabesthistory',history_Best);
history_Score = [history_Score;state.Score];
assignin('base','gascorehistory',history_Score);
case 'iter'
history_pop = [history_pop;state.Population];
assignin('base','gapopulationhistory',history_pop);
history_Best = [history_Best;state.Best];
assignin('base','gabesthistory',history_Best);
history_Score = [history_Score;state.Score];
assignin('base','gascorehistory',history_Score);
case 'done'
history_pop = [history_pop;state.Population];
assignin('base','gapopulationhistory',history_pop);
history_Best = [history_Best;state.Best];
assignin('base','gabesthistory',history_Best);
history_Score = [history_Score;state.Score];
assignin('base','gascorehistory',history_Score);
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!