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_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_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_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(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 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 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 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 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