This function performs a MultiObjective 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.
Víctor MartínezCagigal (2020). MultiObjective Particle Swarm Optimization (MOPSO) (https://www.mathworks.com/matlabcentral/fileexchange/62074multiobjectiveparticleswarmoptimizationmopso), MATLAB Central File Exchange. Retrieved .
1.3.2.0  Now the function raises a warning if the objective function is badly programmed, which is usually the issue that is reported in some comments. 

1.3.1.0  ParetoFront folder is now uploaded 

1.3.0.0  Function is modified in order to return the data form the repository 

1.3.0.0  Optimal Pareto Fronts are updated. 

1.2.0.0  More benchmark functions and optimal Pareto Fronts are implemented 

1.1.0.0  Mutation operator and crowding factor for repository removing are applied. 
Create scripts with code, output, and formatted text in a single executable document.
zaki dahia (view profile)
hi ,@ naf_001@yahoo.com, I couldn't understand how can i implemented my problem in MultiObjective 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
MultiObj.var_min = 1.*ones(1,MultiObj.nVar); % instead of zero write your lower limit
MultiObj.var_max = 25000.*ones(1,MultiObj.nVar); % instead of one write your upper limit
Thank you very much in advance Nafees Ahamad
Wagner Saldanha (view profile)
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.*(101)+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 (view profile)
Hello Sir,
How to call the MOPSO results? How to get the optimum function value and the corresponding independent variables?
Richard Lee (view profile)
Nafees Ahamad (view profile)
Thanks, @Víctor MartínezCagigal, 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];
Nafees Ahamad (view profile)
@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
MultiObj.var_min = 0.*ones(1,MultiObj.nVar); % instead of zero write your lower limit
MultiObj.var_max = 1.*ones(1,MultiObj.nVar); % instead of one write your upper limit
happy MOPSO
rahul chaurasiya (view profile)
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 Paretooptimal solutions)?
Huaiyu Wang (view profile)
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 (view profile)
@Victor MartinezCagigal
I have sent you the email on : victor.martinez@gib.tel.uva.es
Regarding the problem.
Thanks
Parvesh Raheja (view profile)
@Victor MartinezCagigal
Sir, the code you have made is for 2 objectives. how can i change for 6 objectives. Can you please help.
Víctor MartínezCagigal (view profile)
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 (view profile)
@Victor MartinezCagigal
@ahmad eldeep
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
ahmad eldeeb (view profile)
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);
ahmad eldeeb (view profile)
@Víctor MartínezCagigal
I am not understanding what do you mean by vectorization.
ahmad eldeeb (view profile)
@ Víctor MartínezCagigal
I sent you an email.
Víctor MartínezCagigal (view profile)
Check the previous comments.
ahmad eldeeb (view profile)
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);
Víctor MartínezCagigal (view profile)
The PF is only used in the examples. It represents the optimal solution, it is just for visualization.
ahmad eldeeb (view profile)
what is the PF if i am going to use my own objective function to be optimized
Víctor MartínezCagigal (view profile)
There are no missing files, you need to download the entire .zip folder, instead of copying the script from the web editor.
ahmad eldeeb (view profile)
in the main file "MOPSO" there are functions not identified or missing from the folder.
T Kalog (view profile)
Víctor MartínezCagigal (view profile)
@sim_892 MATLAB is now complaining because you cannot do bitwise 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 (view profile)
Thank you @Víctor MartínezCagigal, 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)
Víctor MartínezCagigal (view profile)
@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/arrayvsmatrixoperations.html).
sim_892 (view profile)
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 (view profile)
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 (view profile)
Víctor MartínezCagigal (view profile)
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 (view profile)
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 (view profile)
Thank you!
I can't use functions with if conditions, would it be possible to use if in function handle?
Sharifah Zarith Rahmah Bt Syed Ahmad (view profile)
Hey i am very new with matlab , how to start using the tool box ?
yujin (view profile)
tianqing liu (view profile)
xiaofu zhang (view profile)
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?
Error using load
Unable to read file 'ParetoFronts/Kursawe.mat'. There are no such files or directories.
Xiancai Wei (view profile)
Parmod Yadav (view profile)
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.
Fodhil Fadhila (view profile)
Thank you for this very nice code
Víctor MartínezCagigal (view profile)
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 (view profile)
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.
can someone please help me .
thanks in advance.
shdotcom shdotcom (view profile)
Great work，Thank you very much
Víctor MartínezCagigal (view profile)
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 email and perhaps I can help you.
Regards.
Srinivas Kolluru (view profile)
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 (view profile)
Works and easy to understand and modify.
Xu Shuhui (view profile)
great work，Thank you very much
jogo (view profile)
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!
Víctor MartínezCagigal (view profile)
@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.
Víctor MartínezCagigal (view profile)
@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 (view profile)
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 (view profile)
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 (view profile)
Zenith Zhou (view profile)
Great！thx for sharing.I'm now studying MOPSO and do meet some trouble in coding...
milan joshi (view profile)
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 (view profile)
hao Liu (view profile)
Víctor MartínezCagigal (view profile)
@bulin wen, Thank you for your feedback. The references are indicated in comments inside the MOPSO.m function. Mainly, I based my algorithm in:
[1] 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), 256279.
[2] Sierra, M. R., & Coello, C. A. C. (2005, March). Improving PSObased multiobjective optimization using crowding, mutation and edominance. In International Conference on Evolutionary MultiCriterion Optimization (pp. 505519). 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 (view profile)
thank you very much! It's very useful.And could you tell me what the references you have used?
Mengying Zhang (view profile)
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 (view profile)
Great job.
Víctor MartínezCagigal (view profile)
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.
Víctor MartínezCagigal (view profile)
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 (view profile)
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);