Clear Filters
Clear Filters

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

15 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 (0)

Community Treasure Hunt

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

Start Hunting!