-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tournament.java
121 lines (106 loc) · 3.24 KB
/
Tournament.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
import java.util.ArrayList;
import java.util.List;
public class Tournament {
double[] fitnessThisGeneration;
public NeuralNetwork[] TournamentSelection(int tournamentSize,int amountOfWinners, int runTime, int radius, NeuralNetwork[] nn) {
NeuralNetwork[] tournamentWinners = new NeuralNetwork[amountOfWinners];
int field = Main.field;
double[] fitness = new double[nn.length];
Evaluation e = new Evaluation();
Walls w = new Walls();
int[][] walls = w.getWalls(field);
CircleIntersections intersect = new CircleIntersections();
double minX = 9999;
double maxX = 0;
double minY = 9999;
double maxY = 0;
for (int z = 0; z < walls.length; z++) {
if (walls[z][0] < minX || walls[z][2] < minX) {
minX = Math.min(walls[z][0], walls[z][2]);
}
if (walls[z][0] > maxX || walls[z][2] > maxX) {
maxX = Math.max(walls[z][0], walls[z][2]);
}
if (walls[z][1] < minY || walls[z][3] < minY) {
minY = Math.min(walls[z][1], walls[z][3]);
}
if (walls[z][1] > maxY || walls[z][3] > maxY) {
maxY = Math.max(walls[z][1], walls[z][3]);
}
}
double Xstart = 200;
double Ystart = 200;
double angleStart = 0;
boolean collision = true;
while(collision) {
//Xstart = Math.random()*(maxX-minX - radius-1)+ minX+radius+1;
//Ystart = Math.random()*(maxY-minY - radius-1)+ minY+radius+1;
//angleStart = (int) (Math.random()*359);
int collisions =0;
for (int i = 0; i < walls.length; i++) {
List<Point> p = CircleIntersections.getCircleLineIntersectionPoint(new Point(walls[i][0], walls[i][1]),
new Point(walls[i][2], walls[i][3]), new Point(Xstart, Ystart), radius);
if(p.size() > 0) {
collisions++;
}
}
if(collisions ==0) {
collision = false;
}
if (field == 0) {
if (Xstart >= 100-radius && Xstart <= 250+radius && Ystart >= 100-radius && Ystart <= 150+radius) {
collision = true;
}
}
}
for(int i=0; i< nn.length;i++) {
fitness[i] = e.SimulateRun(nn[i], field, runTime, Xstart, Ystart, angleStart, radius);
}
fitnessThisGeneration = fitness;
double bestFitness = -99;
int bestEntrant = 0;
for(int i=0; i< nn.length;i++) {
if(bestFitness< fitness[i]) {
bestFitness= fitness[i];
bestEntrant = i;
}
}
tournamentWinners[0]= nn[bestEntrant];
for(int i=1; i< amountOfWinners;i++) {
bestFitness = -99;
bestEntrant = 0;
for(int z = 0;z<tournamentSize;z++) {
int tournamentEntrant = (int) (Math.random()*nn.length);
if(bestFitness< fitness[tournamentEntrant]) {
bestFitness= fitness[tournamentEntrant];
bestEntrant = tournamentEntrant;
}
}
tournamentWinners[i]= nn[bestEntrant];
}
return tournamentWinners;
}
public double[] GetFitness() {
return fitnessThisGeneration;
}
public double GetBestFitness() {
double bestFitness = -99;
for(int i=0; i<fitnessThisGeneration.length;i++) {
if(fitnessThisGeneration[i]>bestFitness) {
bestFitness = fitnessThisGeneration[i];
}
}
return bestFitness;
}
public int GetBestNN() {
double bestFitness = -99;
int bestEntrant = 0;
for(int i=0; i<fitnessThisGeneration.length;i++) {
if(fitnessThisGeneration[i]>bestFitness) {
bestFitness = fitnessThisGeneration[i];
bestEntrant = i;
}
}
return bestEntrant;
}
}