# Notes from Monday, November 20

# A simple model of a neuron

import random

#---------------------------------------------------------

class Unit:

    def __init__(self, name):
        self.name = name
        self.activation = 0.0
        self.connectionsIn = []

    def __str__(self):
        return "Unit %s (activation %.3f)" % (self.name, self.activation)

    def connectTo(self, other, strength):
        c = Connection(self, other, strength)
        other.connectionsIn.append(c)

    def updateActivation(self):
        sum = 0.0
        for c in self.connectionsIn:
            other = c.fromUnit
            sum = sum + other.activation * c.strength
        if sum > 0.5:
            self.activation = 1.0
        else:
            self.activation = 0.0
        return self.activation

#---------------------------------------------------------

class Connection:

    def __init__(self, u1, u2, s):
        self.fromUnit = u1
        self.toUnit = u2
        self.strength = s

    def __str__(self):
        return "Connection %s --> %s (strength %.3f)" % \
               (self.fromUnit.name, self.toUnit.name, self.strength)

#---------------------------------------------------------

neuron = Unit("neuron")

in1 = Unit("in1")
in2 = Unit("in2")
in3 = Unit("in3")
in4 = Unit("in4")

in1.connectTo(neuron, 0.2)
in2.connectTo(neuron, 0.1)
in3.connectTo(neuron, 0.7)
in4.connectTo(neuron, 0.4)

def inputs():
    print in1.activation, in2.activation, in3.activation, in4.activation

def strengths():
    for i in range(len(neuron.connectionsIn)):
        c = neuron.connectionsIn[i]
        print "strength #%d = %.3f" % (i, c.strength)
