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 = [];
endDear 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 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 genetic algorithm MATLAB Answers — New Questions