MATLAB Answers

Returning an additional value that is not part of the fitness function or objective for all population evaluation in GA

2 views (last 30 days)
Yaser Khojah
Yaser Khojah on 22 Aug 2019
Commented: Yaser Khojah on 23 Aug 2019
Can anyone please help by showing the code how can I return an additional value (Sigma) that is not part of my fitness function (Mean). I know how to return all the populations, scores but I'm not sure how to return this additional value (sigma). I see many have asked this question but no one showed in a code rather than just directing us to use the nested functions. Here is my code:
% the function to be optimized
[objective] = Optimization_Function(x,Pi,Pa,LogG,d,lifetime,Demand,BasePrice,HighPrice,...
LowPrice,dis_rate_lamda,Geo,Wells_cost,Wells_rate,DStage,Operating_Fields,row,Amt,FOPEX,VOPEX)
.
.
.
.
expectedNPV = mean(NPV_C);
sigma = std(NPV_C); %%%%% I need this value for every population evaluation
NewObjective = (expectedNPV)
objective = - NewObjective;
% my optimization code
clear gaoutfunction
options = optimoptions('ga','OutputFcn',@gaoutfunction,'UseParallel',true);
startTime = tic;
fun = @(x)Optimization_Function(x,Pi,Pa,LogNormal_G,d_cline,lifetime,Demand,BasePrice,...
HighPrice,LowPrice,dis_rate_lamda,Geo,Wells_cost,Wells_rate,DStage,Operating_Fields,row,Amt,FOPEX,VOPEX);
[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);
Results = [gapopulationhistory gascorehistory];
% my output funciton which includes populations, scores but does not include sigma :(
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
Can anyone please show me how to return sigma as I’m returning all the populations and their scores. Please note the score here is only the objective which is the mean. The sigma value is not part of the optimization but I need it to save the time rather than running the model again to evaluate it. Please help.

  0 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 22 Aug 2019
Edited: Matt J on 22 Aug 2019
function run_optimization
allSigmas=[];
[xGA,fval] = ga(fun,nvars,[],[],[],[],lowbond,upbond,[],[],options);
% the function to be optimized
function [objective] = Optimization_Function(x,Pi,Pa,...) .
....
objective = - NewObjective;
allSigmas(end+1) = std(NPV_C); %%%%% I need this value for every population evaluation
end
end

  13 Comments

Yaser Khojah
Yaser Khojah on 23 Aug 2019
Dear Matt, thanks for sharing this. I have tried and it is working. I never used parfor before. Do you think 'UseVectorized' is faster than using the parallel?
Matt J
Matt J on 23 Aug 2019
UseVectorized can be faster than parfor-parallelization when the fitness of all population members can be computed all at once without a for-loop. I assumed that that was not the case for you.

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!

Translated by