I need to maximize an objective function using genetic algorithm but i am having errors
7 次查看(过去 30 天)
显示 更早的评论
Hello all,
I am trying to optimize the dimension of a piezoelectric cantilever beam with parameters specified with symbolic function (syms) as shown in fitness function script below. But when I run the genetic algorithm script, I am encountering the following error
"The following error occurred converting from sym to double:
Error using symengine (line 59)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in fcnvectorizer (line 13)
y(i,:) = feval(fun,(pop(i,:)));
Error in makeState (line 58)
Score = fcnvectorizer(state.Population(initScoreProvided+2:end,:),FitnessFcn,1,options.SerialUserFcn);
Error in galincon (line 17)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 359)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in GA (line 8)
[Lbeam,width,thickbeam,thickpiezo,Lpiezo,fval] = ga(ObjFun,nvars,[],[],[],[],LB,UB);
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue."
Please find attached my main GA code and the fitness function code
Thank you as I await your reply
Ibrahim
% Main code for maximizing fitness function using GA (Genetic algorithm)
ObjFun =@myfitness;
nvars = 5;
LB = [1e-3 0.5e-3 0.1e-3 0.01e-3 0.5e-3];
UB = [5e-3 2.5e-3 0.2e-3 0.05e-3 5e-3];
[Lbeam,width,thickbeam,thickpiezo,Lpiezo,fval] = ga(ObjFun,nvars,[],[],[],[],LB,UB);
%Fitness function
function Veb = myfitness(Lbeam,width,thickbeam,thickpiezo,Lpiezo)
rho = 2700; %kg/m^3 density
Ebeam = 70e9; %Pa Young's modulus
Lf = .003; %m length to forcing f(t)
%piezo properties
g31 = -9.5e-3; %V*m/N voltage constant
Epiezo = 50e9; %Pa Young's Modulus
%Parameters for optimization
syms x
syms Lbeam
syms width
syms thickbeam
syms thickpiezo
syms Lpiezo
%Natural frequencies of the beam for a clamped-free beam
Area = thickbeam*width; %m^2 Area
I = width*thickbeam^3/12; %m^4 M. A. of inertia
BNl = 1.8751;
BN = BNl/Lbeam;
wn = ((BN)^2)*sqrt((Ebeam*I)/(rho*Area));
%Mode shapes
alpha =((sin(BNl)+sinh(BNl)))/(cos(BNl)+cosh(BNl));
ModeShape =sin(BN*x)-sinh(BN*x)-(alpha*(cos(BN*x)-cosh(BN*x)));
%Harmonic forcing function
w = wn;
zeta = .03;
wd = wn*(1-zeta^2)^5;
syms t tt
Force = (.35)*sin(w*tt)*1/(rho*(thickbeam*width))*subs(ModeShape,x,Lf);
q = 1/wd*exp(-zeta*wn*t)...
*int(Force*exp(zeta*wn*tt)*sin(wd*(t-tt)),tt,0,t);
%Develop equation for voltage
before_w3 = q*ModeShape;
w3 = before_w3;
K_x_t = vpa(diff(w3,x,2),5);
Kavg = vpa((1/Lpiezo)*int(K_x_t,x,0,Lpiezo),5);
tau = 2;
%tau = 2:.01:5;
I = width*thickbeam^3/12;
Moment = Ebeam*I*Kavg;
psi = (Ebeam*thickbeam)/(Epiezo*thickpiezo);
T = thickbeam/thickpiezo;
Veb_sym= (6*g31*psi*(1+T))/(width*thickpiezo*(1+psi^2*T^2+2*psi*(2+3*T+2*T^2)))*Moment;
Veb1 = subs(Veb_sym,'t',tau);
Veb =(-Veb1);
end
0 个评论
采纳的回答
Matt J
2020-7-18
编辑:Matt J
2020-7-18
You should not have symbolic input/output in myfitness(). ga() is a numerical solver, so the operations in myfitness() should take numbers as input and produce numbers as output, not symbolic expressions. Also, your fitness function should have all the unknowns contained in a single input vector:
function Veb = myfitness(unknowns)
[Lbeam,width,thickbeam,thickpiezo,Lpiezo] ...
= deal(unknowns(1),unknowns(2), unknowns(3), unknowns(4), unknowns(5));
....
Veb = .... ;
end
4 个评论
Matt J
2020-7-20
I don't even know what that would mean. If the fitness function returns a vector, what does it mean to "minimize" it?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!