Particle swarm optimisation problem

Hello all,
I am trying to use PSO for solving a system of nonlinear transcendental equations used in SHE PWM.
Following is the code i am using to solve
fun = @(alpha) pwm_equations_multilevel_pso(alpha0,1,2,harmonics)
nvars = 3;
lb=[0, 0, 0];ub=[deg2rad(90),deg2rad(90),deg2rad(90)];
options = optimoptions('particleswarm','SwarmSize',200,'MaxStallIterations',200)
options = optimoptions(options,'PlotFcn',@pswplotbestf);
[x,fval,exitflag,output] = particleswarm(fun,nvars,lb,ub,options)
where 'pwm_equations_multilevel_pso' is simply as follows
function F_dot = pwm_equations_multilevel_pso(alpha,M,harmonic_cancellation,harmonics)
F(1) = cos(harmonics(1)*alpha(1)) + cos(harmonics(1)*alpha(2)) - cos(harmonics(1)*alpha(3));
F(2) = cos(harmonics(2)*alpha(1)) + cos(harmonics(2)*alpha(2)) - cos(harmonics(2)*alpha(3));
F(3) = (cos(alpha(1)) + cos(alpha(2)) - cos(alpha(3))) - M;
F_dot=dot(F,F)
end
So the scalar value returned is a dot product of F with itself. I want to minimize F_dot to zero, but when i use PSO i get the following result
Unfortunately PSO does not converge to zero, it does not do anything in my case.
Obviously I am doing something wrong, but I am not sure what
Any help will be appreciated!

6 Comments

fun = @(alpha) pwm_equations_multilevel_pso(alpha0,1,2,harmonics)
Your objective function ignores the current state vector, alpha, and always calculates the equations on the same inputs, alpha0, 1, 2, harmonics . That is not going to change with the state vector, so you are going to get constant output instead of minimization.
Hi Walter,
Thank you for your reply.
Yes you are right, changing 'alpha0' to 'alpha' did the trick.
untitled1.jpg
However it still does not converge to zero which is what i want. I have attached the plot. I tried to use ObjectiveLimit option to +0.01 and -0.01, in that case since the value of the first swarms being considered is already less than 0.01, the iteration stop.
Again would be nice if you can help me out here
Please post enough so that we can test your code -- in particular we need the value of harmonics
Question: why do you have harmonic_cancellation as a parameter of the function when you do not use it?
RAN
RAN on 18 Aug 2019
Edited: RAN on 18 Aug 2019
Hello Walter,
harmonics = [5 7]
Actually the objective function looks as follows
function F_dot = pwm_equations_multilevel_pso(alpha,M,harmonic_cancellation,harmonics)
% Cancellation of 5th harmonic
if harmonic_cancellation == 1
F(1) = cos(harmonics(1)*alpha(1)) + cos(harmonics(1)*alpha(2));
F(2) = 4/pi*(cos(alpha(1)) + cos(alpha(2))) -M;
end
% Cancellation of 5th, 7th harmonics
if harmonic_cancellation == 2
F(1) = cos(harmonics(1)*alpha(1)) + cos(harmonics(1)*alpha(2)) - cos(harmonics(1)*alpha(3));
F(2) = cos(harmonics(2)*alpha(1)) + cos(harmonics(2)*alpha(2)) - cos(harmonics(2)*alpha(3));
F(3) = (cos(alpha(1)) + cos(alpha(2)) - cos(alpha(3))) - M;
F_dot=sqrt(dot(F,F));
end
end
Its just different set of equations depending on the number of harmonics cancelled.
However i found a bug in my code, and after solving that the algorithm seems to work ok. I am posting the picture below
So now PSO tries to converge it to zero. But if the objective function already solves to a negative value in the first iteration then PSO stops.
Now the question remains is how to put constraints on alpha while solving the equations. I have ub and lb set as mentioned above for each alpha, but i also want to restrict the solutions such that alpha1<alpha2<alpha3. Is there a way to do that as well?
particleswarm() does not offer any constraints other than lb / ub .
You could add a penalty to the output for inputs that are out of order.
Ok, I already found some constrained based PSO.
Thank you for your help, now i got the hang of PSO.

Sign in to comment.

Answers (1)

Nishma Sai
Nishma Sai on 22 Jan 2021
can u just provide the entire code as i got some errors with my code

1 Comment

You could post your attempt, along with the error message, and we could help you debug the problems.

Sign in to comment.

Asked:

RAN
on 18 Aug 2019

Commented:

on 22 Jan 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!