def fire(self):
        # X = αX + (1-α)[WI*I + W*X]
        X = plus(times(X, self.alpha),
                 times(plus(dot(WI, I), dot(W, X)), 1 - self.alpha))
        X = tanh(X)  #sigmoid(X)

        # Y = WO*X
        Y = dot(WO, X)
 def fire(self):
     # X = αX + (1-α)[WI*I + W*X]
     self.X = plus(
         times(self.X, self.alpha),
         times(plus(dot(self.WI, [1] + self.I), dot(self.W, [1] + self.X)),
               1 - self.alpha))
     self.X = tanh(self.X)
     # Y = WO*X
     self.Y = dot(self.WO, [1] + self.X)
コード例 #3
0
    def iterate(self, measurement, control ):
        if measurement:
            self.lost = False
	
        velocity = control
                
        # known are speeds, convert to absolute movements
        nao_movement_x     = velocity[0]
        nao_movement_y     = velocity[1]
        nao_movement_t     = velocity[2]
        # step forward using control vector u
        self.u[0][0] = nao_movement_x
        self.u[1][0] = nao_movement_y
        muBelief = self.mu
        
        # ________ PREDICTION ________
        
        # rotate using nao_movements theta
        t = nao_movement_t
        rotationmatrix = [[ math.cos( t ), -math.sin(t) ],[ math.sin(t), math.cos(t) ]]

        # Predict new measurement based on nao movement.
        # Nao moves x,y towards the ball 
        muBelief = matrix.subtract( muBelief , self.u )
        #print muBelief
        # Nao rotates theta
        muBelief = matrix.mult( rotationmatrix, muBelief )

        # add noise to motion
        muBelief = sample( muBelief, self.Sigma, 2)
        
        # covariance matrix update
        SigmaBelief = matrix.plus( self.Sigma , self.R)

        # ________ CORRECTION _________

        if measurement:
            self.z[0][0] = measurement[0]
            self.z[1][0] = measurement[1]

            # Since C = [1,0;0,1], drop it
            s = matrix.inverse2D( matrix.plus(  SigmaBelief, self.Q) )
            K = matrix.mult(SigmaBelief, s ) 

            self.mu = matrix.plus(  muBelief,  matrix.mult(K, matrix.subtract(self.z , muBelief)) )
            self.Sigma = matrix.mult(matrix.subtract( self.I, K ), SigmaBelief)
        else:
            # if no ball is found, use the predicted state!
            self.mu = muBelief
            self.Sigma = SigmaBelief

        self.ballPos = self.mu[0][0], self.mu[1][0]    
コード例 #4
0
def sample(mean, cov , dimensions):
    r = matrix.transpose( matrix.Cholesky(cov) )

    randoms = matrix.zero(dimensions, 1)
    for i in range(len(randoms)):
        randoms[i][0] = random.gauss(0,0.025)

    return matrix.plus( mean , matrix.mult(r, randoms))
コード例 #5
0
ファイル: kalmanFilter.py プロジェクト: Haya1/Master
def sample(mean, cov , dimensions):
    r = matrix.transpose( matrix.Cholesky(cov) )

    randoms = matrix.zero(dimensions, 1)
    for i in range(len(randoms)):
        randoms[i][0] = random.gauss(0,0.05)

    return matrix.plus( mean , matrix.mult(r, randoms))
def ljapexp(q=150, iterations=40, gamma0=10**-8, sigma=0.1):
	# input, hidden and output layer size
	p = 1 
	#q = 150
	r = 1 #300

	# strength of input activations
	INPUT_STRENGTH = 10**0
	def input_dist():
		return INPUT_STRENGTH*uniform(-1,1)

	def normgen(mu, sigma):
		def n():
			return normalvariate(mu, sigma)
		return n

	def unigen(a,b):
		def u():
			return uniform(a,b)
		return u

	def zerogen():
		def zero():	
			return 0
		return zero

	# matrix and initial activation randomization
	r = Reservoir(p, q, r)
	r.randomize_matrices(normgen(0,1))
	r.WI = random_matrix(q,p+1, unigen(-0.1,0.1))
	r.W  = random_matrix(q,q+1, normgen(0,sigma))
	#r.randomize_vectors(normgen(0,1))

	# make a copy of the reservoir
	r2 = copy.deepcopy(r)

	#perturbation = random_vector(q)
	#perturbation = times(perturbation, gamma0 / vector_len(perturbation))
	priemersum = 0
	for perturbed_node in range(q):
		r.randomize_vectors(normgen(0,1))
		perturbation = [0 for _ in range(q)] 
		perturbation[perturbed_node] = gamma0

		r2.X = plus(r2.X, perturbation)

		difvec = plus(times(r.X, -1), r2.X)

		exponentsum = 0
		lambdy = [0]*iterations
		for it in range(0, iterations):
			# introduce a random input
			r.I = random_vector(r.p, input_dist)
			r2.I = r.I
			
			# run one setp
			r.fire()
			r2.fire()

			# calculate the difference
			difvec = plus(times(r.X, -1), r2.X)
			gammaK = vector_len(difvec)

			# renormalize
			r2.X = plus(r.X, times(difvec, gamma0 / gammaK))
			
			# record lambda
			mylog = math.log(gammaK / gamma0)
			lambdy[it] = mylog

		priemer = sum(lambdy) / iterations
		disperzia = sum([ (x-priemer)*(x-priemer) for x in lambdy]) / iterations
		priemersum += priemer
		print("\rnodeindex=%d" % perturbed_node, end="")

	return priemersum / q
コード例 #7
0
ファイル: kalmanFilter.py プロジェクト: Haya1/Master
    def iterate(self, measurement, control):
        now = time.time()
        # timestamps matter. IMPORTANT: Do not use if first iteration has not been set. 
        if self.firstCall:
            self.firstCall = False
            
        timeTaken = time.time() - self.timeStamp
        # major screwup if this happens 
        if timeTaken > 1.0:
            print 'Interval was way too long: ', timeTaken, 'seconds.'
            timeTaken = 0.0
        self.timeStamp = time.time() 

        #velocity = motProxy.getRobotVelocity()
        velocity = control
                
        # known are speeds, convert to absolute movements
        nao_movement_x     = velocity[0] * timeTaken
        nao_movement_y     = velocity[1] * timeTaken
        nao_movement_t     = velocity[2] * timeTaken
        #print timeTaken
        # step forward using control vector u
        self.u[0][0] = nao_movement_x
        self.u[1][0] = nao_movement_y
        print 'Increment control ', self.u 
        muBelief = self.mu
        
        # ________ PREDICTION ________
        
        # rotate using nao_movements theta
        t = nao_movement_t
        rotationmatrix = [[ math.cos( t ), -math.sin(t) ],[ math.sin(t), math.cos(t) ]]

        # Predict new measurement based on nao movement.
        # Nao moves x,y towards the ball 
        muBelief = matrix.subtract( muBelief , self.u )
        #print muBelief
        # Nao rotates theta
        muBelief = matrix.mult( rotationmatrix, muBelief )

        # add noise to motion
        muBelief = sample( muBelief, self.Sigma, 2)
        
        # covariance matrix update
        SigmaBelief = matrix.plus( self.Sigma , self.R)

        # ________ CORRECTION _________

        if measurement:
            self.z[0][0] = measurement[0]
            self.z[1][0] = measurement[1]

            # Since C = [1,0;0,1], drop it
            s = matrix.inverse2D( matrix.plus(  SigmaBelief, self.Q) )
            K = matrix.mult(SigmaBelief, s ) 

            self.mu = matrix.plus(  muBelief,  matrix.mult(K, matrix.subtract(self.z , muBelief)) )
            self.Sigma = matrix.mult(matrix.subtract( self.I, K ), SigmaBelief)
        else:
            # if no ball is found, use the predicted state!
            self.mu = muBelief
            self.Sigma = SigmaBelief

        #print 'Mu:',self.mu
        #print 'Sigma: '
        #matrix.show(self.Sigma)
        #print ''
        return (self.mu[0][0], self.mu[1][0])
r = Reservoir(p, q, r)
r.randomize_matrices(norm)
r.WI = random_matrix(q, p + 1, unigen(-0.1, 0.1))
r.W = random_matrix(q, q + 1, normgen(0, 10**(-0.8)))
r.randomize_vectors(normgen(0, 1))

# make a copy of the reservoir
r2 = copy.deepcopy(r)

#perturbation = random_vector(q)
#perturbation = times(perturbation, gamma0 / vector_len(perturbation))
perturbation = [0] * q
perturbation[randrange(q)] = gamma0

#print(perturbation)
r2.X = plus(r2.X, perturbation)

difvec = plus(times(r.X, -1), r2.X)
#print("difvec = %s" % difvec)
f = open('output.dat', 'w')

exponentsum = 0

lambdy = [0] * ITERATIONS
for it in range(0, ITERATIONS):
    r.I = random_vector(r.p, input_dist)
    r2.I = r.I
    r.fire()
    r2.fire()
    difvec = plus(times(r.X, -1), r2.X)
    gammaK = vector_len(difvec)
	def fire(self):
		# X = αX + (1-α)[WI*I + W*X]
		self.X = plus(times(self.X, self.alpha), times(plus( dot(self.WI, [1]+self.I), dot(self.W,[1]+self.X)), 1 - self.alpha))
		self.X = tanh(self.X)
		# Y = WO*X
		self.Y = dot(self.WO, [1]+self.X)
コード例 #10
0
def ljapexp(q=150, iterations=40, gamma0=10**-8, sigma=0.1):
    # input, hidden and output layer size
    p = 1
    #q = 150
    r = 1  #300

    # strength of input activations
    INPUT_STRENGTH = 10**0

    def input_dist():
        return INPUT_STRENGTH * uniform(-1, 1)

    def normgen(mu, sigma):
        def n():
            return normalvariate(mu, sigma)

        return n

    def unigen(a, b):
        def u():
            return uniform(a, b)

        return u

    def zerogen():
        def zero():
            return 0

        return zero

    # matrix and initial activation randomization
    r = Reservoir(p, q, r)
    r.randomize_matrices(normgen(0, 1))
    r.WI = random_matrix(q, p + 1, unigen(-0.1, 0.1))
    r.W = random_matrix(q, q + 1, normgen(0, sigma))
    #r.randomize_vectors(normgen(0,1))

    # make a copy of the reservoir
    r2 = copy.deepcopy(r)

    #perturbation = random_vector(q)
    #perturbation = times(perturbation, gamma0 / vector_len(perturbation))
    priemersum = 0
    for perturbed_node in range(q):
        r.randomize_vectors(normgen(0, 1))
        perturbation = [0 for _ in range(q)]
        perturbation[perturbed_node] = gamma0

        r2.X = plus(r2.X, perturbation)

        difvec = plus(times(r.X, -1), r2.X)

        exponentsum = 0
        lambdy = [0] * iterations
        for it in range(0, iterations):
            # introduce a random input
            r.I = random_vector(r.p, input_dist)
            r2.I = r.I

            # run one setp
            r.fire()
            r2.fire()

            # calculate the difference
            difvec = plus(times(r.X, -1), r2.X)
            gammaK = vector_len(difvec)

            # renormalize
            r2.X = plus(r.X, times(difvec, gamma0 / gammaK))

            # record lambda
            mylog = math.log(gammaK / gamma0)
            lambdy[it] = mylog

        priemer = sum(lambdy) / iterations
        disperzia = sum([(x - priemer) * (x - priemer)
                         for x in lambdy]) / iterations
        priemersum += priemer
        print("\rnodeindex=%d" % perturbed_node, end="")

    return priemersum / q
r = Reservoir(p, q, r)
r.randomize_matrices(norm)
r.WI = random_matrix(q,p+1, unigen(-0.1,0.1))
r.W  = random_matrix(q,q+1, normgen(0,10**(-0.8)))
r.randomize_vectors(normgen(0,1))

# make a copy of the reservoir
r2 = copy.deepcopy(r)

#perturbation = random_vector(q)
#perturbation = times(perturbation, gamma0 / vector_len(perturbation))
perturbation = [0]*q
perturbation[randrange(q)] = gamma0

#print(perturbation)
r2.X = plus(r2.X, perturbation)

difvec = plus(times(r.X, -1), r2.X)
#print("difvec = %s" % difvec)
f = open('output.dat','w')

exponentsum = 0

lambdy = [0]*ITERATIONS
for it in range(0, ITERATIONS):
	r.I = random_vector(r.p, input_dist)
	r2.I = r.I
	r.fire()
	r2.fire()
	difvec = plus(times(r.X, -1), r2.X)
	gammaK = vector_len(difvec)