How to return an additional variable that is not part of the objective for each population while using GA optimizer?
1 次查看(过去 30 天)
显示 更早的评论
I have a function that I'm optimizing by GA. In addition, I would like to show all the population (by using an output function) that participated to find the best answer with the value of objective and another value (simga). The latter is not an objective and I just want to present its value for each population in the output function but not sure how to do it. The final matrix I want to have is showing [all the variables, sigma, objective] for all the population
I have listed the code I’m using as below. I’m not really sure how to show the sigma value. Anyone can help please.
% the script to run the optimizer
clear gaoutfunction
options = optimoptions('ga','OutputFcn',@gaoutfunction,'UseParallel',true);
startTime = tic;
fun = @(x)Stochastic_Model_Function_TOP(x,Pi,Pa,LogNormal_G,d,lifetime,Demand,BasePrice,HighPrice,LowPrice,dis_rate_lamda,Geo,Wells_cost,Wells_rate,DStage,Operating_Fields,row);
[xGA,fval] = ga(fun,nvars,[],[],[],[],lowbond,upbond,[],[],options);
time_ga_parallel = toc(startTime);
record = gaoutfunction();
gapopulationhistory = vertcat(record.Population);
gabesthistory = vertcat(record.Best);
gascorehistory = vertcat(record.Score);
% the output function
function [state,options,optchanged] = gaoutfunction(options,state,flag)
persistent state_record
if isempty(state_record)
state_record = struct('Population', {}, 'Best', {}, 'Score', {});
end
if nargin == 0
state = state_record;
options = [];
optchanged = [];
else
state_record(end+1) = struct('Population', state.Population, 'Best', state.Best', 'Score', state.Score);
optchanged = false;
end
end
An idea about the function i'm optimizing.
function [objective] = Stochastic_Model_Function_TOP(x,Pi,Pa,LogG,d,lifetime,Demand,BasePrice,HighPrice,...
LowPrice,dis_rate_lamda,Geo,Wells_cost,Wells_rate,DStage,Operating_Fields,row)
.
.
.
..
% just showing the final part. I'm optimizng the objective and I want to show sigma and objective for each population
NPV_C = (Revenue - CAPEX);
expectedNPV = mean(NPV_C);
sigma = std(NPV_C);
NewObjective = (expectedNPV + 0*sigma);
objective = - NewObjective;
end
0 个评论
回答(1 个)
Walter Roberson
2019-5-29
In order to use OutputFcn for that purpose, you would have to re-compute sigma in that gaoutfunction . There is no way to get ga to automatically gather any extra variables that the function might want to return.
I have to ask whether using the OutputFcn would even be what you would want. The OutputFcn is called once per iteration, not once per objective function call. If you wanted to collect all of the sigma values over all of the objective function evaluations for some reason (e.g., for estimation of confidence bounds) then using OutputFcn would not be the way to do it.
Do you actually want all of the sigma values for each objective function evaluation, or do you want the sigma values associated with each iteration, or do you just need the sigma value associated with the final location? The best approach depends upon which of these you want.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!