# NSGA ii algorithm implementation

• January 9th, 2013, 13:12
Melo1991
NSGA ii algorithm implementation
Hi Guys

Very sorry if I am posting this in the wrong forum!

At the minute I am working on implementing the nsga ii algorithm like so http://www.cs.nott.ac.uk/~mvh/teachi...tedSorting.pdf

At the minute I am using the same figures in the example provided and am looking to generate the first front, otherwise known as the undominated front.

I have started to program this and I have got the part working where either 1 dominates the other but the problem comes in when I try program the code that works when neither solution dominates the other, this is severely hindering progress on my project and any help would greatly obliged.

Please tell me this makes sense to some1 out there :D

Code Provided:

Code Java:

```  package dominationcheck;   import java.util.ArrayList;   /** * * @author 09523642 */ public class DominationCheck {   /** * @param args the command line arguments */ public static void main(String[] args) {   Solution s1 = new Solution(13,35); Solution s2 = new Solution(20,18); Solution s3 = new Solution(18,5); Solution s4 = new Solution(8,10); Solution s5 = new Solution(10,25); Solution s6 = new Solution(5,30);   ArrayList<Solution> Undominated = new ArrayList<Solution>(); ArrayList<Solution> Unsorted= new ArrayList<Solution>();   Unsorted.add(s1); Unsorted.add(s2); Unsorted.add(s3); Unsorted.add(s4); Unsorted.add(s5); Unsorted.add(s6);   int z; System.out.println("First List:"); for(int i =0;i<=Unsorted.size()-1;i++) { z=Unsorted.get(i).f1;   System.out.println(""+z+""); }   System.out.println("------------------\n");     Undominated.add(Unsorted.get(0)); Unsorted.remove(0);     int endIdx = 0;   for(int x = 0; x <= Undominated.size()-1; x++) { for(int i=0; i <= Unsorted.size()-1; i++) {   Solution unsorted,undominated;   unsorted = Unsorted.get(i); undominated = Undominated.get(x);       int p = DominationCheck(unsorted,undominated);   switch (p) { case 1: Undominated.remove(x); Undominated.add(unsorted);   break;   case 2:   break;   case 3: if(x==Undominated.size()-1) { Undominated.add(unsorted); break; } else{ break; }   default: System.out.println("Error"); System.exit(0); break;     } } }       System.out.println("Second List:"); for(int a =0;a<=Undominated.size()-1;a++) { z=Undominated.get(a).f1;   System.out.println(""+z+""); }   System.out.println("------------------\n");   }     public static int DominationCheck(Solution unsorted, Solution undominated) { int ans=0; if(unsorted.f1<undominated.f1 && unsorted.f2<undominated.f2) { ans = 1; }   else if(unsorted.f1>undominated.f1 && unsorted.f2>undominated.f2) //s2 dominates s1 { ans = 2; }     else //neither dominate { ans = 3; }       return ans; }   }```
• January 9th, 2013, 13:34
Norm
Re: NSGA ii algorithm implementation
Quote:

the problem comes in when I try program the code that works when neither solution dominates the other
Can you explain the problem?
• January 9th, 2013, 17:09
Melo1991
Re: NSGA ii algorithm implementation
Sorry if I didn't explain this well

If you can see when neither dominates the other the solution taken from the unsorted list is added if the element it's tested against is the last in the undominated list, other wise if there are more elements to be tested against we move on to the next to be tested against .

I hope I have this cleared up, the link above shows what I'm aiming for
• January 9th, 2013, 17:13
Norm
Re: NSGA ii algorithm implementation
How can the posted code be tested? How would the problem be shown when the program is executed?
What does the current program print out when executed? What should it print out?

How can anyone tell if the algorithm is correctly implemented?
• January 9th, 2013, 17:30
Melo1991
Re: NSGA ii algorithm implementation
It's a main class it should run as it is, all I'm missing from above is the solution class which all contains 2 int attributes, f1 and f2

The link I have left in my 1st should describe what I'm trying to do

A the min my program prints the f1 value of the unsorted list then when it goes to find the dominant front it hits a loop it cannot get out of.

If it was correct it should print out the f1 values of the undominated solutions, so it should print out 18, 8, 5

If it's implemented properly I should get that output
• January 9th, 2013, 18:15
Norm
Re: NSGA ii algorithm implementation
The posted code does not compile without errors.

What does the current program print? Post its output.
What should it print? Post what it should output.
• January 10th, 2013, 05:25
Melo1991
Re: NSGA ii algorithm implementation
Code Java:

```    package dominationcheck;   /** * * @author 09523642 */ public class Solution { public int f1; public int f2;   public Solution(int f1,int f2) { this.f1=f1; this.f2=f2;   }   }```

That is the solution class i mentioned earlier, the program should compile

The current out put is :

First List:
13
20
18
8
10
5
------------------

It then hits a loop

Desired Output would be:

First List:
13
20
18
8
10
5
------------------

Second List

18
8
5
---------------------

Have I made myself clearer?
• January 10th, 2013, 10:42
Melo1991
Re: NSGA ii algorithm implementation
I Figured out.... If any1 wants to look at the code by any chance here it is

package dominationcheck;

import java.util.ArrayList;

/**
*
* @author 09523642
*/
public class DominationCheck {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{

Solution s1 = new Solution(13,35);
Solution s2 = new Solution(20,18);
Solution s3 = new Solution(18,5);
Solution s4 = new Solution(8,10);
Solution s5 = new Solution(10,25);
Solution s6 = new Solution(5,30);

ArrayList<Solution> Undominated = new ArrayList<Solution>();
ArrayList<Solution> Unsorted= new ArrayList<Solution>();

int z;
System.out.println("First List:");
for(int i =0;i<=Unsorted.size()-1;i++)
{
z=Unsorted.get(i).f1;

System.out.println(""+z+"");
}

System.out.println("------------------\n");

Unsorted.remove(0);

for(int x = 0; x <= Unsorted.size()-1; x++)
{
for(int i=0; i <= Undominated.size()-1; i++)
{

Solution unsorted,undominated;

unsorted = Unsorted.get(x);
undominated = Undominated.get(i);

int p = DominationCheck(unsorted,undominated);

switch (p) {
case 1:
if(i==Undominated.size()-1)
{
Undominated.remove(i);
i++;
}

else
{
Undominated.remove(i);
i--;
}

break;

case 2: i=Undominated.size()-1;

break;

case 3: if(i==Undominated.size()-1)
{
i++;
}
else
{

}

break;

default: System.out.println("Error");
System.exit(0);
break;

}
}
}

System.out.println("Second List:");
for(int a =0;a<=Undominated.size()-1;a++)
{
z=Undominated.get(a).f1;

System.out.println(""+z+"");
}

System.out.println("------------------\n");

}

public static int DominationCheck(Solution unsorted, Solution undominated)
{
int ans=0;
if(unsorted.f1<undominated.f1 && unsorted.f2<undominated.f2)
{
ans = 1;
}

else if(unsorted.f1>undominated.f1 && unsorted.f2>undominated.f2) //s2 dominates s1
{
ans = 2;
}

else //neither dominate
{
ans = 3;
}

return ans;
}

}