我是 MATLAB 新手,并且在使用 PSO 的 3D 环境中制作了一个关于路径规划的项目。我设法将我的主要功能写为:
clc;
close all;
MaxIt = 100;
noOfPointsInSolution = 1 ;
n=(noOfPointsInSolution + 1) / 2 ;
options = optimoptions('particleswarm', 'MaxIter', MaxIt, 'Plotfcn', @pswplotbestf);
rng default;
[solution, cost] = particleswarm(@PathCostPSO, nVar, zeros(n*4,1),ones(n*4,1), options);
以上是我的主程序的一部分。我的 PathCostPSO 函数是:
function cost = PathCostPSO(X)
global threat_center threat_radius source goal splineSmoothing cost_arr
lam = 0.5;
path = [source; [X(1:2:end-2)'*500 X(2:2:end-1)'*500 X(3:2:end)'*500]; goal];
cost = lam * getPathLength(path) + (1-lam) * getPenalty(path,threat_center,threat_radius) ;
cost_arr = cat(1,cost_arr,cost/1e+29) ;
如果在主程序中实现我的成本函数时出现一些错误,请帮助,当程序运行时没有输出最佳函数值,而是一个“inf”值。