Skip to content

Commit

Permalink
Use float-epsilon calculation in Sabre best scores (#9117)
Browse files Browse the repository at this point in the history
This should not meaningfully affect any small-scale outputs, but at
larger scales, there are floating-point instabilities that can make the
"best swaps" set miss swaps that are logically at the same score as
others.  This does not (cannot) fully stabilise us against all minor
floating-point rounding errors in calculations; the order of iteration
through the swaps can still technically change the swap set, for example
if three swaps with relative differences `{0, 0.8, 1.6} * BEST_EPSILON`
are encountered in a different orders.  The float epsilon is chosen to
be significantly larger than the actual magnitude of fluctuations, and
significantly smaller than the changes caused by all the actual
components of scoring, in order to mitigate this effect.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
jakelishman and mergify[bot] authored Nov 14, 2022
1 parent 712bf24 commit 9270868
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/sabre_swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ use neighbor_table::NeighborTable;
use sabre_dag::SabreDAG;
use swap_map::SwapMap;

const BEST_EPSILON: f64 = 1e-10; // Epsilon used in minimum-score calculations.

const EXTENDED_SET_SIZE: usize = 20; // Size of lookahead window.
const DECAY_RATE: f64 = 0.001; // Decay coefficient for penalizing serial swaps.
const DECAY_RESET_INTERVAL: u8 = 5; // How often to reset all decay rates to 1.
Expand Down Expand Up @@ -522,11 +524,11 @@ fn choose_best_swap(
+ EXTENDED_SET_WEIGHT * extended_set.score(swap, layout, dist))
}
};
if score < min_score {
if score < min_score - BEST_EPSILON {
min_score = score;
best_swaps.clear();
best_swaps.push(swap);
} else if score == min_score {
} else if (score - min_score).abs() < BEST_EPSILON {
best_swaps.push(swap);
}
}
Expand Down

0 comments on commit 9270868

Please sign in to comment.