-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolution.py
163 lines (146 loc) · 6.16 KB
/
evolution.py
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
import copy
from player import Player
import numpy as np
import json
class Evolution:
def __init__(self):
self.generation_number = 0
self.game_mode = "Neuroevolution"
def next_population_selection(self, players, num_players):
"""
Gets list of previous and current players (μ + λ) and returns num_players number of players based on their
fitness value.
:param players: list of players in the previous generation
:param num_players: number of players that we return
"""
# TODO (Implement top-k algorithm here)
players.sort(key=lambda x: x.fitness, reverse=True)
# # TODO (Additional: Implement roulette wheel here)
# players = self.roulette_wheel(players, num_players)
# # TODO (Additional: Implement SUS here)
# players = self.SUS(players, num_players)
# # Q-tournament
# players = self.Q_tournament(players, num_players)
# # TODO (Additional: Learning curve)
fitness_list = [player.fitness for player in players]
best_fitness = float(np.max(fitness_list))
average_fitness = float(np.mean(fitness_list))
worst_fitness = float(np.min(fitness_list))
self.save_fitness_results(best_fitness, worst_fitness, average_fitness)
return players[: num_players]
def generate_new_population(self, num_players, prev_players=None):
"""
Gets survivors and returns a list containing num_players number of children.
:param num_players: Length of returning list
:param prev_players: List of survivors
:return: A list of children
"""
first_generation = prev_players is None
if first_generation:
return [Player(self.game_mode) for _ in range(num_players)]
else:
# TODO ( Parent selection and child generation )
parents = self.Q_tournament(prev_players, num_players)
# parents = prev_players
# parents = self.roulette_wheel(prev_players, num_players)
children = []
for i in range(0, len(parents), 2):
p = parents[i]
q = parents[i + 1]
child1, child2 = self.crossover(p, q)
child1_mut = self.mutate(child1, 0.5)
child2_mut = self.mutate(child2, 0.5)
children.append(child1_mut)
children.append(child2_mut)
return children
def clone_player(self, player):
"""
Gets a player as an input and produces a clone of that player.
"""
new_player = Player(self.game_mode)
new_player.nn = copy.deepcopy(player.nn)
new_player.fitness = player.fitness
return new_player
def roulette_wheel(self, players, num_players):
fitness_sum = sum([p.fitness for p in players])
probs = [p.fitness / fitness_sum for p in players]
for i in range(1, len(probs)):
probs[i] = probs[i] + probs[i - 1]
next_pop = []
for i in range(num_players):
r = np.random.uniform(0, 1)
for j, p in enumerate(probs):
if r <= p:
next_pop.append(players[j])
break
return next_pop
def SUS(self, players, num_players):
fitness_sum = sum([p.fitness for p in players])
probs = [p.fitness / fitness_sum for p in players]
for i in range(1, len(probs)):
probs[i] = probs[i] + probs[i - 1]
next_pop = []
N2 = 1.0 / num_players
r = np.random.uniform(0, N2)
for i in range(num_players):
for j, p in enumerate(probs):
if r <= p:
next_pop.append(players[j])
break
r += N2
return next_pop
def Q_tournament(self, players, num_players):
q = 2
next_pop = []
for i in range(num_players):
q_random_players = np.random.choice(players, q)
next_pop.append(max(q_random_players, key=lambda player: player.fitness))
return next_pop
def crossover(self, p, q):
child1 = self.clone_player(p)
child2 = self.clone_player(q)
child1.nn.w1, child2.nn.w1 = self.cross(p.nn.w1, q.nn.w1)
child1.nn.b1, child2.nn.b1 = self.cross(p.nn.b1, q.nn.b1)
child1.nn.w2, child2.nn.w2 = self.cross(p.nn.w2, q.nn.w2)
child1.nn.b2, child2.nn.b2 = self.cross(p.nn.b2, q.nn.b2)
return child1, child2
def mutate(self, child, thresh):
prob = 0.25
r = np.random.uniform(0, 1)
if r >= prob:
return child
child.nn.w1 += np.random.normal(0, thresh, child.nn.w1.shape)
child.nn.w2 += np.random.normal(0, thresh, child.nn.w2.shape)
child.nn.b1 += np.random.normal(0, thresh, child.nn.b1.shape)
child.nn.b2 += np.random.normal(0, thresh, child.nn.b2.shape)
return child
def cross(self, a, b):
prob = 0.8
r = np.random.uniform(0, 1)
mid = int(a.shape[0] / 2)
if r >= prob:
return a, b
ch1 = np.concatenate((a[:mid], b[mid:]), axis=0)
ch2 = np.concatenate((b[:mid], a[mid:]), axis=0)
return ch1, ch2
def save_fitness_results(self, max_fitness, min_fitness, average_fitness):
if (self.generation_number == 0):
generation_results = {
'best_fitnesses': [max_fitness],
'worst_fitnesses': [min_fitness],
'average_fitnesses': [average_fitness]
}
with open('generation_results.json', 'w') as file:
json.dump(generation_results, file)
file.close()
else:
with open('generation_results.json', 'r') as file:
generation_results = json.load(file)
file.close()
generation_results['best_fitnesses'].append(max_fitness)
generation_results['worst_fitnesses'].append(min_fitness)
generation_results['average_fitnesses'].append(average_fitness)
with open('generation_results.json', 'w') as file:
json.dump(generation_results, file)
file.close()
self.generation_number += 1