From b347ccd076e27034cde97f736cf301ce7f892340 Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 23 Mar 2019 17:48:42 +1100 Subject: [PATCH 1/7] Fix bug where optimise methods return incorrect pos. This resolves #321 --- pyswarms/backend/handlers.py | 12 +-- pyswarms/backend/topology/ring.py | 4 +- pyswarms/discrete/binary.py | 4 +- pyswarms/single/general_optimizer.py | 4 +- pyswarms/single/local_best.py | 4 +- tests.py | 93 ++++++++++++++++++++++ tests/backend/conftest.py | 74 ++++++++++------- tests/backend/test_handlers.py | 44 ++++++---- tests/backend/test_operators.py | 16 ++-- tests/backend/topology/test_pyramid.py | 4 +- tests/backend/topology/test_random.py | 13 ++- tests/backend/topology/test_ring.py | 13 ++- tests/backend/topology/test_von_neumann.py | 13 ++- tests/optimizers/test_binary.py | 22 +++++ tests/optimizers/test_general_optimizer.py | 12 +++ tests/optimizers/test_global_best.py | 9 +++ tests/optimizers/test_local_best.py | 9 +++ 17 files changed, 275 insertions(+), 75 deletions(-) create mode 100644 tests.py diff --git a/pyswarms/backend/handlers.py b/pyswarms/backend/handlers.py index ab755950..d0d2ff26 100644 --- a/pyswarms/backend/handlers.py +++ b/pyswarms/backend/handlers.py @@ -196,13 +196,15 @@ def reflective(self, position, bounds, **kwargs): new_pos = position while lower_than_bound[0].size != 0 or greater_than_bound[0].size != 0: if lower_than_bound[0].size > 0: - new_pos[lower_than_bound] = 2 * lb[lower_than_bound[1]] - \ - new_pos[lower_than_bound] + new_pos[lower_than_bound] = ( + 2 * lb[lower_than_bound[1]] - new_pos[lower_than_bound] + ) if greater_than_bound[0].size > 0: - new_pos[greater_than_bound] = 2 * ub[greater_than_bound[1]] - \ - new_pos[greater_than_bound] + new_pos[greater_than_bound] = ( + 2 * ub[greater_than_bound[1]] - new_pos[greater_than_bound] + ) lower_than_bound, greater_than_bound = self._out_of_bounds( - new_pos, bounds + new_pos, bounds ) return new_pos diff --git a/pyswarms/backend/topology/ring.py b/pyswarms/backend/topology/ring.py index 573e0fae..0f1ca901 100644 --- a/pyswarms/backend/topology/ring.py +++ b/pyswarms/backend/topology/ring.py @@ -65,9 +65,7 @@ def compute_gbest(self, swarm, p, k, **kwargs): if (self.static and self.neighbor_idx is None) or not self.static: # Obtain the nearest-neighbors for each particle tree = cKDTree(swarm.position) - _, self.neighbor_idx = tree.query( - swarm.position, p=p, k=k - ) + _, self.neighbor_idx = tree.query(swarm.position, p=p, k=k) # Map the computed costs to the neighbour indices and take the # argmin. If k-neighbors is equal to 1, then the swarm acts diff --git a/pyswarms/discrete/binary.py b/pyswarms/discrete/binary.py index 49804452..caaff700 100644 --- a/pyswarms/discrete/binary.py +++ b/pyswarms/discrete/binary.py @@ -202,7 +202,9 @@ 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.best_pos[ + self.swarm.pbest_cost.argmin(axis=0) + ].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..d473334c 100644 --- a/pyswarms/single/general_optimizer.py +++ b/pyswarms/single/general_optimizer.py @@ -248,7 +248,9 @@ 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.best_pos[ + self.swarm.best_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..eec2bbbd 100644 --- a/pyswarms/single/local_best.py +++ b/pyswarms/single/local_best.py @@ -235,7 +235,9 @@ 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.best_pos[ + self.swarm.pbest_cost.argmin(axis=0) + ].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.py b/tests.py new file mode 100644 index 00000000..199bfb8a --- /dev/null +++ b/tests.py @@ -0,0 +1,93 @@ + + +####################################### +## TEST: BinaryPSO +""" +print("Testing BinaryPSO...\n") + +def f_per_particle(m): + return sum(m) + +def f(x): + n_particles = x.shape[0] + j = [f_per_particle(x[i]) for i in range(n_particles)] + return np.array(j) + +options = {'c1': 0.5, 'c2': 0.5, 'w':0.9, 'k': 30, 'p':2} +optimizer = ps.discrete.BinaryPSO(n_particles=30, dimensions=15, options=options) +cost, pos = optimizer.optimize(f, iters=5) + +assert f_per_particle(pos) == cost + +###################### +## TEST: GlobalBestPOS +print("\nTesting GlobalBestPSO...\n") +options = {'c1': 0.5, 'c2': 0.3, 'w':0.9} +optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options) +cost, pos = optimizer.optimize(fx.sphere, iters=5) + +assert sum(pos ** 2) == cost + +##################### +## TEST: LocalBestPOS +print("\nTesting LocalBestPSO...\n") +options = {'c1': 0.5, 'c2': 0.3, 'w':0.9, 'k': 2, 'p': 2} +optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, options=options) +cost, pos = optimizer.optimize(fx.sphere, iters=5) + +assert sum(pos ** 2) == cost + + +####################################### +## TEST: GeneralBestPOS +# No test implemented +""" + +import numpy as np +import pyswarms as ps +from pyswarms.utils.functions import single_obj as fx +from pyswarms.backend.topology import Pyramid +import unittest + +class TestOptimizer(unittest.TestCase): + def test_binary(self): + dim = 10 + x = np.random.rand(dim,dim) + def f_per_particle(m): + # Get the subset of the features from the binary mask + if np.count_nonzero(m) == 0: + return sum(x) + return sum(x[:, m==1]).mean() + def f(x): + n_particles = x.shape[0] + j = [f_per_particle(x[i]) for i in range(n_particles)] + return np.array(j) + options = {'c1': 0.5, 'c2': 0.5, 'w':0.9, 'k': 2, 'p':2} + optimizer = ps.discrete.BinaryPSO(n_particles=5, dimensions=dim, options=options) + cost, pos = optimizer.optimize(f, iters=5) + self.assertTrue(f_per_particle(pos) == cost) + + def test_global(self): + options = {'c1': 0.5, 'c2': 0.3, 'w':0.9} + optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options) + cost, pos = optimizer.optimize(fx.sphere, iters=5) + self.assertTrue(sum(pos ** 2) == cost) + + def test_local(self): + options = {'c1': 0.5, 'c2': 0.3, 'w':0.9, 'k': 2, 'p': 2} + optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, options=options) + cost, pos = optimizer.optimize(fx.sphere, iters=5) + self.assertTrue(sum(pos ** 2) == cost) + + def test_general(self): + options = {'c1': 0.5, 'c2': 0.3, 'w':0.9} + my_topology = Pyramid(static=False) + optimizer = ps.single.GeneralOptimizerPSO(n_particles=10, dimensions=2, + options=options, topology=my_topology) + cost, pos = optimizer.optimize(fx.sphere, iters=5) + print("\n\n", pos) + print(sum(pos ** 2), '\n\n') + self.assertTrue(sum(pos ** 2) == cost) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/backend/conftest.py b/tests/backend/conftest.py index 2d4baf56..0a3581aa 100644 --- a/tests/backend/conftest.py +++ b/tests/backend/conftest.py @@ -27,56 +27,74 @@ def swarm(): } return Swarm(**attrs_at_t) + @pytest.fixture def bounds(): bounds_ = (np.array([2, 3, 1]), np.array([4, 7, 8])) return bounds_ + @pytest.fixture def clamp(): clamp_ = (np.array([2, 3, 1]), np.array([4, 7, 8])) return clamp_ + @pytest.fixture def positions_inbound(): - pos_ = np.array([[3.3, 4.4, 2.3], - [3.7, 5.2, 7.0], - [2.5, 6.8, 2.3], - [2.1, 6.9, 4.7], - [2.7, 3.2, 3.5], - [2.5, 5.1, 1.2] - ]) + pos_ = np.array( + [ + [3.3, 4.4, 2.3], + [3.7, 5.2, 7.0], + [2.5, 6.8, 2.3], + [2.1, 6.9, 4.7], + [2.7, 3.2, 3.5], + [2.5, 5.1, 1.2], + ] + ) return pos_ + @pytest.fixture def positions_out_of_bound(): - pos_ = np.array([[5.3, 4.4, 2.3], - [3.7, 9.2, 7.0], - [8.5, 0.8, 2.3], - [2.1, 6.9, 0.7], - [2.7, 9.2, 3.5], - [1.5, 5.1, 9.2] - ]) + pos_ = np.array( + [ + [5.3, 4.4, 2.3], + [3.7, 9.2, 7.0], + [8.5, 0.8, 2.3], + [2.1, 6.9, 0.7], + [2.7, 9.2, 3.5], + [1.5, 5.1, 9.2], + ] + ) return pos_ + @pytest.fixture def velocities_inbound(): - pos_ = np.array([[3.3, 4.4, 2.3], - [3.7, 5.2, 7.0], - [2.5, 6.8, 2.3], - [2.1, 6.9, 4.7], - [2.7, 3.2, 3.5], - [2.5, 5.1, 1.2] - ]) + pos_ = np.array( + [ + [3.3, 4.4, 2.3], + [3.7, 5.2, 7.0], + [2.5, 6.8, 2.3], + [2.1, 6.9, 4.7], + [2.7, 3.2, 3.5], + [2.5, 5.1, 1.2], + ] + ) return pos_ + @pytest.fixture def velocities_out_of_bound(): - pos_ = np.array([[5.3, 4.4, 2.3], - [3.7, 9.2, 7.0], - [8.5, 0.8, 2.3], - [2.1, 6.9, 0.7], - [2.7, 9.2, 3.5], - [1.5, 5.1, 9.2] - ]) + pos_ = np.array( + [ + [5.3, 4.4, 2.3], + [3.7, 9.2, 7.0], + [8.5, 0.8, 2.3], + [2.1, 6.9, 0.7], + [2.7, 9.2, 3.5], + [1.5, 5.1, 9.2], + ] + ) return pos_ diff --git a/tests/backend/test_handlers.py b/tests/backend/test_handlers.py index 33852efa..6b887102 100644 --- a/tests/backend/test_handlers.py +++ b/tests/backend/test_handlers.py @@ -9,10 +9,21 @@ ) import pyswarms.backend.handlers as h -bh_strategies = [name for name, _ in inspect.getmembers(h.BoundaryHandler(""), - predicate=inspect.ismethod) if not name.startswith(("__", "_"))] -vh_strategies = [name for name, _ in inspect.getmembers(h.VelocityHandler(""), - predicate=inspect.ismethod) if not name.startswith(("__", "_"))] +bh_strategies = [ + name + for name, _ in inspect.getmembers( + h.BoundaryHandler(""), predicate=inspect.ismethod + ) + if not name.startswith(("__", "_")) +] +vh_strategies = [ + name + for name, _ in inspect.getmembers( + h.VelocityHandler(""), predicate=inspect.ismethod + ) + if not name.startswith(("__", "_")) +] + def test_out_of_bounds(bounds, positions_inbound, positions_out_of_bound): hm = HandlerMixin() @@ -34,9 +45,8 @@ def test_out_of_bounds(bounds, positions_inbound, positions_out_of_bound): np.ravel(idx_out_of_bounds[1]).all() == np.ravel(expected_idx[1]).all() ) -@pytest.mark.parametrize( - "strategy", bh_strategies -) + +@pytest.mark.parametrize("strategy", bh_strategies) def test_bound_handling( bounds, positions_inbound, positions_out_of_bound, strategy ): @@ -52,6 +62,7 @@ def test_bound_handling( assert lower_than_bound.all() assert greater_than_bound.all() + def test_nearest_strategy(bounds, positions_inbound, positions_out_of_bound): bh = BoundaryHandler(strategy="nearest") # TODO Add strategy specific tests @@ -86,16 +97,17 @@ def test_periodic_strategy(bounds, positions_inbound, positions_out_of_bound): bh = BoundaryHandler(strategy="periodic") # TODO Add strategy specific tests + def assert_clamp( -clamp, -velocities_inbound, -velocities_out_of_bound, -positions_inbound, -positions_out_of_bound, -vh, -bounds=None, + clamp, + velocities_inbound, + velocities_out_of_bound, + positions_inbound, + positions_out_of_bound, + vh, + bounds=None, ): -# Test if it doesn't handle inclamp velocities + # Test if it doesn't handle inclamp velocities inbound_handled = vh( velocities_inbound, clamp, position=positions_inbound, bounds=bounds ) @@ -114,8 +126,6 @@ def assert_clamp( assert not greater_than_clamp.all() - - def test_unmodified_strategy( clamp, velocities_inbound, velocities_out_of_bound ): diff --git a/tests/backend/test_operators.py b/tests/backend/test_operators.py index 94e6dd6b..716a3cfa 100644 --- a/tests/backend/test_operators.py +++ b/tests/backend/test_operators.py @@ -41,7 +41,9 @@ def test_return_values(self, swarm, clamp): assert (clamp[0] <= v).all() and (clamp[1] >= v).all() @pytest.mark.parametrize("swarm", [0, (1, 2, 3)]) - @pytest.mark.parametrize("vh_strat", ["unmodified", "zero", "invert", "adjust"]) + @pytest.mark.parametrize( + "vh_strat", ["unmodified", "zero", "invert", "adjust"] + ) def test_input_swarm(self, swarm, vh_strat): """Test if method raises AttributeError with wrong swarm""" vh = VelocityHandler(strategy=vh_strat) @@ -49,7 +51,9 @@ def test_input_swarm(self, swarm, vh_strat): P.compute_velocity(swarm, clamp=(0, 1), vh=vh) @pytest.mark.parametrize("options", [{"c1": 0.5, "c2": 0.3}]) - @pytest.mark.parametrize("vh_strat", ["unmodified", "zero", "invert", "adjust"]) + @pytest.mark.parametrize( + "vh_strat", ["unmodified", "zero", "invert", "adjust"] + ) def test_missing_kwargs(self, swarm, options, vh_strat): """Test if method raises KeyError with missing kwargs""" vh = VelocityHandler(strategy=vh_strat) @@ -74,13 +78,13 @@ def test_return_values(self, swarm, bounds, bh_strat): assert p.shape == swarm.velocity.shape if bounds is not None: assert (bounds[0] <= p).all() and (bounds[1] >= p).all() - + @pytest.mark.parametrize("swarm", [0, (1, 2, 3)]) - @pytest.mark.parametrize("bh_strat", ["nearest", "random", "shrink", - "intermediate"]) + @pytest.mark.parametrize( + "bh_strat", ["nearest", "random", "shrink", "intermediate"] + ) def test_input_swarm(self, swarm, bh_strat): """Test if method raises AttributeError with wrong swarm""" bh = BoundaryHandler(strategy=bh_strat) with pytest.raises(AttributeError): P.compute_position(swarm, bounds=([-5, -5], [5, 5]), bh=bh) - diff --git a/tests/backend/topology/test_pyramid.py b/tests/backend/topology/test_pyramid.py index eb3e0b16..02cd131e 100644 --- a/tests/backend/topology/test_pyramid.py +++ b/tests/backend/topology/test_pyramid.py @@ -28,7 +28,9 @@ def test_compute_gbest_return_values( """Test if compute_gbest() gives the expected return values""" topo = topology(static=static) expected_cost = 1.0002528364353296 - expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) + expected_pos = np.array( + [9.90438476e-01, 2.50379538e-03, 1.87405987e-05] + ) pos, cost = topo.compute_gbest(swarm, **options) assert cost == pytest.approx(expected_cost) assert pos[np.argmin(cost)] == pytest.approx(expected_pos) diff --git a/tests/backend/topology/test_random.py b/tests/backend/topology/test_random.py index 50ef3235..8c675036 100644 --- a/tests/backend/topology/test_random.py +++ b/tests/backend/topology/test_random.py @@ -29,12 +29,17 @@ def test_compute_gbest_return_values( """Test if update_gbest_neighborhood gives the expected return values""" topo = topology(static=static) expected_cost = 1.0002528364353296 - expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) - expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03]) + expected_pos = np.array( + [9.90438476e-01, 2.50379538e-03, 1.87405987e-05] + ) + expected_pos_2 = np.array( + [9.98033031e-01, 4.97392619e-03, 3.07726256e-03] + ) pos, cost = topo.compute_gbest(swarm, **options) assert cost == pytest.approx(expected_cost) - assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or - (pos[np.argmin(cost)] == pytest.approx(expected_pos_2))) + assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or ( + pos[np.argmin(cost)] == pytest.approx(expected_pos_2) + ) @pytest.mark.parametrize("static", [True, False]) @pytest.mark.parametrize("k", [1, 2]) diff --git a/tests/backend/topology/test_ring.py b/tests/backend/topology/test_ring.py index 03fa8948..8d55c81c 100644 --- a/tests/backend/topology/test_ring.py +++ b/tests/backend/topology/test_ring.py @@ -30,8 +30,13 @@ def test_compute_gbest_return_values(self, swarm, topology, p, k, static): topo = topology(static=static) pos, cost = topo.compute_gbest(swarm, p=p, k=k) expected_cost = 1.0002528364353296 - expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) - expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03]) + expected_pos = np.array( + [9.90438476e-01, 2.50379538e-03, 1.87405987e-05] + ) + expected_pos_2 = np.array( + [9.98033031e-01, 4.97392619e-03, 3.07726256e-03] + ) assert cost == pytest.approx(expected_cost) - assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or - (pos[np.argmin(cost)] == pytest.approx(expected_pos_2))) + assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or ( + pos[np.argmin(cost)] == pytest.approx(expected_pos_2) + ) diff --git a/tests/backend/topology/test_von_neumann.py b/tests/backend/topology/test_von_neumann.py index 7e3eed53..7331189b 100644 --- a/tests/backend/topology/test_von_neumann.py +++ b/tests/backend/topology/test_von_neumann.py @@ -27,12 +27,17 @@ def test_update_gbest_neighborhood(self, swarm, topology, p, r): """Test if update_gbest_neighborhood gives the expected return values""" topo = topology() pos, cost = topo.compute_gbest(swarm, p=p, r=r) - expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) - expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03]) + expected_pos = np.array( + [9.90438476e-01, 2.50379538e-03, 1.87405987e-05] + ) + expected_pos_2 = np.array( + [9.98033031e-01, 4.97392619e-03, 3.07726256e-03] + ) expected_cost = 1.0002528364353296 assert cost == pytest.approx(expected_cost) - assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or - (pos[np.argmin(cost)] == pytest.approx(expected_pos_2))) + assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or ( + pos[np.argmin(cost)] == pytest.approx(expected_pos_2) + ) @pytest.mark.parametrize("m", [i for i in range(3)]) @pytest.mark.parametrize("n", [i for i in range(3)]) diff --git a/tests/optimizers/test_binary.py b/tests/optimizers/test_binary.py index 0fca5fc4..87080893 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,24 @@ def optimizer_reset(self, options): opt.optimize(sphere, 10) opt.reset() return opt + + def test_binary_correct_pos(self, options): + dim = 10 + x = np.random.rand(dim, dim) + + def f_per_particle(m): + if np.count_nonzero(m) == 0: + return sum(x) + return sum(x[:, m == 1]).mean() + + def binary_eg(options): + def f(x): + n_particles = x.shape[0] + j = [f_per_particle(x[i]) for i in range(n_particles)] + return np.array(j) + + opt = BinaryPSO(n_particles=5, dimensions=dim, options=options) + return opt.optimize(f, iters=5) + + cost, pos = binary_eg(options) + assert f_per_particle(pos) == cost diff --git a/tests/optimizers/test_general_optimizer.py b/tests/optimizers/test_general_optimizer.py index 96cee252..93249b24 100644 --- a/tests/optimizers/test_general_optimizer.py +++ b/tests/optimizers/test_general_optimizer.py @@ -12,6 +12,7 @@ import pyswarms.backend.topology as t from pyswarms.single import GeneralOptimizerPSO from pyswarms.utils.functions.single_obj import sphere +from pyswarms.backend.topology import Pyramid from .abc_test_optimizer import ABCTestOptimizer @@ -95,3 +96,14 @@ 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 general_eg(self, options): + my_topology = Pyramid(static=False) + opt = GeneralOptimizerPSO( + n_particles=10, dimensions=2, options=options, topology=my_topology + ) + return opt.optimize(sphere, iters=5) + + def test_general_correct_pos(self, options): + cost, pos = self.general_eg(options) + assert sum(pos ** 2) == cost diff --git a/tests/optimizers/test_global_best.py b/tests/optimizers/test_global_best.py index 58187288..59b2e486 100644 --- a/tests/optimizers/test_global_best.py +++ b/tests/optimizers/test_global_best.py @@ -28,3 +28,12 @@ def optimizer_reset(self, options): opt.optimize(sphere, 10) opt.reset() return opt + + def global_eg(self, options): + opt = GlobalBestPSO(n_particles=10, dimensions=2, options=options) + return opt.optimize(sphere, iters=5) + + def test_global_correct_pos(self, options): + print("Running local test") + cost, pos = self.global_eg(options) + assert sum(pos ** 2) == cost diff --git a/tests/optimizers/test_local_best.py b/tests/optimizers/test_local_best.py index 40df0e47..a1f6a859 100644 --- a/tests/optimizers/test_local_best.py +++ b/tests/optimizers/test_local_best.py @@ -28,3 +28,12 @@ def optimizer_reset(self, options): opt.optimize(sphere, 10) opt.reset() return opt + + def local_eg(self, options): + opt = LocalBestPSO(n_particles=10, dimensions=2, options=options) + return opt.optimize(sphere, iters=5) + + def test_local_correct_pos(self, options): + print("Running local test") + cost, pos = self.local_eg(options) + assert sum(pos ** 2) == cost From 226a56ec1fce1d2826b24ced0ba0338bfeb01f6d Mon Sep 17 00:00:00 2001 From: ichbinjakes Date: Sat, 23 Mar 2019 17:54:51 +1100 Subject: [PATCH 2/7] Delete tests.py --- tests.py | 93 -------------------------------------------------------- 1 file changed, 93 deletions(-) delete mode 100644 tests.py diff --git a/tests.py b/tests.py deleted file mode 100644 index 199bfb8a..00000000 --- a/tests.py +++ /dev/null @@ -1,93 +0,0 @@ - - -####################################### -## TEST: BinaryPSO -""" -print("Testing BinaryPSO...\n") - -def f_per_particle(m): - return sum(m) - -def f(x): - n_particles = x.shape[0] - j = [f_per_particle(x[i]) for i in range(n_particles)] - return np.array(j) - -options = {'c1': 0.5, 'c2': 0.5, 'w':0.9, 'k': 30, 'p':2} -optimizer = ps.discrete.BinaryPSO(n_particles=30, dimensions=15, options=options) -cost, pos = optimizer.optimize(f, iters=5) - -assert f_per_particle(pos) == cost - -###################### -## TEST: GlobalBestPOS -print("\nTesting GlobalBestPSO...\n") -options = {'c1': 0.5, 'c2': 0.3, 'w':0.9} -optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options) -cost, pos = optimizer.optimize(fx.sphere, iters=5) - -assert sum(pos ** 2) == cost - -##################### -## TEST: LocalBestPOS -print("\nTesting LocalBestPSO...\n") -options = {'c1': 0.5, 'c2': 0.3, 'w':0.9, 'k': 2, 'p': 2} -optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, options=options) -cost, pos = optimizer.optimize(fx.sphere, iters=5) - -assert sum(pos ** 2) == cost - - -####################################### -## TEST: GeneralBestPOS -# No test implemented -""" - -import numpy as np -import pyswarms as ps -from pyswarms.utils.functions import single_obj as fx -from pyswarms.backend.topology import Pyramid -import unittest - -class TestOptimizer(unittest.TestCase): - def test_binary(self): - dim = 10 - x = np.random.rand(dim,dim) - def f_per_particle(m): - # Get the subset of the features from the binary mask - if np.count_nonzero(m) == 0: - return sum(x) - return sum(x[:, m==1]).mean() - def f(x): - n_particles = x.shape[0] - j = [f_per_particle(x[i]) for i in range(n_particles)] - return np.array(j) - options = {'c1': 0.5, 'c2': 0.5, 'w':0.9, 'k': 2, 'p':2} - optimizer = ps.discrete.BinaryPSO(n_particles=5, dimensions=dim, options=options) - cost, pos = optimizer.optimize(f, iters=5) - self.assertTrue(f_per_particle(pos) == cost) - - def test_global(self): - options = {'c1': 0.5, 'c2': 0.3, 'w':0.9} - optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options) - cost, pos = optimizer.optimize(fx.sphere, iters=5) - self.assertTrue(sum(pos ** 2) == cost) - - def test_local(self): - options = {'c1': 0.5, 'c2': 0.3, 'w':0.9, 'k': 2, 'p': 2} - optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, options=options) - cost, pos = optimizer.optimize(fx.sphere, iters=5) - self.assertTrue(sum(pos ** 2) == cost) - - def test_general(self): - options = {'c1': 0.5, 'c2': 0.3, 'w':0.9} - my_topology = Pyramid(static=False) - optimizer = ps.single.GeneralOptimizerPSO(n_particles=10, dimensions=2, - options=options, topology=my_topology) - cost, pos = optimizer.optimize(fx.sphere, iters=5) - print("\n\n", pos) - print(sum(pos ** 2), '\n\n') - self.assertTrue(sum(pos ** 2) == cost) - -if __name__ == '__main__': - unittest.main() From 1192964954608f0ed36871f5cb674a04fd6fc204 Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 30 Mar 2019 14:12:49 +1100 Subject: [PATCH 3/7] Revert flake formatting --- pyswarms/backend/handlers.py | 12 ++-- pyswarms/backend/topology/ring.py | 4 +- tests/backend/conftest.py | 74 ++++++++-------------- tests/backend/test_handlers.py | 44 +++++-------- tests/backend/test_operators.py | 16 ++--- tests/backend/topology/test_pyramid.py | 4 +- tests/backend/topology/test_random.py | 13 ++-- tests/backend/topology/test_ring.py | 13 ++-- tests/backend/topology/test_von_neumann.py | 13 ++-- 9 files changed, 72 insertions(+), 121 deletions(-) diff --git a/pyswarms/backend/handlers.py b/pyswarms/backend/handlers.py index d0d2ff26..ab755950 100644 --- a/pyswarms/backend/handlers.py +++ b/pyswarms/backend/handlers.py @@ -196,15 +196,13 @@ def reflective(self, position, bounds, **kwargs): new_pos = position while lower_than_bound[0].size != 0 or greater_than_bound[0].size != 0: if lower_than_bound[0].size > 0: - new_pos[lower_than_bound] = ( - 2 * lb[lower_than_bound[1]] - new_pos[lower_than_bound] - ) + new_pos[lower_than_bound] = 2 * lb[lower_than_bound[1]] - \ + new_pos[lower_than_bound] if greater_than_bound[0].size > 0: - new_pos[greater_than_bound] = ( - 2 * ub[greater_than_bound[1]] - new_pos[greater_than_bound] - ) + new_pos[greater_than_bound] = 2 * ub[greater_than_bound[1]] - \ + new_pos[greater_than_bound] lower_than_bound, greater_than_bound = self._out_of_bounds( - new_pos, bounds + new_pos, bounds ) return new_pos diff --git a/pyswarms/backend/topology/ring.py b/pyswarms/backend/topology/ring.py index 0f1ca901..573e0fae 100644 --- a/pyswarms/backend/topology/ring.py +++ b/pyswarms/backend/topology/ring.py @@ -65,7 +65,9 @@ def compute_gbest(self, swarm, p, k, **kwargs): if (self.static and self.neighbor_idx is None) or not self.static: # Obtain the nearest-neighbors for each particle tree = cKDTree(swarm.position) - _, self.neighbor_idx = tree.query(swarm.position, p=p, k=k) + _, self.neighbor_idx = tree.query( + swarm.position, p=p, k=k + ) # Map the computed costs to the neighbour indices and take the # argmin. If k-neighbors is equal to 1, then the swarm acts diff --git a/tests/backend/conftest.py b/tests/backend/conftest.py index 0a3581aa..2d4baf56 100644 --- a/tests/backend/conftest.py +++ b/tests/backend/conftest.py @@ -27,74 +27,56 @@ def swarm(): } return Swarm(**attrs_at_t) - @pytest.fixture def bounds(): bounds_ = (np.array([2, 3, 1]), np.array([4, 7, 8])) return bounds_ - @pytest.fixture def clamp(): clamp_ = (np.array([2, 3, 1]), np.array([4, 7, 8])) return clamp_ - @pytest.fixture def positions_inbound(): - pos_ = np.array( - [ - [3.3, 4.4, 2.3], - [3.7, 5.2, 7.0], - [2.5, 6.8, 2.3], - [2.1, 6.9, 4.7], - [2.7, 3.2, 3.5], - [2.5, 5.1, 1.2], - ] - ) + pos_ = np.array([[3.3, 4.4, 2.3], + [3.7, 5.2, 7.0], + [2.5, 6.8, 2.3], + [2.1, 6.9, 4.7], + [2.7, 3.2, 3.5], + [2.5, 5.1, 1.2] + ]) return pos_ - @pytest.fixture def positions_out_of_bound(): - pos_ = np.array( - [ - [5.3, 4.4, 2.3], - [3.7, 9.2, 7.0], - [8.5, 0.8, 2.3], - [2.1, 6.9, 0.7], - [2.7, 9.2, 3.5], - [1.5, 5.1, 9.2], - ] - ) + pos_ = np.array([[5.3, 4.4, 2.3], + [3.7, 9.2, 7.0], + [8.5, 0.8, 2.3], + [2.1, 6.9, 0.7], + [2.7, 9.2, 3.5], + [1.5, 5.1, 9.2] + ]) return pos_ - @pytest.fixture def velocities_inbound(): - pos_ = np.array( - [ - [3.3, 4.4, 2.3], - [3.7, 5.2, 7.0], - [2.5, 6.8, 2.3], - [2.1, 6.9, 4.7], - [2.7, 3.2, 3.5], - [2.5, 5.1, 1.2], - ] - ) + pos_ = np.array([[3.3, 4.4, 2.3], + [3.7, 5.2, 7.0], + [2.5, 6.8, 2.3], + [2.1, 6.9, 4.7], + [2.7, 3.2, 3.5], + [2.5, 5.1, 1.2] + ]) return pos_ - @pytest.fixture def velocities_out_of_bound(): - pos_ = np.array( - [ - [5.3, 4.4, 2.3], - [3.7, 9.2, 7.0], - [8.5, 0.8, 2.3], - [2.1, 6.9, 0.7], - [2.7, 9.2, 3.5], - [1.5, 5.1, 9.2], - ] - ) + pos_ = np.array([[5.3, 4.4, 2.3], + [3.7, 9.2, 7.0], + [8.5, 0.8, 2.3], + [2.1, 6.9, 0.7], + [2.7, 9.2, 3.5], + [1.5, 5.1, 9.2] + ]) return pos_ diff --git a/tests/backend/test_handlers.py b/tests/backend/test_handlers.py index 6b887102..33852efa 100644 --- a/tests/backend/test_handlers.py +++ b/tests/backend/test_handlers.py @@ -9,21 +9,10 @@ ) import pyswarms.backend.handlers as h -bh_strategies = [ - name - for name, _ in inspect.getmembers( - h.BoundaryHandler(""), predicate=inspect.ismethod - ) - if not name.startswith(("__", "_")) -] -vh_strategies = [ - name - for name, _ in inspect.getmembers( - h.VelocityHandler(""), predicate=inspect.ismethod - ) - if not name.startswith(("__", "_")) -] - +bh_strategies = [name for name, _ in inspect.getmembers(h.BoundaryHandler(""), + predicate=inspect.ismethod) if not name.startswith(("__", "_"))] +vh_strategies = [name for name, _ in inspect.getmembers(h.VelocityHandler(""), + predicate=inspect.ismethod) if not name.startswith(("__", "_"))] def test_out_of_bounds(bounds, positions_inbound, positions_out_of_bound): hm = HandlerMixin() @@ -45,8 +34,9 @@ def test_out_of_bounds(bounds, positions_inbound, positions_out_of_bound): np.ravel(idx_out_of_bounds[1]).all() == np.ravel(expected_idx[1]).all() ) - -@pytest.mark.parametrize("strategy", bh_strategies) +@pytest.mark.parametrize( + "strategy", bh_strategies +) def test_bound_handling( bounds, positions_inbound, positions_out_of_bound, strategy ): @@ -62,7 +52,6 @@ def test_bound_handling( assert lower_than_bound.all() assert greater_than_bound.all() - def test_nearest_strategy(bounds, positions_inbound, positions_out_of_bound): bh = BoundaryHandler(strategy="nearest") # TODO Add strategy specific tests @@ -97,17 +86,16 @@ def test_periodic_strategy(bounds, positions_inbound, positions_out_of_bound): bh = BoundaryHandler(strategy="periodic") # TODO Add strategy specific tests - def assert_clamp( - clamp, - velocities_inbound, - velocities_out_of_bound, - positions_inbound, - positions_out_of_bound, - vh, - bounds=None, +clamp, +velocities_inbound, +velocities_out_of_bound, +positions_inbound, +positions_out_of_bound, +vh, +bounds=None, ): - # Test if it doesn't handle inclamp velocities +# Test if it doesn't handle inclamp velocities inbound_handled = vh( velocities_inbound, clamp, position=positions_inbound, bounds=bounds ) @@ -126,6 +114,8 @@ def assert_clamp( assert not greater_than_clamp.all() + + def test_unmodified_strategy( clamp, velocities_inbound, velocities_out_of_bound ): diff --git a/tests/backend/test_operators.py b/tests/backend/test_operators.py index 716a3cfa..94e6dd6b 100644 --- a/tests/backend/test_operators.py +++ b/tests/backend/test_operators.py @@ -41,9 +41,7 @@ def test_return_values(self, swarm, clamp): assert (clamp[0] <= v).all() and (clamp[1] >= v).all() @pytest.mark.parametrize("swarm", [0, (1, 2, 3)]) - @pytest.mark.parametrize( - "vh_strat", ["unmodified", "zero", "invert", "adjust"] - ) + @pytest.mark.parametrize("vh_strat", ["unmodified", "zero", "invert", "adjust"]) def test_input_swarm(self, swarm, vh_strat): """Test if method raises AttributeError with wrong swarm""" vh = VelocityHandler(strategy=vh_strat) @@ -51,9 +49,7 @@ def test_input_swarm(self, swarm, vh_strat): P.compute_velocity(swarm, clamp=(0, 1), vh=vh) @pytest.mark.parametrize("options", [{"c1": 0.5, "c2": 0.3}]) - @pytest.mark.parametrize( - "vh_strat", ["unmodified", "zero", "invert", "adjust"] - ) + @pytest.mark.parametrize("vh_strat", ["unmodified", "zero", "invert", "adjust"]) def test_missing_kwargs(self, swarm, options, vh_strat): """Test if method raises KeyError with missing kwargs""" vh = VelocityHandler(strategy=vh_strat) @@ -78,13 +74,13 @@ def test_return_values(self, swarm, bounds, bh_strat): assert p.shape == swarm.velocity.shape if bounds is not None: assert (bounds[0] <= p).all() and (bounds[1] >= p).all() - + @pytest.mark.parametrize("swarm", [0, (1, 2, 3)]) - @pytest.mark.parametrize( - "bh_strat", ["nearest", "random", "shrink", "intermediate"] - ) + @pytest.mark.parametrize("bh_strat", ["nearest", "random", "shrink", + "intermediate"]) def test_input_swarm(self, swarm, bh_strat): """Test if method raises AttributeError with wrong swarm""" bh = BoundaryHandler(strategy=bh_strat) with pytest.raises(AttributeError): P.compute_position(swarm, bounds=([-5, -5], [5, 5]), bh=bh) + diff --git a/tests/backend/topology/test_pyramid.py b/tests/backend/topology/test_pyramid.py index 02cd131e..eb3e0b16 100644 --- a/tests/backend/topology/test_pyramid.py +++ b/tests/backend/topology/test_pyramid.py @@ -28,9 +28,7 @@ def test_compute_gbest_return_values( """Test if compute_gbest() gives the expected return values""" topo = topology(static=static) expected_cost = 1.0002528364353296 - expected_pos = np.array( - [9.90438476e-01, 2.50379538e-03, 1.87405987e-05] - ) + expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) pos, cost = topo.compute_gbest(swarm, **options) assert cost == pytest.approx(expected_cost) assert pos[np.argmin(cost)] == pytest.approx(expected_pos) diff --git a/tests/backend/topology/test_random.py b/tests/backend/topology/test_random.py index 8c675036..50ef3235 100644 --- a/tests/backend/topology/test_random.py +++ b/tests/backend/topology/test_random.py @@ -29,17 +29,12 @@ def test_compute_gbest_return_values( """Test if update_gbest_neighborhood gives the expected return values""" topo = topology(static=static) expected_cost = 1.0002528364353296 - expected_pos = np.array( - [9.90438476e-01, 2.50379538e-03, 1.87405987e-05] - ) - expected_pos_2 = np.array( - [9.98033031e-01, 4.97392619e-03, 3.07726256e-03] - ) + expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) + expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03]) pos, cost = topo.compute_gbest(swarm, **options) assert cost == pytest.approx(expected_cost) - assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or ( - pos[np.argmin(cost)] == pytest.approx(expected_pos_2) - ) + assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or + (pos[np.argmin(cost)] == pytest.approx(expected_pos_2))) @pytest.mark.parametrize("static", [True, False]) @pytest.mark.parametrize("k", [1, 2]) diff --git a/tests/backend/topology/test_ring.py b/tests/backend/topology/test_ring.py index 8d55c81c..03fa8948 100644 --- a/tests/backend/topology/test_ring.py +++ b/tests/backend/topology/test_ring.py @@ -30,13 +30,8 @@ def test_compute_gbest_return_values(self, swarm, topology, p, k, static): topo = topology(static=static) pos, cost = topo.compute_gbest(swarm, p=p, k=k) expected_cost = 1.0002528364353296 - expected_pos = np.array( - [9.90438476e-01, 2.50379538e-03, 1.87405987e-05] - ) - expected_pos_2 = np.array( - [9.98033031e-01, 4.97392619e-03, 3.07726256e-03] - ) + expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) + expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03]) assert cost == pytest.approx(expected_cost) - assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or ( - pos[np.argmin(cost)] == pytest.approx(expected_pos_2) - ) + assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or + (pos[np.argmin(cost)] == pytest.approx(expected_pos_2))) diff --git a/tests/backend/topology/test_von_neumann.py b/tests/backend/topology/test_von_neumann.py index 7331189b..7e3eed53 100644 --- a/tests/backend/topology/test_von_neumann.py +++ b/tests/backend/topology/test_von_neumann.py @@ -27,17 +27,12 @@ def test_update_gbest_neighborhood(self, swarm, topology, p, r): """Test if update_gbest_neighborhood gives the expected return values""" topo = topology() pos, cost = topo.compute_gbest(swarm, p=p, r=r) - expected_pos = np.array( - [9.90438476e-01, 2.50379538e-03, 1.87405987e-05] - ) - expected_pos_2 = np.array( - [9.98033031e-01, 4.97392619e-03, 3.07726256e-03] - ) + expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) + expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03]) expected_cost = 1.0002528364353296 assert cost == pytest.approx(expected_cost) - assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or ( - pos[np.argmin(cost)] == pytest.approx(expected_pos_2) - ) + assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or + (pos[np.argmin(cost)] == pytest.approx(expected_pos_2))) @pytest.mark.parametrize("m", [i for i in range(3)]) @pytest.mark.parametrize("n", [i for i in range(3)]) From 8c64a4de3106c9f643f9d24926d324380b2933c3 Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 31 Mar 2019 13:58:55 +1100 Subject: [PATCH 4/7] Correct return best pos code --- pyswarms/discrete/binary.py | 4 +--- pyswarms/single/general_optimizer.py | 4 +--- pyswarms/single/global_best.py | 2 +- pyswarms/single/local_best.py | 4 +--- tests/optimizers/test_binary.py | 23 ++++------------------ tests/optimizers/test_general_optimizer.py | 14 ++++--------- tests/optimizers/test_global_best.py | 9 +++------ tests/optimizers/test_local_best.py | 9 +++------ 8 files changed, 18 insertions(+), 51 deletions(-) diff --git a/pyswarms/discrete/binary.py b/pyswarms/discrete/binary.py index caaff700..01ca32ca 100644 --- a/pyswarms/discrete/binary.py +++ b/pyswarms/discrete/binary.py @@ -202,9 +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.best_pos[ - 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 d473334c..07004f6a 100644 --- a/pyswarms/single/general_optimizer.py +++ b/pyswarms/single/general_optimizer.py @@ -248,9 +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.best_pos[ - self.swarm.best_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 eec2bbbd..a89a2692 100644 --- a/pyswarms/single/local_best.py +++ b/pyswarms/single/local_best.py @@ -235,9 +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.best_pos[ - 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 87080893..bcb59b40 100644 --- a/tests/optimizers/test_binary.py +++ b/tests/optimizers/test_binary.py @@ -31,22 +31,7 @@ def optimizer_reset(self, options): return opt def test_binary_correct_pos(self, options): - dim = 10 - x = np.random.rand(dim, dim) - - def f_per_particle(m): - if np.count_nonzero(m) == 0: - return sum(x) - return sum(x[:, m == 1]).mean() - - def binary_eg(options): - def f(x): - n_particles = x.shape[0] - j = [f_per_particle(x[i]) for i in range(n_particles)] - return np.array(j) - - opt = BinaryPSO(n_particles=5, dimensions=dim, options=options) - return opt.optimize(f, iters=5) - - cost, pos = binary_eg(options) - assert f_per_particle(pos) == cost + """ 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) + assert sum(pos**2) == cost diff --git a/tests/optimizers/test_general_optimizer.py b/tests/optimizers/test_general_optimizer.py index 93249b24..1b4c1568 100644 --- a/tests/optimizers/test_general_optimizer.py +++ b/tests/optimizers/test_general_optimizer.py @@ -12,7 +12,6 @@ import pyswarms.backend.topology as t from pyswarms.single import GeneralOptimizerPSO from pyswarms.utils.functions.single_obj import sphere -from pyswarms.backend.topology import Pyramid from .abc_test_optimizer import ABCTestOptimizer @@ -97,13 +96,8 @@ def test_obj_incorrect_kwargs(self, obj_with_args, optimizer): # Wrong kwargs cost, pos = optimizer.optimize(obj_with_args, 1000, c=1, d=100) - def general_eg(self, options): - my_topology = Pyramid(static=False) - opt = GeneralOptimizerPSO( - n_particles=10, dimensions=2, options=options, topology=my_topology - ) - return opt.optimize(sphere, iters=5) - - def test_general_correct_pos(self, options): - cost, pos = self.general_eg(options) + 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) + print(cost, '---', pos) assert sum(pos ** 2) == cost diff --git a/tests/optimizers/test_global_best.py b/tests/optimizers/test_global_best.py index 59b2e486..401398b9 100644 --- a/tests/optimizers/test_global_best.py +++ b/tests/optimizers/test_global_best.py @@ -29,11 +29,8 @@ def optimizer_reset(self, options): opt.reset() return opt - def global_eg(self, options): - opt = GlobalBestPSO(n_particles=10, dimensions=2, options=options) - return opt.optimize(sphere, iters=5) - def test_global_correct_pos(self, options): - print("Running local test") - cost, pos = self.global_eg(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) assert sum(pos ** 2) == cost diff --git a/tests/optimizers/test_local_best.py b/tests/optimizers/test_local_best.py index a1f6a859..945ce3e6 100644 --- a/tests/optimizers/test_local_best.py +++ b/tests/optimizers/test_local_best.py @@ -29,11 +29,8 @@ def optimizer_reset(self, options): opt.reset() return opt - def local_eg(self, options): - opt = LocalBestPSO(n_particles=10, dimensions=2, options=options) - return opt.optimize(sphere, iters=5) - def test_local_correct_pos(self, options): - print("Running local test") - cost, pos = self.local_eg(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) assert sum(pos ** 2) == cost From ebf564d7272be22ea59689ccedd34236494b8cb6 Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 31 Mar 2019 14:00:22 +1100 Subject: [PATCH 5/7] Remove numpy import --- tests/optimizers/test_binary.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/optimizers/test_binary.py b/tests/optimizers/test_binary.py index bcb59b40..937f71a6 100644 --- a/tests/optimizers/test_binary.py +++ b/tests/optimizers/test_binary.py @@ -3,7 +3,6 @@ # Import modules import pytest -import numpy as np # Import from pyswarms from pyswarms.discrete import BinaryPSO From 487ec7d4312f9801917126d74f4aaad7012bc0e5 Mon Sep 17 00:00:00 2001 From: Jake Date: Thu, 25 Apr 2019 09:43:07 +1000 Subject: [PATCH 6/7] Change test to use optimizer history --- examples/basic_optimization.ipynb | 667 ++++++++++++++++++++- tests/optimizers/test_binary.py | 9 +- tests/optimizers/test_general_optimizer.py | 6 +- tests/optimizers/test_global_best.py | 6 +- tests/optimizers/test_local_best.py | 6 +- 5 files changed, 666 insertions(+), 28 deletions(-) diff --git a/examples/basic_optimization.ipynb b/examples/basic_optimization.ipynb index 1e395c21..46698191 100644 --- a/examples/basic_optimization.ipynb +++ b/examples/basic_optimization.ipynb @@ -28,8 +28,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Running on Python version: 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49) \n", - "[GCC 7.2.0]\n" + "Running on Python version: 3.7.2 (default, Dec 29 2018, 06:19:36) \n", + "[GCC 7.3.0]\n" ] } ], @@ -79,36 +79,36 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "INFO:pyswarms.single.global_best:Iteration 1/1000, cost: 0.11075768527574707\n", - "INFO:pyswarms.single.global_best:Iteration 101/1000, cost: 7.521863508083004e-08\n", - "INFO:pyswarms.single.global_best:Iteration 201/1000, cost: 2.8159915186067273e-11\n", - "INFO:pyswarms.single.global_best:Iteration 301/1000, cost: 8.794923638889175e-17\n", - "INFO:pyswarms.single.global_best:Iteration 401/1000, cost: 1.4699516547190895e-21\n", - "INFO:pyswarms.single.global_best:Iteration 501/1000, cost: 5.111264897313781e-23\n", - "INFO:pyswarms.single.global_best:Iteration 601/1000, cost: 8.329697430155943e-27\n", - "INFO:pyswarms.single.global_best:Iteration 701/1000, cost: 1.662161785541961e-30\n", - "INFO:pyswarms.single.global_best:Iteration 801/1000, cost: 6.140424420222279e-34\n", - "INFO:pyswarms.single.global_best:Iteration 901/1000, cost: 2.0523902169204634e-39\n", - "INFO:pyswarms.single.global_best:================================\n", - "Optimization finished!\n", - "Final cost: 0.0000\n", - "Best value: [-2.431421462417008e-22, -9.502018378214418e-23]\n", - "\n" + "2019-04-24 18:23:08,556 - pyswarms.single.global_best - INFO - Optimize for 10 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n", + "\n", + "pyswarms.single.global_best: 0%| |0/10\u001b[A\n", + "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", + "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", + "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", + "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", + "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", + "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0424\u001b[A\n", + "pyswarms.single.global_best: 0%| |0/10, best_cost=8.36e-06\u001b[A\n", + "pyswarms.single.global_best: 0%| |0/10, best_cost=8.36e-06\u001b[A\n", + "pyswarms.single.global_best: 80%|████████ |8/10, best_cost=8.36e-06\u001b[A\n", + "pyswarms.single.global_best: 80%|████████ |8/10, best_cost=8.36e-06\u001b[A\n", + "pyswarms.single.global_best: 80%|████████ |8/10, best_cost=8.36e-06\u001b[A\n", + "pyswarms.single.global_best: 100%|██████████|10/10, best_cost=8.36e-06\u001b[A2019-04-24 18:23:08,708 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 8.356164158276717e-06, best pos: [ 0.00249681 -0.00145674]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 144 ms, sys: 14.8 ms, total: 159 ms\n", - "Wall time: 151 ms\n" + "CPU times: user 52 ms, sys: 0 ns, total: 52 ms\n", + "Wall time: 157 ms\n" ] } ], @@ -121,9 +121,632 @@ "optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options)\n", "\n", "# Perform optimization\n", - "cost, pos = optimizer.optimize(fx.sphere, print_step=100, iters=1000, verbose=3)" + "cost, pos = optimizer.optimize(fx.sphere, iters=10)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "z=np.array(optimizer.cost_history)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8.356164158276717e-06" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "z[z.argmin()]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "ename": "AxisError", + "evalue": "axis 1 is out of bounds for array of dimension 1", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAxisError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msphere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pyswarms/utils/functions/single_obj.py\u001b[0m in \u001b[0;36msphere\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 715\u001b[0m \u001b[0mcomputed\u001b[0m \u001b[0mcost\u001b[0m \u001b[0mof\u001b[0m \u001b[0msize\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0mcode\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn_particles\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 716\u001b[0m \"\"\"\n\u001b[0;32m--> 717\u001b[0;31m \u001b[0mj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0;36m2.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 718\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 719\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/numpy/core/_methods.py\u001b[0m in \u001b[0;36m_sum\u001b[0;34m(a, axis, dtype, out, keepdims, initial)\u001b[0m\n\u001b[1;32m 34\u001b[0m def _sum(a, axis=None, dtype=None, out=None, keepdims=False,\n\u001b[1;32m 35\u001b[0m initial=_NoValue):\n\u001b[0;32m---> 36\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mumr_sum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 37\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 38\u001b[0m def _prod(a, axis=None, dtype=None, out=None, keepdims=False,\n", + "\u001b[0;31mAxisError\u001b[0m: axis 1 is out of bounds for array of dimension 1" + ] + } + ], + "source": [ + "fx.sphere(pos.T)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0.00249681, -0.00145674])" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pos.T" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([6.23406772e-06, 2.12209644e-06])" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(pos.reshape(-1, 1) ** 2).sum(axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([6.23406772e-06, 2.12209644e-06])" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fx.sphere(pos.reshape(-1, 1))" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0.00249681, -0.00145674])" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pos" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8.356164158276717e-06" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cost" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['ToHistory',\n", + " '__abstractmethods__',\n", + " '__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__slots__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__',\n", + " '_abc_impl',\n", + " '_populate_history',\n", + " 'bh',\n", + " 'bounds',\n", + " 'center',\n", + " 'cost_history',\n", + " 'dimensions',\n", + " 'ftol',\n", + " 'init_pos',\n", + " 'mean_neighbor_history',\n", + " 'mean_pbest_history',\n", + " 'n_particles',\n", + " 'name',\n", + " 'optimize',\n", + " 'options',\n", + " 'pos_history',\n", + " 'rep',\n", + " 'reset',\n", + " 'swarm',\n", + " 'swarm_size',\n", + " 'top',\n", + " 'velocity_clamp',\n", + " 'velocity_history',\n", + " 'vh']" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(optimizer)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[array([[0.90124935, 0.17491093],\n", + " [0.80474654, 0.92141545],\n", + " [0.82737053, 0.08275101],\n", + " [0.43055521, 0.16029267],\n", + " [0.7748099 , 0.83678948],\n", + " [0.21088674, 0.49822131],\n", + " [0.35918509, 0.7553067 ],\n", + " [0.10156838, 0.22410303],\n", + " [0.58793823, 0.30151857],\n", + " [0.17084816, 0.75461516]]), array([[1.43727288, 0.58528291],\n", + " [0.93435275, 1.42905162],\n", + " [1.13856742, 0.33461069],\n", + " [0.72586063, 0.32709616],\n", + " [1.5094666 , 1.18965563],\n", + " [0.90403286, 0.49314525],\n", + " [0.67649282, 1.00266235],\n", + " [0.5043016 , 0.67703065],\n", + " [0.75097075, 0.41294503],\n", + " [0.4881155 , 0.80689386]]), array([[1.65419275, 0.77490286],\n", + " [0.77212441, 1.42704143],\n", + " [0.98198008, 0.50241268],\n", + " [0.79360331, 0.46185756],\n", + " [1.7394455 , 1.39718321],\n", + " [1.34641821, 0.40948887],\n", + " [0.9158877 , 1.14369282],\n", + " [0.67208624, 0.96319035],\n", + " [0.70009302, 0.44887179],\n", + " [0.57512907, 0.69416884]]), array([[1.21344488, 0.61992065],\n", + " [0.45232105, 1.17197039],\n", + " [0.66917004, 0.41051598],\n", + " [0.68143011, 0.47378703],\n", + " [1.65180215, 1.39952274],\n", + " [1.41919708, 0.35495412],\n", + " [0.82031158, 1.13631049],\n", + " [0.63125209, 0.98728533],\n", + " [0.55975409, 0.38715874],\n", + " [0.53502928, 0.52391748]]), array([[0.5447976 , 0.46467561],\n", + " [0.33144311, 0.71443207],\n", + " [0.30345822, 0.31535249],\n", + " [0.49027688, 0.35467089],\n", + " [1.22966712, 0.99730451],\n", + " [0.7226531 , 0.29773023],\n", + " [0.39199028, 0.80877984],\n", + " [0.54763719, 0.59748084],\n", + " [0.3568341 , 0.24559238],\n", + " [0.43745753, 0.33405719]]), array([[-0.18582241, 0.30254416],\n", + " [ 0.18521129, 0.19873369],\n", + " [-0.02654187, 0.2203464 ],\n", + " [ 0.25351304, 0.17935622],\n", + " [ 0.47076599, 0.47001543],\n", + " [-0.18585261, 0.29629828],\n", + " [-0.05865941, 0.42130528],\n", + " [ 0.18595814, 0.13376584],\n", + " [ 0.17034079, 0.11556653],\n", + " [ 0.332588 , 0.15416082]]), array([[-0.75208666, 0.13358625],\n", + " [ 0.05030533, -0.27457037],\n", + " [-0.30552283, 0.12318309],\n", + " [ 0.03198256, 0.00709269],\n", + " [-0.23476459, -0.06529374],\n", + " [-0.9488298 , 0.25177129],\n", + " [-0.40068151, 0.07099387],\n", + " [-0.14094749, -0.28401897],\n", + " [ 0.00249681, -0.00145674],\n", + " [ 0.19438228, -0.01001684]]), array([[-0.94229611, 0.02982517],\n", + " [-0.06892539, -0.58493354],\n", + " [-0.38847327, 0.0277909 ],\n", + " [-0.17233487, -0.14907231],\n", + " [-0.81222803, -0.53376366],\n", + " [-1.26384299, 0.16073674],\n", + " [-0.69344037, -0.2511363 ],\n", + " [-0.34909747, -0.43797897],\n", + " [-0.14856277, -0.10677768],\n", + " [ 0.03806729, -0.15522386]]), array([[-0.72402378, 0.00299605],\n", + " [-0.10746193, -0.5584984 ],\n", + " [-0.2264908 , -0.0401891 ],\n", + " [-0.3201045 , -0.23810478],\n", + " [-1.25419726, -0.72882366],\n", + " [-0.9590918 , 0.09803825],\n", + " [-0.89113748, -0.47244531],\n", + " [-0.32266201, -0.55880999],\n", + " [-0.23650561, -0.14437043],\n", + " [-0.10854115, -0.25264529]]), array([[-0.34292605, 0.05533529],\n", + " [ 0.00479474, -0.22896048],\n", + " [ 0.0409274 , -0.00397033],\n", + " [-0.33712626, -0.20313865],\n", + " [-1.44693963, -0.52632436],\n", + " [-0.62908024, 0.05772904],\n", + " [-0.86707901, -0.30730053],\n", + " [-0.08983331, -0.5830162 ],\n", + " [-0.22397181, -0.12428935],\n", + " [-0.21618396, -0.31741636]])]" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "optimizer.pos_history" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0.06053830594849705,\n", + " 0.06053830594849705,\n", + " 0.06053830594849705,\n", + " 0.06053830594849705,\n", + " 0.06053830594849705,\n", + " 0.042371607406169756,\n", + " 8.356164158276717e-06,\n", + " 8.356164158276717e-06,\n", + " 8.356164158276717e-06,\n", + " 8.356164158276717e-06]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "optimizer.cost_history" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.argmin(fx.sphere(optimizer.pos_history[np.argmin(optimizer.cost_history)]))" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.argmin(optimizer.cost_history)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[-0.75208666, 0.13358625],\n", + " [ 0.05030533, -0.27457037],\n", + " [-0.30552283, 0.12318309],\n", + " [ 0.03198256, 0.00709269],\n", + " [-0.23476459, -0.06529374],\n", + " [-0.9488298 , 0.25177129],\n", + " [-0.40068151, 0.07099387],\n", + " [-0.14094749, -0.28401897],\n", + " [ 0.00249681, -0.00145674],\n", + " [ 0.19438228, -0.01001684]])" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "optimizer.pos_history[6]" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0.00249681, -0.00145674])" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "optimizer.pos_history[np.argmin(optimizer.cost_history)][np.argmin(fx.sphere(optimizer.pos_history[np.argmin(optimizer.cost_history)]))]" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0.00249681, -0.00145674])" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pos" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min_cost_idx = np.argmin(optimizer.cost_history)\n", + "min_cost_idx" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min_pos_idx = np.argmin(fx.sphere(optimizer.pos_history[min_cost_idx]))\n", + "min_pos_idx" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0.00249681, -0.00145674])" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "optimizer.pos_history[min_cost_idx][min_pos_idx]" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "optimizer.cost_history[min_cost_idx] == cost" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.array_equal(optimizer.pos_history[min_cost_idx][min_pos_idx], pos)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -498,7 +1121,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.0" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/tests/optimizers/test_binary.py b/tests/optimizers/test_binary.py index 937f71a6..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 @@ -30,7 +31,11 @@ def optimizer_reset(self, options): return opt def test_binary_correct_pos(self, options): - """ Test to check binary optimiser returns the correct position corresponding to the best cost """ + """ 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) - assert sum(pos**2) == cost + # 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 1b4c1568..20a4bef9 100644 --- a/tests/optimizers/test_general_optimizer.py +++ b/tests/optimizers/test_general_optimizer.py @@ -99,5 +99,7 @@ def test_obj_incorrect_kwargs(self, obj_with_args, optimizer): 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) - print(cost, '---', pos) - assert sum(pos ** 2) == cost + # 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 401398b9..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 @@ -33,4 +34,7 @@ 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) - assert sum(pos ** 2) == cost + # 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 945ce3e6..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 @@ -33,4 +34,7 @@ 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) - assert sum(pos ** 2) == cost + # 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) From 8aedc979787863e0b134c093b0d91f61f2ca868c Mon Sep 17 00:00:00 2001 From: Jake Date: Thu, 25 Apr 2019 09:47:51 +1000 Subject: [PATCH 7/7] Revert notebook changes --- examples/basic_optimization.ipynb | 667 +----------------------------- 1 file changed, 22 insertions(+), 645 deletions(-) diff --git a/examples/basic_optimization.ipynb b/examples/basic_optimization.ipynb index 46698191..1e395c21 100644 --- a/examples/basic_optimization.ipynb +++ b/examples/basic_optimization.ipynb @@ -28,8 +28,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Running on Python version: 3.7.2 (default, Dec 29 2018, 06:19:36) \n", - "[GCC 7.3.0]\n" + "Running on Python version: 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49) \n", + "[GCC 7.2.0]\n" ] } ], @@ -79,36 +79,36 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "2019-04-24 18:23:08,556 - pyswarms.single.global_best - INFO - Optimize for 10 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n", - "\n", - "pyswarms.single.global_best: 0%| |0/10\u001b[A\n", - "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", - "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", - "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", - "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", - "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0605\u001b[A\n", - "pyswarms.single.global_best: 0%| |0/10, best_cost=0.0424\u001b[A\n", - "pyswarms.single.global_best: 0%| |0/10, best_cost=8.36e-06\u001b[A\n", - "pyswarms.single.global_best: 0%| |0/10, best_cost=8.36e-06\u001b[A\n", - "pyswarms.single.global_best: 80%|████████ |8/10, best_cost=8.36e-06\u001b[A\n", - "pyswarms.single.global_best: 80%|████████ |8/10, best_cost=8.36e-06\u001b[A\n", - "pyswarms.single.global_best: 80%|████████ |8/10, best_cost=8.36e-06\u001b[A\n", - "pyswarms.single.global_best: 100%|██████████|10/10, best_cost=8.36e-06\u001b[A2019-04-24 18:23:08,708 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 8.356164158276717e-06, best pos: [ 0.00249681 -0.00145674]\n" + "INFO:pyswarms.single.global_best:Iteration 1/1000, cost: 0.11075768527574707\n", + "INFO:pyswarms.single.global_best:Iteration 101/1000, cost: 7.521863508083004e-08\n", + "INFO:pyswarms.single.global_best:Iteration 201/1000, cost: 2.8159915186067273e-11\n", + "INFO:pyswarms.single.global_best:Iteration 301/1000, cost: 8.794923638889175e-17\n", + "INFO:pyswarms.single.global_best:Iteration 401/1000, cost: 1.4699516547190895e-21\n", + "INFO:pyswarms.single.global_best:Iteration 501/1000, cost: 5.111264897313781e-23\n", + "INFO:pyswarms.single.global_best:Iteration 601/1000, cost: 8.329697430155943e-27\n", + "INFO:pyswarms.single.global_best:Iteration 701/1000, cost: 1.662161785541961e-30\n", + "INFO:pyswarms.single.global_best:Iteration 801/1000, cost: 6.140424420222279e-34\n", + "INFO:pyswarms.single.global_best:Iteration 901/1000, cost: 2.0523902169204634e-39\n", + "INFO:pyswarms.single.global_best:================================\n", + "Optimization finished!\n", + "Final cost: 0.0000\n", + "Best value: [-2.431421462417008e-22, -9.502018378214418e-23]\n", + "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 52 ms, sys: 0 ns, total: 52 ms\n", - "Wall time: 157 ms\n" + "CPU times: user 144 ms, sys: 14.8 ms, total: 159 ms\n", + "Wall time: 151 ms\n" ] } ], @@ -121,632 +121,9 @@ "optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options)\n", "\n", "# Perform optimization\n", - "cost, pos = optimizer.optimize(fx.sphere, iters=10)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], - "source": [ - "z=np.array(optimizer.cost_history)" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8.356164158276717e-06" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "z[z.argmin()]" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "ename": "AxisError", - "evalue": "axis 1 is out of bounds for array of dimension 1", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAxisError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msphere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pyswarms/utils/functions/single_obj.py\u001b[0m in \u001b[0;36msphere\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 715\u001b[0m \u001b[0mcomputed\u001b[0m \u001b[0mcost\u001b[0m \u001b[0mof\u001b[0m \u001b[0msize\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0mcode\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn_particles\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 716\u001b[0m \"\"\"\n\u001b[0;32m--> 717\u001b[0;31m \u001b[0mj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0;36m2.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 718\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 719\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/numpy/core/_methods.py\u001b[0m in \u001b[0;36m_sum\u001b[0;34m(a, axis, dtype, out, keepdims, initial)\u001b[0m\n\u001b[1;32m 34\u001b[0m def _sum(a, axis=None, dtype=None, out=None, keepdims=False,\n\u001b[1;32m 35\u001b[0m initial=_NoValue):\n\u001b[0;32m---> 36\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mumr_sum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 37\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 38\u001b[0m def _prod(a, axis=None, dtype=None, out=None, keepdims=False,\n", - "\u001b[0;31mAxisError\u001b[0m: axis 1 is out of bounds for array of dimension 1" - ] - } - ], - "source": [ - "fx.sphere(pos.T)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 0.00249681, -0.00145674])" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "pos.T" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([6.23406772e-06, 2.12209644e-06])" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "(pos.reshape(-1, 1) ** 2).sum(axis=1)" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([6.23406772e-06, 2.12209644e-06])" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fx.sphere(pos.reshape(-1, 1))" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 0.00249681, -0.00145674])" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "pos" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8.356164158276717e-06" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "cost" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['ToHistory',\n", - " '__abstractmethods__',\n", - " '__class__',\n", - " '__delattr__',\n", - " '__dict__',\n", - " '__dir__',\n", - " '__doc__',\n", - " '__eq__',\n", - " '__format__',\n", - " '__ge__',\n", - " '__getattribute__',\n", - " '__gt__',\n", - " '__hash__',\n", - " '__init__',\n", - " '__init_subclass__',\n", - " '__le__',\n", - " '__lt__',\n", - " '__module__',\n", - " '__ne__',\n", - " '__new__',\n", - " '__reduce__',\n", - " '__reduce_ex__',\n", - " '__repr__',\n", - " '__setattr__',\n", - " '__sizeof__',\n", - " '__slots__',\n", - " '__str__',\n", - " '__subclasshook__',\n", - " '__weakref__',\n", - " '_abc_impl',\n", - " '_populate_history',\n", - " 'bh',\n", - " 'bounds',\n", - " 'center',\n", - " 'cost_history',\n", - " 'dimensions',\n", - " 'ftol',\n", - " 'init_pos',\n", - " 'mean_neighbor_history',\n", - " 'mean_pbest_history',\n", - " 'n_particles',\n", - " 'name',\n", - " 'optimize',\n", - " 'options',\n", - " 'pos_history',\n", - " 'rep',\n", - " 'reset',\n", - " 'swarm',\n", - " 'swarm_size',\n", - " 'top',\n", - " 'velocity_clamp',\n", - " 'velocity_history',\n", - " 'vh']" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(optimizer)" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[array([[0.90124935, 0.17491093],\n", - " [0.80474654, 0.92141545],\n", - " [0.82737053, 0.08275101],\n", - " [0.43055521, 0.16029267],\n", - " [0.7748099 , 0.83678948],\n", - " [0.21088674, 0.49822131],\n", - " [0.35918509, 0.7553067 ],\n", - " [0.10156838, 0.22410303],\n", - " [0.58793823, 0.30151857],\n", - " [0.17084816, 0.75461516]]), array([[1.43727288, 0.58528291],\n", - " [0.93435275, 1.42905162],\n", - " [1.13856742, 0.33461069],\n", - " [0.72586063, 0.32709616],\n", - " [1.5094666 , 1.18965563],\n", - " [0.90403286, 0.49314525],\n", - " [0.67649282, 1.00266235],\n", - " [0.5043016 , 0.67703065],\n", - " [0.75097075, 0.41294503],\n", - " [0.4881155 , 0.80689386]]), array([[1.65419275, 0.77490286],\n", - " [0.77212441, 1.42704143],\n", - " [0.98198008, 0.50241268],\n", - " [0.79360331, 0.46185756],\n", - " [1.7394455 , 1.39718321],\n", - " [1.34641821, 0.40948887],\n", - " [0.9158877 , 1.14369282],\n", - " [0.67208624, 0.96319035],\n", - " [0.70009302, 0.44887179],\n", - " [0.57512907, 0.69416884]]), array([[1.21344488, 0.61992065],\n", - " [0.45232105, 1.17197039],\n", - " [0.66917004, 0.41051598],\n", - " [0.68143011, 0.47378703],\n", - " [1.65180215, 1.39952274],\n", - " [1.41919708, 0.35495412],\n", - " [0.82031158, 1.13631049],\n", - " [0.63125209, 0.98728533],\n", - " [0.55975409, 0.38715874],\n", - " [0.53502928, 0.52391748]]), array([[0.5447976 , 0.46467561],\n", - " [0.33144311, 0.71443207],\n", - " [0.30345822, 0.31535249],\n", - " [0.49027688, 0.35467089],\n", - " [1.22966712, 0.99730451],\n", - " [0.7226531 , 0.29773023],\n", - " [0.39199028, 0.80877984],\n", - " [0.54763719, 0.59748084],\n", - " [0.3568341 , 0.24559238],\n", - " [0.43745753, 0.33405719]]), array([[-0.18582241, 0.30254416],\n", - " [ 0.18521129, 0.19873369],\n", - " [-0.02654187, 0.2203464 ],\n", - " [ 0.25351304, 0.17935622],\n", - " [ 0.47076599, 0.47001543],\n", - " [-0.18585261, 0.29629828],\n", - " [-0.05865941, 0.42130528],\n", - " [ 0.18595814, 0.13376584],\n", - " [ 0.17034079, 0.11556653],\n", - " [ 0.332588 , 0.15416082]]), array([[-0.75208666, 0.13358625],\n", - " [ 0.05030533, -0.27457037],\n", - " [-0.30552283, 0.12318309],\n", - " [ 0.03198256, 0.00709269],\n", - " [-0.23476459, -0.06529374],\n", - " [-0.9488298 , 0.25177129],\n", - " [-0.40068151, 0.07099387],\n", - " [-0.14094749, -0.28401897],\n", - " [ 0.00249681, -0.00145674],\n", - " [ 0.19438228, -0.01001684]]), array([[-0.94229611, 0.02982517],\n", - " [-0.06892539, -0.58493354],\n", - " [-0.38847327, 0.0277909 ],\n", - " [-0.17233487, -0.14907231],\n", - " [-0.81222803, -0.53376366],\n", - " [-1.26384299, 0.16073674],\n", - " [-0.69344037, -0.2511363 ],\n", - " [-0.34909747, -0.43797897],\n", - " [-0.14856277, -0.10677768],\n", - " [ 0.03806729, -0.15522386]]), array([[-0.72402378, 0.00299605],\n", - " [-0.10746193, -0.5584984 ],\n", - " [-0.2264908 , -0.0401891 ],\n", - " [-0.3201045 , -0.23810478],\n", - " [-1.25419726, -0.72882366],\n", - " [-0.9590918 , 0.09803825],\n", - " [-0.89113748, -0.47244531],\n", - " [-0.32266201, -0.55880999],\n", - " [-0.23650561, -0.14437043],\n", - " [-0.10854115, -0.25264529]]), array([[-0.34292605, 0.05533529],\n", - " [ 0.00479474, -0.22896048],\n", - " [ 0.0409274 , -0.00397033],\n", - " [-0.33712626, -0.20313865],\n", - " [-1.44693963, -0.52632436],\n", - " [-0.62908024, 0.05772904],\n", - " [-0.86707901, -0.30730053],\n", - " [-0.08983331, -0.5830162 ],\n", - " [-0.22397181, -0.12428935],\n", - " [-0.21618396, -0.31741636]])]" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "optimizer.pos_history" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[0.06053830594849705,\n", - " 0.06053830594849705,\n", - " 0.06053830594849705,\n", - " 0.06053830594849705,\n", - " 0.06053830594849705,\n", - " 0.042371607406169756,\n", - " 8.356164158276717e-06,\n", - " 8.356164158276717e-06,\n", - " 8.356164158276717e-06,\n", - " 8.356164158276717e-06]" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "optimizer.cost_history" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.argmin(fx.sphere(optimizer.pos_history[np.argmin(optimizer.cost_history)]))" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.argmin(optimizer.cost_history)" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[-0.75208666, 0.13358625],\n", - " [ 0.05030533, -0.27457037],\n", - " [-0.30552283, 0.12318309],\n", - " [ 0.03198256, 0.00709269],\n", - " [-0.23476459, -0.06529374],\n", - " [-0.9488298 , 0.25177129],\n", - " [-0.40068151, 0.07099387],\n", - " [-0.14094749, -0.28401897],\n", - " [ 0.00249681, -0.00145674],\n", - " [ 0.19438228, -0.01001684]])" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "optimizer.pos_history[6]" - ] - }, - { - "cell_type": "code", - "execution_count": 72, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 0.00249681, -0.00145674])" - ] - }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "optimizer.pos_history[np.argmin(optimizer.cost_history)][np.argmin(fx.sphere(optimizer.pos_history[np.argmin(optimizer.cost_history)]))]" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 0.00249681, -0.00145674])" - ] - }, - "execution_count": 73, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "pos" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 74, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "min_cost_idx = np.argmin(optimizer.cost_history)\n", - "min_cost_idx" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 75, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "min_pos_idx = np.argmin(fx.sphere(optimizer.pos_history[min_cost_idx]))\n", - "min_pos_idx" - ] - }, - { - "cell_type": "code", - "execution_count": 76, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 0.00249681, -0.00145674])" - ] - }, - "execution_count": 76, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "optimizer.pos_history[min_cost_idx][min_pos_idx]" - ] - }, - { - "cell_type": "code", - "execution_count": 77, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 77, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "optimizer.cost_history[min_cost_idx] == cost" - ] - }, - { - "cell_type": "code", - "execution_count": 78, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 78, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.array_equal(optimizer.pos_history[min_cost_idx][min_pos_idx], pos)" + "cost, pos = optimizer.optimize(fx.sphere, print_step=100, iters=1000, verbose=3)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, @@ -1121,7 +498,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.6.0" } }, "nbformat": 4,