Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as num
- import random
- import matplotlib.pyplot as mpl
- import math
- def MatrixCreate(rows, cols):
- return num.zeros((rows,cols))
- def VectorCreate(length):
- return num.zeros((length), dtype='f')
- def MatrixRandomize(matrix):
- for x in range(len(matrix)):
- for y in range(len(matrix[x])):
- matrix[x][y] = float(random.random() + random.random() - 1)
- return matrix;
- def MeanDistance(v1, v2):
- total = 0
- for i in range(10):
- total += abs(v1[i] - v2[1])
- return total / 10
- def Fitness(finalValues, desiredValues):
- for j in range(0, 10, 2):
- desiredValues[j] = 1
- distance = MeanDistance(finalValues, desiredValues)
- return 1 - distance
- def Fitness2(neuronValues):
- diff=0.0
- for i in range(0,9):
- for j in range(0,9):
- diff=diff + abs(neuronValues[i,j]-neuronValues[i,j+1])
- diff=diff + abs(neuronValues[i+1,j]-neuronValues[i,j])
- diff=diff/(2*9*9)
- return diff
- def MatrixPerturb(matrix, prob):
- child = matrix.copy()
- x, y = child.shape
- for i in range(x):
- for j in range(y):
- if prob > random.random():
- child[i, j] = random.random()
- return child
- def GenerateGene(genes):
- parent = MatrixCreate(1, 50)
- parent = MatrixRandomize(parent)
- parentFitness = Fitness(parent)
- fits = []
- for currentGeneration in range(0, 5000):
- fits.append(parentFitness)
- for i in range(50):
- genes[i, currentGeneration] = parent[0][i]
- child = MatrixPerturb(parent, 0.05)
- childFitness = Fitness(child)
- if childFitness > parentFitness:
- parent = child
- parentFitness = childFitness
- mpl.plot(fits)
- def ComputePositions(amt):
- angle = 0.0
- pi = math.pi
- angleUpdate = 2.0 * pi / float(amt)
- neuronPositions = MatrixCreate(2,amt)
- for i in range(amt):
- neuronPositions[0,i] = math.sin(angle)
- neuronPositions[1,i] = math.cos(angle)
- angle = angle + angleUpdate
- return neuronPositions
- def PlotSynapses(positions, synapses):
- for i in range(10):
- for j in (x for x in range(10) if x != i):
- if synapses[i, j] < 0:
- line_color = [.3, .9, .9]
- else:
- line_color = 'black'
- w = int(10*abs(synapses[i,j]))+1
- mpl.plot([positions[0][i], positions[0,j]], [positions[1][i], positions[1][j]], color=line_color, linewidth = w)
- def UpdateNeurons(synapses, values, i):
- for x in range(10):
- val = 0
- for y in range(10):
- val += synapses[x, y] * values[i, y]
- if val < 0:
- val = 0
- elif val > 1:
- val = 1
- values[i + 1, x] = val
- parent = MatrixCreate(10,50)
- parent = MatrixRandomize(parent)
- neuronValues = MatrixCreate(10,10)
- for i in range(10):
- neuronValues[0,i] = .5
- for i in range(9):
- UpdateNeurons(parent, neuronValues,i)
- #mpl.imshow(neuronValues, cmap=mpl.cm.gray, aspect = 'auto', interpolation='nearest')
- # mpl.show()
- finalValues = neuronValues[9,:]
- desiredValues = VectorCreate(10)
- parentFitness = Fitness2(neuronValues)
- fitness = VectorCreate(10000)
- neuronVals = MatrixCreate(10,10)
- #mpl.imshow(parent, cmap=mpl.cm.gray, aspect = 'auto', interpolation='nearest')
- #mpl.show()
- for currentGen in range(0, 10000):
- fitness[currentGen] = parentFitness
- child = MatrixPerturb(parent, 0.05)
- neuronVals[0,:] = .5
- for i in range(9):
- UpdateNeurons(child, neuronVals,i)
- childFitness = Fitness2(neuronVals)
- if (childFitness > parentFitness):
- parent = child
- parentFitness = childFitness
- #mpl.plot(fitness)
- mpl.imshow(neuronValues, cmap=mpl.cm.gray, aspect = 'auto', interpolation='nearest')
- mpl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement