示例#1
0
def test_compute_position_return_values(swarm, bounds, static):
    """Test if compute_position() gives the expected shape and range"""
    topology = Pyramid(static=static)
    p = topology.compute_position(swarm, bounds)
    assert p.shape == swarm.velocity.shape
    if bounds is not None:
        assert (bounds[0] <= p).all() and (bounds[1] >= p).all()
示例#2
0
def test_compute_velocity_return_values(swarm, clamp, static):
    """Test if compute_velocity() gives the expected shape and range"""
    topology = Pyramid(static=static)
    v = topology.compute_velocity(swarm, clamp)
    assert v.shape == swarm.position.shape
    if clamp is not None:
        assert (clamp[0] <= v).all() and (clamp[1] >= v).all()
示例#3
0
def test_compute_gbest_return_values(swarm, static):
    """Test if compute_gbest() gives the expected return values"""
    topology = Pyramid(static=static)
    expected_cost = 1.0002528364353296
    expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05])
    pos, cost = topology.compute_gbest(swarm)
    assert cost == pytest.approx(expected_cost)
    assert pos == pytest.approx(expected_pos)
示例#4
0
    variables = np.array(variables)

    dist = 1 / (IF**CONVERT_FACTOR)
    dim = n * 3  # Dimension of X
    epsilon = 1

    max_bound = maxD / 2 * np.ones(dim)
    min_bound = -max_bound
    bounds = (min_bound, max_bound)

    lr = 0.5
    clamp = (-lr, lr)

    if (topoModel == "pyramid"):
        from pyswarms.backend.topology import Pyramid
        my_topology = Pyramid(static=False)
        optimizer = ps.single.GeneralOptimizerPSO(n_particles=swarm_size,
                                                  dimensions=dim,
                                                  options=options,
                                                  velocity_clamp=clamp,
                                                  topology=my_topology)
    elif (topoModel == "random"):
        from pyswarms.backend.topology import Random as PSOTopoRandom
        my_topology = PSOTopoRandom()
        options = {'c1': c1, 'c2': c2, 'w': w, 'k': k, 'p': p}
        optimizer = ps.single.GeneralOptimizerPSO(n_particles=swarm_size,
                                                  dimensions=dim,
                                                  options=options,
                                                  velocity_clamp=clamp,
                                                  topology=my_topology)
    elif (topoModel == "star"):
示例#5
0
def test_neighbor_idx(swarm, static):
    """Test if the neighbor_idx attribute is assigned"""
    topology = Pyramid(static=static)
    topology.compute_gbest(swarm)
    assert topology.neighbor_idx is not None
示例#6
0

@pytest.fixture(scope="module")
def binary_reset():
    """Returns a BinaryPSO instance that has been run and reset to check
    default value"""
    pso = BinaryPSO(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


@pytest.fixture
def options():
    """Default options dictionary for most PSO use-cases"""
    options_ = {"c1": 0.5, "c2": 0.7, "w": 0.5, "k": 2, "p": 2, "r": 1}
    return options_


@pytest.fixture(params=[
                Star(),
                Ring(static=False), Ring(static=True),
                Pyramid(static=False), Pyramid(static=True),
                Random(static=False), Random(static=True),
                VonNeumann()
                ])
def topology(request):
    """Parametrized topology parameter"""
    topology_ = request.param
    return topology_