-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetics.pde
135 lines (112 loc) · 3.12 KB
/
genetics.pde
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
class Population {
ArrayList<Player> players = new ArrayList<Player>();
int id;
Population() {
this.id = populationId++;
}
public void init(int playerCount) {
for (int i=0; i<playerCount; i++) {
players.add(new Player(str(i)));
}
}
boolean isAlive() {
for (Player player : players) {
if (!player.killed) {
return true;
}
}
return false;
}
int liveCount() {
int live = 0;
for (Player player : players) {
if (!player.killed) {
live++;
}
}
return live;
}
void update() {
for (Player player : players) {
if (!player.killed) {
player.think();
player.update();
}
}
}
void display() {
for (Player player : players) {
if (!player.killed) {
player.display();
}
}
}
};
class Evolution {
ArrayList<Player> matingPool = new ArrayList<Player>();
ArrayList<Player> kids = new ArrayList<Player>();
Population population;
Evolution(Population population) {
this.population = population;
}
Population evolve() {
selection();
reproduction();
mutation();
return buildPopulation();
}
void selection() {
float sum = 0;
for (Player player : population.players) {
sum += player.fitness;
}
for (Player player : population.players) {
player.fitness = (player.fitness * 100) / sum;
}
println("------------------------------------------");
for (Player player : population.players) {
int n = int(player.fitness);
//println("Adding " + n + " player ", player.name, " score: " + player.fitness);
for (int i = 0; i < n; i++) {
matingPool.add(player);
}
}
//println("Mating pool size: " + matingPool.size());
}
void reproduction() {
int populationSize = population.players.size();
for (int i=0; i<populationSize; i++) {
Player p1 = matingPool.get(int(random(matingPool.size())));
Player p2 = matingPool.get(int(random(matingPool.size())));
kids.add(mate(p1, p2));
}
}
Player mate(Player p1, Player p2) {
Player kid = new Player(p1.name);
NetworkSerializer serializer1 = new NetworkSerializer(p1.brain);
NetworkSerializer serializer2 = new NetworkSerializer(p2.brain);
ArrayList<Float> p1weights = serializer1.collectWeights();
ArrayList<Float> p2weights = serializer2.collectWeights();
ArrayList<Float> kidWights = new ArrayList<Float>(p1weights.subList(0, p1weights.size()/2));
int addOneMore = p1weights.size() % 3 == 0 ? 1 : 0;
kidWights.addAll(p2weights.subList(0, p2weights.size()/2 + addOneMore));
NetworkSerializer kidSerializer = new NetworkSerializer(kid.brain);
kidSerializer.storeWeights(kidWights);
return kid;
}
void mutation() {
float mutationRate = 0.01;
for (Player kid : kids) {
if (random(1) < mutationRate) {
kid.mutate();
}
}
}
Population buildPopulation() {
Population newPopulation = new Population();
for (Player kid : kids) {
newPopulation.players.add(kid);
}
return newPopulation;
}
};