How to create a .gif animation visualizing the progress of an optimization process?

17 vues (au cours des 30 derniers jours)
I am using a genetic algorithm to optimize the shape of a bezier curve for a specific application. The curve is generated using the x- and y-coordinates of its four control points. The two end control points are kept fixed during the optimization process, while the x- and y-coordinates of the two other (middle) control points are considered the design variables.
I am asking about the possibility of creating an animation to visualize the progress of the optimization process (i.e., plot the bezier curve at each iteration and create a .gif animation from those plots).
Note: some irrelevant parts of the code are removed for simplicity.
[x,fval,exitflag,output] = ga(@objectiveFcn,4,A,b,Aeq,beq,lb,ub,@nonlcon,[],options)
function f = objectiveFcn(x)
xc = [0 x(1) x(2) 1]; % x-coordinates of the 4 control points of the bezier curve
yc = [0 x(3) x(4) 0]; % y-coordinates of the 4 control points of the bezier curve
% generating the bezier curve given the control points
t = 0:0.01:1;
for i = 1:length(t)
xb(i) = xc(1)*(1-t(i))^3 + 3*xc(2)*t(i)*(1-t(i))^2 + 3*xc(3)*(1-t(i))*t(i)^2 + xc(4)*t(i)^3;
yb(i) = yc(1)*(1-t(i))^3 + 3*yc(2)*t(i)*(1-t(i))^2 + 3*yc(3)*(1-t(i))*t(i)^2 + yc(4)*t(i)^3;
end
% plot the bezier curve
plot(xb,yb)
.
.
.
% calculation of the objective function in ANSYS
system('C:\"Program Files"\"ANSYS Inc"\v221\Framework\bin\Win64\RunWB2.exe -B -R finaljournal.wbjn');
% reading the value of the objective function
f = xlsread('export data1.csv','export data1','J8')
end
  2 commentaires
Usama
Usama le 3 Août 2023
Thanks so much! This is really helpful.
Any idea how to update the value of idx inside the objective function in:
im{idx} = frame2im(frame);
Because I want to save a seperate image (im) each time the objective function is evaluated.

Connectez-vous pour commenter.

Réponse acceptée

chicken vector
chicken vector le 1 Août 2023
Modifié(e) : chicken vector le 1 Août 2023
The simplest way is to add a few lines of command that saves your current objective function data.
You can add something like this to objectiveFcn after the xlsread command:
for j = 1 : 10
f = rand(10,1);
try
load('optimisationData.mat');
iterationData(length(iterationData)+1).objectiveFunction = f;
save('optimisationData.mat','iterationData');
catch
iterationData = struct;
iterationData(1).objectiveFunction = f;
save('optimisationData.mat','iterationData');
end
end
You can later access the data from the .mat file to create the .gif:
load('optimisationData.mat');
xLimits = [0 length(iterationData)] +.5;
yLimits = [.9*min([iterationData(:).objectiveFunction]) 1.1*max([iterationData(:).objectiveFunction])];
figure('WindowState','maximized')
grid on;
for frame = 1 : length(iterationData)
b = bar([iterationData(1:frame).objectiveFunction]);
xlim(xLimits);
ylim(yLimits);
exportgraphics(gcf,'optimisation.gif','Append',true);
delete b
end
  2 commentaires
Usama
Usama le 3 Août 2023
I don't understand why you added the line
f = rand(10,1);
Please note that the value of the objective function is stored in f as well, and now you are updating it's value by the vector rand(10,1).
Also, what is the pupose of the for loop:
for j = 1 : 10
chicken vector
chicken vector le 3 Août 2023
I tried to setup an example to show you the working principle but I should have explained it better.
The part of code you are interested in is the following:
try
load('optimisationData.mat');
iterationData(length(iterationData)+1).objectiveFunction = f;
save('optimisationData.mat','iterationData');
catch
iterationData = struct;
iterationData(1).objectiveFunction = f;
save('optimisationData.mat','iterationData');
end

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by