49 views (last 30 days)
Show older comments
noura on 19 Aug 2024 at 7:19
Commented: Star Strider on 19 Aug 2024 at 15:12
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 Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
Sam Chak on 19 Aug 2024 at 10:46
Direct link to this comment
https://jmaab.mathworks.com/matlabcentral/answers/2146194-i-need-help-for-optimization-using-ga#comment_3240489
Hi @noura
What do you mean by "to minimize the error between force and displacement"? The reason I ask is that force and displacement do not have the same physical unit dimension. Although it is possible to compute the error between two variables of different units, does the optimal result retain interpretable meaning in the sense of human reasoning?
noura on 19 Aug 2024 at 11:02
Direct link to this comment
https://jmaab.mathworks.com/matlabcentral/answers/2146194-i-need-help-for-optimization-using-ga#comment_3240519
I mean to minimize the error between the chart that obtained from ansys and the desired chart
Note : chart between force and displacement
Sam Chak on 19 Aug 2024 at 11:11
Direct link to this comment
https://jmaab.mathworks.com/matlabcentral/answers/2146194-i-need-help-for-optimization-using-ga#comment_3240534
Hi @noura, Could you please provide a sketch of the "charting error" and evaluate how you plan to make effective use of the code snippet in the Answer below? Your feedback is essential for implementing the given code correctly.
noura on 19 Aug 2024 at 12:06
Direct link to this comment
https://jmaab.mathworks.com/matlabcentral/answers/2146194-i-need-help-for-optimization-using-ga#comment_3240569
- Messenger_creation_58bdd1d5-44b2-4fb8-9048-c9b4ef71aba6.jpeg
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 on 19 Aug 2024 at 13:12
Direct link to this comment
https://jmaab.mathworks.com/matlabcentral/answers/2146194-i-need-help-for-optimization-using-ga#comment_3240654
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.
Sign in to answer this question.
Answers (2)
Walter Roberson on 19 Aug 2024 at 9:56
Open in MATLAB Online
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
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Star Strider on 19 Aug 2024 at 13:16
Open in MATLAB Online
- Messenger_creation_58bdd1d5-44b2-4fb8-9048-c9b4ef71aba6.jpeg
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 Show NoneHide None
Show NoneHide None
Sam Chak on 19 Aug 2024 at 15:05
Direct link to this comment
https://jmaab.mathworks.com/matlabcentral/answers/2146194-i-need-help-for-optimization-using-ga#comment_3240769
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 on 19 Aug 2024 at 15:12
Direct link to this comment
https://jmaab.mathworks.com/matlabcentral/answers/2146194-i-need-help-for-optimization-using-ga#comment_3240779
Noted. That is the reason I used what has been provided to create an example.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationGlobal Optimization ToolboxGenetic Algorithm
Find more on Genetic Algorithm in Help Center and File Exchange
Tags
- matlab
- genetic algorithm
Products
- MATLAB
- Optimization Toolbox
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office