Advertisement
Guest User

Untitled

a guest
Aug 15th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.00 KB | None | 0 0
  1. import numpy as num
  2. import random
  3. import matplotlib.pyplot as mpl
  4. import math
  5.  
  6. def MatrixCreate(rows, cols):
  7.     return num.zeros((rows,cols))
  8.    
  9. def VectorCreate(length):
  10.     return num.zeros((length), dtype='f')
  11.    
  12. def MatrixRandomize(matrix):
  13.     for x in range(len(matrix)):
  14.         for y in range(len(matrix[x])):
  15.             matrix[x][y] = float(random.random() + random.random() - 1)
  16.     return matrix;
  17.    
  18. def MeanDistance(v1, v2):
  19.     total = 0
  20.     for i in range(10):
  21.         total += abs(v1[i] - v2[1])
  22.        
  23.     return total / 10
  24.            
  25. def Fitness(finalValues, desiredValues):
  26.    
  27.     for j in range(0, 10, 2):
  28.         desiredValues[j] = 1
  29.    
  30.     distance = MeanDistance(finalValues, desiredValues)
  31.    
  32.     return 1 - distance
  33.    
  34. def Fitness2(neuronValues):
  35.    
  36.     diff=0.0
  37.  
  38.     for i in range(0,9):
  39.         for j in range(0,9):
  40.             diff=diff + abs(neuronValues[i,j]-neuronValues[i,j+1])
  41.             diff=diff + abs(neuronValues[i+1,j]-neuronValues[i,j])
  42.  
  43.     diff=diff/(2*9*9)
  44.    
  45.     return diff
  46.    
  47. def MatrixPerturb(matrix, prob):
  48.     child = matrix.copy()
  49.     x, y = child.shape
  50.     for i in range(x):
  51.         for j in range(y):
  52.             if prob > random.random():
  53.                 child[i, j] = random.random()
  54.     return child
  55.    
  56. def GenerateGene(genes):
  57.     parent = MatrixCreate(1, 50)
  58.     parent = MatrixRandomize(parent)
  59.     parentFitness = Fitness(parent)
  60.    
  61.     fits = []
  62.  
  63.     for currentGeneration in range(0, 5000):
  64.         fits.append(parentFitness)
  65.         for i in range(50):
  66.             genes[i, currentGeneration] = parent[0][i]
  67.         child = MatrixPerturb(parent, 0.05)
  68.         childFitness = Fitness(child)
  69.    
  70.         if childFitness > parentFitness:
  71.             parent = child
  72.             parentFitness = childFitness
  73.            
  74.     mpl.plot(fits)
  75.    
  76. def ComputePositions(amt):
  77.     angle = 0.0
  78.     pi = math.pi
  79.     angleUpdate = 2.0 * pi / float(amt)
  80.     neuronPositions = MatrixCreate(2,amt)
  81.  
  82.     for i in range(amt):
  83.         neuronPositions[0,i] = math.sin(angle)
  84.         neuronPositions[1,i] = math.cos(angle)
  85.         angle = angle + angleUpdate
  86.        
  87.     return neuronPositions
  88.    
  89. def PlotSynapses(positions, synapses):
  90.     for i in range(10):
  91.         for j in (x for x in range(10) if x != i):
  92.             if synapses[i, j] < 0:
  93.                 line_color = [.3, .9, .9]
  94.             else:
  95.                 line_color = 'black'
  96.             w = int(10*abs(synapses[i,j]))+1
  97.             mpl.plot([positions[0][i], positions[0,j]], [positions[1][i], positions[1][j]], color=line_color, linewidth = w)
  98.  
  99. def UpdateNeurons(synapses, values, i):
  100.     for x in range(10):
  101.         val = 0
  102.         for y in range(10):
  103.             val += synapses[x, y] * values[i, y]
  104.         if val < 0:
  105.             val = 0
  106.         elif val > 1:
  107.             val = 1
  108.         values[i + 1, x] = val
  109.  
  110. parent = MatrixCreate(10,50)
  111. parent = MatrixRandomize(parent)
  112.  
  113. neuronValues = MatrixCreate(10,10)
  114. for i in range(10):
  115.     neuronValues[0,i] = .5
  116.    
  117. for i in range(9):
  118.     UpdateNeurons(parent, neuronValues,i)
  119.        
  120.     #mpl.imshow(neuronValues, cmap=mpl.cm.gray, aspect = 'auto', interpolation='nearest')
  121.    # mpl.show()
  122.    
  123. finalValues = neuronValues[9,:]
  124. desiredValues = VectorCreate(10)
  125.  
  126. parentFitness = Fitness2(neuronValues)
  127.  
  128. fitness = VectorCreate(10000)
  129.  
  130. neuronVals = MatrixCreate(10,10)
  131.  
  132.  
  133. #mpl.imshow(parent, cmap=mpl.cm.gray, aspect = 'auto', interpolation='nearest')
  134. #mpl.show()
  135.  
  136. for currentGen in range(0, 10000):
  137.    
  138.     fitness[currentGen] = parentFitness
  139.    
  140.     child = MatrixPerturb(parent, 0.05)
  141.     neuronVals[0,:] = .5
  142.    
  143.     for i in range(9):
  144.         UpdateNeurons(child, neuronVals,i)
  145.        
  146.     childFitness = Fitness2(neuronVals)
  147.    
  148.     if (childFitness > parentFitness):
  149.         parent = child
  150.         parentFitness = childFitness
  151.  
  152. #mpl.plot(fitness)
  153. mpl.imshow(neuronValues, cmap=mpl.cm.gray, aspect = 'auto', interpolation='nearest')
  154. mpl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement