-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer66.java
866 lines (707 loc) · 29.6 KB
/
player66.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
import org.vu.contest.ContestSubmission;
import org.vu.contest.ContestEvaluation;
import java.util.Random;
import java.util.Properties;
import java.util.Arrays;
import java.lang.Math;
import java.util.ArrayList;
public class player66 implements ContestSubmission
{
Random rnd_;
private ContestWrapper _contest;
IMutationOperator mutationOperator;
ICrossOverOperator crossOverOperator;
IParentSelectionOperator parentSelectionOperator;
ISurvivorSelectionMethod survivorSelectionMethod = new CommaSelection();
IParentSelectionOperator migrationSelector;
int offspringCount;
int populationCount;
int islandCount;
int migrationCount;
int migrationInterval;
public player66()
{
rnd_ = new Random();
}
public void setSeed(long seed)
{
// Set seed of algortihms random process
rnd_.setSeed(seed);
}
public void setEvaluation(ContestEvaluation evaluation)
{
// Set evaluation problem used in the run
// Get evaluation properties
Properties props = evaluation.getProperties();
// Get evaluation limit
int evaluations_limit_ = Integer.parseInt(props.getProperty("Evaluations"));
// Property keys depend on specific evaluation
// E.g. double param = Double.parseDouble(props.getProperty("property_name"));
boolean isMultimodal = Boolean.parseBoolean(props.getProperty("Multimodal"));
boolean hasStructure = Boolean.parseBoolean(props.getProperty("Regular"));
boolean isSeparable = Boolean.parseBoolean(props.getProperty("Separable"));
this._contest = new ContestWrapper(evaluation, evaluations_limit_);
// Do sth with property values, e.g. specify relevant settings of your algorithm
if (isMultimodal && !hasStructure && !isSeparable) {
// Set the settings for katsuura function.
mutationOperator = new SelfAdaptiveMutation(
1.82545602511028e-9,
1.0763482317922e-10,
8.6234881156227e-7
);
crossOverOperator = new UniformCrossOver(0.0);
parentSelectionOperator = new TournamentSelection(399);
migrationSelector = new TournamentSelection(589);
offspringCount = 10000;
populationCount = 1000;
islandCount = 1;
migrationCount = 984; // Doesn't matter as there is only one island...
migrationInterval = 40; // Also doesn't matter as there is only one island...
}
else if (!isMultimodal && hasStructure && isSeparable) {
// Set the settings for the sphere function. Unoptimized only care if works.
mutationOperator = new SelfAdaptiveMutation(
0.07,
0.22,
1.0e-9
);
crossOverOperator = new UniformCrossOver(0.3);
parentSelectionOperator = new TournamentSelection(5);
migrationSelector = new TournamentSelection(5);
offspringCount = 100;
populationCount = 100;
islandCount = 10;
migrationCount = 5;
migrationInterval = 50;
}
else if (!isMultimodal && !hasStructure && !isSeparable) {
// Set the settings for the bent cigar function.
mutationOperator = new SelfAdaptiveMutation(
0.0741957587,
3.5290221408,
0.1138382321
);
crossOverOperator = new UniformCrossOver(0.56);
parentSelectionOperator = new TournamentSelection(29);
migrationSelector = new TournamentSelection(110);
offspringCount = 538;
populationCount = 149;
islandCount = 1;
migrationCount = 132; // Doesn't matter as there is only one island...
migrationInterval = 6553; // Also doesn't matter as there is only one island...
}
else {
// Set the setting for the schaffers function.
mutationOperator = new SelfAdaptiveMutation(
3.69547148148232e-12,
2.9335794754,
4.76403381053298e-09
);
crossOverOperator = new UniformCrossOver(0.2514602094);
parentSelectionOperator = new TournamentSelection(1);
migrationSelector = new TournamentSelection(180); // Doesn't really matter as there is no migration.
offspringCount = 3298;
populationCount = 690;
islandCount = 1;
migrationCount = 372; // Doesn't matter as there is only one island...
migrationInterval = 1; // Also doesn't matter as there is only one island...
}
}
public void run()
{
// Initialization
Instance[][] islands = new Instance[islandCount][];
for (int i = 0; i < islandCount; i += 1) {
islands[i] = init_population(populationCount);
for (int j = 0; j < islands[i].length && !this._contest.isDone(); j += 1) {
this._contest.evaluate(islands[i][j]);
}
}
// calculate fitness
int generationCount = 0;
Instance[] populationAllIslands = new Instance[islandCount*populationCount];
// Program is exiting, print some useful information
System.out.println("Diversity after initialization:");
for (int i = 0; i < islands.length; i += 1){
System.arraycopy(islands[i], 0, populationAllIslands, i*populationCount, populationCount);
}
System.out.print("Diversity for generation ");
System.out.print(generationCount);
System.out.print(" is ");
System.out.println(calculate_diversity(populationAllIslands));
while (!this._contest.isDone()) {
generationCount += 1;
for (int k = 0; k < islandCount; k += 1) {
Instance[] population = islands[k];
// Evaluate fitness of all population members
for (int i = 0; i < population.length && !this._contest.isDone(); i += 1) {
this._contest.evaluate(population[i]);
}
if (this._contest.isDone()){
break;
}
Instance[] parents;
// Perform migration once in migrationInterval
if (generationCount % migrationInterval == 0 && islandCount > 1) {
// Migration copies indivuduals, so candidateSize = populationlength + migrators
int candidateSize = population.length + (migrationCount * 2);
Instance[] candidates = new Instance[candidateSize];
// Fill first part of candidates with population
for (int i = 0; i < population.length; i += 1) {
candidates[i] = population[i];
}
// Fill second part of candidates with migrators from neighbouring islands
int leftNumber = k - 1;
int rightNumber = k + 1;
if (k == 0){
leftNumber = islandCount-1;
}
else if (k == islandCount - 1){
rightNumber = 0;
}
Instance[] left = migrationSelector.selectParents(islands[leftNumber], migrationCount, this.rnd_);
for (int j = 0; j < left.length; j += 1) {
candidates[population.length + j] = left[j];
}
Instance[] right = migrationSelector.selectParents(islands[rightNumber], migrationCount, this.rnd_);
for (int j = 0; j < right.length; j += 1) {
candidates[population.length + migrationCount + j] = right[j];
}
parents = parentSelectionOperator.selectParents(candidates, offspringCount, this.rnd_);
}
else {
// Parent selection
parents = parentSelectionOperator.selectParents(population, offspringCount, this.rnd_);
}
// Crossover
Instance[] offspring = new Instance[parents.length];
for (int i = 0; i < parents.length; i += 2) {
Instance[] new_offspring = crossOverOperator.crossOver(
new Instance[] {
parents[i],
parents[i + 1]
},
this.rnd_
);
offspring[i] = new_offspring[0];
offspring[i + 1] = new_offspring[1];
}
// Mutation
for (int i = 0; i < offspring.length; i += 1) {
offspring[i].mutate(this.rnd_, mutationOperator);
}
// Evaluate the new offspring
for (int i = 0; i < offspring.length && !this._contest.isDone(); i += 1) {
this._contest.evaluate(offspring[i]);
}
if (this._contest.isDone()){
break;
}
// Survivor selection
population = survivorSelectionMethod.selectSurvivors(population, offspring, this.rnd_);
islands[k] = population;
}
// Program is exiting, print some useful information
populationAllIslands = new Instance[islandCount*populationCount];
// Program is exiting, print some useful information
// System.out.println("Diversity after run:");
for (int i = 0; i < islands.length; i += 1){
System.arraycopy(islands[i], 0, populationAllIslands, i*populationCount, populationCount);
}
System.out.print("Diversity for generation ");
System.out.print(generationCount);
System.out.print(" is ");
System.out.println(calculate_diversity(populationAllIslands));
}
}
public Instance[] init_population(int n){
Instance population[] = new Instance[n];
for(int x=0; x < n; x++){
double[] child = new double[10];
double[] mutationRates = new double[10];
for (int j=0; j < 10; j++){
child[j] = rnd_.nextDouble() * 10.0 - 5.0;
mutationRates[j] = rnd_.nextGaussian();
}
population[x] = new Instance(child, mutationRates);
}
return population;
}
public double calculate_diversity(Instance[] population){
int amountOfAlleles = 10;
double[] alleleMean = new double[amountOfAlleles];
// Calculate mean, per allele, over all individuals
for (int i = 0; i < amountOfAlleles; i += 1){
double alleleSum = 0.0;
for (int j = 0; j < population.length; j += 1){
alleleSum += population[j].getGene()[i];
}
alleleMean[i] = alleleSum / population.length;
}
// Calculate average standard deviation, per allele, over all individuals
double[] totalAlleleStd = new double[amountOfAlleles];
double differenceOfMean = 0.0;
double alleleVariance = 0.0;
for (int i = 0; i < amountOfAlleles; i += 1){
for (int j = 0; j < population.length; j += 1){
differenceOfMean = alleleMean[i] - population[j].getGene()[i];
alleleVariance = (differenceOfMean * differenceOfMean) / (population.length-1);
totalAlleleStd[i] = totalAlleleStd[i] + alleleVariance;
}
totalAlleleStd[i] = Math.sqrt(totalAlleleStd[i]);
}
// Take mean of standard deviation per allele
double sum = 0.0;
for (int i = 0; i < amountOfAlleles; i += 1){
sum += totalAlleleStd[i];
}
return sum / amountOfAlleles;
}
public Instance[] merge_islands(Instance[][] islands, int populationCount, int islandCount){
Instance[] populationAllIslands = new Instance[islandCount*populationCount];
for (int i = 0; i < islands.length; i += 1){
System.arraycopy(islands[i], 0, populationAllIslands, i*populationCount, populationCount);
}
return populationAllIslands;
}
}
// Handy class to keep track of the amount of evaluations that have been performed.
class ContestWrapper
{
private ContestEvaluation _contest;
private int _evaluationLimit;
private int _evaluationCount;
public ContestWrapper(ContestEvaluation contest, int evaluationLimit) {
this._contest = contest;
this._evaluationLimit = evaluationLimit;
this._evaluationCount = 0;
}
public boolean isDone() {
return this._evaluationLimit == this._evaluationCount;
}
public double evaluate(Instance instance) {
if (!instance.hasFitness()) {
this._evaluationCount += 1;
return instance.calculate_fitness(this._contest);
}
else {
return instance.getFitness();
}
}
}
// Common interface for the parent selection operators.
interface IParentSelectionOperator
{
public Instance[] selectParents(Instance[] parents, int parentCount, Random rnd);
}
// Implements linear rank based parent selection.
class RankBasedSelection implements IParentSelectionOperator
{
private double _selectionWeight;
public RankBasedSelection(double selectionWeight) {
this._selectionWeight = selectionWeight;
}
public Instance[] selectParents(Instance[] population, int parentCount, Random rnd) {
if (this._selectionWeight < 1.0 || this._selectionWeight > 2.0){
throw new IllegalArgumentException("Selection pressure should be between 1.0 and 2.0");
}
// Sort them base on their performance (from worst to best)
Arrays.sort(population);
Instance[] selections = new Instance[parentCount];
double[] probabilities = new double[population.length];
for (int i = 0; i < population.length; i += 1) {
double p = (2 - this._selectionWeight) / population.length;
probabilities[i] = p + (2 * i * (this._selectionWeight - 1)) / (population.length * (population.length - 1));
}
for (int i = 0; i < parentCount; i += 1) {
selections[i] = population[Utils.rouletteWheelSelection(probabilities, rnd)];
}
return selections;
}
}
// Implements tournament selection
class TournamentSelection implements IParentSelectionOperator
{
private int _tournamentSize;
public TournamentSelection(int tournamentSize) {
this._tournamentSize = tournamentSize;
}
public Instance[] selectParents(Instance[] population, int parentCount, Random rnd) {
Instance[] selections = new Instance[parentCount];
for (int i = 0; i < parentCount; i += 1) {
Instance[] participants = new Instance[this._tournamentSize];
for (int j = 0; j < participants.length; j += 1) {
participants[j] = population[rnd.nextInt(population.length)];
}
Arrays.sort(participants);
selections[i] = participants[participants.length - 1];
}
return selections;
}
}
// Implements uniform parent selection.
class UniformSelection implements IParentSelectionOperator
{
public Instance[] selectParents(Instance[] population, int parentCount, Random rnd) {
Instance[] selections = new Instance[parentCount];
for (int i = 0;i < parentCount; i += 1) {
selections[i] = population[rnd.nextInt(population.length)];
}
return selections;
}
}
// Common interface for the cross over operator.
interface ICrossOverOperator
{
public Instance[] crossOver(Instance[] parents, Random rnd);
}
// Implements the identity cross over operator.
// Basically clones the parents into children.
class IdentityCrossOver implements ICrossOverOperator
{
public Instance[] crossOver(Instance[] parents, Random rnd) {
Instance[] children = new Instance[parents.length];
for (int i = 0; i < parents.length; i += 1) {
children[i] = new Instance(
parents[i].getGene(),
parents[i].getMutationRates()
);
}
return children;
}
}
// Implements simple arithmetic recombintation
class SingleArithmeticRecombination implements ICrossOverOperator
{
private double _weight;
public SingleArithmeticRecombination(double weight) {
this._weight = weight;
}
public Instance[] crossOver(Instance[] parents, Random rnd) {
int geneCount = parents[0].getGene().length;
int crossOverPoint = rnd.nextInt(geneCount);
double[] c1 = new double[geneCount];
double[] c2 = new double[geneCount];
double[] m1 = new double[geneCount];
double[] m2 = new double[geneCount];
for (int i = 0; i < geneCount; i += 1) {
if (i == crossOverPoint) {
c1[i] = parents[0].getGene()[i] * this._weight + parents[1].getGene()[i] * (1 - this._weight);
c2[i] = parents[1].getGene()[i] * this._weight + parents[0].getGene()[i] * (1 - this._weight);
m1[i] = parents[0].getGene()[i] * this._weight + parents[1].getGene()[i] * (1 - this._weight);
m2[i] = parents[1].getGene()[i] * this._weight + parents[0].getGene()[i] * (1 - this._weight);
}
else {
c1[i] = parents[0].getGene()[i];
c2[i] = parents[1].getGene()[i];
m1[i] = parents[0].getMutationRates()[i];
m2[i] = parents[1].getMutationRates()[i];
}
}
return new Instance[] {
new Instance(c1, m1),
new Instance(c2, m2)
};
}
}
// Implements simple arithmetic recombintation
class SimpleArithmeticRecombination implements ICrossOverOperator
{
private double _weight;
public SimpleArithmeticRecombination(double weight) {
this._weight = weight;
}
public Instance[] crossOver(Instance[] parents, Random rnd) {
int geneCount = parents[0].getGene().length;
int crossOverPoint = rnd.nextInt(geneCount);
double[] c1 = new double[geneCount];
double[] c2 = new double[geneCount];
double[] m1 = new double[geneCount];
double[] m2 = new double[geneCount];
for (int i = 0; i < crossOverPoint; i += 1) {
c1[i] = parents[0].getGene()[i];
c2[i] = parents[1].getGene()[i];
m1[i] = parents[0].getMutationRates()[i];
m2[i] = parents[1].getMutationRates()[i];
}
for (int i = crossOverPoint; i < geneCount; i += 1) {
c1[i] = parents[0].getGene()[i] * this._weight + parents[1].getGene()[i] * (1 - this._weight);
c2[i] = parents[1].getGene()[i] * this._weight + parents[0].getGene()[i] * (1 - this._weight);
m1[i] = parents[0].getGene()[i] * this._weight + parents[1].getGene()[i] * (1 - this._weight);
m2[i] = parents[1].getGene()[i] * this._weight + parents[0].getGene()[i] * (1 - this._weight);
}
return new Instance[] {
new Instance(c1, m1),
new Instance(c2, m2)
};
}
}
// Implements whole arithmetic recombintation
class WholeArithmeticRecombination implements ICrossOverOperator
{
private double _weight;
public WholeArithmeticRecombination(double weight) {
this._weight = weight;
}
public Instance[] crossOver(Instance[] parents, Random rnd) {
int geneCount = parents[0].getGene().length;
double[] c1 = new double[geneCount];
double[] c2 = new double[geneCount];
double[] m1 = new double[geneCount];
double[] m2 = new double[geneCount];
for (int i = 0; i < geneCount; i += 1) {
c1[i] = parents[0].getGene()[i] * this._weight + parents[1].getGene()[i] * (1 - this._weight);
c2[i] = parents[1].getGene()[i] * this._weight + parents[0].getGene()[i] * (1 - this._weight);
m1[i] = parents[0].getGene()[i] * this._weight + parents[1].getGene()[i] * (1 - this._weight);
m2[i] = parents[1].getGene()[i] * this._weight + parents[0].getGene()[i] * (1 - this._weight);
}
return new Instance[] {
new Instance(c1, m1),
new Instance(c2, m2)
};
}
}
// Implements the one point cross over operator.
class OnePointCrossOver implements ICrossOverOperator
{
public Instance[] crossOver(Instance[] parents, Random rnd) {
int geneCount = parents[0].getGene().length;
int cross_over_point = rnd.nextInt(geneCount);
double[] c1 = new double[geneCount];
double[] c2 = new double[geneCount];
double[] m1 = new double[geneCount];
double[] m2 = new double[geneCount];
for (int i = 0; i < cross_over_point; i += 1) {
c1[i] = parents[0].getGene()[i];
c2[i] = parents[1].getGene()[i];
m1[i] = parents[0].getMutationRates()[i];
m2[i] = parents[1].getMutationRates()[i];
}
for (int i = cross_over_point; i < geneCount; i += 1) {
c1[i] = parents[1].getGene()[i];
c2[i] = parents[0].getGene()[i];
m1[i] = parents[1].getMutationRates()[i];
m2[i] = parents[0].getMutationRates()[i];
}
return new Instance[] {
new Instance(c1, m1),
new Instance(c2, m2)
};
}
}
// Implement the uniform cross over operation.
class UniformCrossOver implements ICrossOverOperator
{
private double _crossOverProbabilty;
public UniformCrossOver(double crossOverProbability) {
this._crossOverProbabilty = crossOverProbability;
}
public Instance[] crossOver(Instance[] parents, Random rnd) {
int geneCount = parents[0].getGene().length;
double[] c1 = new double[geneCount];
double[] c2 = new double[geneCount];
double[] m1 = new double[geneCount];
double[] m2 = new double[geneCount];
for (int i = 0; i < geneCount; i += 1) {
if (rnd.nextDouble() < this._crossOverProbabilty) {
c1[i] = parents[0].getGene()[i];
c2[i] = parents[1].getGene()[i];
m1[i] = parents[0].getMutationRates()[i];
m2[i] = parents[1].getMutationRates()[i];
}
else {
c1[i] = parents[1].getGene()[i];
c2[i] = parents[0].getGene()[i];
m1[i] = parents[1].getMutationRates()[i];
m2[i] = parents[0].getMutationRates()[i];
}
}
return new Instance[] {
new Instance(c1, m1),
new Instance(c2, m2)
};
}
}
// The common interface for the mutation operator.
interface IMutationOperator
{
public void mutate(double[] genes, double[] mutationRates, Random rnd);
}
// Implements the identity mutation.
class IdentityMutation implements IMutationOperator {
public void mutate(double[] genes, double[] mutationRates, Random rnd) {
// Here we just do nothing.
}
}
// Implements self adaptive mutation with per gene mutation rates.
class SelfAdaptiveMutation implements IMutationOperator {
private double _tau;
private double _tauPrime;
private double _mutationBoundary;
public SelfAdaptiveMutation(double tau, double tauPrime, double mutationBoundary) {
this._tau = _tau;
this._tauPrime = tauPrime;
this._mutationBoundary = mutationBoundary;
}
public void mutate(double[] genes, double[] mutationRates, Random rnd) {
// Perform self adaptive mutation.
// First we mutate the array of mutation rates.
double globalMutationRate = this._tauPrime * rnd.nextGaussian();
for (int i = 0; i < mutationRates.length; i += 1) {
double individualMutationRate = this._tau * rnd.nextGaussian();
mutationRates[i] = mutationRates[i] * Math.exp(globalMutationRate + individualMutationRate);
if (mutationRates[i] < this._mutationBoundary) {
mutationRates[i] = this._mutationBoundary;
}
}
// Now that we have adjusted the mutation rates for each gene we can apply those.
for (int i = 0; i < genes.length; i += 1) {
genes[i] = Utils.clamp(
Constants.MIN_VALUE,
Constants.MAX_VALUE,
genes[i] + mutationRates[i] * rnd.nextGaussian()
);
}
}
}
//Implements the Uniform Mutation operator
class UniformMutation implements IMutationOperator {
private double _mutationRate;
public UniformMutation(double mutationRate) {
this._mutationRate = mutationRate;
}
// Give the mutationrate, the chance of mutation.
public void mutate(double[] genes, double[] mutationRates, Random rnd){
for (int i = 0; i < genes.length; i += 1) {
double randomNum = rnd.nextDouble(); // rnd.nextInt(1/mutationRate);
if(randomNum < this._mutationRate ){
genes[i] = rnd.nextDouble() * 10.0 - 5.0;
}
}
}
}
// Common interface for survivor selection methods.
interface ISurvivorSelectionMethod
{
public Instance[] selectSurvivors(Instance[] parents, Instance[] children, Random rnd);
}
// Implements genetor survivor selection method I.E. replace worst (plus selection (μ+λ))
class GenetorSelection implements ISurvivorSelectionMethod
{
public Instance[] selectSurvivors(Instance[] parents, Instance[] offspring, Random rnd)
{
Instance[] final_population = new Instance[parents.length];
Instance[] temp_population = new Instance[parents.length + offspring.length];
// Fill temporary population array with both parents and offspring
System.arraycopy(parents, 0, temp_population, 0, parents.length);
System.arraycopy(offspring, 0, temp_population, parents.length, offspring.length);
// Sort the temporary population by fitness (worst to best)
Arrays.sort(temp_population);
// Reverse order so it becomes best to worst
for(int i = 0; i < temp_population.length / 2; i++){
Instance temp = temp_population[i];
temp_population[i] = temp_population[temp_population.length - i - 1];
temp_population[temp_population.length - i - 1] = temp;
}
// Only keep first μ best individuals
// (analogous to throwing away λ worst individuals as discussed in the book)
System.arraycopy(temp_population, 0, final_population, 0, parents.length);
return final_population;
}
}
// Implements comma (μ, λ) survivor selection method
class CommaSelection implements ISurvivorSelectionMethod
{
public Instance[] selectSurvivors(Instance[] parents, Instance[] offspring, Random rnd)
{
Instance[] final_population = new Instance[parents.length];
// Sort the offspring by fitness (worst to best)
Arrays.sort(offspring);
// Reverse order so it becomes best to worst
offspring = Utils.reversePopulationOrder(offspring);
// Only keep first μ best individuals of offspring
System.arraycopy(offspring, 0, final_population, 0, parents.length);
return final_population;
}
}
// Utility class to hold usefull functions.
class Utils {
public static double clamp(double lowerBound, double upperBound, double value) {
if (value > upperBound) {
return upperBound;
}
else if (value < lowerBound) {
return lowerBound;
}
else {
return value;
}
}
public static int rouletteWheelSelection(double[] probabilities, Random rnd) {
double prob = rnd.nextDouble();
double probability_sum = 0.0;
for (int i = 0 ; i < probabilities.length; i += 1) {
probability_sum += probabilities[i];
if (prob < probability_sum) {
return i;
}
}
return probabilities.length - 1;
}
public static Instance[] reversePopulationOrder(Instance[] myPopulation){
for(int i = 0; i < myPopulation.length / 2; i++){
Instance temp = myPopulation[i];
myPopulation[i] = myPopulation[myPopulation.length - i - 1];
myPopulation[myPopulation.length - i - 1] = temp;
}
return myPopulation;
}
}
// Constants used.
class Constants {
// Maximum value of a gene.
public static double MAX_VALUE = 5.0;
// Minimum value for a gene.
public static double MIN_VALUE = -5.0;
}
// Class that keeps track of all the information pertaining a single individual.
class Instance implements Comparable<Instance>
{
private double[] _gene;
private Double _fitness;
private Instance[] _parents;
private double[] _mutationRates;
public Instance(double[] genes, double[] mutationRates) {
this._gene = genes;
this._mutationRates = mutationRates;
}
public Instance(double[] genes, Instance[] parents, double[] mutationRates) {
this._gene = genes;
this._parents = parents;
this._mutationRates = _mutationRates;
}
public boolean hasFitness() {
return this._fitness != null;
}
public double getFitness() {
return this._fitness;
}
public double[] getGene() {
return this._gene;
}
public double[] getMutationRates() {
return this._mutationRates;
}
public double calculate_fitness(ContestEvaluation evaluation) {
if (this._fitness == null) {
this._fitness = (Double)evaluation.evaluate(this._gene);
}
return this._fitness;
}
public int compareTo(Instance other) {
return Double.compare(this._fitness, other._fitness);
}
public void mutate(Random rnd, IMutationOperator mutationOperator) {
mutationOperator.mutate(this._gene, this._mutationRates, rnd);
}
}