def test_simulated_annealing_threshold_restart(self): """ Tests that the simulated annealing heuristic restarts properly when using the threshold method for finding the next relevant parameter. """ np.random.seed(10) ranges = np.array([np.arange(20), np.arange(20)]) sa = SimulatedAnnealing( initial_temperature=50, neighbor_function=hop_to_next_value, cooldown_function=multiplicative_schedule, cooling_factor=3, restart=threshold_restart, probability_threshold=0.3, ) next_parameter = sa.choose_next_parameter(fake_history, ranges) expected_next_parameter = np.array([2, 6]) np.testing.assert_array_equal(next_parameter, expected_next_parameter)
def test_simulated_annealing_temp_stop(self): """ Tests that the simulated annealing heuristic stop properly when reaching a low temperature. """ np.random.seed(10) ranges = np.array([np.arange(20), np.arange(20)]) sa = SimulatedAnnealing( initial_temperature=0.1, neighbor_function=hop_to_next_value, cooldown_function=multiplicative_schedule, cooling_factor=10, restart=threshold_restart, probability_threshold=0.3, ) sa.choose_next_parameter(fake_history, ranges) # first computation next_parameter = sa.choose_next_parameter(fake_history, ranges) expected_next_parameter = np.array([4, 3]) np.testing.assert_array_equal(next_parameter, expected_next_parameter) self.assertTrue(sa.stop)
def test_reset(self): """ Tests that the "reset" method reset the attributes. """ np.random.seed(10) ranges = np.array([np.arange(20), np.arange(20)]) sa = SimulatedAnnealing( initial_temperature=50, neighbor_function=hop_to_next_value, cooldown_function=multiplicative_schedule, cooling_factor=3, restart=threshold_restart, probability_threshold=0.3, ) next_parameter = sa.choose_next_parameter(fake_history, ranges) expected_next_parameter = np.array([2, 6]) np.testing.assert_array_equal(next_parameter, expected_next_parameter) sa.reset() self.assertEqual(sa.current_t, 50) self.assertEqual(sa.nbr_iteration, 0) self.assertEqual(sa.restart, 0) self.assertListEqual(sa.energy, list())
def test_simulated_annealing_neg_temp(self): """ Tests that the simulated annealing raises an error when given a negative initial temperature. """ with self.assertRaises(TypeError): SimulatedAnnealing( initial_temperature=-1, neighbor_function=hop_to_next_value, cooldown_function=multiplicative_schedule, cooling_factor=2, restart=False, )
def test_simulated_annealing_wrong_temp(self): """ Tests that the simulated annealing class raises an error when the temperature is of the wrong type. """ with self.assertRaises(TypeError): SimulatedAnnealing( initial_temperature="tutu", neighbor_function=hop_to_next_value, cooldown_function=multiplicative_schedule, cooling_factor=2, restart=False, )