1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Transition policies for simulated annealing.
//!
//! This module provides functions to determine whether a proposed state
//! transition should be accepted during the annealing process.
use Rng;
/// The classic Metropolis-Hastings acceptance criterion for simulated annealing.
///
/// This function implements the standard acceptance probability function:
/// - If the new state has lower energy (delta < 0), accept it with probability 1
/// - If the new state has higher energy (delta >= 0), accept it with probability exp(-delta/temperature)
///
/// This allows the algorithm to occasionally accept worse solutions, helping it
/// escape local minima. As the temperature decreases, the probability of accepting
/// worse solutions also decreases, allowing the algorithm to converge.
///
/// # Mathematical Background
///
/// The acceptance probability is given by:
///
/// P(accept) = min(1, exp(-delta/T))
///
/// where:
/// - delta is the energy difference (new_energy - current_energy)
/// - T is the current temperature
///
/// # Parameters
///
/// * `delta`: The energy difference (new_energy - current_energy)
/// * `temperature`: The current temperature in the annealing process
/// * `rng`: A random number generator
///
/// # Returns
///
/// `true` if the transition should be accepted, `false` otherwise.
///
/// # Examples
///
/// ```
/// use frostfire::core::transition::accept;
/// use rand::thread_rng;
///
/// let mut rng = thread_rng();
///
/// // Always accept improvements
/// assert!(accept(-10.0, 1.0, &mut rng));
///
/// // May sometimes accept worse solutions, especially at high temperatures
/// let accept_count = (0..1000)
/// .filter(|_| accept(5.0, 10.0, &mut rng))
/// .count();
///
/// // At T=10.0 and delta=5.0, acceptance probability should be around 60%
/// assert!(accept_count > 500 && accept_count < 700);
/// ```