Skip to content

Commit

Permalink
fix the archive error
Browse files Browse the repository at this point in the history
  • Loading branch information
chen0040 committed Jun 17, 2017
1 parent 8cf7be8 commit 616ff03
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 11 deletions.
22 changes: 17 additions & 5 deletions src/main/java/com/github/chen0040/moea/algorithms/NSGAII.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
public class NSGAII {
private Mediator mediator = new Mediator();

private int displayEvery = -1;

@Setter(AccessLevel.NONE)
private NondominatedPopulation archive = new NondominatedPopulation();

Expand All @@ -35,12 +37,18 @@ public NondominatedPopulation solve(){
int maxGenerations = mediator.getMaxGenerations();
for(int generation = 0; generation < maxGenerations; ++generation) {
evolve();
if(displayEvery > 0 && generation % displayEvery == 0){
System.out.println("Generation #" + generation + "\tArchive size: " + archive.size());
}
}

return archive;
}

public void initialize(){
archive.setMediator(mediator);
archive.clear();

population.setMediator(mediator);
population.initialize();
evaluate(population);
Expand All @@ -59,16 +67,16 @@ public void evolve()
{
TournamentSelectionResult<Solution> tournament = TournamentSelection.select(population.getSolutions(), mediator.getRandomGenerator(), (s1, s2) ->
{
int flag = 0;
if ((flag = InvertedCompareUtils.ConstraintCompare(s1, s2))==0)
int flag;
if ((flag = InvertedCompareUtils.ConstraintCompare(s1, s2))==0) // return -1 if s1 is better
{
if ((flag = InvertedCompareUtils.ParetoObjectiveCompare(s1, s2)) == 0)
if ((flag = InvertedCompareUtils.ParetoObjectiveCompare(s1, s2)) == 0) // return -1 if s1 is better
{
flag = InvertedCompareUtils.CrowdingDistanceCompare(s1, s2);
flag = InvertedCompareUtils.CrowdingDistanceCompare(s1, s2); // return -1 if s1 is better
}
}

return flag < 0; // should return better
return flag < 0; // return -1 if s1 is better
});

TupleTwo<Solution, Solution> tournament_winners = tournament.getWinners();
Expand All @@ -77,6 +85,7 @@ public void evolve()

Mutation.apply(mediator, children._1());
Mutation.apply(mediator, children._2());

offspring.add(children._1());
offspring.add(children._2());
}
Expand All @@ -99,7 +108,10 @@ private void evaluate(Population population) {
Solution s = population.getSolutions().get(i);
s.evaluate(mediator);

//System.out.println("cost1: " + s.getCost(0) + "\tcost2:" + s.getCost(1));

boolean is_archivable = archive.add(s);

if (archive.size() > mediator.getMaxArchive())
{
archive.truncate(mediator.getMaxArchive());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ public Population makeCopy(){
}

// compare by costs and constraints
// return -1 if s2 is better
// return 1 if s1 is better
public static int compare(Solution s1, Solution s2){
return - invertedCompare(s1, s2);
}

// compare by costs and constraints but invert the sign
// return -1 if s1 is better
// return 1 if s2 is better
public static int invertedCompare(Solution s1, Solution s2) {
int flag = InvertedCompareUtils.ConstraintCompare(s1, s2);

Expand All @@ -42,6 +46,7 @@ public static int invertedCompare(Solution s1, Solution s2) {
return flag;
}

// only add solution if solution is not worse than any solution in the non-dominated population
@Override
public boolean add(Solution solution_to_add)
{
Expand All @@ -52,7 +57,7 @@ public boolean add(Solution solution_to_add)
{
int flag = invertedCompare(solution_to_add, solution);

if (flag < 0)
if (flag < 0) // solution_to_add is better
{
solutions_to_remove.add(solution);
}
Expand Down Expand Up @@ -104,4 +109,9 @@ public void truncate(int size)
public static boolean better(Solution s1, Solution s2) {
return invertedCompare(s1, s2) < 0;
}


public void clear() {
solutions.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public Population makeCopy(){
}

// compare by rank and crowding distance
// return -1 if s2 is better
// return 1 is s1 is better
public static int compare(Solution s1, Solution s2){
return - invertedCompare(s1, s2);
}

// compare by rank and crowding distance but invert the sign
// return 1 if s1 is better
// return -1 if s2 is better
public static int invertedCompare(Solution s1, Solution s2) {
int flag = InvertedCompareUtils.RankCompare(s1, s2);

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/github/chen0040/moea/tutorials/NDND.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.chen0040.moea.tutorials;


import com.github.chen0040.moea.components.Solution;

import java.util.Arrays;
import java.util.List;


/**
* Created by xschen on 17/6/2017.
*/
public class NDND implements Tutorial {
@Override public double getCost(Solution x, int objective_index) {

double f1 = 1 - Math.exp((-4) * x.get(0)) * Math.pow(Math.sin(5 * Math.PI * x.get(0)), 4);
if (objective_index == 0)
{
return f1;
}
else
{
double f2, g, h;
if (x.get(1) > 0 && x.get(1) < 0.4)
g = 4 - 3 * Math.exp(-2500 * (x.get(1) - 0.2) * (x.get(1) - 0.2));
else
g = 4 - 3 * Math.exp(-25 * (x.get(1) - 0.7) * (x.get(1) - 0.7));
double a = 4;
if (f1 < g)
h = 1 - Math.pow(f1 / g, a);
else
h = 0;
f2 = g * h;
return f2;
}
}

@Override public int getObjectiveCount() {
return 2;
}


@Override public int getDimension() {
return 2;
}


@Override public List<Double> getLowerBounds() {
return Arrays.asList(0.0, 0.0);
}


@Override public List<Double> getUpperBounds() {
return Arrays.asList(1.0, 1.0);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/github/chen0040/moea/tutorials/TNK.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public double getCost(Solution x, int objective_index)
}

// use the design parameters to compute the constraint equation to get the value
@Override public double getConstraint(double x1, double x2)
private double getConstraint(double x1, double x2)
{
double c1,c2,h;
c1 = -x1*x1-x2*x2+1+0.1*Math.cos(16*Math.atan(x1/x2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
public interface Tutorial {
double getCost(Solution x, int objective_index);

// use the design parameters to compute the constraint equation to Get the value
double getConstraint(double x1, double x2);

int getObjectiveCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

/**
* Created by xschen on 16/6/2017.
* All the methods in this class has the followin behavior
*
* return -1 if solution 1 is better
* return 1 if solution 2 is better
* return 0 if both solutions dominating
*/
public class InvertedCompareUtils {
public static TupleTwo<Integer, Boolean> EpsilonObjectiveCompare(Solution solution1, Solution solution2, double[] epsilons)
Expand Down Expand Up @@ -89,6 +94,8 @@ else if (dominate1)
}
}

// return -1 if solution 1 is better
// return 1 if solution 2 is better
public static int ConstraintCompare(Solution solution1, Solution solution2)
{
double constraints1 = 0;
Expand Down Expand Up @@ -125,6 +132,8 @@ else if (constraints2 == 0.0)
}
}

// return -1 if solution 1 is better
// return 1 if solution 2 is better
public static int ParetoConstraintCompare(Solution solution1, Solution solution2)
{
boolean dominate1 = false;
Expand Down Expand Up @@ -165,6 +174,8 @@ else if (dominate1)
}
}

// return -1 if solution 1 is better
// return 1 if solution 2 is better
public static int ParetoObjectiveCompare(Solution solution1, Solution solution2)
{
boolean dominate1 = false;
Expand Down Expand Up @@ -208,7 +219,8 @@ else if (dominate1)
}



// return -1 if solution 1 is better
// return 1 if solution 2 is better
public static int CrowdingDistanceCompare(Solution solution1, Solution solution2)
{
double crowding1 = solution1.getCrowdingDistance();
Expand All @@ -228,6 +240,8 @@ else if (crowding1 < crowding2)
}
}

// return -1 if solution 1 is better
// return 1 if solution 2 is better
public static int RankCompare(Solution solution1, Solution solution2)
{
int rank1 = solution1.getRank();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.github.chen0040.moea.components.NondominatedPopulation;
import com.github.chen0040.moea.tutorials.NDND;
import com.github.chen0040.moea.tutorials.TNK;
import org.testng.annotations.Test;

Expand All @@ -19,11 +20,23 @@ public void test_tnk(){
algorithm.getMediator().read(new TNK());
algorithm.getMediator().setPopulationSize(1000);
algorithm.getMediator().setMaxGenerations(100);
algorithm.setDisplayEvery(10);

NondominatedPopulation pareto_front = algorithm.solve();



}

@Test
public void test_ndnd(){

NSGAII algorithm = new NSGAII();
algorithm.getMediator().read(new NDND());
algorithm.getMediator().setPopulationSize(1000);
algorithm.getMediator().setMaxGenerations(100);
algorithm.setDisplayEvery(10);

NondominatedPopulation pareto_front = algorithm.solve();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void test_add(){

assertTrue(SortUtils.isSortedDesc(population.solutions, NondominatedSortingPopulation::compare));

for(int i=0; i < 10; ++i){
for(int i=0; i < 100; ++i){
Solution s1 = population.get(i);
System.out.println("rank: " + s1.getRank());
}
Expand Down

0 comments on commit 616ff03

Please sign in to comment.