Genetic Algorithm (GA)
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (1)
Alan Weiss
il 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 Commenti
Walter Roberson
il 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);
%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.
Vedere anche
Categorie
Scopri di più su MATLAB Mobile Fundamentals in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!