Whats wrong with in my code attached

2 views (last 30 days)
Please see my attached files the issue I am facing is that the value of f that I am calculating via the objective function is not getting displayed instead another value is being displayed and the program is running indefinitely which i dont want

Accepted Answer

Geoff Hayes
Geoff Hayes on 28 Sep 2014
Bhavz - as you have not specified any options (see ga options) then the default number of generations for your optimization problem is 100 multiplied by the number of variables. Since you have 32 variables, then there will be 3200 generations/iterations for this problem, which explains why it appears to be running indefinitely.
To limit the number of generations to (for example) 50, you should be able to do the following in your main.m file
options = gaoptimset('Generations',50);
nonlcon=@constraint;
[x,fval,exitflag] = ga(@objectiveFun,...
32,[],[],[],[],lb,ub,nonlcon, options);
So we just pass the options structure as the last input to the ga function.
NOTE how the above code makes use of the ub variable which you have commented out in main.m. You will need to review this to see what this variable should be set to.
As for the value f from your objective function not being displayed, what do you see instead? Something other than f or just something that you don't expect to be f? The last line in your objectiveFun.m file is
f=ro_max
without a semi-colon, so whenever this function is called, you should be seeing something like the following in the Command Window
f =
<some number>
And you will see something similar to this up to 100 times per generation/iteration since you have not specified a population size and so the default of 100 is used (again, based on an equation that makes use of the number of variables).
  9 Comments
Bhavz
Bhavz on 7 Oct 2014
^ Thankyou so much I cant thankyou enough for all your help ,just one last pointer if I wanted to plot these fval vector values how would i do that ?
Geoff Hayes
Geoff Hayes on 8 Oct 2014
In your options object, try adding the following so that the best fitness (your fval) for each generation is plotted
options = gaoptimset('Generations',50,'PopulationSize',42, ...
'PlotFcns',@gaplotbestf);
See GA plot options for more details.

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!