def updateParticlePosition(self): for i in range(0, self.dimension): self.position[i] = self.position[i] + self.velocity[i] clip(self.position[i], self.searchMins[i], self.searchMaxs[i])
def updateParticleVelocity(self): for i in range(0, self.dimension): # if mins[i] == maxs[i] then ignore, we aren't optimizing it if self.searchMins[i] == self.searchMaxs[i]: continue # Random component to avoid local minima R1 = random.random() R2 = random.random() self.velocity[i] = self.INERTIAL*self.velocity[i] \ + COG * R1 * (self.pBest_vars[i] - self.position[i]) \ + SOC * R2 * (self.gBest_position[i] - self.position[i]) clip(self.velocity[i], -self.velocityCap[i], self.velocityCap[i]) if fabs(self.velocity[i]) < VELOCITY_MINIMUM_MAGNITUDE: self.velocity[i] = 0
def updateParticlePosition(self): for i in range(0, self.dimension): newPosition = self.position[i] + self.velocity[i] if ((newPosition > self.searchMaxs[i]) or (newPosition < self.searchMins[i]) and RANDOMIZE_LOST_PARTICLES): if DEBUG: print 'Randomized a lost particle (dimension {0})'.format(i) self.position[i] = random.uniform(self.searchMins[i], self.searchMaxs[i]) self.velocity[i] = 0 else: self.position[i] = clip(newPosition, self.searchMins[i], self.searchMaxs[i])
def updateParticleVelocity(self): for i in range(0, self.dimension): # if mins[i] == maxs[i] then ignore, we aren't optimizing it if self.searchMins[i] == self.searchMaxs[i]: continue # Random component to avoid local minima R1 = random.random() R2 = random.random() newVelocity = self.INERTIAL*self.velocity[i] \ + COG * R1 * (self.pBest_position[i] - self.position[i]) \ + SOC * R2 * (self.gBest_position[i] - self.position[i]) self.velocity[i] = clip(newVelocity, -self.velocityCap[i], self.velocityCap[i]) if fabs(self.velocity[i]) < VELOCITY_MINIMUM_MAGNITUDE: self.velocity[i] = 0