Genetic algorithm constraint definition

1 visualización (últimos 30 días)
Mirhan
Mirhan el 14 de Jun. de 2024
Comentada: Mirhan el 18 de Jun. de 2024
Dear All,
I am currently working on a project involving a genetic algorithm. The project focuses on optimizing a circular structure, where the variables under consideration are the strut radius (tr) and the fillet radius (fr). The genetic algorithm selects these variables, and based on the chosen values, a subroutine is invoked to generate the structure. Subsequently, a finite element analysis (FEA) is conducted using a PDE solver. From this analysis, stress and relative density values are exported.
The objective of my optimization is to minimize stress while maintaining a relative density below 0.2. For example, I have defined a population size of 10. The genetic algorithm selects different values for the design variables; however, the 11th population set is identical to the 1st, leading to an error stating "Optimization finished: no feasible point found."
I believe the issue lies in the constraint definition or the linkage between the objective function and the constraint function.
Thank you for your time and assistance.
Best regards,
Here is some lines of my code,
% Clean up
clear all; close all; clc;
tic
% Set up optimization options
format long
options = gaoptimset('OutputFcn',@SaveOut,...
'Display', 'iter',...
'PopulationSize', 10,...
'Generations', 10,...
'EliteCount', 2,...
'CrossoverFraction', 0.95,...
'TolFun', 1.0e-9,...
'TolCon', 1.0e-9,...
'StallTimeLimit', Inf,...
'FitnessLimit', -Inf,...
'PlotFcn',{@gaplotbestf,@gaplotstopping});
% Define variables and bounds
nvars = 2; % Strut radius: tr and Fillet radius: fr
LB = [1, 0]; % Lower bounds for tr and fr
UB = [5, 5]; % Upper bounds for tr and fr
X0 = [1 0]; % Start point
options.InitialPopulationMatrix = X0;
% Define objective function and constraints
ObjectiveFunction = @(x) simple_objective(x);
nonlcon = @(x) simple_const(x);
% Call ga
[x,fval,output,population,exitflag] = ga(ObjectiveFunction,nvars,...
[],[],[],[],LB,UB,nonlcon,[],options);
toc
%% Objective Function
function cost = simple_objective(x)
% Generate new structure
tr = x(1); % strut radius
fr = x(2); % fillet radius
a = 10; % unit cell size
BCC_stl_generator(a, tr, fr);
% RUN FEA
FEA_ANALYSIS(tr); % Then, export results to a text file FEA_Results.txt
% Read results
fileID = fopen('FEA_Results.txt', 'r');
results = fscanf(fileID,'%f',[1,inf]);
fclose(fileID);
MaxVonMisesStress = results(1);
RelativeDensity = results(2);
cost = MaxVonMisesStress; % Objective is to minimize MaxVonMisesStress
end
%% Constraint Function
function [c, ceq] = simple_const(x)
fileID = fopen('FEA_Results.txt', 'r');
results = fscanf(fileID,'%f',[1,inf]);
fclose(fileID);
MaxVonMisesStress = results(1);
RelativeDensity = results(2);
tol=0.001;
c = RelativeDensity - 0.2 - tol; % Constraint: RelativeDensity must be <= 0.2
% The minimum and maximum relative density values are 0.116 and 0.494, rescpectively.
ceq = [];
end
  6 comentarios
Torsten
Torsten el 17 de Jun. de 2024
Therefore, FEA must be conducted in both the constraint and the objective functions.
Maybe of interest to save calls to your objective:
Mirhan
Mirhan el 18 de Jun. de 2024
Thanks a lot @Torsten for the document!

Iniciar sesión para comentar.

Respuestas (1)

Nipun
Nipun el 17 de Jun. de 2024
Hi Mirhan,
I understand that you are working on a genetic algorithm to optimize a circular structure with constraints on stress and relative density. The issue arising is that the optimization process finishes without finding a feasible point due to repeated populations. This might be due to how the constraints are linked to the objective function. I recommend the following sanity checks for the given code:
  1. Ensure the initial population is diverse to avoid repeated values.
  2. Combine the constraint and objective functions effectively.
Here is the updated code:
% Clean up
clear all; close all; clc;
tic
% Set up optimization options
format long
options = gaoptimset('OutputFcn',@SaveOut,...
'Display', 'iter',...
'PopulationSize', 10,...
'Generations', 10,...
'EliteCount', 2,...
'CrossoverFraction', 0.95,...
'TolFun', 1.0e-9,...
'TolCon', 1.0e-9,...
'StallTimeLimit', Inf,...
'FitnessLimit', -Inf,...
'PlotFcn',{@gaplotbestf,@gaplotstopping});
% Define variables and bounds
nvars = 2; % Strut radius: tr and Fillet radius: fr
LB = [1, 0]; % Lower bounds for tr and fr
UB = [5, 5]; % Upper bounds for tr and fr
% Define objective function and constraints
ObjectiveFunction = @(x) simple_objective(x);
nonlcon = @(x) simple_const(x);
% Call ga with an initial population matrix
options.InitialPopulationMatrix = LB + (UB - LB).*rand(10, nvars);
[x,fval,output,population,exitflag] = ga(ObjectiveFunction,nvars,...
[],[],[],[],LB,UB,nonlcon,[],options);
toc
%% Objective Function
function cost = simple_objective(x)
% Generate new structure
tr = x(1); % strut radius
fr = x(2); % fillet radius
a = 10; % unit cell size
BCC_stl_generator(a, tr, fr);
% RUN FEA
FEA_ANALYSIS(tr); % Then, export results to a text file FEA_Results.txt
% Read results
fileID = fopen('FEA_Results.txt', 'r');
results = fscanf(fileID,'%f',[1,inf]);
fclose(fileID);
MaxVonMisesStress = results(1);
RelativeDensity = results(2);
cost = MaxVonMisesStress; % Objective is to minimize MaxVonMisesStress
end
%% Constraint Function
function [c, ceq] = simple_const(x)
FEA_ANALYSIS(x(1)); % Ensure FEA results are updated
fileID = fopen('FEA_Results.txt', 'r');
results = fscanf(fileID,'%f',[1,inf]);
fclose(fileID);
RelativeDensity = results(2);
tol = 0.001;
c = RelativeDensity - 0.2 - tol; % Constraint: RelativeDensity must be <= 0.2
ceq = [];
end
Notes:
  1. Initial Population Diversity: Ensure that the initial population is not repeated. The updated initial population matrix uses random values within the specified bounds.
  2. Ensure FEA Analysis Updates: Ensure that the FEA_ANALYSIS function is called within the constraint function to update the FEA results accurately before checking constraints.
For more details on genetic algorithms in MATLAB, refer to the following MathWorks documentation: https://www.mathworks.com/help/gads/genetic-algorithm.html
Hope this helps.
Regards,
Nipun
  1 comentario
Mirhan
Mirhan el 18 de Jun. de 2024
Dear @Nipun,
Thank you very much for the detailed explanation!
Following your suggestion, I have updated my code to calculate the objective and constraints within both the objective and constraint functions. This has helped resolve the convergence problem. I will also implement your initial population approach.
Once again, thank you for your support.
Kind regards,
Mirhan

Iniciar sesión para comentar.

Categorías

Más información sobre Genetic Algorithm en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by