diff --git a/pyswarms/discrete/binary.py b/pyswarms/discrete/binary.py index 49804452..01ca32ca 100644 --- a/pyswarms/discrete/binary.py +++ b/pyswarms/discrete/binary.py @@ -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 diff --git a/pyswarms/single/general_optimizer.py b/pyswarms/single/general_optimizer.py index 35f24ac0..07004f6a 100644 --- a/pyswarms/single/general_optimizer.py +++ b/pyswarms/single/general_optimizer.py @@ -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( diff --git a/pyswarms/single/global_best.py b/pyswarms/single/global_best.py index 3f9e9724..b815d6d4 100644 --- a/pyswarms/single/global_best.py +++ b/pyswarms/single/global_best.py @@ -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( diff --git a/pyswarms/single/local_best.py b/pyswarms/single/local_best.py index 446eba44..a89a2692 100644 --- a/pyswarms/single/local_best.py +++ b/pyswarms/single/local_best.py @@ -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( diff --git a/tests/optimizers/test_binary.py b/tests/optimizers/test_binary.py index 0fca5fc4..8a2f5a2f 100644 --- a/tests/optimizers/test_binary.py +++ b/tests/optimizers/test_binary.py @@ -3,6 +3,7 @@ # Import modules import pytest +import numpy as np # Import from pyswarms from pyswarms.discrete import BinaryPSO @@ -28,3 +29,13 @@ def optimizer_reset(self, options): opt.optimize(sphere, 10) opt.reset() return opt + + def test_binary_correct_pos(self, options): + """ 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) diff --git a/tests/optimizers/test_general_optimizer.py b/tests/optimizers/test_general_optimizer.py index 96cee252..20a4bef9 100644 --- a/tests/optimizers/test_general_optimizer.py +++ b/tests/optimizers/test_general_optimizer.py @@ -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) diff --git a/tests/optimizers/test_global_best.py b/tests/optimizers/test_global_best.py index 58187288..1f32fc67 100644 --- a/tests/optimizers/test_global_best.py +++ b/tests/optimizers/test_global_best.py @@ -3,6 +3,7 @@ # Import modules import pytest +import numpy as np # Import from pyswarms from pyswarms.single import GlobalBestPSO @@ -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) diff --git a/tests/optimizers/test_local_best.py b/tests/optimizers/test_local_best.py index 40df0e47..3650e697 100644 --- a/tests/optimizers/test_local_best.py +++ b/tests/optimizers/test_local_best.py @@ -3,6 +3,7 @@ # Import modules import pytest +import numpy as np # Import from pyswarms from pyswarms.single import LocalBestPSO @@ -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)