Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def sgn(x):
- if x>0:
- return 1
- elif x<0:
- return -1
- else:
- return 0
- class Sensor:
- state=0
- def set(self,x):
- self.state=x
- def __repr__(self):
- return f"Sensor({id(self)})<state={self.state}>"
- class Associative:
- weights=[]
- teta=0
- state=0
- def run(self,sensors):
- summ=0
- for sensor in range(len(sensors)):
- summ+=sensors[sensor].state*self.weights[sensor]
- #print(summ,self.teta)
- if summ>self.teta:
- self.state=1
- else:
- self.state=0
- def __repr__(self):
- return f"Associative({id(self)})<teta={self.teta}, state={self.state}, weights={self.weights}>"
- class Reacting:
- teta=0
- def run(self,x,w):
- return sgn(sum([w[i]*x[i].state for i in range(len(w))])-self.teta)
- def __repr__(self):
- return f"Reacting({id(self)})<teta={self.teta}>"
- class OLRB:
- weights=[]
- def __init__(self,sensors,associatives):
- self.sensors=[Sensor()]*sensors
- #self.associatives=[Associative()]*associatives
- self.associatives = [Associative() for i in range(associatives)]
- self.reacting=Reacting()
- def run(self,x):
- for sensor in range(len(self.sensors)):
- self.sensors[sensor].set(x[sensor])
- for associative in range(len(self.associatives)):
- self.associatives[associative].run(self.sensors)
- return self.reacting.run(self.associatives,self.weights)
- def __repr__(self):
- return f"OLRB({id(self)})<weights={self.weights}, sensors={self.sensors}, associatives={self.associatives}, reacting={self.reacting}>"
- def EC_prepare(p):
- while 1:
- done=[0]*len(p.sensors)
- for i in range(len(p.associatives)):
- p.associatives[i].teta=random.randint(0,len(p.sensors))
- p.associatives[i].weights=[]
- for q in range(len(p.sensors)):
- p.associatives[i].weights.append(random.randint(-1,1))
- if (p.associatives[i].weights[q]!=0):
- done[q]+=1
- print(p.associatives[i].weights)
- #эта команда выводит веса i-того ассоциативного элемента
- if done.count(0)==0:
- print(p.associatives[0].weights)
- break
- else:
- print('-'*32)
- p.weights=[0]*len(p.associatives)
- return p
- olrb=OLRB(2,2)
- olrb=EC_prepare(olrb)
- print (olrb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement