/
NSGAII.java
177 lines (139 loc) · 5.56 KB
/
NSGAII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package org.uma.jmetal.algorithm.multiobjective.nsgaii;
import org.uma.jmetal.algorithm.impl.AbstractGeneticAlgorithm;
import org.uma.jmetal.operator.CrossoverOperator;
import org.uma.jmetal.operator.MutationOperator;
import org.uma.jmetal.operator.SelectionOperator;
import org.uma.jmetal.problem.Problem;
import org.uma.jmetal.solution.Solution;
import org.uma.jmetal.util.SolutionListUtils;
import org.uma.jmetal.util.comparator.CrowdingDistanceComparator;
import org.uma.jmetal.util.evaluator.SolutionListEvaluator;
import org.uma.jmetal.util.solutionattribute.Ranking;
import org.uma.jmetal.util.solutionattribute.impl.CrowdingDistance;
import org.uma.jmetal.util.solutionattribute.impl.DominanceRanking;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author Antonio J. Nebro <antonio@lcc.uma.es>
*/
public class NSGAII<S extends Solution<?>> extends AbstractGeneticAlgorithm<S, List<S>> {
protected final int maxIterations;
protected final int populationSize;
protected final Problem<S> problem;
protected final SolutionListEvaluator<S> evaluator;
protected int iterations;
/**
* Constructor
*/
public NSGAII(Problem<S> problem, int maxIterations, int populationSize,
CrossoverOperator<S> crossoverOperator, MutationOperator<S> mutationOperator,
SelectionOperator<List<S>, S> selectionOperator, SolutionListEvaluator<S> evaluator) {
super() ;
this.problem = problem;
this.maxIterations = maxIterations;
this.populationSize = populationSize;
this.crossoverOperator = crossoverOperator;
this.mutationOperator = mutationOperator;
this.selectionOperator = selectionOperator;
this.evaluator = evaluator;
}
@Override protected void initProgress() {
iterations = 1;
}
@Override protected void updateProgress() {
iterations++;
}
@Override protected boolean isStoppingConditionReached() {
return iterations >= maxIterations;
}
@Override protected List<S> createInitialPopulation() {
List<S> population = new ArrayList<>(populationSize);
for (int i = 0; i < populationSize; i++) {
S newIndividual = problem.createSolution();
population.add(newIndividual);
}
return population;
}
@Override protected List<S> evaluatePopulation(List<S> population) {
population = evaluator.evaluate(population, problem);
return population;
}
@Override protected List<S> selection(List<S> population) {
List<S> matingPopulation = new ArrayList<>(population.size());
for (int i = 0; i < populationSize; i++) {
S solution = selectionOperator.execute(population);
matingPopulation.add(solution);
}
return matingPopulation;
}
@Override protected List<S> reproduction(List<S> population) {
List<S> offspringPopulation = new ArrayList<>(populationSize);
for (int i = 0; i < populationSize; i += 2) {
List<S> parents = new ArrayList<>(2);
parents.add(population.get(i));
parents.add(population.get(i + 1));
List<S> offspring = crossoverOperator.execute(parents);
mutationOperator.execute(offspring.get(0));
mutationOperator.execute(offspring.get(1));
offspringPopulation.add(offspring.get(0));
offspringPopulation.add(offspring.get(1));
}
return offspringPopulation;
}
@Override protected List<S> replacement(List<S> population, List<S> offspringPopulation) {
List<S> jointPopulation = new ArrayList<>();
jointPopulation.addAll(population);
jointPopulation.addAll(offspringPopulation);
Ranking<S> ranking = computeRanking(jointPopulation);
return crowdingDistanceSelection(ranking);
}
@Override public List<S> getResult() {
return getNonDominatedSolutions(getPopulation());
}
protected Ranking<S> computeRanking(List<S> solutionList) {
Ranking<S> ranking = new DominanceRanking<S>();
ranking.computeRanking(solutionList);
return ranking;
}
protected List<S> crowdingDistanceSelection(Ranking<S> ranking) {
CrowdingDistance<S> crowdingDistance = new CrowdingDistance<S>();
List<S> population = new ArrayList<>(populationSize);
int rankingIndex = 0;
while (populationIsNotFull(population)) {
if (subfrontFillsIntoThePopulation(ranking, rankingIndex, population)) {
addRankedSolutionsToPopulation(ranking, rankingIndex, population);
rankingIndex++;
} else {
crowdingDistance.computeDensityEstimator(ranking.getSubfront(rankingIndex));
addLastRankedSolutionsToPopulation(ranking, rankingIndex, population);
}
}
return population;
}
protected boolean populationIsNotFull(List<S> population) {
return population.size() < populationSize;
}
protected boolean subfrontFillsIntoThePopulation(Ranking<S> ranking, int rank, List<S> population) {
return ranking.getSubfront(rank).size() < (populationSize - population.size());
}
protected void addRankedSolutionsToPopulation(Ranking<S> ranking, int rank, List<S> population) {
List<S> front;
front = ranking.getSubfront(rank);
for (S solution : front) {
population.add(solution);
}
}
protected void addLastRankedSolutionsToPopulation(Ranking<S> ranking, int rank, List<S> population) {
List<S> currentRankedFront = ranking.getSubfront(rank);
Collections.sort(currentRankedFront, new CrowdingDistanceComparator<S>());
int i = 0;
while (population.size() < populationSize) {
population.add(currentRankedFront.get(i));
i++;
}
}
protected List<S> getNonDominatedSolutions(List<S> solutionList) {
return SolutionListUtils.getNondominatedSolutions(solutionList);
}
}