diff --git a/include/core/Numerics.h b/include/core/Numerics.h index f2afe1f6..f951ca9a 100644 --- a/include/core/Numerics.h +++ b/include/core/Numerics.h @@ -61,6 +61,15 @@ columns, which keeps only one of the two costs. */ #define IS_EQUAL_REL(x, y) (ABS((x) - (y)) <= CANCEL_TOL_REL * MAX(ABS(x), ABS(y))) +/* 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) +{ + double d = a - b; + return (ABS(d) <= CANCEL_TOL_REL * MAX(ABS(a), ABS(b))) ? 0.0 : d; +} + /* 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/src/explorers/SimpleReductions.c b/src/explorers/SimpleReductions.c index 0ff007bb..caf4c030 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] -= contribution; + lhs[row] = sub_snap(lhs[row], contribution); } if (!HAS_TAG(row_tags[row], R_TAG_RHS_INF)) { - rhs[row] -= contribution; + rhs[row] = sub_snap(rhs[row], contribution); } // not necessary to update n_inf_min and n_in_max since a fixed @@ -297,6 +297,22 @@ PresolveStatus remove_ston_rows(Problem *prob) return REDUCED; } +/* Scale of a row for a comparison of an activity with a side: the largest + magnitude among the side and the finite activities. */ +static inline double act_scale(const Activity *act, double side) +{ + double scale = ABS(side); + if (act->n_inf_min == 0) + { + scale = MAX(scale, ABS(act->min)); + } + if (act->n_inf_max == 0) + { + scale = MAX(scale, ABS(act->max)); + } + return scale; +} + // If the returned row tag is R_TAG_INFEAS, then the constraint is feasible. // If HAS_TAG(return_tag, R_TAG_RHS_INF), then the rhs is finite and redundant. // If HAS_TAG(return_tag, R_TAG_LHS_INF), then the lhs is finite and redundant. @@ -316,7 +332,8 @@ static inline RowTag check_activity(const Activity *act, double lhs, double rhs, { return R_TAG_INFEAS; } - else if (act->n_inf_max == 0 && act->max <= rhs + FEAS_TOL) + else if (act->n_inf_max == 0 && + act->max <= rhs + CANCEL_TOL_REL * act_scale(act, rhs)) { UPDATE_TAG(return_tag, R_TAG_RHS_INF); } @@ -332,7 +349,8 @@ static inline RowTag check_activity(const Activity *act, double lhs, double rhs, { return R_TAG_INFEAS; } - else if (act->n_inf_min == 0 && act->min >= lhs - FEAS_TOL) + else if (act->n_inf_min == 0 && + act->min >= lhs - CANCEL_TOL_REL * act_scale(act, lhs)) { UPDATE_TAG(return_tag, R_TAG_LHS_INF); } diff --git a/src/explorers/dton/DtonsEq_apply.c b/src/explorers/dton/DtonsEq_apply.c index 4153c8fd..85c31ecb 100644 --- a/src/explorers/dton/DtonsEq_apply.c +++ b/src/explorers/dton/DtonsEq_apply.c @@ -140,11 +140,11 @@ 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] -= side_shift; + constraints->lhs[row] = sub_snap(constraints->lhs[row], side_shift); } if (!HAS_TAG(row_tags[row], R_TAG_RHS_INF)) { - constraints->rhs[row] -= side_shift; + constraints->rhs[row] = sub_snap(constraints->rhs[row], side_shift); } } diff --git a/src/explorers/dton/DtonsEq_maps.c b/src/explorers/dton/DtonsEq_maps.c index efacce81..a0c93a9a 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 = (rec->rhs - aik * bound_k->lb) / aij; + double bound = sub_snap(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 = (rec->rhs - aik * bound_k->ub) / aij; + double bound = sub_snap(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_SimpleReductions.h b/tests/test_SimpleReductions.h index e154ba26..5cd32efb 100644 --- a/tests/test_SimpleReductions.h +++ b/tests/test_SimpleReductions.h @@ -538,6 +538,48 @@ static char *test_20_simple() return 0; } +/* A row whose terms are all tiny is not redundant just because its max + activity is within an absolute tolerance of its side: here the max activity + exceeds the rhs by 5e-7, which is the size of the row itself. + + min. -x1 + x2 + s.t. x1 - x2 <= 0 + 0 <= x1 <= 5e-7, 0 <= x2 <= 1e-7 +*/ +static char *test_21_simple() +{ + double Ax[] = {1, -1}; + int Ai[] = {0, 1}; + int Ap[] = {0, 2}; + int nnz = 2; + int n_rows = 1; + int n_cols = 2; + + double lhs[] = {-INF}; + double rhs[] = {0}; + double lbs[] = {0, 0}; + double ubs[] = {5e-7, 1e-7}; + double c[] = {-1, 1}; + + Settings *stgs = default_settings(); + Presolver *presolver = + new_presolver(Ax, Ai, Ap, n_rows, n_cols, nnz, lhs, rhs, lbs, ubs, c, stgs); + + Problem *prob = presolver->prob; + Constraints *constraints = prob->constraints; + PresolveStatus status = check_activities(prob); + mu_assert("error status", status != INFEASIBLE); + mu_assert("row kept", !HAS_TAG(constraints->row_tags[0], R_TAG_INACTIVE)); + mu_assert("rhs kept", !HAS_TAG(constraints->row_tags[0], R_TAG_RHS_INF) && + constraints->rhs[0] == 0.0); + problem_clean(prob); + + PS_FREE(stgs); + DEBUG(run_debugger(constraints, false)); + free_presolver(presolver); + return 0; +} + static const char *all_tests_simple() { mu_run_test(test_4_simple, counter_simple); @@ -550,6 +592,7 @@ static const char *all_tests_simple() mu_run_test(test_18_simple, counter_simple); mu_run_test(test_19_simple, counter_simple); mu_run_test(test_20_simple, counter_simple); + mu_run_test(test_21_simple, counter_simple); return 0; }