I want the genetic algorithm to begin with intial parameter and this is not happened can you help me for making the genetic algorithm begin with the intial parameter?
3 views (last 30 days)
Show older comments
% Main script for running the optimization loop
clear objectiveFunction; % Clear persistent variables in the objective function
% Define desired deformation and force data
desiredDeformation = [0.348347797, 1.173153205, 2.21822933, 3.812862584, ...
4.803884609, 5.90554205, 6.953464755, 7.89172824, 8.664936558, ...
9.714926366, 10.60098324, 11.59845866, 12.48718235, 13.32165075, ...
14.26752652, 15.15919716, 15.88505542, 16.66788445, 17.34050795, ...
18.01377124, 18.63121851, 19.13718778, 19.75680325, 20.43350892, ...
20.88563395, 21.22577958, 21.79170524, 22.13235055, 22.41673682];
desiredForce = [-188.7501842, -200.5384007, -217.2529154, -232.9123719, ...
-249.6333495, -267.3318313, -289.9889256, -309.6876558, -325.4440548, ...
-344.1394294, -368.7967721, -392.4507591, -423.0506813, -454.6474963, ...
-480.288806, -505.9365786, -530.6133097, -558.2548678, -588.8806413, ...
-617.525555, -639.2439221, -658.994355, -687.6457315, -717.2810752, ...
-740.9996906, -765.7216616, -792.3986411, -815.1397523, -836.8968964];
% Plot desired deformation once
figure(1); % Create or select figure 1
clf; % Clear current figure
hold on; % Hold on for multiple plots
plot(desiredDeformation, desiredForce, '-x', 'DisplayName', 'Desired Deformation', 'LineWidth', 1.5);
xlabel('Deformation');
ylabel('Force');
title('Comparison between Optimized and Desired Force-Deformation Curves');
legend('show');
grid on;
% Define number of variables and bounds
nVars = 11; % Number of variables: D1 to D11
lb = [9, 9, 12.5, 15, 17.5, 22.5, 25, 22.5, 17.5, 15, 12.5]; % Lower bounds for D1 to D11
ub = [30, 30, 40, 50, 60, 70, 100, 70, 60, 50, 30]; % Upper bounds for D1 to D11
% Define initial condition
initialCondition = [18, 18, 25, 30, 35, 45, 50, 45, 35, 30, 25];
% Replicate the initial condition across the population
initialPopulation = repmat(initialCondition, 200, 1); % Create a 200x11 matrix where each row is the initial condition
% Set up the options for the GA, now using the full initial population matrix
options = optimoptions('ga', ...
'PopulationSize', 200, ...% Set population size to 200
'CrossoverFraction', 0.7, ...
'MutationFcn', @mutationadaptfeasible, ...
'EliteCount', 1, ... % Only one individual to select
'SelectionFcn', @selectiontournament, ...
'MaxGenerations', 100, ...
'Display', 'iter', ...
'OutputFcn', @gaOutputFcn, ...
'PlotFcn', {@gaplotbestf, @gaplotstopping}, ...
'InitialPopulationMatrix', initialPopulation); % Use the replicated initial population
% Run the genetic algorithm
[x_opt, fval] = ga(@(x)objectiveFunction(x, desiredForce), nVars, [], [], [], [], lb, ub, [], options);
% Display optimized parameters after obtaining them from the GA
disp('Optimized Parameters:');
for i = 1:nVars
fprintf('D%d: %.2f\n', i, x_opt(i));
end
% Load the final optimized data from ANSYS
optimizedDisplacement = readmatrix("E:\Barrel spring\Barrel spring.csv", 'Range', 'AP8:BR8');
optimizedForce = readmatrix("E:\Barrel spring\Barrel spring.csv", 'Range', 'M8:AO8');
% Plot the final optimized force-deformation curve
plot(optimizedDisplacement, optimizedForce, '-o', 'DisplayName', 'Final Optimized', 'LineWidth', 1.5);
% Update the legend and force MATLAB to update the figure immediately
legend('show');
drawnow;
hold off;
2 Comments
Walter Roberson
on 25 Oct 2024
I have formatted your code for you, which was a bit of a nuisance. In future, please press the > button in the CODE toolstrip, then paste in your formatted code into the code section that appears.
Answers (1)
John D'Errico
on 25 Oct 2024
Edited: John D'Errico
on 25 Oct 2024
I think you don't understand GA. It does not use a start point. It starts with an initial population of points from the sample space.
If you want a solver that uses a start point, then you might want to use fmincon, or one of the others.
If you really feel like you need to use GA, and you insist on providing a start point, then you will find an option in the initialPopulationMatrix parameter.
HOWEVER!!!!!
It appears you are using the initialPopulation parameter as just the same point, replicated 200 times. DO NOT DO THIS!!!!!! That will screw things up completely. Just ONE point is all that you need. GA will generate others, which will allow it to search the sample space properly.
4 Comments
Walter Roberson
on 27 Oct 2024
please press the > button in the CODE toolstrip, then paste in your formatted code into the code section that appears. I am not interested in spending the time reformatting your code.
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!