Genetic Algorithm objective and constraint

2 Ansichten (letzte 30 Tage)
Mirhan
Mirhan am 9 Mär. 2021
Kommentiert: Alan Weiss am 10 Mär. 2021
Hi to all,
I have been modeling an optimization algorithm w/ ga. In my algorithm, I coupled matlab w/ ANSYS to run my model. I have 8 different design variables which can take 16 different values (from 1 to 16). These 16 variables are corresponding different material properties and I assign them to 8 boxes. After assigning these materials to 8 boxes I run a simple modal analysis in ANSYS. I use ga to obtain the best 8 material couple among these 16 material properties for the 8 boxes. I constained the model by defining a min and max values for first and second frequencies. My objective is minimizing mass of the structure (8boxes) since each material have an different density. I want ga to select lowest relative density materials which satisfy frequency constraint. I have a code that was previously working but with no nonlinear constraint. Then, I defined a nonlinear constraint function in the code but this time ga only generates vairables but does not call objective function (where I run the ANSYS). This results to give same frequency value for each generation. To sum up, I run my model but it does not call the objective function in each generation.
This is my main code where I set ga options
tic % start time of computation
%% Set up optimization options
format long
options = gaoptimset('OutputFcn',@SaveOut,...
'Display', 'iter',...
'PopulationSize',300,...
'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 = [];
b = [];
%upper and lower bound of variables LB < x < UB
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
And my objective function;
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));
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
And my nonlinear constaint function;
function [c,ceq] = nonlcon(x)
% Call ANSYS to get data for variable value x
% Get frequencies f1 and f2
%% 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 = [];
% Frequency constrain f1 < 32000 && f2 > 44000
c = [f1 - 32000;
44000 - f2];
end
when I run model, ANYSY runs only the last set of design variables since objective function is being called only at last step. For the rest of it the code gives same frequency values.

Antworten (1)

Alan Weiss
Alan Weiss am 9 Mär. 2021
I suggest that you use the debugger. Put a break point in your nonlinear constraint function (or maybe elsewhere) and find out what is going on.
Alan Weiss
MATLAB mathematical toolbox documentation
  4 Kommentare
Mirhan
Mirhan am 9 Mär. 2021
Sir, the design variables (x1, x2 ,x3, x4 x5, x6, x7, x8) of my code do not effect my constraint. I select the eight materials with the help of ga and after that I assign these materials to my structure. Then, I run a modal analysis to obtain frequency values of the structure. I contraint frequency values of the structure. Therefore, there is no need to constraint the design variables. However, if my approach is not logical I would happy to hear your suggestions. In the following figure, I tried to summarize my flowchart. Thank you
Alan Weiss
Alan Weiss am 10 Mär. 2021
Either you are not thinking about this correctly or I misunderstand you entirely.
It sounds to me as if the design variables can lead to frequency values that you do not want. They are related, the design variables and frequencies. You need to include the design variables x in your nonlcon function because they give rise to frequencies.
If I am wrong then I do not understand your problem at all, and am not likely to, so I probably will not respond to this thread anymore.
Alan Weiss
MATLAB mathematical toolbox documentation

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by