예제 #1
0
def checkCollision(particle1: Particle, particle2: Particle):
    """Returns True if the two particles collided."""
    distance = magnitude(particle1.position - particle2.position)
    if distance < particle1.radius + particle2.radius and distance > magnitude(
            particle1.position + particle1.direction - particle2.position -
            particle2.direction):
        return True
    return False
예제 #2
0
	def execute_training_1 (self, pattern) :
		best_matched_prototype = None
		best_matched_activation = None
		matched_prototypes = list ()
		unmatched_prototypes = list ()
		for prototype in self.prototypes :
			if self.match (prototype, pattern) > self.vigilance:
				activation = self.activation (prototype, pattern)
				if best_matched_activation is None :
					best_matched_prototype = prototype
					best_matched_activation = activation
				elif best_matched_activation < activation :
					matched_prototypes.append (best_matched_prototype)
					best_matched_prototype = prototype
					best_matched_activation = activation
				else :
					matched_prototypes.append (prototype)
			else :
				unmatched_prototypes.append (prototype)
		if best_matched_prototype is None :
			best_matched_prototype = pattern
		adjusted_prototypes = list ()
		adjusted_prototypes.append (self.adjust (best_matched_prototype, pattern, self.learning))
		for prototype in matched_prototypes :
			adjusted_prototypes.append (prototype)
		for prototype in unmatched_prototypes :
			adjusted_prototypes.append (prototype)
		new_prototypes = list ()
		for prototype in adjusted_prototypes :
			if vectors.magnitude (prototype) <= 0.000000001 :
				transcripts.printf_warning ('Prototype vector is almost 0; pruning.')
			else :
				new_prototypes.append (prototype)
		self.prototypes = new_prototypes
		self.prototype_stepper.update (len (self.prototypes))
예제 #3
0
	def execute_training (self, pattern) :
		vector_length = len (pattern)
		if self.vector_length is None :
			self.vector_length = vector_length
		else :
			if self.vector_length != vector_length :
				raise Exception ()
		if vectors.magnitude (pattern) <= 0.000000001 :
			transcripts.printf_warning ('Pattern vector is almost 0; ignoring.')
			return
		self.training_stepper.increment ()
		self.execute_training_1 (pattern)
예제 #4
0
def collision(particle1: Particle, particle2: Particle):
    xrel = particle2.position - particle1.position
    # We shift the impulse space to give the particle2 zero impulse and save particle 1 in p1.
    impulseShift = particle2.getImpulseVector()
    p1 = particle1.getImpulseVector() - impulseShift
    p2neu = normalize(xrel) * np.dot(p1, xrel) / magnitude(xrel)
    particle1.speed, particle1.direction = combilize(
        (p1 - p2neu + impulseShift) / particle1.mass)
    particle2.speed, particle2.direction = combilize(
        (p2neu + impulseShift) / particle2.mass)
    particle1.numberOfCollisions += 1
    particle2.numberOfCollisions += 1
예제 #5
0
 def CalculateSensor(self, sensor):
     RUDirVec = rotateClockwise(self.UDirVec, 90)
     location = self.Back
     location = vectorAddScaled(location, RUDirVec, sensor[0])
     location = vectorAddScaled(location, self.UDirVec, sensor[1])
     dirVec = rotateClockwise(self.UDirVec, sensor[2])
     x = 0
     while True:
         trackTested = vectorAddScaled(location, dirVec, x)
         trackTested = int(trackTested[0]), int(trackTested[1])
         if screen.get_at(trackTested) != white:
             return magnitude(vectorSubtract(trackTested, location))
         x = x + 1
     return 0
예제 #6
0
    def RobotUpdate(self, speed, wheelDir):  # x,y of mid Front/Back
        #global Front, Back, DirVec, MagDirVec, UDirVec
        self.DirVec = vectorSubtract(self.Front, self.Back)
        self.MagDirVec = (self.DirVec[0]**2 + self.DirVec[1]**2)**.5
        self.UDirVec = self.DirVec[0] / self.MagDirVec, self.DirVec[
            1] / self.MagDirVec
        self.Back = vectorAddScaled(self.Back, self.UDirVec, speed)

        RUDirVec = rotateClockwise(self.UDirVec, wheelDir)
        self.Front = vectorAddScaled(self.Front, RUDirVec, speed)

        self.DirVec = vectorSubtract(self.Front, self.Back)
        self.MagDirVec = magnitude(self.DirVec)
        self.UDirVec = self.DirVec[0] / self.MagDirVec, self.DirVec[
            1] / self.MagDirVec
        self.Front = vectorAddScaled(self.Back, self.UDirVec, car_height)
        return
예제 #7
0
 def __init__(self, Front, direction, FileName, color):
     _temp = __import__(FileName, globals(), locals(),
                        ['IRSensors', 'RobotMove'], 0)
     self.IRSensors = _temp.IRSensors
     self.RobotMove = _temp.RobotMove
     self.Front = Front
     self.Back = [self.Front[0], self.Front[1] + car_height]
     self.IR = [0, 0, 0, 0, 0, 0, 0, 0]
     self.US = [0, 0, 0, 0]
     self.sensors = self.IR, self.US
     self.DirVec = vectorSubtract(self.Front, self.Back)
     self.MagDirVec = magnitude(self.DirVec)
     self.UDirVec = [
         self.DirVec[0] / self.MagDirVec, self.DirVec[1] / self.MagDirVec
     ]
     self.color = color
     return
예제 #8
0
	def end_training_1 (self) :
		prototypes = list ()
		for index in xrange (self.prototype_count) :
			numerator = self.prototype_numerators[index]
			denominator = self.prototype_denominators[index]
			if denominator == 0 :
				denominator = 1
			prototype = numerator / denominator
			prototype /= vectors.magnitude (prototype)
			prototypes.append (prototype)
		if self.prototypes is not None :
			prototypes_delta = 0
			for index in xrange (self.prototype_count) :
				old_prototype = self.prototypes[index]
				new_prototype = prototypes[index]
				prototype_delta = self.distance (old_prototype, new_prototype)
				prototypes_delta += prototype_delta
			self.training_delta = prototypes_delta
			transcripts.printf_output ('Training delta == %.4f;', self.training_delta)
		else :
			self.training_delta = None
		self.prototypes = prototypes
예제 #9
0
 def test_can_calculate_magnitude_of_vector(self):
     self.assertEqual(vectors.magnitude([4, 3]), 5)
예제 #10
0
def adjust_b (prototype, pattern, beta) :
	prototype_ = (1.0 - beta) * prototype + beta * vectors.minimum (prototype, pattern)
	prototype_ /= vectors.magnitude (prototype_)
	return prototype_
예제 #11
0
def adjust_a (prototype, pattern, distance, beta) :
	prototype_ = prototype + (beta * math.exp (- distance ** 2)) * (pattern - prototype)
	prototype_ = vectors.maximum (prototype_, 0.0)
	prototype_ /= vectors.magnitude (prototype_)
	return prototype_
예제 #12
0
	def adjust (self, prototype, pattern, learning) :
		distance = vectors.euclidean_distance (prototype, pattern)
		prototype_ = prototype + (learning * math.exp (- distance ** 2)) * (pattern - prototype)
		prototype_ = vectors.maximum (prototype_, 0.0)
		prototype_ /= vectors.magnitude (prototype_)
		return prototype_
예제 #13
0
	def adjust (self, prototype, pattern, learning) :
		prototype_ = (1.0 - learning) * prototype + learning * vectors.minimum (prototype, pattern)
		prototype_ /= vectors.magnitude (prototype_)
		return prototype_
예제 #14
0
	def adjust (self, prototype, pattern, learning) :
		prototype_ = prototype + learning * (pattern - prototype)
		prototype_ = vectors.maximum (prototype_, 0.0)
		prototype_ /= vectors.magnitude (prototype_)
		return prototype_