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

Fix optimise methods returning incorrect best_pos #322

Merged
merged 7 commits into from
Apr 25, 2019
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
2 changes: 1 addition & 1 deletion pyswarms/discrete/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def optimize(self, objective_func, iters, **kwargs):
self.swarm.position = self._compute_position(self.swarm)
# Obtain the final best_cost and the final best_position
final_best_cost = self.swarm.best_cost.copy()
final_best_pos = self.swarm.position[self.swarm.pbest_cost.argmin(axis=0)].copy()
final_best_pos = self.swarm.pbest_pos[self.swarm.pbest_cost.argmin()].copy()
self.rep.log(
"Optimization finished | best cost: {}, best pos: {}".format(
final_best_cost, final_best_pos
Expand Down
2 changes: 1 addition & 1 deletion pyswarms/single/general_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def optimize(self, objective_func, iters, **kwargs):
)
# Obtain the final best_cost and the final best_position
final_best_cost = self.swarm.best_cost.copy()
final_best_pos = self.swarm.position[self.swarm.pbest_cost.argmin()].copy()
final_best_pos = self.swarm.pbest_pos[self.swarm.pbest_cost.argmin()].copy()
# Write report in log and return final cost and position
self.rep.log(
"Optimization finished | best cost: {}, best pos: {}".format(
Expand Down
2 changes: 1 addition & 1 deletion pyswarms/single/global_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def optimize(self, objective_func, iters, **kwargs):
)
# Obtain the final best_cost and the final best_position
final_best_cost = self.swarm.best_cost.copy()
final_best_pos = self.swarm.best_pos.copy()
final_best_pos = self.swarm.pbest_pos[self.swarm.pbest_cost.argmin()].copy()
# Write report in log and return final cost and position
self.rep.log(
"Optimization finished | best cost: {}, best pos: {}".format(
Expand Down
2 changes: 1 addition & 1 deletion pyswarms/single/local_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def optimize(self, objective_func, iters, **kwargs):
)
# Obtain the final best_cost and the final best_position
final_best_cost = self.swarm.best_cost.copy()
final_best_pos = self.swarm.position[self.swarm.pbest_cost.argmin(axis=0)].copy()
final_best_pos = self.swarm.pbest_pos[self.swarm.pbest_cost.argmin()].copy()
# Write report in log and return final cost and position
self.rep.log(
"Optimization finished | best cost: {}, best pos: {}".format(
Expand Down
11 changes: 11 additions & 0 deletions tests/optimizers/test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Import modules
import pytest
import numpy as np

# Import from pyswarms
from pyswarms.discrete import BinaryPSO
Expand All @@ -28,3 +29,13 @@ def optimizer_reset(self, options):
opt.optimize(sphere, 10)
opt.reset()
return opt

def test_binary_correct_pos(self, options):
whzup marked this conversation as resolved.
Show resolved Hide resolved
""" Test to check binary optimiser returns the correct position
corresponding to the best cost """
opt = BinaryPSO(10, 2, options=options)
cost, pos = opt.optimize(sphere, 10)
# find best pos from history
min_cost_idx = np.argmin(opt.cost_history)
min_pos_idx = np.argmin(sphere(opt.pos_history[min_cost_idx]))
assert np.array_equal(opt.pos_history[min_cost_idx][min_pos_idx], pos)
8 changes: 8 additions & 0 deletions tests/optimizers/test_general_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ def test_obj_incorrect_kwargs(self, obj_with_args, optimizer):
with pytest.raises(TypeError):
# Wrong kwargs
cost, pos = optimizer.optimize(obj_with_args, 1000, c=1, d=100)

def test_general_correct_pos(self, options, optimizer):
""" Test to check general optimiser returns the correct position corresponding to the best cost """
cost, pos = optimizer.optimize(sphere, iters=5)
# find best pos from history
min_cost_idx = np.argmin(optimizer.cost_history)
min_pos_idx = np.argmin(sphere(optimizer.pos_history[min_cost_idx]))
assert np.array_equal(optimizer.pos_history[min_cost_idx][min_pos_idx], pos)
10 changes: 10 additions & 0 deletions tests/optimizers/test_global_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Import modules
import pytest
import numpy as np

# Import from pyswarms
from pyswarms.single import GlobalBestPSO
Expand All @@ -28,3 +29,12 @@ def optimizer_reset(self, options):
opt.optimize(sphere, 10)
opt.reset()
return opt

def test_global_correct_pos(self, options):
""" Test to check global optimiser returns the correct position corresponding to the best cost """
opt = GlobalBestPSO(n_particles=10, dimensions=2, options=options)
cost, pos = opt.optimize(sphere, iters=5)
# find best pos from history
min_cost_idx = np.argmin(opt.cost_history)
min_pos_idx = np.argmin(sphere(opt.pos_history[min_cost_idx]))
assert np.array_equal(opt.pos_history[min_cost_idx][min_pos_idx], pos)
10 changes: 10 additions & 0 deletions tests/optimizers/test_local_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Import modules
import pytest
import numpy as np

# Import from pyswarms
from pyswarms.single import LocalBestPSO
Expand All @@ -28,3 +29,12 @@ def optimizer_reset(self, options):
opt.optimize(sphere, 10)
opt.reset()
return opt

def test_local_correct_pos(self, options):
""" Test to check local optimiser returns the correct position corresponding to the best cost """
opt = LocalBestPSO(n_particles=10, dimensions=2, options=options)
cost, pos = opt.optimize(sphere, iters=5)
# find best pos from history
min_cost_idx = np.argmin(opt.cost_history)
min_pos_idx = np.argmin(sphere(opt.pos_history[min_cost_idx]))
assert np.array_equal(opt.pos_history[min_cost_idx][min_pos_idx], pos)