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

4 ビュー (過去 30 日間)
Maria
Maria 2023 年 2 月 23 日
コメント済み: Walter Roberson 2023 年 2 月 25 日
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

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 2 月 24 日
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 件のコメント
Maria
Maria 2023 年 2 月 24 日
@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 2023 年 2 月 25 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by