File Exchange

## Multi-Objective Particle Swarm Optimization (MOPSO)

version 1.3.2.0 (433 KB) by
Bearable and compressed implementation of Multi-Objective Particle Swarm Optimization (MOPSO)

Updated 27 Nov 2019

This function performs a Multi-Objective Particle Swarm Optimization (MOPSO) for minimizing continuous functions. The implementation is bearable, computationally cheap, and compressed (the algorithm only requires one file: MPSO.m). An 'example.m' script is provided in order to help users to use the implementation. It is also noteworthy to mention that the code is highly commented for easing the understanding. This implementation is based on the paper of Coello et al. (2004), "Handling multiple objectives with particle swarm optimization".

IMPORTANT: the objetive function that you specify must be vectorized. This means that it will take the entire population (i.e., a matrix Np x nVar, which Np is the number of particles and nVar is the number of variables) and it expects to receive a fitness value for each particle (i.e., a vector Np x 1). If the function is not vectoriyed and receives only a single value, the code will obviously rise an error.

### Cite As

Víctor Martínez-Cagigal (2021). Multi-Objective Particle Swarm Optimization (MOPSO) (https://www.mathworks.com/matlabcentral/fileexchange/62074-multi-objective-particle-swarm-optimization-mopso), MATLAB Central File Exchange. Retrieved .

Upama Dey

thank you so much for this code. it has helped me a lot. I also need a MOPSO for maximizing continuous functions. can you tell me how to do that?

Moritz Schappler

Chengtao Xu

Yuanqing Liu

great job

zaki dahia

hi ,@ naf_001@yahoo.com, I couldn't understand how can i implemented my problem in Multi-Objective Particle Swarm Optimization because i am not understanding the code properly.
%%%%%%%%%%%%%%%% example.m
Tp=140 ;Tf= 600;Cp=5000;Cf=35000;m=3;n=8000;
f = @(x) (Cp+Cf*((x/n)^m))/(x+Tp+Tf*((x/n)^m));
g = @(x) x/(x+Tp+Tf*((x/n)^m));
MultiObj.fun = @(x) [f(x), g(x)];
MultiObj.nVar = 1; % Change this as per your no of variables

Wagner Saldanha

I would like to ask for help. I couldn't understand why the ZDT 4 function doesn't converge. Thank you very much in advance.

case 'ZDT4'
f = @(x) x(:,1);
g = @(x) 1+10.*(10-1)+sum(((x(:,2:end).^2)-10.*cos(4.*pi.*x(:,2:end))),2);
h = @(x) 1 - sqrt(f(x)./g(x));
MultiObj.fun = @(x) [f(x), g(x).*h(x)];
MultiObj.nVar = 10;
MultiObj.var_min = [0, -5, -5, -5, -5, -5, -5, -5, -5, -5];
MultiObj.var_max = [1, 5, 5, 5, 5, 5, 5, 5, 5, 5];

chandan mahata

Hello Sir,
How to call the MOPSO results? How to get the optimum function value and the corresponding independent variables?

Richard Lee

Thanks, @Víctor Martínez-Cagigal, I have solved my problem after understanding your code properly. Anyone if face the problem like me plz drop a msg to me @ naf_001@yahoo.com. This is a really good code for MOPSO. My problem was something like this ...
______________________________________
MultiObj.fun = @(x) [f1(x), f2(x), f3(x), f4(x)];
MultiObj.nVar = 5;
MultiObj.var_min = [0, 2, 1, 1, 1];
MultiObj.var_max = [1, 10, 5, 5, 2000];

@Rahul Chaurasiya, it can be easily be applied to 3 objective functions, as you see there are some examples of 3 objective functions. Something like this ...
f = @(x) 1 - exp(-4.*x(:,1)).*sin(6.*pi.*x(:,1));
g = @(x) 1 + 9.*(sum(x(:,2:end),2)./(size(x,2)-1)).^0.25;
h = @(x) 1 - (f(x)./g(x)).^2;
MultiObj.fun = @(x) [f(x), g(x), h(x)];
MultiObj.nVar = 10; % Change this as per your no of variables

happy MOPSO

rahul chaurasiya

I want to optimize a 3 objective optimization problem (f1,f2,f3). Is it possible to apply the code for the same. What major changes are required in the current version (including 3D scattering of the Pareto-optimal solutions)?

Huaiyu Wang

It is perfect and easy to apply a new function for optimization.
What should I do if I want to get the dominate fit and fit_pos and draw a new figure.
And I always use your nsga2 demo to the same function ,but I can't find the similarly 'REP' like MOPSO REP.

Parvesh Raheja

@Victor Martinez-Cagigal

I have sent you the email on : victor.martinez@gib.tel.uva.es

Regarding the problem.
Thanks

Parvesh Raheja

@Victor Martinez-Cagigal

I'm sorry, but if the examples work, the problems you're encountering are because of your code. The function is completely commented. Read, at least, the headers; and implement your code according to the instructions. If you keep getting an error, and you are completely lost, email me with the code so I can reproduce it.

Parvesh Raheja

@Victor Martinez-Cagigal
I am having the same problem, during the MOPSO, Please let me know, if you have any solution for the same.

You can email me also on
raheja.parvesh@gmail.com

what is the solution for this error??
It appears after some number of iterations.
I wish to have a solution

Unable to perform assignment because the left and right sides have a different number of elements.

Error in GetGridIndex (line 40)
SubIndex(j)=i;

Error in MOPSO (line 113)
[rep(i).GridIndex rep(i).GridSubIndex]=GetGridIndex(rep(i),G);

@Víctor Martínez-Cagigal

I am not understanding what do you mean by vectorization.

@ Víctor Martínez-Cagigal

I sent you an email.

what is the solution of this error?
Error using nchoosek (line 65)
K must be an integer between 0 and N.

Error in MOPSO2>checkDomination (line 186)
all_perm = nchoosek(1:Np,2); % Possible permutations

Error in MOPSO2 (line 27)
DOMINATED= checkDomination(POS_fit);

Error in RUN (line 22)
REP = MOPSO2(params,MultiObj);

The PF is only used in the examples. It represents the optimal solution, it is just for visualization.

what is the PF if i am going to use my own objective function to be optimized

There are no missing files, you need to download the entire .zip folder, instead of copying the script from the web editor.

in the main file "MOPSO" there are functions not identified or missing from the folder.

T Kalog

@sim_892 MATLAB is now complaining because you cannot do bit-wise operations (with the point .) if the size of the matrices are not the same... So I guess your objective function is not properly definet yet.

Anyway, you can write your objective function in a file if it suits better to you. For example, a file named myFunction, which declares a function out = myFunction(x) that takes the matrix "x" and returns a vector "out". Then the objective function could be called like this: @(x) myFunction(x);

sim_892

Thank you @Víctor Martínez-Cagigal, now I understand, but my problem remains, unfortunately. I specified all my variables of the objective function in the correct form: x(:,1), x(:,2), x(:,3) etc.. I also use everywhere the . opertator as you explained but now I get the following error:
Matrix dimensions must agree.

Error in example>@(x)1i.*w.*C2_25(x)+G3_25(x)

Error in example>@(x)(Y_qw_25(x).*Y_ser_25(x))./(Y_qw_25(x)+Y_ser_25(x))

Error in example>@(x)Y_1_25(x)+1i.*w.*C1_25(x)+G1_25(x)

Error in example>@(x)(Y_tb_rtd_25(x).*(1./(1i.*w.*x(:,11))))./(Y_tb_rtd_25(x)+(1./(1i.*w.*x(:,11))))

Error in example>@(x)Y_1p_25(x)+1i.*w.*x(:,12)

Error in example>@(x)((1./Y_tot_25(x))-50)./((1./Y_tot_25(x))+50)

Error in example>@(x)sum((abs(real(S11(:,25))-real(S_tot_25(x)))+abs(imag(S11(:,25))-imag(S_tot_25(x)))).^2)

Error in example>@(x)[f1(x),f2(x)]

As you can see I need some functions of functions to specify my objective function since otherwise its too big to write in a single expression. But I think that the real problem is my final objective function is now a single value but rather an array (a function vs frequency points), and I guess that the vector of the frequency w as I use in my objective functions, somehow messes up with your definition of the variables x(:,1), x(:,2), x(:,3)

@sim_892 As stated in the comments, my objective function performs a vectorized approach to evaluate all the particles at once and return a fitness for each one. Therefore, as the input must be the population (matrix of n_particles x n_variables), x(:,1) just takes the first variable of all the particles at once.

Regarding your second issue, you are not using array operations (.^, .*, ./, etc) and thus MATLAB complains about it. This is a very basic concept of MATLAB, so I suggest you to take some courses or tutorials (https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html).

sim_892

If I use your formatting (from examples) for the Objective function I get the following error:
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers,
use '.^'.

sim_892

Hi, I don't understand how to properly program the objective functions and I am pretty new with Matlab: In my previous implementations of PSO I would simply express the variables of my objective functions in the form of x(1), x(2), x(3), ..... but you express those variables sometimes in form x(:,1) some others just x. Can you give an example in which the Objective functions are expressed in function of single element variables (x(1), x(2),... etc) ?
Thank you

Saiara Samira Sajid

So, for those that have the problem in nchoosek. The error says that you are taking <2 particles when updating the repository. This is usually because you have specified a badly programmed objective function.

The objetive function that you specify must be vectorized. This means that it will take the entire population (i.e., a matrix Np x nVar, which Np is the number of particles and nVar is the number of variables) and it expects to receive a fitness value for each particle (i.e., a vector Np x 1). If the function is not vectoriyed and receives only a single value, the code will obviously rise an error.

Saiara Samira Sajid

Hello,
Thanks a lot for the code . However, I am facing the same problem as Srinivas Kolluru.
My objective Function contains for loop. So I am not sure how to solve it.

Error using nchoosek (line 65)
K must be an integer between 0 and N.

Error in MOPSO>checkDomination (line 179)
all_perm = nchoosek(1:Np,2); % Possible permutations

Error in MOPSO (line 23)
DOMINATED= checkDomination(POS_fit);%this is causing problem

Error in Flowshop (line 20)
REP = MOPSO(params,MultiObj);

Wagner Saldanha

Thank you!
I can't use functions with if conditions, would it be possible to use if in function handle?

Hey i am very new with matlab , how to start using the tool box ?

yujin

tianqing liu

xiaofu zhang

Thank you for this very nice code.
But when i run the 'example.m' ,it has the error as follow.Could you give me some advice?

Unable to read file 'ParetoFronts/Kursawe.mat'. There are no such files or directories.

Xiancai Wei

Sir in single objective 'artificial bee colony optimization' we calculate fitness using the formula fit_i=1/1+f_i, where f_i is the cost function value. Could you please tell me how can we calculate fitness in multi objective artificial bee colony optimization.

Thank you for this very nice code

Hi Nima,

Just refer to the example of Kursake function. As can be noticed, the function has 3 variables (nVar parameter). The limits are defined in var_min and var_max as vectors which, in that case, are [-5 -5 -5] and [5 5 5], respectively. You should define your limits in that way.

nima rezaee

firs of all awesome code and algorithm. thank you.
but unfortunately i have the same problem as RAVI MANDAVA. i want to have different varmin and varmax for each variable in mopso.
i can change the code but im not sure it will work. i dont know maybe im missing something.

shdotcom shdotcom

Great work，Thank you very much

Dear Srinivas Kolluru,

since your error is returned from a function called "MOPSO_new", I suppose that the problem lies in your own version of the algorithm. Send me an e-mail and perhaps I can help you.

Regards.

Srinivas Kolluru

Hello Martinez,

Thank you for the code. But when i run the code, i am facing a similar error reported earlier by Devin.
The number of particles = 200;

Error using nchoosek (line 65)
K must be an integer between 0 and N.
Error in MOPSO_new>checkDomination (line 226)
all_perm = nchoosek(1:Np,2); % Possible permutations
Error in MOPSO_new (line 67)
DOMINATED= checkDomination(POS_fit);

Can you help me with this please

Yuyang Wang

Works and easy to understand and modify.

Xu Shuhui

great work，Thank you very much

jogo

as expected it outperforms the gamultiobj from Matlab
I can´t understand why they have a single PSO and not a MOPSO ....
Thanks for this nice code!

@zkl, Actually, the code plots the current generation positions in red, and the Pareto Front particles in black. Thus, the Pareto Front are only the black points.

@Ali Chamseddine, Indeed, constraints are not implemented here, since it is a "simple" example. In order to add them, you will need to create a feasibility function that check, in each generation, if the particles positions are feasible taking into account the given constraints. In case that some of they were not, you should modify their positions.

zkl

hi, thank you for sharing. i want to ask a question. why not all the Particles are at the pareto front when i run your example? Is it correct when most of them are at the front while a little of them are not.

Ali Chamseddine

Thank you for sharing it! However I would like to ask about setting up constraints, I haven't found any constraints implemented in any of the examples! Any tips on how to do that would be greatly appreciated, thanks again!
Ali

Andrés F. Abril

Zenith Zhou

Great！thx for sharing.I'm now studying MOPSO and do meet some trouble in coding...

milan joshi

how do i test new function ...I am very new to MATLAB...any help would be highly appreciated. I dont know how to check MOPSO for new function...

haifeng lu

hao Liu

@bulin wen, Thank you for your feedback. The references are indicated in comments inside the MOPSO.m function. Mainly, I based my algorithm in:
 Coello, C. A. C., Pulido, G. T., & Lechuga, M. S. (2004). Handling multiple objectives with particle swarm optimization. IEEE Transactions on evolutionary computation, 8(3), 256-279.
 Sierra, M. R., & Coello, C. A. C. (2005, March). Improving PSO-based multi-objective optimization using crowding, mutation and e-dominance. In International Conference on Evolutionary Multi-Criterion Optimization (pp. 505-519). Springer Berlin Heidelberg.

@Mengying Zhang, Thank you for the feedback. There wasn't rationale for not including the ZDT4 problem, perhaps I didn't found the equations when I wrote the code. However, it is easy to implement, feel free to add it ;)

bulin wen

thank you very much! It's very useful.And could you tell me what the references you have used?

Mengying Zhang

Thank you for uploading this code. Yet I found that you excluded ZDT4 test problem in this code. Have you ever tested ZDT4 with it ?

Mehmet Gulec

Great job.

Edit: The error says that "K must be an integer between 0 and N". Since K is 2, it indicates that Np is 1 or 0. In other words, the number of particles that you have specified is less than 2 and thus, MOPSO cannot run.

Hi Devin,

I have tested the algorithm again with the 5 functions that I provide in the "example.m" script and it does not throw any error. Probably, the MultiObj function that you are using is not correct. Can you provide me more information about your run?

Despite that, although I do not believe that this is the cause of the problem, what version of MATLAB are you using? In my case, I tested the algorithm with versions greater than 2015a.

Regards,
Víctor.

Devin

Error using nchoosek (line 65)
K must be an integer between 0 and N.

Error in MOPSO>checkDomination (line 218)
all_perm = nchoosek(1:Np,2); % Possible permutations

Error in MOPSO (line 67)
DOMINATED= checkDomination(POS_fit);

Error in example (line 76)
MOPSO(params,MultiObj);

##### MATLAB Release Compatibility
Created with R2016a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux