Hi,
I have a multiobjective opimization problem and want to solve it with gamultiobj. I want to save the optimized values of my objective functions after every iteration in an array. I found the example code for the output function here: http://de.mathworks.com/help/optim/ug/output-functions.html . But when I apply the example code to my problem, like this:
function [history,searchdir] = runfmincon
FitnessFunction = @needfunction;
numberOfVariables = 8;
lb = zeros(1,8);
ub = [];
% Set up shared variables with OUTFUN
history.x = [];
history.fval = [];
searchdir = [];
options = gaoptimset('PlotFcns',@gaplotpareto,'UseParallel',true,'OutputFcns', @outfun,'PopulationSize',5);
xsol = gamultiobj(FitnessFunction,numberOfVariables,[],[],[],[],lb,[],options);
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
% Concatenate current search direction with
% searchdir.
searchdir = [searchdir;...
optimValues.searchdirection'];
plot(x(1),x(2),'o');
% Label points with iteration number and add title.
% Add .15 to x(1) to separate label from plotted 'o'
text(x(1)+.15,x(2),...
num2str(optimValues.iteration));
title('Sequence of Points Computed by fmincon');
case 'done'
hold off
otherwise
end
end
end
I get the following error:
Error using runfmincon/outfun
Too many output arguments.
Error in gaoutput (line 39)
[state,optnew,changed] = feval(functions{i},options.OutputPlotFcnOptions,state,flag,args{i}{:});
Error in gamultiobjsolve (line 13)
[state,options] = gaoutput(FitnessFcn,options,state,currentState);
Error in gamultiobj (line 274)
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in runfmincon (line 16)
xsol = gamultiobj(FitnessFunction,numberOfVariables,[],[],[],[],lb,[],options);
It would be great, if someone could help me out

 채택된 답변

Alan Weiss
Alan Weiss 2016년 9월 1일

0 개 추천

The genetic algorithm has a different syntax for output functions than Optimization Toolbox functions. The syntax is described here. After you correct your syntax things will most likely work (I didn't check in detail, but everything else looks reasonable).
Alan Weiss
MATLAB mathematical toolbox documentation

댓글 수: 9

Hi I have now changed the sytnax to:
function state = outfun(x,optimValues,state)
optchanged = false;
switch flag
case 'init'
disp('Starting the algorithm');
case {'iter','interrupt'}
disp('Iterating ...')
case 'done'
disp('Performing final task');
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
% Concatenate current search direction with
% searchdir.
searchdir = [searchdir;...
optimValues.searchdirection'];
plot(x(1),x(2),'o');
% Label points with iteration number and add title.
% Add .15 to x(1) to separate label from plotted 'o'
text(x(1)+.15,x(2),...
num2str(optimValues.iteration));
title('Sequence of Points Computed by fmincon');
case 'done'
hold off
otherwise
end
end
But the same error still occurs. I just don't know what I am doing wrong.
The syntax is
[state,options,optchanged] = myfun(options,state,flag)
but you only have state on the left hand side, so you are only defining one output argument in a context that needs three output arguments.
Maximilian Ernst
Maximilian Ernst 2016년 9월 1일
편집: Maximilian Ernst 2016년 9월 1일
Thats true and if I change the syntax to
[state,options,optchanged] = myfun(options,state,flag)
The error does not occur anymore. But then I get an error
Undefined variable "optimValues" or class "optimValues.fval".
what makes sense because I don't pass optimValues anymore to the output function. How can I avoid that error? I just want to store the optimal values after each iteration in the array 'history'.
The error probably comes from this line of code:
history.fval = [history.fval; optimValues.fval];
You will probably also get an error from the line
history.x = [history.x; x];
Instead, use the appropriate values that gamultiobj passes, namely, state.Population. If you want the fitness function of the population, I think that you need to calculate it yourself. You can get the state.Score matrix, but it is up to you to interpret the scores.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Maximilian Ernst
Maximilian Ernst 2016년 9월 2일
편집: Maximilian Ernst 2016년 9월 2일
Thank you for your answer. I was now able to save the scorematrix. I created a file that was update in every iteration by the values of state.Score. I also plotted the pareto front, but the last row of my score matrix does not have the same entries as shown in the pareto front, do you know why that could be?
Alan Weiss
Alan Weiss 2016년 9월 2일
  1. Scores are not the same as objective function values. They are scaled fitness. I told you that you would have to interpret the scores.
  2. The Pareto front is only some of the members of the population, not the entire population.
Alan Weiss
MATLAB mathematical toolbox documentation
Maximilian Ernst
Maximilian Ernst 2016년 9월 2일
편집: Maximilian Ernst 2016년 9월 2일
Thank you very much for your answer. May I ask, if there is a way to get the objective function values from the score? I have read the documentation about scaled fitness and there it says the objective function values are somehow proportional to the score by a factor sqrt(r),where r is the rank. Can this be used to obtain the objective function values from the scores? Or is there anaother way to get the objective function values?
Alan Weiss
Alan Weiss 2016년 9월 2일
You can get the entire population at each iteration from the state.Population field. You can then get the fitness function values by evaluating the fitness functions on the population. There is no other way, as I already told you: "If you want the fitness function of the population, I think that you need to calculate it yourself."
Alan Weiss
MATLAB mathematical toolbox documentation
Maximilian Ernst
Maximilian Ernst 2016년 9월 2일
ok thank you very much

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2016년 9월 1일

0 개 추천

댓글 수: 1

Maximilian Ernst
Maximilian Ernst 2016년 9월 1일
편집: Maximilian Ernst 2016년 9월 1일
Hi, I read your answer but I still don't know, what I did wrong. I am using a nested output function as you suggested. Actually I am using almost exactly the same code as in the mathworks example which link I posted in my question. The example works fine, so maybe it has something to do with the different solver? (I am using gamultiobj and not fmincon like in the example)

댓글을 달려면 로그인하십시오.

Modess Seyednezhad
Modess Seyednezhad 2020년 5월 31일
편집: Modess Seyednezhad 2020년 5월 31일

0 개 추천

Hi,
I have the same issue. The syntaxt page was not useful for me. I could not find my error.
function [history,searchdir] = runmultiobjfunc
% Set up shared variables with OUTFUN
history.x = [];
history.fval = [];
searchdir = [];
% call optimization
nvar = 2;
Lb = [1, 3];
Ub = [19.2, 6.27];
options = optimoptions(@gamultiobj,'OutputFcn',@outfun,...
'Display','iter', 'MaxGenerations',200,'PopulationSize',50);
xsol = gamultiobj(@objfun,nvar,[],[],[],[],Lb,Ub,options);
% Set up output function
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
% Concatenate current search direction with searchdir.
searchdir = [searchdir;...
optimValues.searchdirection'];
plot(x(1),x(2),'o');
% Label points with iteration number and add title.
% Add .15 to x(1) to separate label from plotted 'o'
text(x(1)+.15,x(2),...
num2str(optimValues.iteration));
title('Sequence of Points Computed by gamultiobj');
case 'done'
hold off
otherwise
end
end
end
The error is:
Error using runmultiobjfunc/outfun
Too many output arguments.
Error in gaoutput (line 39)
[state,optnew,changed] = feval(functions{i},options.OutputPlotFcnOptions,state,flag,args{i}{:});
Error in gamultiobjsolve (line 13)
[state,options] = gaoutput(FitnessFcn,options,state,currentState);
Error in gamultiobj (line 303)
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in runmultiobjfunc (line 15)
xsol = gamultiobj(@objfun,nvar,[],[],[],[],Lb,Ub,options);

카테고리

제품

Community Treasure Hunt

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

Start Hunting!

Translated by