|
22 | 22 | * @param temperature The current temperature. |
23 | 23 | * @return true if the new state is accepted, false otherwise. |
24 | 24 | */ |
25 | | -static inline bool simulate_annealing(double energy_new, double energy_old, double inv_temperature, rng_state_t *rng){ |
| 25 | +static inline bool simulate_annealing(double energy_new, double energy_old, double temperature, rng_state_t *rng){ |
| 26 | + const double accpt_prob = rng_uniformdouble(rng); |
26 | 27 | const double delta_energy = energy_new - energy_old; |
27 | 28 | if (delta_energy <= 0.0) return true; // Always accept improvements |
28 | | - const double p = exp(-delta_energy * inv_temperature); |
29 | | - return rng_uniformdouble(rng) < p; |
| 29 | + const double p = exp(-delta_energy / temperature); |
| 30 | + return accpt_prob < p; |
30 | 31 | } |
31 | 32 |
|
32 | 33 | /** |
@@ -97,8 +98,6 @@ Status ocn_single_erosion_event(FlowGrid *G, double gamma, double temperature){ |
97 | 98 | CartPair dims = G->dims; |
98 | 99 | linidx_t nverts = (linidx_t)dims.row * (linidx_t)dims.col; |
99 | 100 |
|
100 | | - double inv_temperature = 1.0 / temperature; |
101 | | - |
102 | 101 | double energy_old, energy_new; |
103 | 102 | energy_old = G->energy; |
104 | 103 |
|
@@ -172,7 +171,7 @@ Status ocn_single_erosion_event(FlowGrid *G, double gamma, double temperature){ |
172 | 171 | update_drained_area(G, -da_inc, a_down_old); // remove drainage from old path |
173 | 172 | update_drained_area(G, da_inc, a_down_new); // add drainage to new path |
174 | 173 | energy_new = ocn_compute_energy(G, gamma); // recompute energy from scratch |
175 | | - if (simulate_annealing(energy_new, energy_old, inv_temperature, &G->rng)){ |
| 174 | + if (simulate_annealing(energy_new, energy_old, temperature, &G->rng)){ |
176 | 175 | G->energy = energy_new; |
177 | 176 | return SUCCESS; |
178 | 177 | } |
|
0 commit comments