def test_reset_best_cost_inf(self): """Tests if best cost is set to infinity when reset() is called""" # Perform a simple optimization optimizer = BinaryPSO(5,2, options=self.options) optimizer.optimize(sphere_func, 100, verbose=0) optimizer.reset() self.assertEqual(optimizer.best_cost, np.inf)
def test_reset_best_pos_none(self): """Tests if best pos is set to NoneType when reset() is called""" # Perform a simple optimization optimizer = BinaryPSO(5,2, options=self.options) optimizer.optimize(sphere_func, 100, verbose=0) optimizer.reset() self.assertIsNone(optimizer.best_pos)
def test_binary_correct_pos(self, options): """Test to check binary optimiser returns the correct position corresponding to the best cost""" opt = BinaryPSO(10, 2, options=options) cost, pos = opt.optimize(sphere, 10) # 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)
def test_k_fail(self): """Tests if exception is thrown when feeding an invalid k.""" k_less_than_min = {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'k': -1, 'p': 2} k_more_than_max = {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'k': 6, 'p': 2} with self.assertRaises(ValueError): optimizer = BinaryPSO(5, 2, options=k_less_than_min) with self.assertRaises(ValueError): optimizer = BinaryPSO(5, 2, options=k_more_than_max)
def test_vclamp_minmax_fail(self): """Tests if exception is thrown when velocity_clamp's minmax is wrong""" velocity_clamp = (3, 2) with self.assertRaises(ValueError): optimizer = BinaryPSO(5, 2, velocity_clamp=velocity_clamp, options=self.options)
def test_vclamp_shape_fail(self): """Tests if exception is thrown when velocity_clamp is not equal to 2""" velocity_clamp = (1, 1, 1) with self.assertRaises(IndexError): optimizer = BinaryPSO(5, 2, velocity_clamp=velocity_clamp, options=self.options)
def test_vclamp_type_fail(self): """Tests if exception is thrown when velocity_clamp is not a tuple.""" velocity_clamp = [1, 3] with self.assertRaises(TypeError): optimizer = BinaryPSO(5, 2, velocity_clamp=velocity_clamp, options=self.options)
def test_keyword_check_fail(self): """Tests if exceptions are thrown when keywords are missing""" check_c1 = {'c2': 0.7, 'w': 0.5, 'k': 2, 'p': 2} check_c2 = {'c1': 0.5, 'w': 0.5, 'k': 2, 'p': 2} check_m = {'c1': 0.5, 'c2': 0.7, 'k': 2, 'p': 2} check_k = {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'p': 2} check_p = {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'k': 2} with self.assertRaises(KeyError): optimizer = BinaryPSO(5, 2, options=check_c1) with self.assertRaises(KeyError): optimizer = BinaryPSO(5, 2, options=check_c2) with self.assertRaises(KeyError): optimizer = BinaryPSO(5, 2, options=check_m) with self.assertRaises(KeyError): optimizer = BinaryPSO(5, 2, options=check_k) with self.assertRaises(KeyError): optimizer = BinaryPSO(5, 2, options=check_p)
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
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
def test_reset_best_cost_inf(self): """Tests if best cost is set to infinity when reset() is called""" # Perform a simple optimization optimizer = BinaryPSO(5, 2, options=self.options) optimizer.optimize(sphere_func, 100, verbose=0) optimizer.reset() self.assertEqual(optimizer.best_cost, np.inf)
def test_reset_best_pos_none(self): """Tests if best pos is set to NoneType when reset() is called""" # Perform a simple optimization optimizer = BinaryPSO(5, 2, options=self.options) optimizer.optimize(sphere_func, 100, verbose=0) optimizer.reset() self.assertIsNone(optimizer.best_pos)
def test_reset(self): """Tests if the reset method resets the attributes required""" # Perform a simple optimization optimizer = BinaryPSO(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)
def binary_history(): """Returns a BinaryPSO instance run for 1000 iterations for checking history""" pso = BinaryPSO(10, 2, {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'k': 2, 'p': 2}) pso.optimize(sphere_func, 1000, verbose=0) return pso
def setUp(self): """Set up test fixtures""" self.options = {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'k': 2, 'p': 2} self.optimizer = BinaryPSO(10, 2, options=self.options)
def test_p_fail(self): """Tests if exception is thrown when feeding an invalid p.""" p_fail = {'c1': 0.5, 'c2': 0.7, 'w': 0.5, 'k': 2, 'p': 5} with self.assertRaises(ValueError): optimizer = BinaryPSO(5, 2, options=p_fail)
def optimizer_reset(self, options): opt = BinaryPSO(10, 2, options=options) opt.optimize(sphere, 10) opt.reset() return opt
def optimizer_history(self, options): opt = BinaryPSO(10, 2, options=options) opt.optimize(sphere, 1000) return opt
def test_vclamp_maxmin_exception(velocity_clamp, options): """Tests if the max velocity_clamp is less than min velocity_clamp and vice-versa""" with pytest.raises(ValueError): BinaryPSO(5, 2, velocity_clamp=velocity_clamp, options=options)
def test_vclamp_shape_exception(velocity_clamp, options): """Tests if exception is raised when velocity_clamp's size is not equal to 2""" with pytest.raises(IndexError): BinaryPSO(5, 2, velocity_clamp=velocity_clamp, options=options)
def test_vclamp_type_exception(velocity_clamp, options): """Tests if exception is raised when velocity_clamp type is not a tuple""" with pytest.raises(TypeError): BinaryPSO(5, 2, velocity_clamp=velocity_clamp, options=options)
def test_invalid_k_or_p_values(options): """Tests if exception is thrown when passing an invalid value for k or p""" with pytest.raises(ValueError): BinaryPSO(5, 2, options)
def test_keyword_exception(options): """Tests if exceptions are thrown when keywords are missing""" with pytest.raises(KeyError): BinaryPSO(5, 2, options)
def binary_history(): """Returns a BinaryPSO instance run for 1000 iterations for checking history""" pso = BinaryPSO(10, 2, {"c1": 0.5, "c2": 0.7, "w": 0.5, "k": 2, "p": 2}) pso.optimize(sphere_func, 1000, verbose=0) return pso