Genetic Algorithm (GA)

14 views (last 30 days)
mirhan ozdemir
mirhan ozdemir on 2 Mar 2021
Commented: mirhan ozdemir on 4 Mar 2021
Hi to all,
I am doing an optimization algorithm by MATLAB ga. I have 8 boxes wich are bonded each other and have different material properties. These 8 boxes can take a material properties within 16 different materail properties. My objective for this optimization is minimizing the mass of the overall (sum of 8 boxes) structure. I would like to constraint the ga by using frequency of the strucuture which are obtained from ANSYS analysis. To conclude, I select the material of the 8 boxes by ga and assign them to the boxes, after that I run ansys from MATLAB and obtain the frequency of the structure. This cycle is continue until my constaints are satisfied. I have an issue of defining the constaint since the frequencies are not the design variables (16 material properties) the optimizated strucutre has no feasible result. As it is seen from my objective function, my constraints are independent of design variable. Therefore, I would like ask how can constraint my frequency values as a nonlinear constaint?
Thank you.
%% Set up optimization options
options = gaoptimset('OutputFcn',@SaveOut,...
'Display', 'iter',...
'PopulationSize',200,...
'Generations',500,...
'EliteCount',2,...
'CrossoverFraction', 0.5,...
'StallTimeLimit',Inf,...
'StallGenLimit', 50,...
'FitnessLimit', -Inf,...
'PlotFcn',{@gaplotbestf,@gaplotstopping});
nvars = 8; % number of boxes
% finds a local minimum x to fitnessfcn, subjectto the linear inequalities
A = [];
b = [];
% material variation from 1 to 16 with an increment of 1
% Here each value represents different material properties
LB = [1 1 1 1 1 1 1 1];
UB = [16 16 16 16 16 16 16 16];
% set nonlcon=[] if no constraint exist
nonlcon = [];
%% Call ga
IntCon = [1,2,3,4,5,6,7,8];
[x,fval,output,population,exitflag] = ga(@objective,nvars,...
A,b,[],[],LB,UB,[],IntCon,options);
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
function mass = objective(x)
%% Frequency Constraints
fid = fopen('MODES.txt','r'); % read frequency data from Ansys output
f1 = fscanf(fid,'%f',[1,1]) % f1
f2 = fscanf(fid,'%f',[2,1]) % f2
fclose(fid);
%% Objective Function
if f1<=38000 && f2>=48000
mid = fopen('MASS.txt','r'); % read mass data from Ansys output
mass = fscanf(mid,'%f',[1,1]); % overall mass obtained from Ansys
fclose(mid);
else
mass=1e9;
end
end

Answers (1)

Alan Weiss
Alan Weiss on 2 Mar 2021
You need to include nonlinear constraints that relate to the variable x. I do not understand how you get ANSYS to run based on your variable x, but when you do so you need to have a nonlinear constraint function such as the following:
function [c,ceq] = nonlcon(x)
% Call ANSYS to get data for variable value x
% Get frequencies f1 and f2
% Constraints as I understand them are f1 <= 38000 and f2 >= 48000
ceq = [];
c = [f1 - 38000;
48000 - f2];
end
Include @nonlcon as your nonlinear constraint function.
Alan Weiss
MATLAB mathematical toolbox documentation
  3 Comments
Walter Roberson
Walter Roberson on 3 Mar 2021
tic % start time of computation
%% Set up optimization options
format long
options = gaoptimset('OutputFcn',@SaveOut,...
'Display', 'iter',...
'PopulationSize',200,...
'Generations',500,...
'EliteCount',2,...
'CrossoverFraction', 0.5,...
'StallTimeLimit',Inf,...
'FitnessLimit', -Inf,...
'PlotFcn',{@gaplotbestf,@gaplotstopping});
nvars = 8; % number of variables
% finds a local minimum x to fitnessfcn, subjectto the linear inequalities
% A*x ? b. ga evaluates the matrix product A*x as if x is transposed (A*x')
A = [];
b = [];
%upper and lower bound of variables LB < x < UB
%if there exist any lower/upper bound than write in the matrices
LB = [1 1 1 1 1 1 1 1];
UB = [16 16 16 16 16 16 16 16];
%% Call ga
%diary on
IntCon = [1,2,3,4,5,6,7,8];
[x,fval,output,population,exitflag] = ga(@objective,nvars,...
A,b,[],[],LB,UB,@nonlcon,IntCon,options);
Error using ga (line 394)
Failed to open file "MODES.txt" because "No such file or directory"

Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
%diary off
toc
function mass = objective(x)
%% Print Variables
E = [];
E1 = abs(x(1));
E2 = abs(x(2));
E3 = abs(x(3));
E4 = abs(x(4));
E5 = abs(x(5));
E6 = abs(x(6));
E7 = abs(x(7));
E8 = abs(x(8));
%rewrite new values in parameters.inp
pfilename = 'parameters.inp';
[fid, msg] = fopen(pfilename,'w+');
if fid < 0
error('Unable to open file "%s" because "%s"', pfilename, msg);
end
fprintf(fid,'E1 = %f\n',E1);
fprintf(fid,'E2 = %f\n',E2);
fprintf(fid,'E3 = %f\n',E3);
fprintf(fid,'E4 = %f\n',E4);
fprintf(fid,'E5 = %f\n',E5);
fprintf(fid,'E6 = %f\n',E6);
fprintf(fid,'E7 = %f\n',E7);
fprintf(fid,'E8 = %f\n',E8);
fclose(fid);
%% Run model
system('SET KMP_STACKSIZE = 15000k & "C:\Program Files\ANSYS Inc\v211\ANSYS\bin\winx64\ANSYS211.exe" -b -i 8unit_model.txt -o out_file.txt');
if exist('file.lock','file')
delete('file.lock')
end
mfilename = 'MASS.txt';
[mid, msg] = fopen(mfilename,'r'); % read mass data from Ansys output
if mid < 0
error('Failed to open file "%s" because "%s"', mfilename, msg);
end
mass = fscanf(mid,'%f',[1,1]); % overall mass obtained from Ansys
fclose(mid);
end
function [c,ceq] = nonlcon(x)
% Call ANSYS to get data for variable value x
% Get frequencies f1 and f2
% Constraints as I understand them are f1 <= 38000 and f2 >= 48000
%% Reading frequency from txt file
MOfilename = 'MODES.txt';
[fid, msg] = fopen(MOfilename,'r');
if fid < 0
error('Failed to open file "%s" because "%s"', MOfilename, msg)
end
f1 = fscanf(fid,'%f',[1,1])
f2 = fscanf(fid,'%f',[2,1])
fclose(fid);
ceq = [];
c = [f1 - 38000;
48000 - f2];
end
Reminder: the nonlinear constraint function could be called before the objective function has been called even once.
mirhan ozdemir
mirhan ozdemir on 4 Mar 2021
Thank you for your kind attention sir, but when I run the model frequency values does not changes which means ga supplies same population in each generation. I would like to ask about my fitness function. Previously, I defined the contraints in the fitness function. Wheh the population does to satiffy the constaint I assign a a huge mass value (1e9) for penalization. Does this approach work to obtain minimum mass?
%% Frequency Constraints are obtanined from FEM
fid = fopen('MODES.txt','r');
f1 = fscanf(fid,'%f',[1,1])
f2 = fscanf(fid,'%f',[2,1])
fclose(fid);
%% Objective Function
if f1<=9500 && f2>=11500
mid = fopen('MASS.txt','r'); % read mass data from Ansys output
mass = fscanf(mid,'%f',[1,1]); % overall mass obtained from Ansys
fclose(mid);
else
mass=1e9; %assign a huge number to mass for penalization
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!