Code covered by the BSD License  

Highlights from
NGPM -- A NSGA-II Program in Matlab v1.4

4.0

4.0 | 4 ratings Rate this file 121 Downloads (last 30 days) File Size: 387.39 KB File ID: #31166
image thumbnail

NGPM -- A NSGA-II Program in Matlab v1.4

by Song Lin

 

23 Apr 2011 (Updated 26 Jul 2011)

NSGA-II and R-NSGA-II in Matlab

| Watch this File

File Information
Description

This program is an implementation of nondominated sorting genetic algorithm II (NSGA-II) proposed by K. Deb. Capabilities:
1. R-NSGA-II: Reference-point-based NSGA-II.
2. Coding: real, integer.
3. GA operator: Intermediate crossover, Gaussian mutation.
4. Constraint handling.
5. Parallel computation of objective function evaluation.
6. Population plot in window.
I write this program because Aravind Seshadri’ program (File ID#10429) could not satisfy my request. I need constraint handling, integer coding to solve a finite element optimization problem. The finite element solution is very time-expensive, thus the parallel computation of objective evaluation is implemented in the code.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
NSGA - II: A multi-objective optimization algorithm

MATLAB release MATLAB 7.11 (2010b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (28)
13 May 2011 ANU

Sorry, I think, your code is full of bugs... please upload the clean code again.

13 May 2011 ANU

Please ignore my previous comment. It works fine with MATLAB 7.11 (2010b).

15 May 2011 ANU

Please try running problem ZDT4 described in paper "A Fast and Elitst Multi-Objective Genetic Algorithm: NSGA-II" before using this software for your research.

It doesn't give the correct solution or even close to Deb's original implementation in c language. It seems there are bugs in this software. I did this for ZDT4 problem:

cons = zeros(1,10);
y(1) = x(1);

V = 10;
t = 0;
for i = 2:V
   t = t + x(i)^2 - 10*cos(4*pi*x(i));
end

g_x = 1+10*(V-1) + t;
y(2) = g_x*(1-sqrt(x(1)/g_x));

16 May 2011 ANU  
15 Jul 2011 Song Lin

The original code could not get the correct Pareto-front because the crossover and mutation strategies I used do not fit for the ZDT problem. In original code, all variables of an individual would mutate if it was selected to be mutated. In the version 1.3 code, I change the strategies to mutate several variables instead, and the correct solution could be get now.

24 Nov 2011 Iman

Dear all,
I defined my two objective functions and got only three points as a solutions (in a Pareto front),thanks to Song. One of them looks like a better soultion .How can I extract the value of x which gives me this soultion?
Best regards

24 Nov 2011 Ivan

Yes, very well written too! Congratulations on the good work !

25 Nov 2011 Song Lin

Hi Iman,
You can check the result file(the default file name is "populations.txt"), or you can use command "vertcat(result.pops(end,:).var)" to combine all of the design variables together.

25 Nov 2011 Ivan  
30 Nov 2011 Iman

Dear Lin,
Thanks for your answer regarding population. There is another question and I hope that you or any other people in here would help me out.
Right now I am using one design variable which uses Newton Raphson calculations by a function name as Raphson(x), thus in the objfun m file we have:
function [y, cons] = TP_Test_objfun(x)
y = [0,0];
cons = [];
[Vm, Pl] = Raphson(x);
y(1)=Vm;
y(2) = Pl;
Now I want to have two design variable so I change [Vm, Pl] = Raphson (x(1),x(2));
I added a new bounds for the second design variable as :
options.lb = [2 5]; % lower bound of x
options.ub = [14 25]; % upper bound of x
and also options.numVar = 2;
Consequently I changed the Raphson function as :
function [Vm, Pl] = Raphson(n,m) and changed a constant value with m.
But I unfortunately my program gives me this error:
??? Subscript indices must either be real positive integers or logicals.
Error in ==> Raphson at 34
mpc.gen (n,PG) = m; %add 10 MW to the bus located at row n (if row 7 so it is
added to bus 7)
Error in ==> TP_Test_objfun at 4
[Vm, Pl] = Raphson (x(1),x(2));
Error in ==> evaluate>evalIndividual at 67
[y, cons] = objfun( indi.var, varargin{:} );
Error in ==> evaluate at 45
        [pop(i), allTime(i)] = evalIndividual(pop(i), opt.objfun, varargin{:});
Error in ==> nsga2 at 81
[pop, state] = evaluate(opt, pop, state, varargin{:});
Error in ==> TP_Test at 28
result = nsga2(options); % begin the optimization!

It looks like that the program have a problem in Raphson finction but this seems unlikely since Raphson works quite well with one variable and the only change that I have made in Raphhson is exchanging one constant with a value .
Do you think that I need to define it differently?
Thank you in advance

30 Nov 2011 Iman

Regarding my question on 30 Nov 2011 , I should also mention that I changed options.vartype =[2] to options.vartype =[2 1];as my second design variable is real but still have the error as :
??? Subscript indices must either be real positive integers or logicals.

01 Dec 2011 Iman

Dear all ,Ignore my two comments on 30 Nov.It is resolved.Thank you

09 Dec 2011 Iman

Dear All
I have a question about defining objective function.
As far as the number of design variables are fixed, there is no problem in defining the objective functions. For example my objective function works very well even if it calls another function in it:
function [y, cons] = TP_Test_objfun(x)
y = [0,0];
cons = [];
[Vm, Pl] = MyFunction (x(1),x(2));
SUM=0;
for k=1:14
    SUM=(((1.060-Vm(k))/1.060).^2)+SUM;
end
y(1)=SUM;
y(2) = Pl;

But the problems begin when other design variables depends on the first design variable.
For example if x(1)=1 the MyFunction1 should be called with other two design variables x (2), x(5)
If x(1)=2, the MyFunction2 should be called with other design variables x(2),x(3),x(5),x(6)
Is it correct to write the code as follows for my problem?
function [y, cons] = TP_Test_objfun(x)
y = [0,0];
cons = [];
switch x(1)
    case 1
        [Vm, Pl] = MyFunction1 (x(1),x(2),x(5));

case 2
[Vm, Pl] = MyFunction2 (x(1),x(2),x(3),x(5),x(6));
case 3
[Vm, Pl] = MyFunction3 (x(1),x(2),x(3),x(4),x(5),x(6),x(7));
end
SUM=0;
for k=1:14
    SUM=(((1.060-Vm(k))/1.060).^2)+SUM;
end
y(1)=SUM;
y(2) = Pl;
Is it correct? It is a bit confusing since in population .txt I see all other six design variables (x(2),x(3),x(4),x(5),x(6),x(7)) even for case 1
Thank you for your time and concern

19 Dec 2011 Andreas

Is it possible that the constraints in the output file "populations.txt" are not displayed? They are calculated correctly in the Matlab-workspace, but in "populations.txt" there stands only zero at the constraints' place.
Does anybody have any idea?

21 Dec 2011 Song Lin

I forgot to save the constraint variables to the individual structure in the function 'evalIndividual' in file 'evaluate.m'.

Modify the code as below:

...
indi.obj = y;
indi.cons = cons; // Add this line
if( ~isempty(indi.cons) )
    idx = find( cons );
...

21 Dec 2011 Iman

Dear Song Lin,
Do u have any idea regarding my question asked on 09 Dec?

23 Dec 2011 Song Lin

Hello, Iman. If x(1)=1, the x(3), x(4), x(6), x(7) values do not affect the objective values. These design variables can be ignored when you process the optimzation results. The optimization progress is not affected except for the efficiency. You may need more optimzation generations to get a good result.

10 Jan 2012 Iman

Dear All,
1-What are the default values for crossover and mutation?
From the document we know that crossover could be changed to 1 by “options.crossover={'intermediate', 1};”
and mutation could be changed by options.mutation = {'gaussian', scale, shrink} .If we don't add those line what are the default values?where it is written?
2-Do we need a constant number for scale and shrink ? for example is
options.mutation = {'gaussian', 0.5, 0.6} correct?
Thanks for your time and patience

26 Jan 2012 Jonathan Roy

I get this error (Undefined function or method 'ndsort' for input arguments of type 'struct') when function nsga2 call "fct ndsort".
Some one seen this error before and can help me?

29 Feb 2012 Andreas

Hello to all!
How can I specify the format type of the solution parameters? For example, I would like to have only integer values for x1, but x2 should be of type double.
Hope you can help me.
Best wishes!
Andreas

29 Feb 2012 Iman

Dear Andreas,
NGPM uses integer coding to handle discrete design variables. You can use options.vartype to define integer coding. More info could be found in the manual document .

29 Feb 2012 Andreas

Thanks for your answer, Iman.

05 Mar 2012 Andreas

Hello!
I've got another question and it would be nice if you could help me one more time.
I have 3 design variables but the third one can only reach certain values. Now my question is:
Can I specify a matrix or something similar for this variable which contains the reachable values so that the algorithm does not have to search the whole "solution space"?
I know that I could do it with constraints but does also a "smarter" solution exist?
Thanks in advance for your answer.
Andreas

06 Mar 2012 Iman

Dear Andreas , I limit my design variables only by the means of constraints . I don't know any other way;however, your idea seems intesrting.Please let me know if you found the answer.

08 Mar 2012 Andreas

Hello Iman!

With limiting by constraints the algorithm worked very slowly, so I did the following:
First of all, let me explain my problem to you: I have to develop an electric machine but I can only use some diameters for the winding.
To solve the problem I round the design variable for the diameter to the next possible value at the beginning of the file TP_CONSTR_objfun. That means the algorithm works with the "admissible" diameters. In the populations.txt-file still stands the "not-admissible" value for the diameter, but since the algorithm doesn't work with this value but because of my rounding with the admissible one, this doesn't matter.
All you have to do is to round the result in populations.txt by your own.
Hope you can understand me.

Regards
Andreas

03 May 2012 Kalyani Mahanthesh

Hi Song,

I am using your NGPM code to solve a 2 objective optimization problem.
There are around 3 constraints.It is a min-max problem.So I am appending the second objective with a minus sign.

There are 30 decision variables(all real).

The mutation and xover fractions are set to 0.002 and 0.9 respectively.I am using parallel processing,with two worker threads.

The problem that I'm facing is that the algorithm fails to discover the pareto-front,could you please let me know what could be the possible reason behind this?

Please note that the objectives are not strictly conflicting.

03 May 2012 Kalyani Mahanthesh

In addition to the above furnished fact,please note that the no. of generation and population size are both set to 500

04 May 2012 escato carreira

verification of lb and ub: have lb twice intead of lb and ub.

if( length(opt.lb)~=opt.numVar || length(opt.lb)~=opt.numVar )
  

Please login to add a comment or rating.
Updates
01 Jul 2011

(2011-07-01)v1.1
Load optimization result from file and restart a optimization from previous optimization result.

15 Jul 2011

v1.3 [2011-07-15]
1. Add: Implement reference-point-based NSGA-II procedure -- R-NSGA-II.
2. Improve: Improve efficiency.
3. Modify: Modify the crossover and mutation strategy.

26 Jul 2011

v1.4 [2011-07-26]
1. Add: Support three or more objectives visualization display in "plotnsga".
2. Add: R-NSGA-II problem: DTLZ2.
3. Improve efficiency for large generation.

Tag Activity for this File
Tag Applied By Date/Time
nsgaii Song Lin 25 Apr 2011 12:04:53
genetic algorithm Song Lin 25 Apr 2011 12:04:53
multiobjective Song Lin 25 Apr 2011 12:04:53
optimization Song Lin 25 Apr 2011 12:04:53
reference point Song Lin 15 Jul 2011 09:40:50
genetic algorithm Oriol Lizandra 30 Dec 2011 04:22:04
multiobjective Nerijus 25 May 2012 03:09:52

Contact us at files@mathworks.com