Clear Filters
Clear Filters

How to save each result of objective function solved by genetic algorithm for each iteration?

3 views (last 30 days)
Hello!
  • I have solved my optimization problem using genetic algorithm.My objective function is related to a random matrix that's why it is changed in each execution of my program.I want to put a specific number of iterations and store the result of my objective function of each one.It is possible to do that?
  • any suggestion is appreciated

Answers (1)

Walter Roberson
Walter Roberson on 24 Feb 2023
Yes, of course.
numiter = 50;
results = zeros(nvars, numiter);
fvals = zeros(1, numiter);
for iter = 1 : numiter
[results(:, iter), fvals(iter)] = ga(...);
end
[bestfval, bestidx] = min(fvals);
bestx = results(:,bestidx).';
  2 Comments
Maria
Maria on 24 Feb 2023
@Walter Roberson sorry for my late reply, but "ga" takes a lotof time in order to solve my problem and at the end it doesn't give the required result.
This is my code , could you please show me where should the loop statement
function [sol,fval,exitflag] = MyProblem()
% all my variables are here
%=====================================
data1 = load('all_N1.mat'); % i upload the random matrix,
all_N1 = data1.all_N1;
data2 = load('all_qm1.mat'); % qm1 is related to N1
all_qm1 = data2.all_qm1;
Num = 5; % the number of iteration
sol = zeros(1,Num);
fval = zeros(1,Num);
for iter = 1 : Num
qm = all_qm5{iter};
N = all_N5{iter};
a = floor(L/(UAV_Speed*dt));
q_int = optimvar("q_int","Type","integer", "LowerBound",0,"UpperBound",a);
S = optimvar("S",[M,1],"Type","integer","LowerBound",0,"UpperBound",1);
obj = fcn2optimexpr( @Objective_function ,S);
prob = optimproblem('ObjectiveSense','max');
prob.Objective = obj ;
constr1 = fcn2optimexpr(@NLCON_MAIN,S,q_int)<=0;
constr2 = cumsum(S.*(sum(Energy_Pr,2))) <= E_max(1)-E_prop;
prob.Constraints.constr1 = constr1;
prob.Constraints.constr2 = constr2;
[sol(:,iter),fval(:,iter),] = solve(prob) ; %my problem is solved with "ga"
end
[bestfval, bestidx] = min(fval);
bestx = sol(:,bestidx).';
Also i don't know if there is a solution to accelerate "ga" execution.
Walter Roberson
Walter Roberson on 25 Feb 2023
Your code is already running Num iterations, each with a different qm and N value . However, you do not use qm or N so all of the iterations are running the same problem.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!