-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_4_4_1.py
153 lines (135 loc) · 5.59 KB
/
net_4_4_1.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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import random
from card import Card
from suit import Suit
from neuralnet import NeuralNetwork
def train(self, player, table):
for card in player.hand:
inputs = []
for suit, value in table.field.items():
inputs.append(value)
inputs.append(Suit.toInt(card.getSuit()))
inputs.append(card.getValue())
self.compute(inputs)
expectedValue = 0
if table.cardPlayable(card):
expectedValue = 1
self.backprop([expectedValue])
def testOnGame(self, player, table):
for card in player.hand:
inputs = []
for suit, value in table.field.items():
inputs.append(value)
inputs.append(Suit.toInt(card.getSuit()))
inputs.append(card.getValue())
self.compute(inputs)
expectedValue = 0
if table.cardPlayable(card):
expectedValue = 1
output = self.getOutput()[0]
if output > 0.5:
output = 1
else:
output = 0
self.learnError.append(abs(expectedValue - output))
def generateGoodSample():
inputs = []
inputs.append(Suit.randomColor())
inputs.append("")
inputs.append("cardcol")
inputs.append("cardvalue")
inputs[0] = Suit.randomColor()
inputs[1] = random.randint(1, 5)
c = Card(suit=Suit.randomColor(), value=random.randint(1, 5))
inputs[2] = Suit.toInt(c.getSuit())
inputs[3] = c.getValue()
while not ((inputs[0] == c.getSuit()) and (inputs[1] == (c.getValue() - 1))):
inputs[0] = Suit.randomColor()
inputs[1] = random.randint(1, 5)
c = Card(suit=Suit.randomColor(), value=random.randint(1, 5))
inputs[2] = Suit.toInt(c.getSuit())
inputs[3] = c.getValue()
# print("good: ", inputs, "card: ", c.getSuit(), c .getValue())
return [Suit.toInt(inputs[0]), inputs[1], inputs[2], inputs[3]]
def generateBadSample():
inputs = []
inputs.append("white")
inputs.append("")
inputs.append("cardcol")
inputs.append("cardvalue")
inputs[0] = Suit.randomColor()
inputs[1] = random.randint(1, 5)
c = Card(suit=Suit.randomColor(), value=random.randint(1, 5))
inputs[2] = Suit.toInt(c.getSuit())
inputs[3] = c.getValue()
while ((inputs[0] == c.getSuit()) and (inputs[1] == (c.getValue() - 1))):
inputs[0] = Suit.randomColor()
inputs[1] = random.randint(1, 5)
c = Card(suit=Suit.randomColor(), value=random.randint(1, 5))
inputs[2] = Suit.toInt(c.getSuit())
inputs[3] = c.getValue()
# print("bad: ", inputs, "card: ", c.getSuit(), c .getValue())
return [Suit.toInt(inputs[0]), inputs[1], inputs[2], inputs[3]]
def test2(net, iterations=100000,):
from statistics import mean
errors = []
for i in range(iterations):
net.compute(generateGoodSample())
errors.append(abs(1 - net.getOutput()[0]))
# print("layers: ", nn.layers[0], "\n", nn.layers[1], "\n", nn.layers[2])
# print("biases: ", nn.biases[0], "\n", nn.biases[1], "\n", nn.biases[2])
# print("connexions: ", nn.connexions[0], "\n", nn.connexions[1])
net.compute(generateBadSample())
errors.append(abs(0 - net.getOutput()[0]))
# print("layers: ", nn.layers[0], "\n", nn.layers[1], "\n", nn.layers[2])
# print("biases: ", nn.biases[0], "\n", nn.biases[1], "\n", nn.biases[2])
# print("connexions: ", nn.connexions[0], "\n", nn.connexions[1])
return mean(errors)
def generateBadCombo(seed=None):
""" Generates two cards which can not be played on top of each other in the hanabi game
"""
random.seed(seed)
fireWork = Card()
card = Card()
while fireWork.getSuit() == card.getSuit() and fireWork.getValue() == card.getValue() - 1:
fireWork.setSuit(Suit(random.randint(1, 5)))
fireWork.setValue(random.randint(1, 5))
card.setSuit(Suit(random.randint(1, 5)))
card.setValue(random.randint(1, 5))
return (Suit.toInt(fireWork.getSuit()), fireWork.getValue(), Suit.toInt(card.getSuit()), card.getValue())
def generateGoodCombo(seed=None):
""" Generates two cards which can be played on top of each other in the hanabi game
"""
random.seed(seed)
fireWork = Card()
card = Card()
while not (fireWork.getSuit() == card.getSuit() and fireWork.getValue() == card.getValue() - 1):
fireWork.setSuit(Suit(random.randint(1, 5)))
fireWork.setValue(random.randint(1, 5))
card.setSuit(Suit(random.randint(1, 5)))
card.setValue(random.randint(1, 5))
return (Suit.toInt(fireWork.getSuit()), fireWork.getValue(), Suit.toInt(card.getSuit()), card.getValue())
if __name__ == '__main__':
nn = NeuralNetwork(neuronsPerLayer=[4, 4, 1])
trainKB = []
testKB = []
for i in range(2000):
trainKB.append((generateGoodCombo(), [1]))
trainKB.append((generateBadCombo(), [0]))
testKB.append((generateGoodCombo(), [1]))
testKB.append((generateBadCombo(), [0]))
untrainedErrorOnKB = nn.test(knowledgeBase=trainKB)
untrainedErrorOnTest = nn.test(knowledgeBase=testKB)
trainedErrorOnKB1 = nn.train(knowledgeBase=trainKB)
for _ in range(10):
nn.train(knowledgeBase=trainKB, doTests=False)
print()
trainedErrorOnKB10 = nn.test(knowledgeBase=trainKB)
trainedErrorOnTest = nn.test(knowledgeBase=testKB)
print("untrainedErrorOnKB : ", untrainedErrorOnKB)
print("untrainedErrorOnTest : ", untrainedErrorOnTest)
print("trainedErrorOnKB1 : ", trainedErrorOnKB1)
print("trainedErrorOnKB10 : ", trainedErrorOnKB10)
print("trainedErrorOnTest : ", trainedErrorOnTest)
print()