Genetic algorithm optimization tool not accessing the objective function

2 Ansichten (letzte 30 Tage)
I am trying to optimise an objective function with the name objective function. Optimisation parameter is Charge/Area and Frequency is meant as an constraint which is in objective function itself with an if condition.
I am using Parallel computing. But I observed parallel pool is left idle. Then, I put a breakpoint at the first line of objective function but the code never paused indicating there is something wrong with the code. I would be glad if somebody can suggest what went wrong in this.
clc;
clear;
close all;
%% Initial Conditions
%starting point
L_b_0=200;
w_b_0=60;
L_m_0=200;
w_m_0=400;
t_hSi_0=98;
t_aln_0 = 477;
L_s_0 = 150;
gap_s = 5;
w_s_0 = 0.5*(w_m_0 - w_b_0 - 2*gap_s);
x0=[L_b_0 w_b_0 L_m_0 w_m_0 t_hSi_0 t_aln_0 L_s_0 gap_s]
x0 = 1×8
200 60 200 400 98 477 150 5
objective(x0)
ans = 100000
%% Bounds and Constraints
%bounds
lb=[100 5 100 5 10 100 100 5];
ub=[500 150 500 1000 350 1000 1000 800];
%linear inequalities
A=[-0.3 1 0 0 0 0 0 0; 0 1 0 -1 0 0 0 2];
b=[0; -5];
%linear equalities equalities
Aeq=[];
beq=[];
%nvars
nvars=8;
% CalcCharge_RB([200,156.3643632,200,360.5678223,239.2755624])
% Constraint_RB([200,156.3643632,200,360.5678223,239.2755624])
%Call solver to minimize objective function
%% Optimisation
opts = optimoptions('ga','OutputFcn',@Anju_ga_save_each_gen_NonLin,'InitialPopulationMatrix',x0,'PopulationSize',5000,'EliteCount',20,'CrossoverFraction',0.4,'HybridFcn','fmincon','UseParallel',true);
xopt = ga(@objective,nvars,A,b,Aeq,beq,lb,ub)%,[],opts)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
xopt = 1×8
500 5 100 1000 10 100 100 5
objective(xopt)
ans = 100000
% Charge = CalcCharge_RB(xopt);
%Freq = Constraint_RB(xopt);
%% Objective Function
%objective function - Charge/Area + Constraint - Frequency
function obj = objective(x)
%Defining Parameters
L_b=x(1)*10^-6;
w_b=x(2)*10^-6;
L_m=x(3)*10^-6;
w_m=x(4)*10^-6;
t_hSi=x(5)*10^-6;
t_aln = x(6)*10^-9;
L_s = x(7)*10^-6;
gap_s = x(8)*10^-6;
w_s = 0.5*(w_m - w_b - 2*gap_s);
t_si = 1*10^-6;
t_sio2 = 3*10^-6;
t_al=0.1*10^-6;
if(w_b <= 0.3.*L_b && w_s >= 5)
%% Calculating Required parameters
%Densities
rho_al = 2700;
rho_aln = 3300;
rho_si = 2329;
rho_sio2 = 2200;
%Elastic Modulus of Materials
E_al = 70*10^9;
E_aln = 330*10^9;
E_si = 188*10^9;
%neutral axis
n = (E_al.*t_al.^2 + 2.*E_al.*t_al.*t_aln + 2.*E_al.*t_al.*t_si + E_aln.*t_aln.^2 + 2.*E_aln.*t_aln.*t_si +E_si.*t_si.^2)/(2.*(E_al.*t_al + E_aln.*t_aln + E_si.*t_si));
I_al = (1/12).*w_b.*t_al.^3 + w_b.*t_al.*(t_aln + t_si - n + 0.5.*t_al)^2;
I_aln = (1/12).*w_b.*t_aln.^3 + w_b.*t_aln.*(t_si - n + 0.5.*t_aln)^2;
I_si = (1/12).*w_b.*t_si.^3 + w_b.*t_si.*(0.5.*t_si - n)^2;
m = L_m.*w_m.*(rho_aln.*t_aln + rho_si.*t_si + rho_sio2.*t_sio2 + rho_si.*t_hSi);
Force = m.*9.8;
d_31 = -1.9159*10^-12;
m_s = L_s.*w_s.*(rho_aln.*t_aln + rho_si.*t_si + rho_sio2.*t_sio2 + rho_si.*t_hSi);
%% Charge Calculation
Charge = (d_31.*9.8.*w_b.*L_b.*t_aln./I_aln).*((0.5.*m+m_s)*L_b - m_s.*L_s + 0.5.*m.*L_m);
%% Area Calculation
Area = (L_b+L_m).*w_m;
%% Figure of Merit
Normalised_Charge = Charge.*10^13;
Normalised_Area = Area.*10^9;
Fom = Normalised_Charge./Normalised_Area;
obj = Fom;
%% Frequency
E_tot= E_al.*I_al+E_aln.*I_aln+E_si.*I_si;
rho_t = rho_aln.*t_aln + rho_si.*t_si + rho_al.*t_al;
N = (4.*E_tot*((16/3).*((m./2 + m_s).*L_b).^2 + 4.*(m.*L_m - 2.*m_s.*L_s).*(m./2 + m_s).*L_b + (m.*L_m - 2.*m_s.*L_s).^2));
D1 = (352/105).*(m./2 + m_s).^2.*(rho_t).*(w_b).*L_b.^5;
D2 = (104/45).*(m./2 + m_s).*((80/13).*m_s.^2+((80/13).*m-2.*rho_t.*w_b.*L_s).*m_s+m.*((20/13).*m+L_m.*w_b.*rho_t)).*L_b.^4;
D3 = (2/5).*(m.*L_m-2.*m_s.*L_s).*((160/3).*m_s.^2 + ((160/3).*m-2.*rho_t.*w_b.*L_s).*m_s+((40/3).*m+rho_t.*L_m.*w_b).*m).*L_b.^3;
D4 = 4.*(m./2 + m_s).*((40/3).*L_s.^2.*m_s.^2+m.*(L_m.^2-(34/3).*L_m.*L_s+L_s.^2).*m_s+(10/3).*L_m.^2.*m.^2).*L_b.^2;
D5 = 4.*(m.*L_m-2.*m_s.*L_s).*(4.*L_s.^2.*m_s.^2+m.*(L_m-L_s).^2.*m_s+L_m.^2.*m.^2).*L_b;
D6 = (L_m.^2.*m+2.*L_s.^2.*m_s).*(m.*L_m-2.*m_s.*L_s).^2;
D = L_b.*(D1+D2+D3+D4+D5+D6);
w2=N./D;
Freq =sqrt(w2)./(2.*pi)
if(Freq >= 810 || Freq <= 750)
obj = 10000000;
end
else
obj = 100000;
end
end
  1 Kommentar
Sam Chak
Sam Chak am 9 Jan. 2024
@Pavitra Jain, This is just a test to trigger the "If" condition. Note that there is another objective value "obj = Fom".
xTest = [500 5 100 1e8 10 100 100 5];
J = objective(xTest)
w_b = 5.0000e-06
w_s = 50.0000
Fom = -0.0012
obj = -0.0012
Freq = 0.3471
J = 10000000
function obj = objective(x)
% Defining Parameters
L_b = x(1)*10^-6;
w_b = x(2)*10^-6
L_m = x(3)*10^-6;
w_m = x(4)*10^-6;
t_hSi = x(5)*10^-6;
t_aln = x(6)*10^-9;
L_s = x(7)*10^-6;
gap_s = x(8)*10^-6;
w_s = 0.5*(w_m - w_b - 2*gap_s)
t_si = 1*10^-6;
t_sio2 = 3*10^-6;
t_al = 0.1*10^-6;
if (w_b <= 0.3.*L_b && w_s >= 5)
%% Calculating Required parameters
% Densities
rho_al = 2700;
rho_aln = 3300;
rho_si = 2329;
rho_sio2 = 2200;
% Elastic Modulus of Materials
E_al = 70*10^9;
E_aln = 330*10^9;
E_si = 188*10^9;
% neutral axis
n = (E_al.*t_al.^2 + 2.*E_al.*t_al.*t_aln + 2.*E_al.*t_al.*t_si + E_aln.*t_aln.^2 + 2.*E_aln.*t_aln.*t_si +E_si.*t_si.^2)/(2.*(E_al.*t_al + E_aln.*t_aln + E_si.*t_si));
I_al = (1/12).*w_b.*t_al.^3 + w_b.*t_al.*(t_aln + t_si - n + 0.5.*t_al)^2;
I_aln = (1/12).*w_b.*t_aln.^3 + w_b.*t_aln.*(t_si - n + 0.5.*t_aln)^2;
I_si = (1/12).*w_b.*t_si.^3 + w_b.*t_si.*(0.5.*t_si - n)^2;
m = L_m.*w_m.*(rho_aln.*t_aln + rho_si.*t_si + rho_sio2.*t_sio2 + rho_si.*t_hSi);
Force = m.*9.8;
d_31 = -1.9159*10^-12;
m_s = L_s.*w_s.*(rho_aln.*t_aln + rho_si.*t_si + rho_sio2.*t_sio2 + rho_si.*t_hSi);
%% Charge Calculation
Charge = (d_31.*9.8.*w_b.*L_b.*t_aln./I_aln).*((0.5.*m+m_s)*L_b - m_s.*L_s + 0.5.*m.*L_m);
%% Area Calculation
Area = (L_b+L_m).*w_m;
%% Figure of Merit
Normalised_Charge = Charge.*10^13;
Normalised_Area = Area.*10^9;
Fom = Normalised_Charge./Normalised_Area
obj = Fom
%% Frequency
E_tot = E_al.*I_al+E_aln.*I_aln+E_si.*I_si;
rho_t = rho_aln.*t_aln + rho_si.*t_si + rho_al.*t_al;
N = (4.*E_tot*((16/3).*((m./2 + m_s).*L_b).^2 + 4.*(m.*L_m - 2.*m_s.*L_s).*(m./2 + m_s).*L_b + (m.*L_m - 2.*m_s.*L_s).^2));
D1 = (352/105).*(m./2 + m_s).^2.*(rho_t).*(w_b).*L_b.^5;
D2 = (104/45).*(m./2 + m_s).*((80/13).*m_s.^2+((80/13).*m-2.*rho_t.*w_b.*L_s).*m_s+m.*((20/13).*m+L_m.*w_b.*rho_t)).*L_b.^4;
D3 = (2/5).*(m.*L_m-2.*m_s.*L_s).*((160/3).*m_s.^2 + ((160/3).*m-2.*rho_t.*w_b.*L_s).*m_s+((40/3).*m+rho_t.*L_m.*w_b).*m).*L_b.^3;
D4 = 4.*(m./2 + m_s).*((40/3).*L_s.^2.*m_s.^2+m.*(L_m.^2-(34/3).*L_m.*L_s+L_s.^2).*m_s+(10/3).*L_m.^2.*m.^2).*L_b.^2;
D5 = 4.*(m.*L_m-2.*m_s.*L_s).*(4.*L_s.^2.*m_s.^2+m.*(L_m-L_s).^2.*m_s+L_m.^2.*m.^2).*L_b;
D6 = (L_m.^2.*m+2.*L_s.^2.*m_s).*(m.*L_m-2.*m_s.*L_s).^2;
D = L_b.*(D1+D2+D3+D4+D5+D6);
w2 = N./D;
Freq = sqrt(w2)./(2*pi)
if (Freq >= 810 || Freq <= 750)
obj = 10000000;
% asd = 1;
end
else
obj = 100000;
% asd = 1;
end
end

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Torsten
Torsten am 9 Jan. 2024
Bearbeitet: Torsten am 9 Jan. 2024
As you can see from the result, ga never seems to execute the "if" condition in your objective.
I don't know why you test for the constraint condition in the objective since you tried to implemented it in A and b in the call to "ga" ( but not correctly ).
  9 Kommentare
Pavitra Jain
Pavitra Jain am 9 Jan. 2024
Bearbeitet: Pavitra Jain am 9 Jan. 2024
I have actually done this before to specify OutputFcn along with Parallel Computing. I always have the output function store the result at the end of each generation in a text file. I have never encountered such issue before. Same goes for debugging. Even if I consider your proposal for Breakpoint, parallel pool shouldn't go idle. Even if I dont use parallel pool, each run for objective function takes less than 30 ms. So, even without parallel pool, it would take 30s for 1000 iterations. So, it should store results of first generation in 30 secs time in the Output_Data.txt file.
Torsten
Torsten am 9 Jan. 2024
Bearbeitet: Torsten am 9 Jan. 2024
I always have the output function store the result at the end of each generation in a text file. I have never encountered such issue before.
Then you should think about what you've changed in your code that made it from working before to not working now. I cannot imagine that OutputFcn and Debugging can be combined with Parallel Computing - at least it shouldn't be done in my opinion.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with Optimization 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