diff --git a/include/core/Debugger.h b/include/core/Debugger.h index f9a35b80..330ee40d 100644 --- a/include/core/Debugger.h +++ b/include/core/Debugger.h @@ -130,6 +130,11 @@ void verify_row_tags(const struct Constraints *constraints); inactive columns when finished */ void verify_empty_when_finished(const struct Constraints *constraints); +/* An active, non-empty column whose bounds have met must be waiting in + State.equal_bound_cols, so that fix_cols_with_equal_bounds finds every column + that a sweep over all columns would find. */ +void verify_equal_bounds_queued(const struct Constraints *constraints); + /* True when the empty-column worklist holds only columns that remove_empty_cols keeps: columns in P whose row is not a positive diagonal alone. The explorers assert this on entry. */ diff --git a/include/core/State.h b/include/core/State.h index 95375b26..30813c3c 100644 --- a/include/core/State.h +++ b/include/core/State.h @@ -50,6 +50,7 @@ typedef struct State /* row sizes of P, SIZE_INACTIVE_COL for removed variables, NULL for an LP */ int *P_row_sizes; + iVec *equal_bound_cols; iVec *fixed_cols_to_delete; iVec *sub_cols_to_delete; iVec *rows_to_delete; diff --git a/include/core/pslp_namespace.h b/include/core/pslp_namespace.h index 2597a8f5..8cf844e6 100644 --- a/include/core/pslp_namespace.h +++ b/include/core/pslp_namespace.h @@ -92,6 +92,7 @@ #define fix_col pslp_fix_col #define fix_col_to_negative_inf pslp_fix_col_to_negative_inf #define fix_col_to_positive_inf pslp_fix_col_to_positive_inf +#define fix_cols_with_equal_bounds pslp_fix_cols_with_equal_bounds #define fix_var_in_obj pslp_fix_var_in_obj #define fold_apply pslp_fold_apply #define fold_check pslp_fold_check @@ -243,6 +244,7 @@ #define verify_empty_cols pslp_verify_empty_cols #define verify_empty_rows pslp_verify_empty_rows #define verify_empty_when_finished pslp_verify_empty_when_finished +#define verify_equal_bounds_queued pslp_verify_equal_bounds_queued #define verify_locks pslp_verify_locks #define verify_no_duplicates pslp_verify_no_duplicates #define verify_no_duplicates_lists pslp_verify_no_duplicates_lists diff --git a/include/explorers/SimpleReductions.h b/include/explorers/SimpleReductions.h index a21318e2..d6284c6b 100644 --- a/include/explorers/SimpleReductions.h +++ b/include/explorers/SimpleReductions.h @@ -111,9 +111,12 @@ PresolveStatus check_activities(struct Problem *prob); */ PresolveStatus remove_empty_cols(struct Problem *prob); +/* Fixes every column whose bounds are equal, or within FEAS_TOL when + is_fixing_negligible holds. Run once, on the input bounds. */ PresolveStatus remove_variables_with_close_bounds(struct Problem *prob); -// only used for debugging purposes -// void fix_cols_with_equal_bounds(struct Constraints *constraints); +/* Fixes the columns that update_lb and update_ub queued in + State.equal_bound_cols because their bounds met (lb >= ub or IS_EQUAL_REL). */ +void fix_cols_with_equal_bounds(struct Problem *prob); #endif // SIMPLEREDUCTIONS_H diff --git a/src/core/CoreTransformation.c b/src/core/CoreTransformation.c index 0d8f3fe6..abd14720 100644 --- a/src/core/CoreTransformation.c +++ b/src/core/CoreTransformation.c @@ -260,10 +260,12 @@ PresolveStatus update_lb_with_row(Constraints *constraints, int col, double new_ save_retrieval_bound_change_the_row(data->postsolve_info, row, row_cols, row_vals, row_len, 1); - // TODO: should we check for variable fixing here? And we could potentially - // get rid of remove_variables_with_close_bounds from trivial presolve - // and just run it once in the beginning? But we don't have access to ck - // here. + // check for fixed variable + if (!HAS_TAG(*col_tag, C_TAG_UB_INF) && + (new_lb >= ub || IS_EQUAL_REL(new_lb, ub))) + { + iVec_append(data->equal_bound_cols, col); + } } return UNCHANGED; @@ -321,9 +323,12 @@ PresolveStatus update_ub_with_row(Constraints *constraints, int col, double new_ save_retrieval_bound_change_the_row(data->postsolve_info, row, row_cols, row_vals, row_len, 1); - // TODO: should we check for variable fixing here? And we could potentially - // get rid of remove_variables_with_close_bounds from trivial presolve - // and just run it once in the beginning? + // check for fixed variable + if (!HAS_TAG(*col_tag, C_TAG_LB_INF) && + (new_ub <= lb || IS_EQUAL_REL(new_ub, lb))) + { + iVec_append(data->equal_bound_cols, col); + } } return UNCHANGED; diff --git a/src/core/Debugger.c b/src/core/Debugger.c index bbeae4ac..a525cb22 100644 --- a/src/core/Debugger.c +++ b/src/core/Debugger.c @@ -61,10 +61,36 @@ void run_debugger(const Constraints *constraints, bool finished) } } +void verify_equal_bounds_queued(const Constraints *constraints) +{ + const State *data = constraints->state; + + for (int k = 0; k < (int) constraints->n; ++k) + { + if (data->col_sizes[k] <= 0 || + HAS_TAG(constraints->col_tags[k], + (C_TAG_INACTIVE | C_TAG_LB_INF | C_TAG_UB_INF))) + { + continue; + } + + double lb = constraints->bounds[k].lb, ub = constraints->bounds[k].ub; + if (lb >= ub || IS_EQUAL_REL(lb, ub)) + { + PSLP_ASSERT(iVec_contains(data->equal_bound_cols, k)); + } + } +} + void run_debugger_problem(const Problem *prob, bool finished) { run_debugger(prob->constraints, finished); verify_P(prob, finished); + if (!finished) + { + verify_equal_bounds_queued(prob->constraints); + } + if (finished) { PSLP_ASSERT(empty_cols_drained(prob)); diff --git a/src/core/Presolver.c b/src/core/Presolver.c index 518abf97..fc9a14ae 100644 --- a/src/core/Presolver.c +++ b/src/core/Presolver.c @@ -474,7 +474,8 @@ Presolver *new_presolver_from_problem(const ProblemData *problem, presolver->stats->n_cones_original = n_cones; presolver->stats->n_cone_rows_original = n_rows - first_cone_row; presolver->reduced_prob = (ProblemData *) ps_calloc(1, sizeof(ProblemData)); - DEBUG(run_debugger_problem(presolver->prob, false)); + DEBUG(run_debugger(presolver->prob->constraints, false)); + DEBUG(verify_P(presolver->prob, false)); clock_gettime(CLOCK_MONOTONIC, &timer.end); presolver->stats->time_lp_fold = fold_time; @@ -582,10 +583,7 @@ static inline PresolveStatus run_trivial_explorers(Problem *prob, { PresolveStatus status = UNCHANGED; - // TODO: should we just run this once in the beginning of presolve? - // Very important question. - status = remove_variables_with_close_bounds(prob); - RETURN_IF_INFEASIBLE(status); + fix_cols_with_equal_bounds(prob); if (stgs->dual_fix) { @@ -896,7 +894,11 @@ PresolveStatus run_presolver(Presolver *presolver) print_start_message(stats); } - DEBUG(run_debugger_problem(prob, false)); + // initial check for variables with closed bounds + nnz_before_reduction = A->nnz; + status = remove_variables_with_close_bounds(prob); + stats->nnz_removed_trivial += nnz_before_reduction - A->nnz; + terminate = (status != UNCHANGED); // ------------------------------------------------------------------------ // Main loop for the presolver. The logic is organized into phases and diff --git a/src/core/State.c b/src/core/State.c index 0d2d4004..cb9770fa 100644 --- a/src/core/State.c +++ b/src/core/State.c @@ -34,6 +34,7 @@ #define MIN_INIT_EMPTY_COLS 50 #define INIT_EMPTY_COLS_FRACTION 0.001 #define MIN_INIT_FIXED_COLS 1000 +#define MIN_INIT_EQUAL_BOUND_COLS 100 #define INIT_FIXED_COLS_FRACTION 0.01 #define MIN_INIT_SUB_COLS 1000 #define INIT_SUB_COLS_FRACTION 0.01 @@ -83,14 +84,16 @@ State *new_state(int *row_sizes, int *col_sizes, int *P_row_sizes, Lock *col_loc data->empty_cols = iVec_new(MAX(MIN_INIT_EMPTY_COLS, len_empty_cols)); data->empty_rows = iVec_new(MAX(MIN_INIT_EMPTY_ROWS, len_empty_rows)); data->updated_activities = iVec_new(MAX(MIN_INIT_UPDATED_ACTS, len_updated_act)); + data->equal_bound_cols = iVec_new(MIN_INIT_EQUAL_BOUND_COLS); data->fixed_cols_to_delete = iVec_new(MAX(MIN_INIT_FIXED_COLS, len_fixed_cols)); data->sub_cols_to_delete = iVec_new(MAX(MIN_INIT_SUB_COLS, len_sub_cols)); data->rows_to_delete = iVec_new(MAX(MIN_INIT_ROWS_TO_DELETE, len_rows_delete)); data->postsolve_info = postsolve_info_new(n_rows, n_cols); if (!data->ston_rows || !data->ston_cols || !data->dton_rows || - !data->empty_cols || !data->empty_rows || !data->fixed_cols_to_delete || - !data->sub_cols_to_delete || !data->rows_to_delete || !data->postsolve_info) + !data->empty_cols || !data->empty_rows || !data->equal_bound_cols || + !data->fixed_cols_to_delete || !data->sub_cols_to_delete || + !data->rows_to_delete || !data->postsolve_info) { free_state(data); return NULL; @@ -165,6 +168,7 @@ void free_state(State *data) iVec_free(data->empty_cols); iVec_free(data->empty_rows); iVec_free(data->updated_activities); + iVec_free(data->equal_bound_cols); iVec_free(data->fixed_cols_to_delete); iVec_free(data->sub_cols_to_delete); iVec_free(data->rows_to_delete); diff --git a/src/explorers/SimpleReductions.c b/src/explorers/SimpleReductions.c index d3d94b9a..0ff007bb 100644 --- a/src/explorers/SimpleReductions.c +++ b/src/explorers/SimpleReductions.c @@ -750,6 +750,23 @@ void clean_small_coeff_A(Matrix *A, const Bound *bounds, const RowTag *row_tags, } } +/* The value a column with close bounds is fixed at: an integral bound if + there is one, else the midpoint. */ +static inline double value_between_bounds(Bound bound) +{ + if (IS_INTEGRAL(bound.lb)) + { + return bound.lb; + } + + if (IS_INTEGRAL(bound.ub)) + { + return bound.ub; + } + + return 0.5 * (bound.lb + bound.ub); +} + PresolveStatus remove_variables_with_close_bounds(Problem *prob) { Constraints *constraints = prob->constraints; @@ -785,18 +802,7 @@ PresolveStatus remove_variables_with_close_bounds(Problem *prob) (IS_EQUAL_REL(bounds[ii].lb, bounds[ii].ub) || is_fixing_negligible(constraints, (int) ii, width))) { - // fix at integral bound or at midpoint - double lb = bounds[ii].lb, ub = bounds[ii].ub; - double val = 0.5 * (lb + ub); - if (IS_INTEGRAL(lb)) - { - val = lb; - } - else if (IS_INTEGRAL(ub)) - { - val = ub; - } - fix_col(prob, (int) ii, val); + fix_col(prob, (int) ii, value_between_bounds(bounds[ii])); } } @@ -805,3 +811,34 @@ PresolveStatus remove_variables_with_close_bounds(Problem *prob) delete_inactive_cols_from_A_and_AT(constraints); return UNCHANGED; } + +void fix_cols_with_equal_bounds(Problem *prob) +{ + Constraints *constraints = prob->constraints; + const Bound *bounds = constraints->bounds; + const ColTag *col_tags = constraints->col_tags; + const int *col_sizes = constraints->state->col_sizes; + iVec *queue = constraints->state->equal_bound_cols; + + for (size_t ii = 0; ii < queue->len; ++ii) + { + int col = queue->data[ii]; + double lb = bounds[col].lb, ub = bounds[col].ub; + + // Since it was queued the column can have been fixed, substituted or + // emptied (an empty column is left to remove_empty_cols), or merged + // with a parallel column, which widens its bounds. + if (col_sizes[col] <= 0 || + HAS_TAG(col_tags[col], (C_TAG_INACTIVE | C_TAG_LB_INF | C_TAG_UB_INF)) || + !(lb >= ub || IS_EQUAL_REL(lb, ub))) + { + continue; + } + + fix_col(prob, col, value_between_bounds(bounds[col])); + } + + iVec_clear_no_resize(queue); + delete_fixed_cols_from_problem(prob); + delete_inactive_cols_from_A_and_AT(constraints); +} diff --git a/tests/test_pathological.h b/tests/test_pathological.h index a2cb6ca4..4fa7b036 100644 --- a/tests/test_pathological.h +++ b/tests/test_pathological.h @@ -250,6 +250,88 @@ static const char *test_unbounded_Zedongs_example() return 0; } +/* A feasible LP that used to be declared infeasible. With cap = 1 (row 0) and + v = 9e-7: + -v cap + x_k + y_k = 0 (k = 1, 2, 3), -(y_1 + y_2 + y_3) >= 0, + and x, y >= 0, so y = 0 and x_k = v. The doubleton rows give x_k in [0, v], + an interval below the feasibility tolerance. Fixing x_k anywhere but at v + leaves y_k > 0, and the three shifts add up to 2.7e-6 on the last row. */ +static const char *test_derived_narrow_intervals_are_not_fixed() +{ + double v = 9e-7; + double Ax[] = {1, -v, 1, 1, -v, 1, 1, -v, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1}; + int Ai[] = {0, 0, 1, 4, 0, 2, 5, 0, 3, 6, 4, 5, 6, 1, 2, 3, 0, 1, 2, 3}; + int Ap[] = {0, 1, 4, 7, 10, 13, 16, 20}; + int nnz = 20; + int n_rows = 7; + int n_cols = 7; + + double lhs[] = {1, 0, 0, 0, 0, -INF, -5}; + double rhs[] = {1, 0, 0, 0, INF, 10, INF}; + double lbs[] = {0, 0, 0, 0, 0, 0, 0}; + double ubs[] = {INF, INF, INF, INF, INF, INF, INF}; + double c[] = {0, 1, 1, 1, 0, 0, 0}; + + Settings *stgs = default_settings(); + set_settings_true(stgs); + Presolver *presolver = + new_presolver(Ax, Ai, Ap, n_rows, n_cols, nnz, lhs, rhs, lbs, ubs, c, stgs); + + PresolveStatus status = run_presolver(presolver); + mu_assert("derived narrow intervals: feasible LP declared infeasible", + status == REDUCED); + mu_assert("derived narrow intervals: reduced problem not empty", + presolver->reduced_prob->n == 0 && presolver->reduced_prob->m == 0); + + postsolve(presolver, NULL, NULL, NULL); + const double *x = presolver->sol->x; + mu_assert("derived narrow intervals: cap", x[0] == 1.0); + for (int k = 1; k <= 3; ++k) + { + mu_assert("derived narrow intervals: x_k = v", ABS(x[k] - v) <= 1e-15); + mu_assert("derived narrow intervals: y_k = 0", ABS(x[k + 3]) <= 1e-15); + } + + free_presolver(presolver); + free_settings(stgs); + return 0; +} + +/* The close-bound fixings that must survive: x0 has the input bounds + [0, 1e-8] and is fixed by the sweep over the input, and the singleton row + x1 >= 1 makes the bounds of x1 equal, which fixes it through the queue of + the bound updates. */ +static const char *test_close_input_bounds_and_equal_bounds_are_fixed() +{ + double Ax[] = {1, 1, 1, 1, 1, 1, -1}; + int Ai[] = {0, 2, 3, 1, 1, 2, 3}; + int Ap[] = {0, 3, 4, 7}; + int nnz = 7; + int n_rows = 3; + int n_cols = 4; + + double lhs[] = {-INF, 1, -INF}; + double rhs[] = {4, INF, 3}; + double lbs[] = {0, 0, 0, 0}; + double ubs[] = {1e-8, 1, 5, 5}; + double c[] = {1, 1, -1, -2}; + + // only the trivial explorers + Settings *stgs = default_settings(); + set_settings_false(stgs); + Presolver *presolver = + new_presolver(Ax, Ai, Ap, n_rows, n_cols, nnz, lhs, rhs, lbs, ubs, c, stgs); + + PresolveStatus status = run_presolver(presolver); + mu_assert("close bounds: status", status == REDUCED); + mu_assert("close bounds: x0 and x1 not removed", + presolver->reduced_prob->n == 2 && presolver->reduced_prob->m == 2); + + free_presolver(presolver); + free_settings(stgs); + return 0; +} + /* A feasible LP in which x2 lies in an interval of width 1e-8: -1e-8 x0 + 1e-8 x1 + x2 = 1, -x0 + x1 >= 0.25, 0 <= x <= 1, with x2 >= lb2. Fixing x2 inside the interval leaves a first row of the @@ -304,6 +386,9 @@ static const char *test_close_bounds_are_not_fixed() static const char *all_tests_pathological() { + mu_run_test(test_derived_narrow_intervals_are_not_fixed, counter_pathological); + mu_run_test(test_close_input_bounds_and_equal_bounds_are_fixed, + counter_pathological); mu_run_test(test_narrow_interval_is_not_fixed, counter_pathological); mu_run_test(test_close_bounds_are_not_fixed, counter_pathological); mu_run_test(test_zedongs_example, counter_pathological);