Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions alns/accept/SimulatedAnnealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ def __call__(self, rnd, best, current, candidate):

@classmethod
def autofit(
cls, init_obj: float, worse: float, accept_prob: float, num_iters: int
cls,
init_obj: float,
worse: float,
accept_prob: float,
num_iters: int,
method: str = "exponential",
) -> "SimulatedAnnealing":
"""
Returns an SA object with initial temperature such that there is a
Expand All @@ -126,12 +131,16 @@ def autofit(
worse than the initial solution.
num_iters
Number of iterations the ALNS algorithm will run.
method
The updating method, one of {'linear', 'exponential'}. Default
'exponential'.

Raises
------
ValueError
When ``worse`` not in [0, 1] or when ``accept_prob`` is not in
(0, 1).
When ``worse`` not in [0, 1], ``accept_prob`` is not in (0, 1),
``num_iters`` is not positive, or when ``method`` is not one of
{'linear', 'exponential'}.

Returns
-------
Expand All @@ -154,12 +163,16 @@ def autofit(
if not (0 < accept_prob < 1):
raise ValueError("accept_prob outside (0, 1) not understood.")

if num_iters < 0:
raise ValueError("Negative number of iterations not understood.")
if num_iters <= 0:
raise ValueError("number of iterations <= 0 not understood.")

start_temp = -worse * init_obj / np.log(accept_prob)
step = (1 / start_temp) ** (1 / num_iters)

if method == "exponential":
step = (1 / start_temp) ** (1 / num_iters)
else:
step = (start_temp - 1) / num_iters

logger.info(f"Autofit start_temp {start_temp:.2f}, step {step:.2f}.")

return cls(start_temp, 1, step, method="exponential")
return cls(start_temp, 1, step, method=method)
17 changes: 16 additions & 1 deletion alns/accept/tests/test_simulated_annealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def test_does_not_raise():
These sets of parameters should work correctly.
"""
SimulatedAnnealing(10, 5, 1, "exponential")
SimulatedAnnealing(10, 5, 1, "EXPONENTIAL")

SimulatedAnnealing(10, 5, 2, "linear")
SimulatedAnnealing(10, 5, 2, "LINEAR")


@mark.parametrize("step", range(10))
Expand Down Expand Up @@ -171,7 +174,8 @@ def random(self): # pylint: disable=no-self-use
(-1, 0.5, 10), # negative worse
(0, -1, 10), # negative prob
(1.5, 0.5, 10), # worse outside unit interval
(1, 0.9, -10),
(1, 0.9, -10), # negative iterations
(1, 0.9, 0), # zero iterations
],
) # negative number of iterations
def test_autofit_raises_for_invalid_inputs(
Expand Down Expand Up @@ -206,3 +210,14 @@ def test_autofit_on_several_examples(
assert_almost_equal(sa.end_temperature, sa_end)
assert_almost_equal(sa.step, sa_step)
assert_equal(sa.method, "exponential")


def test_linear_autofit():
sa = SimulatedAnnealing.autofit(100, 0.05, 0.5, 100, "linear")
sa_start = -0.05 * 100 / np.log(0.5)
sa_step = (sa_start - 1) / 100

assert_almost_equal(sa.start_temperature, sa_start)
assert_almost_equal(sa.end_temperature, 1)
assert_almost_equal(sa.step, sa_step)
assert_equal(sa.method, "linear")