-
Notifications
You must be signed in to change notification settings - Fork 0
/
myfunctions.py
82 lines (59 loc) · 2.59 KB
/
myfunctions.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
import random
from player import Player
import numpy as np
from math import floor
from operator import attrgetter
def tournament_selection(population, q):
selected = random.choices(population, k=q)
parents = sorted(selected, key=lambda agent: agent.fitness, reverse=True)
return parents[0]
def save_population_info(population, filename):
maximum = population[0].fitness
minimum = population[0].fitness
average = 0
for item in population:
average = average + item.fitness
maximum = item.fitness if item.fitness > maximum else maximum
minimum = item.fitness if item.fitness < minimum else minimum
average = average/len(population)
name = "hist/evol_history-"+filename+".csv"
with open(name, 'a') as file:
file.write(str(maximum)+","+str(average)+","+str(minimum)+"\n")
def crossover(parent1, parent2, rate):
p1_biases = parent1.nn.biases
p2_biases = parent2.nn.biases
p1_weights = parent1.nn.weights
p2_weights = parent2.nn.weights
child1_biases = []
child2_biases = []
child1_weights = []
child2_weights = []
if rate > random.random():
for u, v in zip(p1_biases, p2_biases):
cross_point = floor(u.shape[0] / 2)
child1_biases.append(np.concatenate((u[0:cross_point], v[cross_point:]), axis=0))
child2_biases.append(np.concatenate((v[0:cross_point], u[cross_point:]), axis=0))
for u, v in zip(p1_weights, p2_weights):
cross_point = floor(u.shape[0] / 2) # cross over from middle of matrix
child1_weights.append(np.concatenate((u[0:cross_point], v[cross_point:]), axis=0))
child2_weights.append(np.concatenate((v[0:cross_point], u[cross_point:]), axis=0))
else:
child1_biases = [x.copy() for x in p1_biases]
child2_biases = [x.copy() for x in p2_biases]
child1_weights = [x.copy() for x in p1_weights]
child2_weights = [x.copy() for x in p2_weights]
child1 = Player(mode=parent1.mode)
child1.nn.biases = child1_biases
child1.nn.weights = child1_weights
child2 = Player(mode=parent2.mode)
child2.nn.biases = child2_biases
child2.nn.weights = child2_weights
return child1, child2
def sus(population, n):
fitnesses = [agent.fitness for agent in population]
sum_fitness = sum(fitnesses)
probabilities = [x / sum_fitness for x in fitnesses]
return np.random.choice(population, p=probabilities, size=n, replace=False).tolist()
def top_k_selection(population, k):
items = sorted(population, key=lambda agent: agent.fitness, reverse=True)
return items[:k]