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
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)
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)
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)
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)
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
def optimizer_history(self, request, options): opt = GeneralOptimizerPSO( n_particles=10, dimensions=2, options=options, topology=request.param, ) opt.optimize(sphere, 1000) return opt
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, )
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, )
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, )
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, )
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
def test_keyword_exception(options, topology): """Tests if exceptions are thrown when keywords are missing""" with pytest.raises(KeyError): GeneralOptimizerPSO(5, 2, options, topology)
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))
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())
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())
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))
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)