forked from d9w/WindFLO
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGA.java
More file actions
174 lines (149 loc) · 4.98 KB
/
GA.java
File metadata and controls
174 lines (149 loc) · 4.98 KB
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
import java.util.ArrayList;
import java.util.Random;
public class GA {
WindFarmLayoutEvaluator wfle;
boolean[][] pops;
double[] fits;
Random rand;
int num_pop;
int tour_size;
double mut_rate;
double cross_rate;
ArrayList<double[]> grid;
public GA(WindFarmLayoutEvaluator evaluator) {
wfle = evaluator;
rand = new Random();
num_pop = 20;
tour_size = 4;
mut_rate = 0.05;
cross_rate = 0.40;
grid = new ArrayList<double[]>();
}
private void evaluate() {
double maxfit = -Double.MAX_VALUE;
for (int p=0; p<num_pop; p++) {
int nturbines=0;
for (int i=0; i<grid.size(); i++) {
if (pops[p][i]) {
nturbines++;
}
}
double[][] layout = new double[nturbines][2];
int l_i = 0;
for (int i=0; i<grid.size(); i++) {
if (pops[p][i]) {
layout[l_i][0] = grid.get(i)[0];
layout[l_i][1] = grid.get(i)[1];
l_i++;
}
}
double coe = wfle.evaluate(layout);
// double[] fitnesses = wfle.getTurbineFitnesses();
// int n_valid = 0;
// for (int i=0; i<fitnesses.length; i++) {
// if (fitnesses[i] > 0.80) {
// n_valid++;
// }
// }
fits[p] = -coe; //n_valid;
if (fits[p] > maxfit) {
maxfit = fits[p];
}
}
System.out.println(maxfit);
}
public void run() {
// set up grid
// centers must be > 8*R apart
double interval = 8.001 * wfle.scenario.R;
for (double x=0.0; x<wfle.scenario.width; x+=interval) {
for (double y=0.0; y<wfle.scenario.height; y+=interval) {
boolean valid = true;
for (int o=0; o<wfle.scenario.obstacles.length; o++) {
double[] obs = wfle.scenario.obstacles[o];
if (x>obs[0] && y>obs[1] && x<obs[2] && y<obs[3]) {
valid = false;
}
}
if (valid) {
double[] point = {x, y};
grid.add(point);
}
}
}
// initialize populations
pops = new boolean[num_pop][grid.size()];
fits = new double[num_pop];
for (int p=0; p<num_pop; p++) {
for (int i=0; i<grid.size(); i++) {
pops[p][i] = rand.nextBoolean();
}
}
// evaluate initial populations (uses num_pop evals)
evaluate();
// GA
for (int i=0; i<(1000/num_pop); i++) {
// rank populations (tournament)
int[] winners = new int[num_pop/tour_size];
int[] competitors = new int[num_pop];
for (int c=0; c<competitors.length; c++) {
competitors[c] = c;
}
for (int c=0; c<competitors.length; c++) {
int index = rand.nextInt(c + 1);
int temp = competitors[index];
competitors[index] = competitors[c];
competitors[c] = temp;
}
for (int t=0; t<winners.length; t++) {
int winner = -1;
double winner_fit = -1.0;
for (int c=0; c<tour_size; c++) {
int competitor = competitors[tour_size*t + c];
if (fits[competitor] > winner_fit) {
winner = competitor;
winner_fit = fits[winner];
}
}
winners[t] = winner;
}
// crossover
boolean[][] children = new boolean[num_pop][grid.size()];
for (int c=0; c<(num_pop-winners.length); c++) {
int s1 = rand.nextInt(winners.length);
int s2 = rand.nextInt(winners.length-1);
if (s2 >= s1) {
s2++;
}
int p1 = winners[s1];
int p2 = winners[s2];
boolean[] parent1 = pops[p1];
boolean[] parent2 = pops[p2];
boolean[] child = new boolean[grid.size()];
for (int j=0; j<child.length; j++) {
if (rand.nextDouble() < cross_rate) {
child[j] = parent2[j];
} else {
child[j] = parent1[j];
}
}
children[c] = child;
}
// mutate
for (int c=0; c<(num_pop-winners.length); c++) {
for (int j=0; j<children[c].length; j++) {
if (rand.nextDouble() < mut_rate) {
children[c][j] = !children[c][j];
}
}
}
// elitism
for (int c=0; c<winners.length; c++) {
children[num_pop-winners.length+c] = pops[winners[c]];
}
pops = children;
// evaluate
evaluate();
}
}
}