cumprod fuction in GA
73 views (last 30 days)
Show older comments
I want to calculate the following formula in GA.
syms Nb Nv K n Tb Hb t Sb F Sv Hv...
D P Cj j r q
Tb=1.2;
Hb=[2.8 2.2 1.7 1.2 0.8 0.6 0 0];
Sb=[25 28 32 36 35 38 0 0];
F=[70 50 48 55 80 65 0 0];
Sv=[0 280 200 320 360 380 420 0];
Hv=[0 2.5 1.9 1.4 1 0.7 0.5 0];
D=[180000 200000 240000 300000 350000 400000 0 0];
P=[0 200000 240000 300000 350000 400000 450000 0];
q=[1 1 1 1 1 1 0 0];
u=[0.009 0.0064 0.0054 0.0072 0.01 0.008 0 0];
t=[0.015 0.01 0.009 0.012 0.016 0.014 0 0];
w=[0.005 0.004 0.003 0.004 0.006 0.004 0 0];
Se=[29 32 38 42 37 40 0 0];
n=[1 0.9 0.75 0.6 0.51 0.45];
p=[3.2 2.6 2.1 1.6 1.2 0.9 0 0];
%
Nb=[1 1 1 1 1 1 0 0];
Nv=[0 3 2 1 1 1 1 0];
K=[0 26 14 12 8 5 5 0];
m=[0.0095 0.0078 0.0072 0.0092 0.0116 0.0104 0 0];
%
idx = 1:6;
Nvc = cumprod(Nv(idx+1));
Nvb = cumprod(Nv(idx));
s = sum(n(idx).*Tb.*Hb(idx).*D(idx).*(m(idx)-u(idx))+...
(n(idx)*Tb).^2./(Nvc.*K(idx+1)).* ...
(D(idx)/2.*(Hb(idx)+Hv(idx+1)-2*Hv(idx+1).*(1-D(idx)./P(idx+1))))+ ...
Nvc.*K(idx+1).*(Se(idx).*(t(idx)-m(idx)).^3.*(t(idx)+3*m(idx)-4*w(idx)) ...
./(t(idx)-w(idx)).^4 +(Hb(idx)+p(idx)).*D(idx).*(t(idx)-m(idx)).^5.*(2*t(idx)+3*m(idx)-5*w(idx)) ...
./(5*(t(idx)-w(idx)).^4)+F(idx))+Nvc.*Sv(idx+1)+...
(n(idx)*Tb).^2./Nvc.*(Hv(idx+1).*D(idx)/2.*(1-D(idx)./P(idx+1)))...
+ Nvb.*Nb(idx).*Sb(idx))
so I put it in the Algorithm
tic
syms Nb Nv K n Tb Hb t Sb F Sv Hv...
D P Cj j r q
options = optimoptions( ...
'ga', ...
'PopulationSize', 30, ...
'MaxGenerations', 1000, ...
'CrossoverFraction', 0.8, ...
'Display', 'iter');
% 'PlotFcn', {@gaplotbestf, @my_plot}, ...
Tb=1.2;
Hb=[2.8 2.2 1.7 1.2 0.8 0.6 0 0];
Sb=[25 28 32 36 35 38 0 0];
F=[70 50 48 55 80 65 0 0];
Sv=[0 280 200 320 360 380 420 0];
Hv=[0 2.5 1.9 1.4 1 0.7 0.5 0];
D=[180000 200000 240000 300000 350000 400000 0 0];
P=[0 200000 240000 300000 350000 400000 450000 0];
q=[1 1 1 1 1 1 0 0];
u=[0.009 0.0064 0.0054 0.0072 0.01 0.008 0 0];
t=[0.015 0.01 0.009 0.012 0.016 0.014 0 0];
w=[0.005 0.004 0.003 0.004 0.006 0.004 0 0];
Se=[29 32 38 42 37 40 0 0];
n=[1 0.9 0.75 0.6 0.51 0.45];
p=[3.2 2.6 2.1 1.6 1.2 0.9 0 0];
[x, fval] = ga(@FitnessFcn,x(1),x(2),x(3), 3, [], [], [], [], [1, 1], [40, 40], ...
[w(idx)<= m(idx)<= t(idx)], [Nv,K,m], options);
function Z = FitnessFcn(x)
Nv = x(1);
K = x(2);
m = x(3);
idx = 1:6;
Nvc = cumprod(Nv(idx+1));
Nvb = cumprod(Nv(idx));
Z = sum(n(idx).*Tb.*Hb(idx).*D(idx).*(m(idx)-u(idx))+...
(n(idx)*Tb).^2./(Nvc.*K(idx+1)).* ...
(D(idx)/2.*(Hb(idx)+Hv(idx+1)-2*Hv(idx+1).*(1-D(idx)./P(idx+1))))+ ...
Nvc.*K(idx+1).*(Se(idx).*(t(idx)-m(idx)).^3.*(t(idx)+3*m(idx)-4*w(idx)) ...
./(t(idx)-w(idx)).^4 +(Hb(idx)+p(idx)).*D(idx).*(t(idx)-m(idx)).^5.*(2*t(idx)+3*m(idx)-5*w(idx)) ...
./(5*(t(idx)-w(idx)).^4)+F(idx))+Nvc.*Sv(idx+1)+...
(n(idx)*Tb).^2./Nvc.*(Hv(idx+1).*D(idx)/2.*(1-D(idx)./P(idx+1)))...
+ Nvb.*Nb(idx).*Sb(idx))
Display Z Nv K m
toc
end
error
Unrecognized function or variable 'x'.
and it also can't seem to use cumprod in algorithm.
Nv,K,m are positive integers.
In addition, I would also like to ask if there is a three-variable drawing method based on the above problem. The information found so far only applies to two-variable equations.
Please help me fix the code so that I can get the result from algorithm, thank you for taking the time to answer.
2 Comments
Torsten
on 6 Apr 2024 at 10:46
Moved: Torsten
on 6 Apr 2024 at 20:21
Your call to ga is wrong:
x(1), x(2) and x(3) do not belong there, idx is undefined, lb and ub have 2 elements instead of 3, [Nv,K,m] don't belong there.
In the fitness function:
Nv, K and m are single numbers, but you reference Nv, K and m as 7-element vectors in your sum expression.
Further, all your arrays Tb, Hb, Sb,... are not visible in the fitness function because you defined them in the script part, but don't transfer them to the function.
Accepted Answer
Torsten
on 6 Apr 2024 at 20:17
Edited: Torsten
on 7 Apr 2024 at 20:25
Note that Nb was not defined in your code - I added it to the known arrays.
rng("default")
t=[0.015 0.01 0.009 0.012 0.016 0.014 0 0];
w=[0.005 0.004 0.003 0.004 0.006 0.004 0 0];
options = optimoptions( ...
'ga', ...
'PopulationSize', 30, ...
'MaxGenerations', 1000, ...
'CrossoverFraction', 0.8, ...
'Display', 'iter');
nvars = 7 + 7 + 6;
lb = [ones(7,1);ones(7,1);w(1:6).'];
ub = [40*ones(7,1);40*ones(7,1);t(1:6).'];
intcon = 1:(7+7);
[x, fval, exitflag,output] = ga(@FitnessFcn,nvars,[],[],[],[],lb,ub,[],intcon,options)
Nv = x(1:7)
K = x(7+1:7+7)
m = x(7+7+1:7+7+6)
fval
FitnessFcn(x)
function Z = FitnessFcn(x)
Nv = x(1:7);
K = x(7+1:7+7);
m = x(7+7+1:7+7+6);
Tb=1.2;
Hb=[2.8 2.2 1.7 1.2 0.8 0.6 0 0];
Sb=[25 28 32 36 35 38 0 0];
F=[70 50 48 55 80 65 0 0];
Sv=[0 280 200 320 360 380 420 0];
Hv=[0 2.5 1.9 1.4 1 0.7 0.5 0];
D=[180000 200000 240000 300000 350000 400000 0 0];
P=[0 200000 240000 300000 350000 400000 450000 0];
q=[1 1 1 1 1 1 0 0];
u=[0.009 0.0064 0.0054 0.0072 0.01 0.008 0 0];
t=[0.015 0.01 0.009 0.012 0.016 0.014 0 0];
w=[0.005 0.004 0.003 0.004 0.006 0.004 0 0];
Se=[29 32 38 42 37 40 0 0];
n=[1 0.9 0.75 0.6 0.51 0.45];
p=[3.2 2.6 2.1 1.6 1.2 0.9 0 0];
Nb=[1 1 1 1 1 1 0 0];
idx = 1:6;
Nvc = cumprod(Nv(idx+1));
Nvb = cumprod(Nv(idx));
Z = sum(n(idx).*Tb.*Hb(idx).*D(idx).*(m(idx)-u(idx))+...
(n(idx)*Tb).^2./(Nvc.*K(idx+1)).* ...
(D(idx)/2.*(Hb(idx)+Hv(idx+1)-2*Hv(idx+1).*(1-D(idx)./P(idx+1))))+ ...
Nvc.*K(idx+1).*(Se(idx).*(t(idx)-m(idx)).^3.*(t(idx)+3*m(idx)-4*w(idx)) ...
./(t(idx)-w(idx)).^4 +(Hb(idx)+p(idx)).*D(idx).*(t(idx)-m(idx)).^5.*(2*t(idx)+3*m(idx)-5*w(idx)) ...
./(5*(t(idx)-w(idx)).^4)+F(idx))+Nvc.*Sv(idx+1)+...
(n(idx)*Tb).^2./Nvc.*(Hv(idx+1).*D(idx)/2.*(1-D(idx)./P(idx+1)))...
+ Nvb.*Nb(idx).*Sb(idx));
end
7 Comments
Torsten
on 7 Apr 2024 at 20:23
Edited: Torsten
on 7 Apr 2024 at 23:48
Yes. But you wrote you want to use iter=100,200,300.
Here are the default options for "ga" - maybe of interest for you to know.
optimoptions(@ga)
And if you want to change the seed of the random generator used in "ga", you can remove or modify the line "rng("default")" which serves the purpose to make the results obtained identical every time you run the code with the same inputs.
More Answers (0)
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!