Hi,there is an answered question about saving objective function value at each evaluation of genetic algorithm here;
https://www.mathworks.com/matlabcentral/answers/453836-how-can-i-store-the-value-at-each-iteration-of-a-genetic-algorithm
Stephan's answer is good solution for serial evaluation of objective function.
[x,fval,vals] = solve_problem function [x,fval,vals] = solve_problem ObjFcn = @myFitness;nvars = 2; %number of variables
LB = [0 0]; %lower bound
UB = [1 13]; %upper bound
ConsFcn = @myConstraints;iter = 1;vals = [];[x,fval] = ga(ObjFcn,nvars,[],[],[],[],LB,UB,ConsFcn);function y = myFitness(x)y = 100*(x(1)^2-x(2))^2 + (1-x(1))^2;vals(iter,1:2) = x;iter = iter+1;endfunction [c,c_eq] = myConstraints(x)c = [x(1)*x(2) + x(1) - x(2) + 1.5; 10 - x(1)*x(2)];c_eq = [];endend
But im trying to parallel evaluation of objective function. My objective function is an external objfunc.m file that calls an external program. In this case "vals" matrix suggested by Stephan doesnt work since "iter" cannot have a value in squence. Every worker tries to increase iter number simultaniously.
In my case;
parpool('AttachedFiles',{'objfunc.m'});options = optimoptions(options,'UseParallel', true);
i send objfunc.m file to all worker first. So, all results coming simultaneously from each worker must be combined in a single matrix according to ascending values of "iter".
I need to save all x and fval values in the same row for each function evaluation according to results coming from workers.
How could be the solution in the parallel case?
Thank you
Best Answer