How to create a .gif animation visualizing the progress of an optimization process?
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Usama
el 1 de Ag. de 2023
Comentada: chicken vector
el 3 de Ag. de 2023
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 comentarios
Respuesta aceptada
chicken vector
el 1 de Ag. de 2023
Editada: chicken vector
el 1 de Ag. de 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 comentarios
chicken vector
el 3 de Ag. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Genetic Algorithm en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!