Exemplo n.º 1
0
 def __optimize(self, cost_function: Callable) -> Tuple[float, np.array]:
     opt = GeneralOptimizerPSO(n_particles=self.vae.latent_dim,
                               dimensions=self.vae.latent_dim,
                               options=self.options,
                               topology=self.topology,
                               bounds=self.bounds)
     cost, z = opt.optimize(cost_function, iters=self.n_iter, fast=True)
     return cost, z
Exemplo n.º 2
0
 def optimizer_history(self, request, options):
     opt = GeneralOptimizerPSO(
         n_particles=10,
         dimensions=2,
         options=options,
         topology=request.param,
     )
     opt.optimize(sphere, 1000)
     return opt
Exemplo n.º 3
0
def test_ftol_effect(options, topology):
    """Test if setting the ftol breaks the optimization process accordingly"""
    pso = GeneralOptimizerPSO(10,
                              2,
                              options=options,
                              topology=topology,
                              ftol=1e-1)
    pso.optimize(sphere, 2000)
    assert np.array(pso.cost_history).shape != (2000, )
Exemplo n.º 4
0
def test_center_exception(err, center, options, topology):
    """Tests if exception is thrown when center is not a list or of different shape"""
    with pytest.raises(err):
        GeneralOptimizerPSO(5,
                            2,
                            center=center,
                            options=options,
                            topology=topology)
Exemplo n.º 5
0
def test_bound_type_exception(bounds, options, topology):
    """Tests if exception is raised when bound type is not a tuple"""
    with pytest.raises(TypeError):
        GeneralOptimizerPSO(5,
                            2,
                            options=options,
                            topology=topology,
                            bounds=bounds)
Exemplo n.º 6
0
def test_bounds_maxmin_exception(bounds, options, topology):
    """Tests if the max bounds is less than min bounds and vice-versa"""
    with pytest.raises(ValueError):
        GeneralOptimizerPSO(5,
                            2,
                            options=options,
                            topology=topology,
                            bounds=bounds)
Exemplo n.º 7
0
def test_bounds_size_exception(bounds, options, topology):
    """Tests if exceptions are raised when bound sizes are wrong"""
    with pytest.raises(IndexError):
        GeneralOptimizerPSO(5,
                            2,
                            options=options,
                            topology=topology,
                            bounds=bounds)
Exemplo n.º 8
0
def test_vclamp_maxmin_exception(velocity_clamp, options, topology):
    """Tests if the max velocity_clamp is less than min velocity_clamp and
    vice-versa"""
    with pytest.raises(ValueError):
        GeneralOptimizerPSO(
            5,
            2,
            velocity_clamp=velocity_clamp,
            options=options,
            topology=topology,
        )
Exemplo n.º 9
0
def test_vclamp_shape_exception(velocity_clamp, options, topology):
    """Tests if exception is raised when velocity_clamp's size is not equal
    to 2"""
    with pytest.raises(IndexError):
        GeneralOptimizerPSO(
            5,
            2,
            velocity_clamp=velocity_clamp,
            options=options,
            topology=topology,
        )
Exemplo n.º 10
0
 def optimizer(self, request, options):
     x_max = 10 * np.ones(2)
     x_min = -1 * x_max
     bounds = (x_min, x_max)
     return GeneralOptimizerPSO(
         n_particles=10,
         dimensions=2,
         options=options,
         bounds=bounds,
         topology=request.param,
     )
Exemplo n.º 11
0
def general_opt_reset(topology):
    """Returns a GeneralOptimizerPSO instance that has been run and reset to check
    default value"""
    pso = GeneralOptimizerPSO(10, 2, {"c1": 0.5, "c2": 0.7, "w": 0.5}, topology=topology)
    pso.optimize(sphere_func, 10, verbose=0)
    pso.reset()
    return pso
Exemplo n.º 12
0
def general_opt_history(topology):
    """Returns a GeneralOptimizerPSO instance run for 1000 iterations for checking
    history"""
    pso = GeneralOptimizerPSO(10, 2, {"c1": 0.5, "c2": 0.7, "w": 0.5}, topology=topology)
    pso.optimize(sphere_func, 1000, verbose=0)
    return pso
Exemplo n.º 13
0
def test_keyword_exception(options, topology):
    """Tests if exceptions are thrown when keywords are missing"""
    with pytest.raises(KeyError):
        GeneralOptimizerPSO(5, 2, options, topology)
Exemplo n.º 14
0
def test_invalid_k_value(options, static):
    """Tests if exception is thrown when passing
    an invalid value for k when using a Random topology"""
    with pytest.raises(ValueError):
        GeneralOptimizerPSO(5, 2, options, Random(static=static))
Exemplo n.º 15
0
def test_invalid_r_or_p_values(options):
    """Tests if exception is thrown when passing
    an invalid value for r or p when using a Von Neumann topology"""
    with pytest.raises(ValueError):
        GeneralOptimizerPSO(5, 2, options, VonNeumann())
Exemplo n.º 16
0
def test_keyword_exception_vonneumann(options, static):
    """Tests if exceptions are thrown when keywords are missing and a VonNeumann topology is chosen"""
    with pytest.raises(KeyError):
        GeneralOptimizerPSO(5, 2, options, VonNeumann())
Exemplo n.º 17
0
def test_keyword_exception_ring(options, static):
    """Tests if exceptions are thrown when keywords are missing and a Ring topology is chosen"""
    with pytest.raises(KeyError):
        GeneralOptimizerPSO(5, 2, options, Ring(static=static))
Exemplo n.º 18
0
def test_topology_type_exception(options, topology):
    """Tests if exceptions are thrown when the topology has the wrong type"""
    with pytest.raises(TypeError):
        GeneralOptimizerPSO(5, 2, options, topology)