How to return an additional variable that is not part of the objective for each population while using GA optimizer?

1 view (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 29 May 2019
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.
  1 Comment
Yaser Khojah
Yaser Khojah on 30 May 2019
Edited: Yaser Khojah on 30 May 2019
Dear Walter,
Thank you for your respond. I'm looking for the sigma values for each objective function evaluation. The reason I'm asking for this because I have all the populations that the optimizer used and their corresponding objective. I assume these populations are found by each objective evaluation as it shows in my output function. So, I need the sigma for them too. In case it was for each iteration, I still want the sigma for that. My output function as below:
% 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
Currently, I'm using different way, but it takes forever to get it. My way is running my model again for all the populations that the optimizer used. Then, return the sigma value. The downside, it will take me for a longer period to get that. Is there a way to get them simultaneously while the optimizer is looking for the best population instead of running my model again once the populations are found? I hope you get my point now.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!