I need help for optimization using Ga

2 views (last 30 days)
noura
noura on 19 Aug 2024
Commented: Star Strider on 19 Aug 2024
I want to make optimization using genetic algorithm to minimize error between force and displacement (simulated and desired ) . I have 3 numbers of variables (height , depth , width) .. can anyone help me for the coding please
  5 Comments
noura
noura on 19 Aug 2024
I need to make optimization using ga to minimize the error between simulated and desired chart as mentioned. The parameters ( height , depth , width ) of a cuboid
Sam Chak
Sam Chak on 19 Aug 2024
Since you already have the code to run the sim, getting the "Simulated Displacement" points and the "Desired Displacement" points into the "data" array in @Walter Roberson's code should be a relatively easy thing to do.
Since the cuboid's dimension cannot be zero or infinity, estimate some realistic values for the lower and upper bounds for the GA to search within these bounded regions.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 19 Aug 2024
data = Something Appropriate to set up data
numvar = 3;
A = []; b = [];
Aeq = []; beq = [];
lb = [0, 0, 0];
ub = [inf, inf, inf];
best = ga(@(x)Simulate_force(x, data), numvar, A, b, Aeq, beq, lb, ub);
function val = Simulate_force(x, data)
force = something appropriate using data
displacement = something else appropriate using data
val = sum((force - displacement).^2);
end

Star Strider
Star Strider on 19 Aug 2024
Perhaps something like this —
figure
imshow(imread('Messenger_crea...ef71aba6.jpeg'))
x = [1 2.5 10:10:100];
simulated = 0.007 * x;
desired = 0.021 * x;
objfcn = @(b,x) b(1) + b(2).*x; % Objective Function
ftns = @(b) norm(desired - objfcn(b,simulated)); % Fitness Function
% PopSz = 500;
% Parms = 2;
% optsAns = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-3, 'MaxGenerations',5E3, 'FunctionTolerance',1E-10); % Options Structure For 'Answers' Problems
% t0 = clock;
% fprintf('\nStart Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t0)
% [B,fval,exitflag,output,population,scores] = ga(ftns, Parms, [],[],[],[],zeros(Parms,1),Inf(Parms,1),[],[],optsAns);
% t1 = clock;
% fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
% GA_Time = etime(t1,t0)
% DT_GA_Time = datetime([0 0 0 0 0 GA_Time], 'Format','HH:mm:ss.SSS');
% fprintf('\nElapsed Time: %23.15E\t\t%s\n\n', GA_Time, DT_GA_Time)
%
% fprintf('Fitness value at convergence = %.4f\nGenerations \t\t\t\t = %d\n\n',fval,output.generations)
%
% fprintf(1,'\tRate Constants:\n')
% for k1 = 1:length(B)
% fprintf(1, '\t\tB(%2d) = %8.5f\n', k1, theta(k1))
% end
B = ga(ftns, 2)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
B = 1x2
0.0106 2.9520
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
plot(x, simulated, 'o-', 'DisplayName','Simulated Displacement')
hold on
plot(x, desired, 'x-', 'DisplayName','Desired Displacement')
plot(x, objfcn(B, simulated), 'sr-', 'DisplayName','Optimised Result')
hold off
grid
legend('Location','best')
The ‘B’ vector here are the intercept and slope transformation required to transform ‘simulated’ to ‘desired’. Using ga may be a bit of ‘overkill’ for this problem, however it can be done, assuming I understand what you want to do.
(I’m getting some sort of weird error with respect to declaring the options structure, so I commented-out that code example. That error is ‘The 'ga' function requires the Global Optimization Toolbox’ so I ran it without the options structure and the code I usually use for ga problems, and it worked.)
.
  2 Comments
Sam Chak
Sam Chak on 19 Aug 2024
For static optimization problems involving linear graphs, using a ga() may indeed be considered excessive. However, it appears that the OP intends to determine the dimensions of a cuboid (length × width × height) such that, when a certain magnitude of force F is applied, the cuboid of mass m is displaced by a specific distance x. While the 2nd-order motion model is certainly available, the OP has not provided it.
Star Strider
Star Strider on 19 Aug 2024
Noted. That is the reason I used what has been provided to create an example.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!