-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIBEATimeLimited.java
322 lines (269 loc) · 9.06 KB
/
IBEATimeLimited.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Original Creator: Christopher Henard
* Date: 01/03/14
*
* Modifier: Jia Hui Liang & Jianmei Guo
* Date: April-May 2016
*
*/
package smtibea;
import jmetal.core.*;
import jmetal.util.JMException;
import jmetal.util.Ranking;
import jmetal.util.comparators.DominanceComparator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import jmetal.encodings.variable.Binary;
public class IBEATimeLimited extends Algorithm {
private int print_time = 0;
private long maxRunTimeMS;
/**
* Defines the number of tournaments for creating the mating pool
*/
public static final int TOURNAMENTS_ROUNDS = 1;
/**
* Stores the value of the indicator between each pair of solutions into the
* solution set
*/
private List<List<Double>> indicatorValues_;
/**
*
*/
private double maxIndicatorValue_;
/**
* Constructor. Create a new IBEA instance
*
* @param problem
* Problem to solve
*/
public IBEATimeLimited(Problem problem, long maxRunTimeMS) {
super(problem);
this.maxRunTimeMS = maxRunTimeMS;
}
/**
* calculates the hypervolume of that portion of the objective space that is
* dominated by individual a but not by individual b
*/
double calcHypervolumeIndicator(Solution p_ind_a, Solution p_ind_b, int d, double maximumValues[],
double minimumValues[]) {
double a, b, r, max;
double volume = 0;
double rho = 2.0;
r = rho * (maximumValues[d - 1] - minimumValues[d - 1]);
max = minimumValues[d - 1] + r;
a = p_ind_a.getObjective(d - 1);
if (p_ind_b == null) {
b = max;
} else {
b = p_ind_b.getObjective(d - 1);
}
if (d == 1) {
if (a < b) {
volume = (b - a) / r;
} else {
volume = 0;
}
} else {
if (a < b) {
volume = calcHypervolumeIndicator(p_ind_a, null, d - 1, maximumValues, minimumValues) * (b - a) / r;
volume += calcHypervolumeIndicator(p_ind_a, p_ind_b, d - 1, maximumValues, minimumValues) * (max - b)
/ r;
} else {
volume = calcHypervolumeIndicator(p_ind_a, p_ind_b, d - 1, maximumValues, minimumValues) * (max - b)
/ r;
}
}
return (volume);
}
/**
* This structure store the indicator values of each pair of elements
*/
public void computeIndicatorValuesHD(SolutionSet solutionSet, double[] maximumValues, double[] minimumValues) {
SolutionSet A, B;
// Initialize the structures
indicatorValues_ = new ArrayList<List<Double>>();
maxIndicatorValue_ = -Double.MAX_VALUE;
for (int j = 0; j < solutionSet.size(); j++) {
A = new SolutionSet(1);
A.add(solutionSet.get(j));
List<Double> aux = new ArrayList<Double>();
for (int i = 0; i < solutionSet.size(); i++) {
B = new SolutionSet(1);
B.add(solutionSet.get(i));
int flag = (new DominanceComparator()).compare(A.get(0), B.get(0));
double value = 0.0;
if (flag == -1) {
value = -calcHypervolumeIndicator(A.get(0), B.get(0), problem_.getNumberOfObjectives(),
maximumValues, minimumValues);
} else {
value = calcHypervolumeIndicator(B.get(0), A.get(0), problem_.getNumberOfObjectives(),
maximumValues, minimumValues);
}
// double value =
// epsilon.epsilon(matrixA,matrixB,problem_.getNumberOfObjectives());
// Update the max value of the indicator
if (Math.abs(value) > maxIndicatorValue_) {
maxIndicatorValue_ = Math.abs(value);
}
aux.add(value);
}
indicatorValues_.add(aux);
}
} // computeIndicatorValues
/**
* Calculate the fitness for the individual at position pos
*/
public void fitness(SolutionSet solutionSet, int pos) {
double fitness = 0.0;
double kappa = 0.05;
for (int i = 0; i < solutionSet.size(); i++) {
if (i != pos) {
fitness += Math.exp((-1 * indicatorValues_.get(i).get(pos) / maxIndicatorValue_) / kappa);
}
}
solutionSet.get(pos).setFitness(fitness);
}
/**
* Calculate the fitness for the entire population.
*
*/
public void calculateFitness(SolutionSet solutionSet) {
// Obtains the lower and upper bounds of the population
double[] maximumValues = new double[problem_.getNumberOfObjectives()];
double[] minimumValues = new double[problem_.getNumberOfObjectives()];
for (int i = 0; i < problem_.getNumberOfObjectives(); i++) {
maximumValues[i] = -Double.MAX_VALUE; // i.e., the minus maxium
// value
minimumValues[i] = Double.MAX_VALUE; // i.e., the maximum value
}
for (int pos = 0; pos < solutionSet.size(); pos++) {
for (int obj = 0; obj < problem_.getNumberOfObjectives(); obj++) {
double value = solutionSet.get(pos).getObjective(obj);
if (value > maximumValues[obj]) {
maximumValues[obj] = value;
}
if (value < minimumValues[obj]) {
minimumValues[obj] = value;
}
}
}
computeIndicatorValuesHD(solutionSet, maximumValues, minimumValues);
for (int pos = 0; pos < solutionSet.size(); pos++) {
fitness(solutionSet, pos);
}
}
/**
* Update the fitness before removing an individual
*/
public void removeWorst(SolutionSet solutionSet) {
// Find the worst;
double worst = solutionSet.get(0).getFitness();
int worstIndex = 0;
double kappa = 0.05;
for (int i = 1; i < solutionSet.size(); i++) {
if (solutionSet.get(i).getFitness() > worst) {
worst = solutionSet.get(i).getFitness();
worstIndex = i;
}
}
// if (worstIndex == -1) {
// System.out.println("Yes " + worst);
// }
// System.out.println("Solution Size "+solutionSet.size());
// System.out.println(worstIndex);
// Update the population
for (int i = 0; i < solutionSet.size(); i++) {
if (i != worstIndex) {
double fitness = solutionSet.get(i).getFitness();
fitness -= Math.exp((-indicatorValues_.get(worstIndex).get(i) / maxIndicatorValue_) / kappa);
solutionSet.get(i).setFitness(fitness);
}
}
// remove worst from the indicatorValues list
indicatorValues_.remove(worstIndex); // Remove its own list
Iterator<List<Double>> it = indicatorValues_.iterator();
while (it.hasNext()) {
it.next().remove(worstIndex);
}
solutionSet.remove(worstIndex);
} // removeWorst
/**
* Runs of the IBEA algorithm.
*
* @return a <code>SolutionSet</code> that is a set of non dominated
* solutions as a result of the algorithm execution
* @throws JMException
*/
public SolutionSet execute() throws JMException, ClassNotFoundException {
long elapsed = 0, last = 0, start = System.currentTimeMillis();
int populationSize, archiveSize, maxEvaluations, evaluations;
Operator crossoverOperator, mutationOperator, selectionOperator;
SolutionSet solutionSet, archive, offSpringSolutionSet;
// Read the params
populationSize = ((Integer) getInputParameter("populationSize")).intValue();
archiveSize = ((Integer) getInputParameter("archiveSize")).intValue();
maxEvaluations = ((Integer) getInputParameter("maxEvaluations")).intValue();
// Read the operators
crossoverOperator = operators_.get("crossover");
mutationOperator = operators_.get("mutation");
selectionOperator = operators_.get("selection");
// Initialize the variables
solutionSet = new SolutionSet(populationSize);
archive = new SolutionSet(archiveSize);
evaluations = 0;
// -> Create the initial solutionSet in two ways
Solution newSolution;
for (int i = 0; i < populationSize; i++) {
// 1. Random generation for the initial population
newSolution = new Solution(problem_);
// 2. SMT solving for the initial population
// Binary smtSolution = ((SMTIBEA_Problem) problem_).ftz.solve();
// newSolution = smtSolution == null ?
// new Solution(problem_):
// new Solution(problem_, new Variable[]{smtSolution});
problem_.evaluate(newSolution);
problem_.evaluateConstraints(newSolution);
evaluations++;
solutionSet.add(newSolution);
}
while (elapsed < this.maxRunTimeMS) {
// while (evaluations < maxEvaluations){
SolutionSet union = ((SolutionSet) solutionSet).union(archive);
calculateFitness(union);
archive = union;
while (archive.size() > populationSize) {
removeWorst(archive);
}
// Create a new offspringPopulation
offSpringSolutionSet = new SolutionSet(populationSize);
Solution[] parents = new Solution[2];
while (offSpringSolutionSet.size() < populationSize) {
int j = 0;
do {
j++;
parents[0] = (Solution) selectionOperator.execute(archive);
} while (j < IBEATimeLimited.TOURNAMENTS_ROUNDS); // do-while
int k = 0;
do {
k++;
parents[1] = (Solution) selectionOperator.execute(archive);
} while (k < IBEATimeLimited.TOURNAMENTS_ROUNDS); // do-while
// make the crossover
Solution[] offSpring = (Solution[]) crossoverOperator.execute(parents);
mutationOperator.execute(offSpring[0]);
problem_.evaluate(offSpring[0]);
problem_.evaluateConstraints(offSpring[0]);
offSpringSolutionSet.add(offSpring[0]);
evaluations++;
} // while
// End Create a offSpring solutionSet
solutionSet = offSpringSolutionSet;
elapsed = System.currentTimeMillis() - start;
} // while
System.out.println("RunTimeMS: " + this.maxRunTimeMS);
System.out.println("Evaluations: " + evaluations);
Ranking ranking = new Ranking(archive);
return ranking.getSubfront(0);
} // execute
}