Skip to content

Instantly share code, notes, and snippets.

@tarlanahad
Last active July 10, 2020 19:40
Show Gist options
  • Save tarlanahad/fda7143ca93ca3618d74f721ea867489 to your computer and use it in GitHub Desktop.
Save tarlanahad/fda7143ca93ca3618d74f721ea867489 to your computer and use it in GitHub Desktop.
def optimize(self):
for i in range(self.numOfEpochs):
for j in range(len(self.swarm_list)):
current_particle = self.swarm_list[j] # get current particle
Vcurr = grad_error(current_particle.position) # calculate current velocity of the particle
deltaV = self.w * Vcurr \
+ self.c1 * (current_particle.best_part_pos - current_particle.position) \
+ self.c2 * (self.best_swarm_position - current_particle.position) # calculate delta V
new_position = self.swarm_list[j].position - self.lr * deltaV # calculate the new position
self.swarm_list[j].setPos(new_position) # update the position of particle
if error(new_position) < self.best_swarm_error: # check the position if it's best for swarm
self.best_swarm_position = new_position
self.best_swarm_error = error(new_position)
print('Epoch: {0} | Best position: [{1},{2}] | Best known error: {3}'.format(i,
self.best_swarm_position[0],
self.best_swarm_position[1],
self.best_swarm_error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment