Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions include/core/Numerics.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <math.h>

#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) */
Expand Down Expand Up @@ -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))))
Expand Down
5 changes: 3 additions & 2 deletions include/core/SparseAccumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
#include <stdint.h>

/* 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
{
Expand Down
10 changes: 0 additions & 10 deletions src/core/Problem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/core/SparseAccumulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "SparseAccumulator.h"
#include "Numerics.h"
#include <string.h>

void sparse_accumulator_init(SparseAccumulator *acc, int *stamp, double *value,
Expand Down Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/explorers/SimpleReductions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions src/explorers/dton/DtonsEq_apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/explorers/dton/DtonsEq_cascade.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/explorers/dton/DtonsEq_maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading