Advertisement
Guest User

Untitled

a guest
May 18th, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. import random
  2.  
  3. def sgn(x):
  4.     if x>0:
  5.         return 1
  6.     elif x<0:
  7.         return -1
  8.     else:
  9.         return 0
  10. class Sensor:
  11.     state=0
  12.     def set(self,x):
  13.         self.state=x
  14.     def __repr__(self):
  15.         return f"Sensor({id(self)})<state={self.state}>"
  16. class Associative:
  17.     weights=[]
  18.     teta=0
  19.     state=0
  20.     def run(self,sensors):
  21.         summ=0
  22.         for sensor in range(len(sensors)):
  23.            summ+=sensors[sensor].state*self.weights[sensor]
  24.         #print(summ,self.teta)
  25.         if summ>self.teta:
  26.             self.state=1
  27.         else:
  28.             self.state=0
  29.     def __repr__(self):
  30.         return f"Associative({id(self)})<teta={self.teta}, state={self.state}, weights={self.weights}>"
  31.  
  32. class Reacting:
  33.     teta=0
  34.     def run(self,x,w):
  35.         return sgn(sum([w[i]*x[i].state for i in range(len(w))])-self.teta)
  36.  
  37.     def __repr__(self):
  38.         return f"Reacting({id(self)})<teta={self.teta}>"
  39.  
  40. class OLRB:
  41.     weights=[]
  42.     def __init__(self,sensors,associatives):
  43.         self.sensors=[Sensor()]*sensors
  44.         #self.associatives=[Associative()]*associatives
  45.         self.associatives = [Associative() for i in range(associatives)]
  46.         self.reacting=Reacting()
  47.        
  48.     def run(self,x):
  49.         for sensor in range(len(self.sensors)):
  50.             self.sensors[sensor].set(x[sensor])
  51.         for associative in range(len(self.associatives)):
  52.             self.associatives[associative].run(self.sensors)
  53.         return self.reacting.run(self.associatives,self.weights)
  54.  
  55.     def __repr__(self):
  56.         return f"OLRB({id(self)})<weights={self.weights}, sensors={self.sensors}, associatives={self.associatives}, reacting={self.reacting}>"
  57.  
  58.  
  59.  
  60. def EC_prepare(p):
  61.     while 1:
  62.         done=[0]*len(p.sensors)
  63.         for i in range(len(p.associatives)):
  64.             p.associatives[i].teta=random.randint(0,len(p.sensors))
  65.             p.associatives[i].weights=[]
  66.             for q in range(len(p.sensors)):
  67.                 p.associatives[i].weights.append(random.randint(-1,1))
  68.                
  69.                 if (p.associatives[i].weights[q]!=0):
  70.                     done[q]+=1
  71.             print(p.associatives[i].weights)
  72.             #эта команда выводит веса i-того ассоциативного элемента
  73.         if done.count(0)==0:
  74.             print(p.associatives[0].weights)
  75.             break
  76.         else:
  77.             print('-'*32)
  78.    
  79.     p.weights=[0]*len(p.associatives)
  80.     return p
  81.  
  82.  
  83. olrb=OLRB(2,2)
  84. olrb=EC_prepare(olrb)
  85. print (olrb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement