1

I am new to MATLAB and making a project on Path Planning on a 3D environment with PSO. I managed to write my main function as :

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);

above is a part of my main program. My PathCostPSO function is:

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) ;

Please help if there is some error in implementing the my cost function in the main program for which when the program is run there is no output of Best Function value, rather it is an "inf" value.

3kka-abhi
  • 11
  • 1
  • have you initialized your `global` variables? (are those absolutely necessary? Since they don't change, you might just define them as input and create an *anonymous function handle*). The input `nVar` is undefined (most likely it is `nVar = n*4` but please create a [minimal **reproducible** example](https://stackoverflow.com/help/minimal-reproducible-example) – max Jun 27 '20 at 06:30
  • Thanks mate, I did the change in nVar and results are coming fine. – 3kka-abhi Jun 27 '20 at 14:04

0 Answers0