MATLAB Answers

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

2 ビュー (過去 30 日間)
Yaser Khojah
Yaser Khojah 2019 年 8 月 22 日
コメント済み: Yaser Khojah 2019 年 8 月 23 日
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,...
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,...
[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', {});
if nargin == 0
state = state_record;
options = [];
optchanged = [];
state_record(end+1) = struct('Population', state.Population, 'Best', state.Best', 'Score', state.Score);
optchanged = false;
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 件のコメント



Matt J
Matt J 2019 年 8 月 22 日
編集済み: Matt J 2019 年 8 月 22 日
function run_optimization
[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

  13 件のコメント

表示 10 件の古いコメント
Yaser Khojah
Yaser Khojah 2019 年 8 月 23 日
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 2019 年 8 月 23 日
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.


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by