-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (47 loc) · 1.69 KB
/
main.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
import random
from graphic.Board import Board
from neuralnet.Network import Network
def getRandom():
a = [0, 1]
output = []
for i in range(2):
output.append(random.choice(a))
return output
def train(): # trains the network to output small values
networks = []
amountOfNetworks = 100
for i in range(amountOfNetworks):
networks.append(Network([2, 10, 1]))
for epoch in range(1, 1000):
inputs = getRandom()
outputs = {}
for network in networks:
outputs[network] = network.calcOutputs(inputs)
for key, value in outputs.items():
outputs[key] = sum(value) / len(value)
# sort based on how good it is at predicting 1/2 inputs[0]
target = inputs[0] and inputs[1]
for key, value in outputs.items():
outputs[key] = (value - target) ** 2
outputs = {k: v for k, v in sorted(outputs.items(), key=lambda item: item[1], reverse=False)}
netA = list(outputs.keys())[0]
netB = list(outputs.keys())[1]
newNetworks = []
for i in range(amountOfNetworks):
newNetworks.append(netA.breed(netB, epoch))
networks = newNetworks
print(sum(networks[0].calcOutputs(inputs)) / len(networks[0].calcOutputs(inputs)))
print(networks[0].calcOutputs([1, 1]))
print(networks[0].calcOutputs([0, 1]))
print(networks[0].calcOutputs([1, 0]))
print(networks[0].calcOutputs([0, 0]))
print(networks[0].toString())
def main():
b = Board(20, 20, 20)
b.draw()
# train()
# network = Network([2, 2])
# print(network.toString())
# print(network.calcOutputs([0, 0]))
if __name__ == "__main__":
main()