/
neuron.py
executable file
·138 lines (105 loc) · 4.29 KB
/
neuron.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
import random, math, GameEnvironment, npc
class neuron:
#num_inputs = total number of inputs + 1 for the bias input
def __init__(self, num_inputs):
#generating an initially random set of weights
self.weights = []
for x in range(num_inputs):
self.weights.append(random.uniform(-5, 5)) #-1, 1
def process(self, inputs):
iSum = 0
#inputs.append(1)
for x in range(len(inputs)):
iSum += self.weights[x] * inputs[x]
return 1 / (1 + math.e ** (-1*iSum))
#mutation with more variance
def mutate1(self):
newNeuron = self.clone()
for x in newNeuron.weights:
if random.random() > 0.5:
x+=random.uniform(-5, 5) #-.3, .3
return newNeuron
#mutation with less variance
def mutate2(self):
newNeuron = self.clone()
index = random.randint(0, len(newNeuron.weights)-1)
newNeuron.weights[index]+=random.uniform(-5, 5)
return newNeuron
def crossover1(self, otherNeuron):
newNeuron = self.clone()
for x in range( len(newNeuron.weights) ):
if random.random() > 0.5:
newNeuron.weights[x] = otherNeuron.weights[x]
return newNeuron
def crossover2(self, otherNeuron):
newNeuron = self.clone()
index = random.randint(0, len(newNeuron.weights)-1)
newNeuron.weights[index] = otherNeuron.weights[index]
return newNeuron
def clone(self):
newNeuron = neuron(len(self.weights))
newNeuron.weights = self.weights[:]
return newNeuron
class network:
def __init__(self):
self.moveNeuron = neuron(5)
self.shootNeuron = neuron(5)
self.neuronList = [self.moveNeuron, self.shootNeuron]
self.fitness = -1000
def processNetwork(self, inputs):
moveOutput = self.moveNeuron.process(inputs)
shootOutput = self.shootNeuron.process(inputs)
return [moveOutput, shootOutput]
def mutate(self):
newNetwork = self.clone()
for neuron in newNetwork.neuronList:
if random.random() > 0.5:
neuron = neuron.mutate2()
return newNetwork
def crossover(self, otherNetwork):
newNetwork = self.clone()
for x in range( len(newNetwork.neuronList) ):
if random.random() > 0.5:
newNetwork.neuronList[x] = newNetwork.neuronList[x].crossover2(otherNetwork.neuronList[x])
return newNetwork
def play(self, display):
#GameEnvironment.game(display, npc, self)
#print '1'
GameEnvironment.game(display, npc.npc1(20,20), self)
"""
#print '2'
GameEnvironment.game(display, npc.npc2(20,20), self)
#print '3'
GameEnvironment.game(display, npc.npc3(20,20), self)
#print '4'
GameEnvironment.game(display, npc.npc4(20,20), self)
#print '5'
GameEnvironment.game(display, npc.npc5(20,20), self)
#print '6'
GameEnvironment.game(display, npc.npc6(20,20), self)
"""
def getFitness(self, display):
self.fitness = 0
#print '1'
self.fitness += GameEnvironment.game(display, npc.npc1(20,20), self)
#print '2'
self.fitness += GameEnvironment.game(display, npc.npc2(20,20), self)
#print '3'
self.fitness += GameEnvironment.game(display, npc.npc3(20,20), self)
#print '4'
self.fitness += GameEnvironment.game(display, npc.npc4(20,20), self)
#print '5'
self.fitness += GameEnvironment.game(display, npc.npc5(20,20), self)
#print '6'
self.fitness += GameEnvironment.game(display, npc.npc6(20,20), self)
self.fitness = self.fitness/6
#print "fit: ",self.fitness
#self.fitness = GameEnvironment.game(display, npc, self)
def clone(self):
newNetwork = network()
newNetwork.moveNeuron = self.moveNeuron.clone()
newNetwork.shootNeuron = self.shootNeuron.clone()
newNetwork.neuronList = [newNetwork.moveNeuron, newNetwork.shootNeuron]
return newNetwork
def toString(self):
return "move: "+str(self.moveNeuron.weights)+"\tshoot: "+str(self.shootNeuron.weights)