Skip to content

Commit

Permalink
Fix best_cost
Browse files Browse the repository at this point in the history
  • Loading branch information
CPapadim authored Sep 25, 2017
1 parent 091202f commit ff79d2f
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pyswarms/single/local_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ def optimize(self, objective_func, iters, print_step=1, verbose=1):
# Obtain the indices of the best position for each
# neighbour-space, and get the local best cost and
# local best positions from it.
nmin_idx = self._get_neighbors(current_cost)
self.best_cost = current_cost[nmin_idx]
self.best_pos = self.pos[nmin_idx]
nmin_idx = self._get_neighbors(pbest_cost)
self.best_cost = pbest_cost[nmin_idx]
self.best_pos = self.personal_best_pos[nmin_idx]

# Print to console
if i % print_step == 0:
Expand Down Expand Up @@ -215,7 +215,7 @@ def optimize(self, objective_func, iters, print_step=1, verbose=1):
end_report(final_best_cost, final_best_pos, verbose, logger=self.logger)
return (final_best_cost, final_best_pos)

def _get_neighbors(self, current_cost):
def _get_neighbors(self, pbest_cost):
"""Helper function to obtain the best position found in the
neighborhood. This uses the cKDTree method from :code:`scipy`
to obtain the nearest neighbours
Expand All @@ -241,9 +241,9 @@ def _get_neighbors(self, current_cost):
# independently of each other.
if self.k == 1:
# The minimum index is itself, no mapping needed.
best_neighbor = current_cost[idx][:,np.newaxis].argmin(axis=1)
best_neighbor = pbest_cost[idx][:,np.newaxis].argmin(axis=1)
else:
idx_min = current_cost[idx].argmin(axis=1)
idx_min = pbest_cost[idx].argmin(axis=1)
best_neighbor = idx[np.arange(len(idx)), idx_min]

return best_neighbor
Expand Down

0 comments on commit ff79d2f

Please sign in to comment.