Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update binary.py #34

Merged
merged 2 commits into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions pyswarms/discrete/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,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 @@ -202,15 +202,15 @@ 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

Parameters
----------
current_cost : numpy.ndarray of size (n_particles, )
the cost incurred at the current position. Will be used for
pbest_cost : numpy.ndarray of size (n_particles, )
the cost incurred at the historically best position. Will be used for
mapping the obtained indices to its actual cost.

Returns
Expand All @@ -228,9 +228,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 Expand Up @@ -284,4 +284,4 @@ def _sigmoid(self, x):
numpy.ndarray
Output sigmoid computation
"""
return 1 / (1 + np.exp(x))
return 1 / (1 + np.exp(x))
16 changes: 8 additions & 8 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,15 +215,15 @@ 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

Parameters
----------
current_cost : numpy.ndarray of size :code:`(n_particles, )`
the cost incurred at the current position. Will be used for
pbest_cost : numpy.ndarray of size :code:`(n_particles, )`
the cost incurred at the historically best position. Will be used for
mapping the obtained indices to its actual cost.

Returns
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