예제 #1
0
def lbest_reset():
    """Returns a LocalBestPSO instance that has been run and reset to check
    default value"""
    pso = LocalBestPSO(10, 2, {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'k': 2, 'p': 2})
    pso.optimize(sphere_func, 10, verbose=0)
    pso.reset()
    return pso
예제 #2
0
def lbest_reset():
    """Returns a LocalBestPSO instance that has been run and reset to check
    default value"""
    pso = LocalBestPSO(10, 2, {"c1": 0.5, "c2": 0.7, "w": 0.5, "k": 2, "p": 2})
    pso.optimize(sphere_func, 10, verbose=0)
    pso.reset()
    return pso
예제 #3
0
 def test_ftol_effect(self):
     """Check if setting ftol breaks the optimization process
     accordingly."""
     # Perform a simple optimization
     optimizer = LocalBestPSO(10, 2, options=self.options, ftol=1e-1)
     optimizer.optimize(sphere_func, 5000, verbose=0)
     cost_hist = optimizer.get_cost_history
     self.assertNotEqual(cost_hist.shape, (5000, ))
예제 #4
0
 def test_ftol_effect(self):
     """Check if setting ftol breaks the optimization process
     accordingly."""
     # Perform a simple optimization
     optimizer = LocalBestPSO(10,2, options=self.options, ftol=1e-1)
     optimizer.optimize(sphere_func, 5000, verbose=0)
     cost_hist = optimizer.get_cost_history
     self.assertNotEqual(cost_hist.shape, (5000, ))
예제 #5
0
    def test_reset_best_cost_inf(self):
        """Tests if best cost is set to infinity when reset() is called"""
        # Perform a simple optimization
        optimizer = LocalBestPSO(5, 2, options=self.options)
        optimizer.optimize(sphere_func, 100, verbose=0)

        optimizer.reset()
        self.assertEqual(optimizer.best_cost, np.inf)
예제 #6
0
    def test_reset_best_pos_none(self):
        """Tests if best pos is set to NoneType when reset() is called"""
        # Perform a simple optimization
        optimizer = LocalBestPSO(5,2, options=self.options)
        optimizer.optimize(sphere_func, 100, verbose=0)

        optimizer.reset()
        self.assertIsNone(optimizer.best_pos)
예제 #7
0
    def test_reset_best_cost_inf(self):
        """Tests if best cost is set to infinity when reset() is called"""
        # Perform a simple optimization
        optimizer = LocalBestPSO(5,2, options=self.options)
        optimizer.optimize(sphere_func, 100, verbose=0)

        optimizer.reset()
        self.assertEqual(optimizer.best_cost, np.inf)
예제 #8
0
    def test_reset_best_pos_none(self):
        """Tests if best pos is set to NoneType when reset() is called"""
        # Perform a simple optimization
        optimizer = LocalBestPSO(5, 2, options=self.options)
        optimizer.optimize(sphere_func, 100, verbose=0)

        optimizer.reset()
        self.assertIsNone(optimizer.best_pos)
예제 #9
0
 def test_reset(self):
     """Tests if the reset method resets the attributes required"""
     # Perform a simple optimization
     optimizer = LocalBestPSO(5,2, options=self.options)
     optimizer.optimize(sphere_func, 100, verbose=0)
     # Reset the attributes
     optimizer.reset()
     # Perform testing
     self.assertEqual(optimizer.best_cost, np.inf)
     self.assertIsNone(optimizer.best_pos)
예제 #10
0
def test_local_kwargs(func):
    """Tests if kwargs are passed properly to the objective function for when kwargs are present"""

    # setup optimizer
    options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9, 'k': 2, 'p': 2}

    x_max = 10 * np.ones(2)
    x_min = -1 * x_max
    bounds = (x_min, x_max)
    opt_ps = LocalBestPSO(n_particles=100,
                          dimensions=2,
                          options=options,
                          bounds=bounds)

    # run it
    cost, pos = opt_ps.optimize(func,
                                1000,
                                print_step=10,
                                verbose=3,
                                a=1,
                                b=100)

    assert np.isclose(cost, 0, rtol=1e-03)
    assert np.isclose(pos[0], 1.0, rtol=1e-03)
    assert np.isclose(pos[1], 1.0, rtol=1e-03)
 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)
예제 #12
0
def test_local_missed_kwargs(func):
    """Tests kwargs are passed the objective function for when kwargs do not exist"""

    # setup optimizer
    options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9, 'k': 2, 'p': 2}

    x_max = 10 * np.ones(2)
    x_min = -1 * x_max
    bounds = (x_min, x_max)
    opt_ps = LocalBestPSO(n_particles=100,
                          dimensions=2,
                          options=options,
                          bounds=bounds)

    # run it
    with pytest.raises(TypeError) as excinfo:
        cost, pos = opt_ps.optimize(func, 1000, print_step=10, verbose=3, a=1)
        assert 'missing 1 required positional argument' in str(excinfo.value)
def test_local_missed_kwargs(func):
    """Tests kwargs are passed the objective function for when kwargs do not exist"""

    # setup optimizer
    options = {"c1": 0.5, "c2": 0.3, "w": 0.9, "k": 2, "p": 2}

    x_max = 10 * np.ones(2)
    x_min = -1 * x_max
    bounds = (x_min, x_max)
    opt_ps = LocalBestPSO(n_particles=100,
                          dimensions=2,
                          options=options,
                          bounds=bounds)

    # run it
    with pytest.raises(TypeError) as excinfo:
        cost, pos = opt_ps.optimize(func, 1000, a=1)
        assert "missing 1 required positional argument" in str(excinfo.value)
def test_local_wrong_kwargs(func):
    """Tests kwargs are passed the objective function for when kwargs do not exist"""

    # setup optimizer
    options = {"c1": 0.5, "c2": 0.3, "w": 0.9, "k": 2, "p": 2}

    x_max = 10 * np.ones(2)
    x_min = -1 * x_max
    bounds = (x_min, x_max)
    opt_ps = LocalBestPSO(n_particles=100,
                          dimensions=2,
                          options=options,
                          bounds=bounds)

    # run it
    with pytest.raises(TypeError) as excinfo:
        cost, pos = opt_ps.optimize(func, 1000, print_step=10, c=1, d=100)
        assert "unexpected keyword" in str(excinfo.value)
def test_local_kwargs(func):
    """Tests if kwargs are passed properly to the objective function for when kwargs are present"""

    # setup optimizer
    options = {"c1": 0.5, "c2": 0.3, "w": 0.9, "k": 2, "p": 2}

    x_max = 10 * np.ones(2)
    x_min = -1 * x_max
    bounds = (x_min, x_max)
    opt_ps = LocalBestPSO(n_particles=100,
                          dimensions=2,
                          options=options,
                          bounds=bounds)

    # run it
    cost, pos = opt_ps.optimize(func, 1000, a=1, b=100)

    assert np.isclose(cost, 0, rtol=1e-03)
    assert np.isclose(pos[0], 1.0, rtol=1e-03)
    assert np.isclose(pos[1], 1.0, rtol=1e-03)
예제 #16
0
 def optimizer_reset(self, options):
     opt = LocalBestPSO(10, 2, options)
     opt.optimize(sphere, 10)
     opt.reset()
     return opt
예제 #17
0
 def optimizer_history(self, options):
     opt = LocalBestPSO(10, 2, options)
     opt.optimize(sphere, 1000)
     return opt
예제 #18
0
def test_ftol_effect(options):
    """Test if setting the ftol breaks the optimization process accodingly"""
    pso = LocalBestPSO(10, 2, options=options, ftol=1e-1)
    pso.optimize(sphere_func, 2000, verbose=0)
    assert np.array(pso.cost_history).shape != (2000,)
예제 #19
0
def lbest_history():
    """Returns a LocalBestPSO instance run for 1000 iterations for checking
    history"""
    pso = LocalBestPSO(10, 2, {"c1": 0.5, "c2": 0.7, "w": 0.5, "k": 2, "p": 2})
    pso.optimize(sphere_func, 1000, verbose=0)
    return pso
예제 #20
0
def lbest_history():
    """Returns a LocalBestPSO instance run for 1000 iterations for checking
    history"""
    pso = LocalBestPSO(10, 2, {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'k': 2, 'p': 2})
    pso.optimize(sphere_func, 1000, verbose=0)
    return pso