-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
527 additions
and
3 deletions.
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
src/main/java/com/github/chen0040/moea/algorithms/NSGAII.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package com.github.chen0040.moea.algorithms; | ||
|
||
|
||
import com.github.chen0040.data.utils.TupleTwo; | ||
import com.github.chen0040.moea.components.*; | ||
import com.github.chen0040.moea.enums.ReplacementType; | ||
import com.github.chen0040.moea.utils.InvertedCompareUtils; | ||
import com.github.chen0040.moea.utils.TournamentSelection; | ||
import com.github.chen0040.moea.utils.TournamentSelectionResult; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
|
||
/** | ||
* Created by xschen on 17/6/2017. | ||
* NSGA-II | ||
*/ | ||
@Getter | ||
@Setter | ||
public class NSGAII { | ||
private Mediator mediator = new Mediator(); | ||
|
||
@Setter(AccessLevel.NONE) | ||
private NondominatedPopulation archive = new NondominatedPopulation(); | ||
|
||
@Setter(AccessLevel.NONE) | ||
private int currentGeneration = 0; | ||
|
||
@Setter(AccessLevel.NONE) | ||
private NondominatedSortingPopulation population = new NondominatedSortingPopulation(); | ||
|
||
public NondominatedPopulation solve(){ | ||
initialize(); | ||
int maxGenerations = mediator.getMaxGenerations(); | ||
for(int generation = 0; generation < maxGenerations; ++generation) { | ||
evolve(); | ||
} | ||
|
||
return archive; | ||
} | ||
|
||
public void initialize(){ | ||
population.setMediator(mediator); | ||
population.initialize(); | ||
evaluate(population); | ||
population.sort(); | ||
currentGeneration = 0; | ||
} | ||
|
||
public void evolve() | ||
{ | ||
Population offspring = new Population(); | ||
offspring.setMediator(mediator); | ||
|
||
int populationSize = mediator.getPopulationSize(); | ||
|
||
while (offspring.size() < populationSize) | ||
{ | ||
TournamentSelectionResult<Solution> tournament = TournamentSelection.select(population.getSolutions(), mediator.getRandomGenerator(), (s1, s2) -> | ||
{ | ||
int flag = 0; | ||
if ((flag = InvertedCompareUtils.ConstraintCompare(s1, s2))==0) | ||
{ | ||
if ((flag = InvertedCompareUtils.ParetoObjectiveCompare(s1, s2)) == 0) | ||
{ | ||
flag = InvertedCompareUtils.CrowdingDistanceCompare(s1, s2); | ||
} | ||
} | ||
|
||
return flag < 0; // should return better | ||
}); | ||
|
||
TupleTwo<Solution, Solution> tournament_winners = tournament.getWinners(); | ||
|
||
TupleTwo<Solution, Solution> children = Crossover.apply(mediator, tournament_winners._1(), tournament_winners._2()); | ||
|
||
Mutation.apply(mediator, children._1()); | ||
Mutation.apply(mediator, children._2()); | ||
offspring.add(children._1()); | ||
offspring.add(children._2()); | ||
} | ||
|
||
evaluate(offspring); | ||
|
||
ReplacementType replacementType = mediator.getReplacementType(); | ||
if(replacementType == ReplacementType.Generational) { | ||
merge1(offspring); | ||
} else if(replacementType == ReplacementType.Tournament) { | ||
merge2(offspring); | ||
} | ||
|
||
currentGeneration++; | ||
} | ||
|
||
private void evaluate(Population population) { | ||
for (int i = 0; i < population.size(); ++i) | ||
{ | ||
Solution s = population.getSolutions().get(i); | ||
s.evaluate(mediator); | ||
|
||
boolean is_archivable = archive.add(s); | ||
if (archive.size() > mediator.getMaxArchive()) | ||
{ | ||
archive.truncate(mediator.getMaxArchive()); | ||
} | ||
} | ||
} | ||
|
||
protected void merge2(Population children) | ||
{ | ||
int populationSize = mediator.getPopulationSize(); | ||
|
||
Population offspring = new Population(); | ||
|
||
for (int i = 0; i < populationSize; i++) | ||
{ | ||
Solution s1 = children.get(i); | ||
Solution s2 = population.get(i); | ||
int flag = 0; | ||
if ((flag = InvertedCompareUtils.ConstraintCompare(s1, s2)) == 0) | ||
{ | ||
if ((flag = InvertedCompareUtils.ParetoObjectiveCompare(s1, s2)) == 0) | ||
{ | ||
flag = InvertedCompareUtils.CrowdingDistanceCompare(s1, s2); | ||
} | ||
} | ||
|
||
if (flag < 0) | ||
{ | ||
offspring.add(children.get(i)); | ||
} | ||
else if (flag > 0) | ||
{ | ||
offspring.add(children.get(i)); | ||
} | ||
else | ||
{ | ||
offspring.add(children.get(i)); | ||
offspring.add(population.get(i)); | ||
} | ||
} | ||
|
||
population.clear(); | ||
|
||
population.add(offspring); | ||
|
||
population.prune(populationSize); | ||
} | ||
|
||
protected void merge1(Population children) | ||
{ | ||
int populationSize = mediator.getPopulationSize(); | ||
|
||
population.add(children); | ||
|
||
population.truncate(populationSize); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/github/chen0040/moea/components/Crossover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.chen0040.moea.components; | ||
|
||
|
||
import com.github.chen0040.data.utils.TupleTwo; | ||
import com.github.chen0040.moea.enums.CrossoverType; | ||
|
||
|
||
/** | ||
* Created by xschen on 17/6/2017. | ||
*/ | ||
public class Crossover { | ||
public static TupleTwo<Solution, Solution> apply(Mediator mediator, Solution solution1, Solution solution2) { | ||
Solution child1 = solution1.makeCopy(); | ||
Solution child2 = solution2.makeCopy(); | ||
|
||
CrossoverType crossoverType = mediator.getCrossoverType(); | ||
if(crossoverType == CrossoverType.OnePoint) { | ||
child1.onePointCrossover(mediator, child2); | ||
} else if(crossoverType == CrossoverType.Uniform) { | ||
child2.uniformCrossover(mediator, child2); | ||
} | ||
|
||
return new TupleTwo<>(child1, child2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/github/chen0040/moea/components/Mutation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.chen0040.moea.components; | ||
|
||
|
||
import com.github.chen0040.moea.enums.MutationType; | ||
|
||
|
||
/** | ||
* Created by xschen on 17/6/2017. | ||
*/ | ||
public class Mutation { | ||
public static void apply(Mediator mediator, Solution solution) { | ||
MutationType mutationType = mediator.getMutationType(); | ||
if(mutationType == MutationType.Uniform) { | ||
solution.mutateUniformly(mediator); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/github/chen0040/moea/enums/CrossoverType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.github.chen0040.moea.enums; | ||
|
||
|
||
/** | ||
* Created by xschen on 17/6/2017. | ||
*/ | ||
public enum CrossoverType { | ||
OnePoint, | ||
Uniform | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/github/chen0040/moea/enums/MutationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.chen0040.moea.enums; | ||
|
||
|
||
/** | ||
* Created by xschen on 17/6/2017. | ||
*/ | ||
public enum MutationType { | ||
Uniform | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/github/chen0040/moea/enums/ReplacementType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.github.chen0040.moea.enums; | ||
|
||
|
||
/** | ||
* Created by xschen on 17/6/2017. | ||
*/ | ||
public enum ReplacementType { | ||
Tournament, | ||
Generational | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/github/chen0040/moea/exceptions/OutOfRangeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.chen0040.moea.exceptions; | ||
|
||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
|
||
/** | ||
* Created by xschen on 17/6/2017. | ||
*/ | ||
@Getter | ||
@Setter | ||
public class OutOfRangeException extends RuntimeException { | ||
private static final long serialVersionUID = -9128963842995898067L; | ||
private final int actualSize; | ||
private final int minSize; | ||
private final int maxSize; | ||
|
||
public OutOfRangeException(int actual_size, int min_size, int max_size) { | ||
this.actualSize = actual_size; | ||
this.minSize = min_size; | ||
this.maxSize = max_size; | ||
} | ||
} |
Oops, something went wrong.