-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotBuildAndRun.py
116 lines (97 loc) · 3.94 KB
/
botBuildAndRun.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
"""
Code by cameron.albin & christopher.vandervoort
"""
import random
def createBotWeights():
#A = inputs (42 nodes)
#B = first hidden layer (16 nodes)
#C = second hidden layer (16 nodes)
#D = 7 outputs
#create list of weights from single node to each node in next layer
def initializeNode(numOutputs):
nodeWeights = []
for x in range(numOutputs):
nodeWeights += [random.uniform(-1,1)]
return nodeWeights
#create list of nodes
def initializeLayer(numNodes,numOutputs):
layer = []
for x in range(numNodes):
node = initializeNode(numOutputs)
layer += [node]
return layer
#create list of layer biases
def initializeBiases(numNodes):
layerBiases = []
for x in range(numNodes):
layerBiases += [random.uniform(-1,1)]
return layerBiases
#create list of all layers
AtoBLayer = initializeLayer(42,16)
BtoCLayer = initializeLayer(16,16)
CtoDLayer = initializeLayer(16,7)
BLayerBiases = initializeBiases(16)
CLayerBiases = initializeBiases(16)
DLayerBiases = initializeBiases(7)
botWeights = [AtoBLayer,BtoCLayer,CtoDLayer,BLayerBiases,CLayerBiases,DLayerBiases]
#access single weight through botWeights[layerNum][nodeNum][nextNodeNum]
return botWeights
def crossParents(bot1,bot2):
newBot = []
for layerNum in range(len(bot1)):
newLayer = []
#if XtoY Layer create new node list
if type(bot1[layerNum][0]) == list:
for nodeNum in range(len(bot1[layerNum])):
newNode = []
for weightNum in range(len(bot1[layerNum][nodeNum])):
parentWeights = [bot1[layerNum][nodeNum][weightNum], bot2[layerNum][nodeNum][weightNum]]
newWeight = random.choice(parentWeights)
if random.uniform(0,1) <= 0.02:
newWeight += random.uniform(-.3,.3)
newNode += [newWeight]
newLayer += [newNode]
#if XLayerBiases create new bias list
if type(bot1[layerNum][0]) != list:
for weightNum in range(len(bot1[layerNum])):
parentWeights = [bot1[layerNum][weightNum], bot2[layerNum][weightNum]]
newWeight = random.choice(parentWeights)
if random.uniform(0,1) <= 0.02:
newWeight += random.uniform(-.3,.3)
newLayer += [newWeight]
newBot += [newLayer]
return newBot
def layerOutputToNextLayerInput(layerOutputs,layer):
nextLayerInputs = [0]*len(layer[0])
for o in range(len(layerOutputs)):
for i in range(len(nextLayerInputs)):
nextLayerInputs[i] += layerOutputs[o]*layer[o][i]
return nextLayerInputs
def layerInputToLayerOutput(layerInputs,layerBiases):
for i in range(len(layerInputs)):
layerInputs[i] += layerBiases[i]
if len(layerBiases) != 7:
layerInputs[i] = max(0,layerInputs[i])
layerOutputs = layerInputs
return layerOutputs
def runBot(boardInput,bot):
BLayerInputs = layerOutputToNextLayerInput(boardInput,bot[0])
BLayerOutputs = layerInputToLayerOutput(BLayerInputs,bot[3])
CLayerInputs = layerOutputToNextLayerInput(BLayerOutputs,bot[1])
CLayerOutputs = layerInputToLayerOutput(CLayerInputs,bot[4])
DLayerInputs = layerOutputToNextLayerInput(CLayerOutputs,bot[2])
boardOutputs = layerInputToLayerOutput(DLayerInputs,bot[5])
sortedOutputs = sorted(boardOutputs,reverse=True)
columnPreferences = []
for x in range(len(boardOutputs)):
pos = boardOutputs.index(sortedOutputs[x])
columnPreferences.append(pos)
boardOutputs[pos] = -1000
return columnPreferences
#boardInput = []
#for x in range(42):
# boardInput += [random.choice([0])]
#daddy = createBotWeights()
#mommy = createBotWeights()
#bot = crossParents(daddy,mommy)
#print(runBot(boardInput,bot))