matlab - Matlab Pso 算法
问题描述
我正在尝试使用 MATLAB 中的 PSO 优化器解决符号优化问题。变量 rxacn theta z 是符号变量,CD 是通过积分 r 计算的。CD 是目标函数,自由变量 a、n、theta 和 lb 和 ub 是边界。完整代码如下:
syms r x a c n theta z
assume(n,'positive');
D=0.24;
L=2;
f=L/D;
b=.8;
a0=0.02;
db=0.05;
V=1;
Re=(V*(D/2))/0.000001;
Cf=(0.075/(((log10(Re))-2)^2))+0.00025;
% Define r(x)
c=L-a-b-a0;
r1=0.5*D*(2*x/a)^(1/n);
I1=simplify(int(2*pi*r1,x,a0,a));
r2=D/2;
I2=simplify(int(2*pi*r2,x,a,a+b));
r3=(0.5*D)-((((3*D)/(2*(c)^2))-(tan(theta)/c))*(x-a-b)^2)+(((D/c^3 ...
(tand(theta)/c^2))*(x-a-b)^3);
I3=simplify(int(2*pi*r3,x,a+b,L));
A=simplify(I1+I2+I3);
Sn=pi*(D^2/4);
Cdstar=Cf*(1+(60*f^-3 )+(0.0025*f))*(A/(L^2));
Cdb=0.029*((db/D)^3)*(Cdstar^-0.5)*(Sn/(L^2));
CD=simplify(Cdstar+Cdb);
%optimization problem
objective=matlabFunction(CD,'Vars',[a,n,theta])
nVar=3;
lb = [deg2rad(5),0.25,a0];
ub = [deg2rad(60),5,L/2];
options =
optimoptions('particleswarm','SwarmSize',100,'HybridFcn',@fmincon);
[z,fval,exitflag,output] = particleswarm(objective,nVar,lb,ub,options)
这是我得到的错误:
@(a,n,theta)pi.*4.404634153141517e-4+pi.*1.0./sqrt( pi.4.404634153141517e-4-pi. (a.*5.0-6.0).* 1.0./(a.5.0 e+1-5.9e+1).^3. (a.*2.32335e+6+tan((theta.*pi)./1.8e+2).*4.779e+6-tan(theta).* 6.2658e+6-a.^2.*tan(theta).*2.655125e+7+a.^3.*tan(theta).*1.4875e+7-a.^4.*tan(theta)。 *3.125e+6-a.*tan((theta.*pi)./1.8e+2).*1.59975e+7+a.*tan(theta).*2.1063e+7+a.^2. *tan((theta.*pi)./1.8e+2).*2.008125e+7-a.^3.*tan((theta.*pi)./1.8e+2).*1.1203125e+7 +a.^4.*tan((theta.*pi)./1.8e+2).*2.34375e+6-a.^2.*1.98e+6+a.^3.*5.625e+5 -9.08811e+5).*1.223509486983755e-5-(n. pi. ((a.*2.5e+1).^(-1.0./n)-2.0.^(1.0./n+1.0)。 *a.*2.5e+1).*1.101158538285379e-5)./(n+1.0))。9.440104166666668e-7-pi。(a.*5.0-6.0).*1.0./(a.5.0e+1-5.9e+1).^3 .(a.*2.32335e+6+tan((theta.*pi)./1.8e+2).*4.779e+6-tan(theta).*6.2658e+6-a.^2.*tan( theta).*2.655125e+7+a.^3.*tan(theta).*1.4875e+7-a.^4.*tan(theta).*3.125e+6-a.*tan((theta .*pi)./1.8e+2).*1.59975e+7+a.*tan(theta).*2.1063e+7+a.^2.*tan((theta.*pi)./1.8e +2).*2.008125e+7-a.^3.*tan((theta.*pi)./1.8e+2).*1.1203125e+7+a.^4.*tan((theta.* pi)./1.8e+2).*2.34375e+6-a.^2.*1.98e+6+a.^3.*5.625e+5-9.08811e+5).*1.223509486983755e-5- (n. pi. ((a.*2.5e+1).^(-1.0./n)-2.0.^(1.0./n+1.0).*a.*2.5e+1).*1.101158538285379e -5)./(n+1.0)
没有足够的输入参数。
symengine 错误>@(a,n,theta)pi.*4.404634153141517e-4+pi.*1.0./sqrt( pi.4.404634153141517e-4-pi. (a.*5.0-6.0).*1.0./ (a. 5.0e+1-5.9e+1).^3. (a.*2.32335e+6+tan((theta.*pi)./1.8e+2).*4.779e+6-tan( theta).*6.2658e+6-a.^2.*tan(theta).*2.655125e+7+a.^3.*tan(theta).*1.4875e+7-a.^4.*tan (theta).*3.125e+6-a.*tan((theta.*pi)./1.8e+2).*1.59975e+7+a.*tan(theta).*2.1063e+7+a .^2.*tan((theta.*pi)./1.8e+2).*2.008125e+7-a.^3.*tan((theta.*pi)./1.8e+2).* 1.1203125e+7+a.^4.*tan((theta.*pi)./1.8e+2).*2.34375e+6-a.^2.*1.98e+6+a.^3.* 5.625e+5-9.08811e+5).*1.223509486983755e-5-(n. pi. ((a.*2.5e+1).^(-1.0./n)-2.0.^(1.0./n +1.0).*a.*2.5e+1).*1.101158538285379e-5)./(n+1.0))。9.440104166666668e-7-pi。(a.*5.0-6.0).*1.0./(a.5.0e+1-5.9e+1).^3 .(a.*2.32335e+6+tan((theta.*pi)./1.8e+2).*4.779e+6-tan(theta).*6.2658e+6-a.^2.*tan( theta).*2.655125e+7+a.^3.*tan(theta).*1.4875e+7-a.^4.*tan(theta).*3.125e+6-a.*tan((theta .*pi)./1.8e+2).*1.59975e+7+a.*tan(theta).*2.1063e+7+a.^2.*tan((theta.*pi)./1.8e +2).*2.008125e+7-a.^3.*tan((theta.*pi)./1.8e+2).*1.1203125e+7+a.^4.*tan((theta.* pi)./1.8e+2).*2.34375e+6-a.^2.*1.98e+6+a.^3.*5.625e+5-9.08811e+5).*1.223509486983755e-5- (n. pi. ((a.*2.5e+1).^(-1.0./n)-2.0.^(1.0./n+1.0).*a.*2.5e+1).*1.101158538285379e -5)./(n+1.0)
particlewarm>makeState 中的错误(第 694 行)firstFval = objFcn(state.Positions(1,:));
particlewarm>pswcore 中的错误(第 169 行) state = makeState(nvars,lbMatrix,ubMatrix,objFcn,options);
particlewarm 中的错误(第 151 行)[x,fval,exitFlag,output] = pswcore(objFcn,nvars,lbRow,ubRow,output,options);
MYRING_SYMS_optimisation_K 中的错误(第 56 行)[z,fval,exitflag,output] =particlewarm(objective,nVar,lb,ub,options)
原因:初始目标函数评估失败。PARTICLESWARM 无法继续。
解决方案
fun
只接受一个参数,即带有nvars
元素的向量。来自particleswarm
文档:
x = particleswarm(fun,nvars)
试图找到一个向量 x 达到 的局部最小值fun
。nvars
是 的维度(设计变量的数量)fun
。
所以你需要声明一个只需要 1 个参数的新目标函数:
[z,fval,exitflag,output] = particleswarm( ...
@(x) objective(x(1), x(2), x(3)), ...
nVar,lb,ub,options)
推荐阅读
- matlab - 用于连接字符串的版本兼容方法
- python-3.x - 我想我在 jupyter notebook/python3 中进行故障排除时搞砸了我的文件目录
- python - 使用python对图像中的像素进行基于密度的异常值检测
- spring - Spring Security 和 i18n
- java - Java 反斜杠命令
- c# - 检查两条 Vector3 线何时相交 - Unity3D
- android-emulator - 无法在禁用 Hyper-V、AMD 处理器的情况下安装 HAXM
- wordpress - 无法更改 workdpress 网站上的背景图片
- java - 如何同步执行异步任务Android java
- java - json 的 java.util.regex.Pattern