How to use the pyswarms.single.GlobalBestPSO function in pyswarms

To help you get started, we’ve selected a few pyswarms examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ljvmiranda921 / pyswarms / tests / optimizers / test_global_best.py View on Github external
def optimizer_history(self, options):
        opt = GlobalBestPSO(10, 2, options=options)
        opt.optimize(sphere, 1000)
        return opt
github ljvmiranda921 / pyswarms / tests / optimizers / test_global_best.py View on Github external
def test_global_correct_pos(self, options):
        """ Test to check global optimiser returns the correct position corresponding to the best cost """
        opt = GlobalBestPSO(n_particles=10, dimensions=2, options=options)
        cost, pos = opt.optimize(sphere, iters=5)
        # find best pos from history
        min_cost_idx = np.argmin(opt.cost_history)
        min_pos_idx = np.argmin(sphere(opt.pos_history[min_cost_idx]))
        assert np.array_equal(opt.pos_history[min_cost_idx][min_pos_idx], pos)
github ljvmiranda921 / pyswarms / tests / optimizers / test_objective_func_with_kwargs.py View on Github external
def test_global_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 = GlobalBestPSO(
        n_particles=100, dimensions=2, options=options, bounds=bounds
    )

    # run it
    with pytest.raises(TypeError) as excinfo:
        cost, pos = opt_ps.optimize(func, 1000, c=1, d=100)
        assert "unexpected keyword" in str(excinfo.value)
github ljvmiranda921 / pyswarms / tests / optimizers / test_objective_func_with_kwargs.py View on Github external
def test_global_no_kwargs(func):
    """Tests if args are passed properly to the objective function for when no args 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 = GlobalBestPSO(
        n_particles=100, dimensions=2, options=options, bounds=bounds
    )

    # run it
    cost, pos = opt_ps.optimize(func, 1000)

    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)
github ljvmiranda921 / pyswarms / tests / optimizers / test_objective_func_with_kwargs.py View on Github external
def test_global_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 = GlobalBestPSO(
        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)
github ljvmiranda921 / pyswarms / tests / optimizers / test_global_best.py View on Github external
def optimizer_reset(self, options):
        opt = GlobalBestPSO(10, 2, options=options)
        opt.optimize(sphere, 10)
        opt.reset()
        return opt
github ljvmiranda921 / pyswarms / tests / optimizers / test_objective_func_with_kwargs.py View on Github external
def test_global_uneeded_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 = GlobalBestPSO(
        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 "unexpected keyword" in str(excinfo.value)
github ksmet1977 / luxpy / luxpy / math / particleswarm.py View on Github external
| keys():
            |   - 'x_final': final solution x
            |   - 'cost': final function value of obj_fcn()
            |   - and some of the input arguments characterizing the 
            |       minimization, such as n_particles, bounds, ftol, options, optimizer.

    Reference:
        1. pyswarms documentation: https://pyswarms.readthedocs.io/
    """
         
    if (bounds[0] is None) & (bounds[1] is None):
        use_bnds = False
    if use_bnds == True:
        kwargs['bounds'] = bounds
        
    optimizer = ps.single.GlobalBestPSO(n_particles=n_particles, 
                                        dimensions=dimensions, 
                                        options=options,
                                        ftol = ftol,
                                        **kwargs)

    cost, pos = optimizer.optimize(objfcn, iters=1000, **args)
    
    if verbosity > 0:
        # Plot cost history:
        plot_cost_history(cost_history=optimizer.cost_history)
    
    # create output dictionary:
    res = {'x_final': pos, 'cost' : cost,
           'iters': iters, 'n_particles' : n_particles,
           'bounds': bounds, 'ftol':ftol,
           'options': options, 'optimizer' : optimizer}