This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer27.java
303 lines (250 loc) · 10.1 KB
/
player27.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
import org.vu.contest.ContestEvaluation;
import org.vu.contest.ContestSubmission;
import src.genetics.AEA;
import src.genetics.CMAEvolutionaryStrategy;
import src.genetics.DifferentialEvolution;
import src.genetics.GA.crossover.*;
import src.genetics.GA.other.Stagnancy;
import src.genetics.GA.GenerationalGA;
import src.genetics.GA.SteadyStateGA;
import src.genetics.GA.mutation.AMutation;
import src.genetics.GA.mutation.MutationGaussian;
import src.genetics.GA.selection.ASelection;
import src.genetics.GA.selection.SelectionLinearRanking;
import src.genetics.GA.survival.ASurvival;
import src.genetics.GA.survival.SurvivalBestFitness;
import src.genetics.PSO;
import java.util.Arrays;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.*;
public class player27 implements ContestSubmission {
public Random rng;
ContestEvaluation evaluation_;
private int evaluations_limit_;
private int stagnancyThreshold;
private int wipeoutThreshold;
private double epurationDegree;
private int epochs = -1;
private double mutationProbability = 1;
private int parentsNumber = 2;
private AEA ea;
private boolean printOutput;
private boolean isMultimodal;
private boolean hasStructure;
private boolean isSeparable;
public player27() {
rng = new Random();
}
public static void main(String[] args) {
player27 p = new player27();
p.run();
}
public void setSeed(long seed) {
// Set seed of algortihms random process
rng.setSeed(seed);
}
public void setEvaluation(ContestEvaluation evaluation) {
evaluation_ = evaluation;
Properties props = evaluation.getProperties();
evaluations_limit_ = Integer.parseInt(props.getProperty("Evaluations"));
isMultimodal = Boolean.parseBoolean(props.getProperty("Multimodal"));
hasStructure = Boolean.parseBoolean(props.getProperty("Regular"));
isSeparable = Boolean.parseBoolean(props.getProperty("Separable"));
}
public void run() {
printOutput = false;
rng.setSeed(System.currentTimeMillis());
setupAlgorithm();
if (printOutput) {
printProperties(evaluation_);
ea.printAlgorithmParameters();
}
runAlgorithm();
}
private void runAlgorithm() {
Runnable fooRunner = new Runnable() {
public void run() {
try {
ea.run();
} catch (Exception e) {}
}
};
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
executor.submit(fooRunner).get(10, TimeUnit.MINUTES); // Timeout of 10 minutes.
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
executor.shutdown();
}
private void printProperties(ContestEvaluation evaluation) {
Properties p = evaluation.getProperties();
int maxEvaluations = Integer.parseInt(p.getProperty("Evaluations"));
boolean isMultimodal = Boolean.parseBoolean(p.getProperty("Multimodal"));
boolean hasStructure = Boolean.parseBoolean(p.getProperty("Regular"));
boolean isSeparable = Boolean.parseBoolean(p.getProperty("Separable"));
System.out.println("Evaluations: " + maxEvaluations + " Multimodal: " + isMultimodal + " Regular: " + hasStructure + " Separable: " + isSeparable);
System.out.println();
}
private void setupAlgorithm() {
String algorithmType = System.getProperty("algorithm");
algorithmType = algorithmType == null ? "CMA-ES" : algorithmType;
switch (algorithmType) {
case "GGA": {
setupGGA();
break;
}
case "SSGA": {
setupSSGA();
break;
}
case "CMA-ES": {
setupCMAES();
break;
}
case "PSO": {
setupPSO();
break;
}
case "DE": {
setupDE();
break;
}
default: {
System.out.println("Unrecognized algorithm type");
break;
}
}
}
private void setupCMAES() {
int mu = 7;
int lambda = 15;
String property = System.getProperty("lambda");
lambda = property == null ? lambda : Integer.parseInt(property);
property = System.getProperty("mu");
mu = property == null ? mu : Integer.parseInt(property);
ea = new CMAEvolutionaryStrategy(mu, lambda, evaluation_, epochs, printOutput);
}
private void setupSSGA() {
int populationSize = evaluations_limit_ <= 100000 ? evaluations_limit_ / 100 : evaluations_limit_ / 1000;
double sigma = 0.1;
int replacementNumber = populationSize / 10;
double selectionPressure = 1.75;
String property = System.getProperty("replacementNumber");
replacementNumber = property == null ? replacementNumber : (int)(1.0 * populationSize / Integer.parseInt(property));
property = System.getProperty("sigma");
sigma = property == null ? sigma : Double.parseDouble(property);
stagnancyThreshold = isMultimodal ? evaluations_limit_ / 4000 : 0;
wipeoutThreshold = evaluations_limit_ / 2000;
epurationDegree = 0.7;
Stagnancy stagnancy = new Stagnancy(rng, stagnancyThreshold, wipeoutThreshold, epurationDegree, populationSize);
ACrossover crossover = getCrossoverProperty();
AMutation mutation = new MutationGaussian(rng, sigma, mutationProbability);
ASelection selection = new SelectionLinearRanking(rng, parentsNumber, selectionPressure);
ASurvival survival = new SurvivalBestFitness(rng);
ea = new SteadyStateGA(rng,
populationSize,
stagnancy,
selection,
crossover,
mutation,
survival,
evaluation_,
epochs,
replacementNumber,
printOutput);
}
private void setupGGA() {
int populationSize = 4;
int elitism = 1;
double sigma = 0.05;
double selectionPressure = 2;
String property = System.getProperty("elitism");
elitism = property == null ? elitism : Integer.parseInt(property);
property = System.getProperty("selectionPressure");
selectionPressure = property == null ? selectionPressure : Double.parseDouble(property);
property = System.getProperty("sigma");
sigma = property == null ? sigma : Double.parseDouble(property);
stagnancyThreshold = 0;
wipeoutThreshold = 0;
epurationDegree = 0;
Stagnancy stagnancy = new Stagnancy(rng, stagnancyThreshold, wipeoutThreshold, epurationDegree, populationSize);
ACrossover crossover = getCrossoverProperty();
AMutation mutation = new MutationGaussian(rng, sigma, mutationProbability);
ASelection selection = new SelectionLinearRanking(rng, parentsNumber, selectionPressure);
ASurvival survival = new SurvivalBestFitness(rng);
ea = new GenerationalGA(rng,
populationSize,
stagnancy,
selection,
crossover,
mutation,
survival,
evaluation_,
epochs,
elitism,
printOutput);
}
private void setupPSO() {
int swarmSize = 100;
double w = 0.8;
double phi1 = 0.1;
double phi2 = 0.7;
String property = System.getProperty("swarmSize");
swarmSize = property == null ? swarmSize : Integer.parseInt(property);
property = System.getProperty("w");
w = property == null ? w : Double.parseDouble(property);
property = System.getProperty("phi1");
phi1 = property == null ? phi1 : Double.parseDouble(property);
property = System.getProperty("phi2");
phi2 = property == null ? phi2 : Double.parseDouble(property);
ea = new PSO(swarmSize, epochs, w, phi1, phi2, rng, evaluation_, printOutput);
}
private void setupDE() {
int popSize = 50;
double f = 0.5;
double cr = 0.4;
char base = 'b';
int diffN = 1;
String property = System.getProperty("popSize");
popSize = property == null ? popSize : Integer.parseInt(property);
property = System.getProperty("f");
f = property == null ? f : Double.parseDouble(property);
property = System.getProperty("cr");
cr = property == null ? cr : Double.parseDouble(property);
property = System.getProperty("base");
base = property == null ? base : property.charAt(0);
property = System.getProperty("diffN");
diffN = property == null ? diffN : Integer.parseInt(property);
ea = new DifferentialEvolution(epochs, popSize, f, cr, base, diffN, rng, evaluation_, printOutput);
}
private ACrossover getCrossoverProperty() {
String crossoverType = System.getProperty("crossover");
if(crossoverType == null) {
return new CrossoverAverageWeighted(rng);
} else {
switch (System.getProperty("crossover")) {
case "CrossoverAverage": {
return new CrossoverAverage(rng);
}
case "CrossoverAverageWeighted": {
return new CrossoverAverageWeighted(rng);
}
case "CrossoverCoinFlip": {
return new CrossoverCoinFlip(rng);
}
case "CrossoverCoinFlipWeighted": {
return new CrossoverCoinFlipWeighted(rng);
}
case "CrossoverNPoints": {
return new CrossoverNPoints(rng, 2);
}
default: {
System.out.println("unrecognized crossover, using CrossoverAverageWeighted");
return new CrossoverAverageWeighted(rng);
}
}
}
}
}