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

17 次查看(过去 30 天)
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

采纳的回答

chicken vector
chicken vector 2023-8-1
编辑:chicken vector 2023-8-1
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 个评论
Usama
Usama 2023-8-3
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 2023-8-3
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by