From c960ed19e640360787f7217fc8eb59256d50faa6 Mon Sep 17 00:00:00 2001 From: dance858 Date: Mon, 16 Nov 2026 14:58:20 -0800 Subject: [PATCH] better cancellation --- include/core/Numerics.h | 13 +++++++++++-- include/core/SparseAccumulator.h | 5 +++-- src/core/Problem.c | 10 ---------- src/core/SparseAccumulator.c | 3 ++- src/explorers/SimpleReductions.c | 4 ++-- src/explorers/dton/DtonsEq_apply.c | 12 +++++++----- src/explorers/dton/DtonsEq_cascade.c | 2 +- src/explorers/dton/DtonsEq_maps.c | 4 ++-- tests/test_dton.h | 2 +- 9 files changed, 29 insertions(+), 26 deletions(-) diff --git a/include/core/Numerics.h b/include/core/Numerics.h index f951ca9a..5a1da0a8 100644 --- a/include/core/Numerics.h +++ b/include/core/Numerics.h @@ -22,7 +22,6 @@ #include #define FEAS_TOL 1e-6 -#define ZERO_TOL 1e-10 #define CANCEL_TOL_REL 1e-12 /* relative tolerance under which an updated diagonal entry of P counts as zero (the 2x2 principal minors of an input P are only PSD up to rounding) */ @@ -64,12 +63,22 @@ /* a - b, set to zero when it is rounding noise relative to the two operands. A difference that small carries no information, and once stored nothing can tell it from a real value. */ -static inline double sub_snap(double a, double b) +static inline double sub_cancelling(double a, double b) { double d = a - b; return (ABS(d) <= CANCEL_TOL_REL * MAX(ABS(a), ABS(b))) ? 0.0 : d; } +/* a + b, set to zero when it is rounding noise relative to the two operands. + An exact cancellation leaves a residue whose sign is noise, and downstream + sign tests (e.g. the unboundedness check in process_colston_ineq) must never + see it. */ +static inline double add_cancelling(double a, double b) +{ + double s = a + b; + return (ABS(s) <= CANCEL_TOL_REL * MAX(ABS(a), ABS(b))) ? 0.0 : s; +} + /* Tolerance of an infeasibility verdict: FEAS_TOL near zero, relative to the larger magnitude beyond one. */ #define INFEAS_TOL(x, y) (FEAS_TOL * MAX(1.0, MAX(ABS(x), ABS(y)))) diff --git a/include/core/SparseAccumulator.h b/include/core/SparseAccumulator.h index a79db8e8..d8fc1224 100644 --- a/include/core/SparseAccumulator.h +++ b/include/core/SparseAccumulator.h @@ -23,8 +23,9 @@ #include /* Builds a sparse vector of length n from (column, value) contributions. - Contributions to the same column are summed, and each column also keeps - the OR of its flag bits. The touched columns are listed in the order they + Contributions to the same column are summed, with a sum that is rounding + noise relative to its terms set to zero, and each column also keeps the OR + of its flag bits. The touched columns are listed in the order they were first added. */ typedef struct SparseAccumulator { diff --git a/src/core/Problem.c b/src/core/Problem.c index c9ec7ed0..ef839d76 100644 --- a/src/core/Problem.c +++ b/src/core/Problem.c @@ -160,16 +160,6 @@ bool P_row_has_diagonal(const Matrix *P, const int *P_row_sizes, int k) return sorted_find(P->i + P->p[k].start, P_row_sizes[k], k) >= 0; } -/* c + upd, snapped to zero when the sum is rounding noise relative to its - terms. An exact cancellation leaves a residue whose sign is noise, and - downstream sign tests (e.g. the unboundedness check in process_colston_ineq) - must never see it. */ -static double add_cancelling(double c, double upd) -{ - double sum = c + upd; - return (ABS(sum) <= CANCEL_TOL_REL * MAX(ABS(c), ABS(upd))) ? 0.0 : sum; -} - void fix_var_in_obj(Problem *prob, int col, double value) { Objective *obj = prob->obj; diff --git a/src/core/SparseAccumulator.c b/src/core/SparseAccumulator.c index 6eb1ad4f..b4903546 100644 --- a/src/core/SparseAccumulator.c +++ b/src/core/SparseAccumulator.c @@ -17,6 +17,7 @@ */ #include "SparseAccumulator.h" +#include "Numerics.h" #include void sparse_accumulator_init(SparseAccumulator *acc, int *stamp, double *value, @@ -47,6 +48,6 @@ void sparse_accumulator_add(SparseAccumulator *acc, int col, double val, acc->flags[col] = 0; acc->touched[acc->n_touched++] = col; } - acc->value[col] += val; + acc->value[col] = add_cancelling(acc->value[col], val); acc->flags[col] |= flag; } diff --git a/src/explorers/SimpleReductions.c b/src/explorers/SimpleReductions.c index caf4c030..bcd77529 100644 --- a/src/explorers/SimpleReductions.c +++ b/src/explorers/SimpleReductions.c @@ -89,12 +89,12 @@ void delete_fixed_cols_from_problem(Problem *prob) if (!HAS_TAG(row_tags[row], R_TAG_LHS_INF)) { - lhs[row] = sub_snap(lhs[row], contribution); + lhs[row] = sub_cancelling(lhs[row], contribution); } if (!HAS_TAG(row_tags[row], R_TAG_RHS_INF)) { - rhs[row] = sub_snap(rhs[row], contribution); + rhs[row] = sub_cancelling(rhs[row], contribution); } // not necessary to update n_inf_min and n_in_max since a fixed diff --git a/src/explorers/dton/DtonsEq_apply.c b/src/explorers/dton/DtonsEq_apply.c index 85c31ecb..9266b2e7 100644 --- a/src/explorers/dton/DtonsEq_apply.c +++ b/src/explorers/dton/DtonsEq_apply.c @@ -140,11 +140,13 @@ static void dton_sweep_row_finish(Problem *prob, int row, int old_len, int new_l { if (!HAS_TAG(row_tags[row], R_TAG_LHS_INF)) { - constraints->lhs[row] = sub_snap(constraints->lhs[row], side_shift); + constraints->lhs[row] = + sub_cancelling(constraints->lhs[row], side_shift); } if (!HAS_TAG(row_tags[row], R_TAG_RHS_INF)) { - constraints->rhs[row] = sub_snap(constraints->rhs[row], side_shift); + constraints->rhs[row] = + sub_cancelling(constraints->rhs[row], side_shift); } } @@ -200,8 +202,8 @@ double dton_accumulate_row(DtonWorkspace *dton_work, const Matrix *A, int row) } /* Rewrites a row of A in place. Every entry of an eliminated column moves - onto the surviving column it is expressed in. A modified entry below - ZERO_TOL is dropped. The other entries are kept unchanged. */ + onto the surviving column it is expressed in. A modified entry that + cancelled to zero is dropped. The other entries are kept unchanged. */ static void dton_sweep_row(Problem *prob, DtonWorkspace *dton_work, int row, int *deferred, int *n_deferred) { @@ -223,7 +225,7 @@ static void dton_sweep_row(Problem *prob, DtonWorkspace *dton_work, int row, int col = acc->touched[ii]; double val = acc->value[col]; bool modified = acc->flags[col] == DTON_ACC_MODIFIED; - bool keep = !modified || ABS(val) > ZERO_TOL; + bool keep = !modified || val != 0.0; if (keep) { A->i[write_pos] = col; diff --git a/src/explorers/dton/DtonsEq_cascade.c b/src/explorers/dton/DtonsEq_cascade.c index c271d7aa..40b37b65 100644 --- a/src/explorers/dton/DtonsEq_cascade.c +++ b/src/explorers/dton/DtonsEq_cascade.c @@ -75,7 +75,7 @@ static int dton_cascade_claim(Problem *prob, DtonWorkspace *dton_work, int s) { int col = acc->touched[ii]; bool modified = acc->flags[col] == DTON_ACC_MODIFIED; - if (!modified || ABS(acc->value[col]) > ZERO_TOL) + if (!modified || acc->value[col] != 0.0) { if (n_kept < 2) { diff --git a/src/explorers/dton/DtonsEq_maps.c b/src/explorers/dton/DtonsEq_maps.c index a0c93a9a..b472fc7c 100644 --- a/src/explorers/dton/DtonsEq_maps.c +++ b/src/explorers/dton/DtonsEq_maps.c @@ -322,7 +322,7 @@ static PresolveStatus dton_transfer_bounds_rec(Constraints *constraints, if (!HAS_TAG(col_tag_k, C_TAG_LB_INF)) { - double bound = sub_snap(rec->rhs, aik * bound_k->lb) / aij; + double bound = sub_cancelling(rec->rhs, aik * bound_k->lb) / aij; status = same_sign ? update_ub_with_row(constraints, j, bound, i, cols, vals, 2 HUGE_BOUND_IS_NOT_OK) : update_lb_with_row(constraints, j, bound, i, cols, vals, @@ -332,7 +332,7 @@ static PresolveStatus dton_transfer_bounds_rec(Constraints *constraints, if (!HAS_TAG(col_tag_k, C_TAG_UB_INF)) { - double bound = sub_snap(rec->rhs, aik * bound_k->ub) / aij; + double bound = sub_cancelling(rec->rhs, aik * bound_k->ub) / aij; status = same_sign ? update_lb_with_row(constraints, j, bound, i, cols, vals, 2 HUGE_BOUND_IS_NOT_OK) : update_ub_with_row(constraints, j, bound, i, cols, vals, diff --git a/tests/test_dton.h b/tests/test_dton.h index f0a81ca7..3082a5f0 100644 --- a/tests/test_dton.h +++ b/tests/test_dton.h @@ -959,7 +959,7 @@ static char *test_dton_postsolve_cycle() 1e9); } -/* Exact cancellation of a substituted entry (dropped below ZERO_TOL). */ +/* Exact cancellation of a substituted entry (the entry is dropped). */ static char *test_dton_postsolve_cancellation() { // r0: x0 + 2 x1 = 0, r1: 0.5 x0 + x1 + x2 <= 5, r2: x1 + x3 + x4 <= 7