-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnnplayer.py
74 lines (50 loc) · 1.58 KB
/
nnplayer.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
import random
import numpy as np
import math
import ANNGenetic.ann as ann
import matplotlib.pyplot as plt
network = ann.ANN(6)
network.add_layer(ann.Layer(16, activation=np.tanh))
network.add_layer(ann.Layer(1, activation=np.tanh))
FAMILY = None
FIRST_GEN = True
GENERATION = 0
PLAYERS = None
MEAN_SCORE = [0]
GEN = [0]
# initialize the gene pool
def init(filePath=None):
global FAMILY
FAMILY = ann.Genetic(200, children_to_select=0.3, verbose=False)
FAMILY.create_family(network)
def getNewBatch(batch_size):
global FIRST_GEN, FAMILY, GENERATION, PLAYERS, MEAN_SCORE, GEN
if not FIRST_GEN:
fitnesses = [x.fitness for x in PLAYERS]
GENERATION += 1
mean = (sum(fitnesses) / len(fitnesses))
print("Generation", GENERATION)
MEAN_SCORE.append(mean)
GEN.append(GENERATION)
plt.plot(GEN, MEAN_SCORE)
#plt.show()
plt.pause(0.5)
FAMILY.evolve(fitnesses)
FIRST_GEN = False
PLAYERS = [Player(x) for x in FAMILY.family]
return PLAYERS
class Player(object):
def __init__(self, dann):
self.fitness = 0
self.dann = dann
def set_fitness(self, fitness):
self.fitness = fitness
def play(self, playerInfo):
# playerInfo = {
# 'playerY': playery,
# 'playerVelY': playerVelY,
# 'playerToPipeX': lowerPipes['x'] - playerx,
# 'playerToPipeY': lowerPipes['y'] - playery
# }
pred = self.dann.prop(np.array(playerInfo))
return pred > 0