i need to make optimization using genetic algorithm to reduce the minimum between desired deformation and simulated deformation

3 views (last 30 days)
i need to make optimization using genetic algorithm to reduce the minimum between desired deformation and simulated deformation
i used ansys software for making simulation and the parameters (Height , depth , width )
but i have problem that the time of optimization took too long time and the error is too big .. can i know where is the problem ?
Thanks
function error = objectiveFunction(x, desiredDeformation)
% x(1) = Height, x(2) = Depth, x(3) = Width
% Modify the journal file with the new input parameters
Height = x(1);
Depth = x(2);
Width = x(3);
% Read and modify the journal file
fid = fopen("E:\Master\part.wbjn",'r');
f = fread(fid,'*char')';
fclose(fid);
f = strrep(f,'Width' , num2str(Width));
f = strrep(f,'Height' , num2str(Height));
f = strrep(f,'Depth' , num2str(Depth));
fid = fopen('finaljournal.wbjn','w');
fprintf(fid,'%s',f);
fclose(fid);
% Run Ansys using the updated journal file
system('"C:\Program Files\ANSYS Inc\v231\Framework\bin\Win64\runwb2.exe" -X -R "finaljournal.wbjn"');
% Load the ANSYS output data (Total Deformation)
simulatedDisplacement = readmatrix("E:\Master\part.csv", 'Range', 'E8:P8');
% Calculate the error between simulated and desired deformation
error = sqrt(mean((simulatedDisplacement - desiredDeformation).^2)); % RMSE as the error metric
end
% Define desired deformation and force data (example values)
desiredDeformation = [1.25066872840705e-09, 2.50133745681411e-09, 6.25334385783699e-09, 1.25066877156740e-08, 1.87600315915789e-08, 2.50133754313480e-08, 3.12667193063999e-08, 3.75200631831578e-08, 4.37734070582097e-08, 5.00267508626959e-08, 5.62800948083135e-08, 6.25334386127997e-08];
desiredForce = [2, 4, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
% Define number of variables and bounds
nVars = 3; % Number of variables: Height, Depth, Width
lb = [10, 5, 10]; % Lower bounds for Height, Depth, Width
ub = [100, 10, 100]; % Upper bounds for Height, Depth, Width
% Set up the genetic algorithm options with a time limit of 10 minutes
options = optimoptions('ga', ...
'Display', 'iter', ...
'MaxTime', 600, ... % Set the maximum time to 600 seconds (10 minutes)
'PlotFcn', @gaplotbestf);
% Run the optimization, passing the correct objective function
[x_opt, fval] = ga(@(x) objectiveFunction(x, desiredDeformation), nVars, [], [], [], [], lb, ub, [], options);
% Display optimized parameters
disp('Optimized Parameters:');
fprintf('Height: %.2f\n', x_opt(1));
fprintf('Depth: %.2f\n', x_opt(2));
fprintf('Width: %.2f\n', x_opt(3));
% Load the optimized data from Ansys
optimizedDisplacement = readmatrix("E:\Master\part.csv", 'Range', 'E8:P8');
optimizedForce = readmatrix("E:\Master\part.csv", 'Range', 'Q8:AB8');
% Plot comparison
figure;
hold on;
plot(optimizedForce, optimizedDisplacement, '-o', 'DisplayName', 'Optimized Displacement');
plot(desiredForce, desiredDeformation, '-x', 'DisplayName', 'Desired Displacement');
xlabel('Force');
ylabel('Displacement');
title('Comparison between Optimized and Desired Force-Displacement Curves');
legend('show');
grid on;

Answers (1)

MULI
MULI on 13 Dec 2024
Hi @noura,
To optimize parameters (Height, Depth, Width) using a genetic algorithm, you can follow these steps:
Each iteration involves running ANSYS via a journal file and retrieving the results, which can be computationally expensive. To address this:
  • MATLAB supports parallel computation in ga using the UseParallel option. This requires the Parallel Computing Toolbox. You can refer to this link for more information on parallel processing: https://www.mathworks.com/help/gads/how-to-use-parallel-processing.html
  • Use a coarser mesh or reduce the number of load steps during optimization to speed up simulations.
A high error between simulated and desired deformation can be reduced by following these steps:
  • Ensure that the search space (parameter bounds) includes feasible designs that align with physical and simulation constraints.
  • Verify that parameters (Height, Depth, Width) are correctly updated in the journal file before each simulation.
  • Use an appropriate metric like Root Mean Square Error (RMSE) to measure the difference between the simulated and desired deformation. https://www.mathworks.com/help/deeplearning/ref/deep.metric.rmsemetric.html
By following these steps, you can reduce computational costs, minimize error, and achieve more accurate optimization results.

Community Treasure Hunt

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

Start Hunting!