-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrain.py
executable file
·69 lines (49 loc) · 1.57 KB
/
Brain.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
""" Class Brain represents a set of Neurons on the Brain Antenna theory
"""
import random
from Neuron import Neuron
random.seed('Andrei')
class Brain:
nNeuronOutputs = 10
def __init__(self):
self.neuronCounter = 0
self.neurons = []
def generateNeuronSet(self,nNeurons):
for i in range(nNeurons):
self.neuronCounter += 1
self.neurons.append(Neuron(self.neuronCounter))
def generateNeuronLinks(self,neurons=None):
nDict = {}
if not neurons:
neurons = self.neurons
for n in neurons:
nDict[n.id] = n
ids = [ n.id for n in neurons ]
for n in neurons:
random.shuffle(ids)
for i in range(self.nNeuronOutputs):
n.addOutNeuron(nDict[ids[i]])
nDict[ids[i]].addInNeuron(n)
def update(self):
delta = 0.
for n in self.neurons:
oldlevel,newlevel = n.getBestMatchToMemory()
if newlevel < 0.1:
oldlevel,newlevel = n.getInputLevel()
else:
n.setLevel(newlevel)
#print ">>>",n.id,oldlevel,newlevel,[x.level for x in n.inNeurons],n.memory
delta += abs(newlevel-oldlevel)
print "Total update delta", delta
def memorize(self):
for n in self.neurons:
n.addInputToMemory()
def dumpState(self):
print "number of Neurons:", self.neuronCounter
for n in self.neurons:
print "ID: %d Level: %s" % (n.id,n.level)
outIDs = [ str(x.id) for x in n.outNeurons]
print "out neurons:",','.join(outIDs)
inIDs = [ str(x.id) for x in n.inNeurons]
print "in neurons:",','.join(inIDs)
print "memory length", len(n.memory)