Genetic algorithm constraint definition

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

Aquatris
Aquatris 2024년 6월 14일
What does your initial population and the 11th one look like when it does not change? what happens if you provide 10 initial population yourself?
Mirhan
Mirhan 2024년 6월 14일
Dear @Aquatris, thanks for the quick respons. Here is the first population generated by the genetic algorithm. Currently, I am only defining the starting point. Could you please advise on how I can define the initial 10 populations? Is there a way to accomplish this?
Best regards,
When you said "the 11th population set is identical to the 1st", I assumed the initial value you defined (e.g., [0 1]) stayed [0 1] in the 11th iteration. So thats why, when I wanted to see the first and last generation, what I meant was all 10 members of the 1st generation and all 10 members of the 11th generation ( you defined population size to be 10 in the options so each generation should have 10 members, or 10 pairs of [tr fr]).
One thing you might wanna check is if your objective function actually gives different values for different [tr fr] values
simple_objective([1 0])
simple_objective([3 2])
Another thing I noticed is you create the FEA_Results.txt during the objective function evaluation but also use it in the constraint function. I do not know the order of the thing but maybe your constraint might not be using the correct text file.
The way to give your own first generation, I think the syntax is something like:
X0 = {[1 0],[2 4],[3 5],[ 2 3],[3 2],[4 1],[5 2],[3.2 4.1],[2.1 4.6],[1.5 3],[3.8 0.4]}; % first generation
options.InitialPopulationMatrix = X0;
Mirhan
Mirhan 2024년 6월 17일
Hi @Aquatris,
Thank you for your assistance. Upon further examination, I realized that FEA needs to be applied not only to the objective but also to the constraint. After debugging, I discovered that the process does not function as initially thought. It begins with the calculation of the objective and constraint, but then proceeds to calculate the constraint without the objective calculation, or vice versa. Therefore, FEA must be conducted in both the constraint and the objective functions.
Warm regards,
Mirhan
Torsten
Torsten 2024년 6월 17일
Therefore, FEA must be conducted in both the constraint and the objective functions.
Maybe of interest to save calls to your objective:
Mirhan
Mirhan 2024년 6월 18일
Thanks a lot @Torsten for the document!

댓글을 달려면 로그인하십시오.

답변 (1개)

Nipun
Nipun 2024년 6월 17일

0 개 추천

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

Mirhan
Mirhan 2024년 6월 18일
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

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2024a

질문:

2024년 6월 14일

댓글:

2024년 6월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by