|
| 1 | +#include <numeric> |
| 2 | + |
| 3 | +#include "HCheckConfig.h" |
| 4 | +#include "Highs.h" |
| 5 | +#include "catch.hpp" |
| 6 | + |
| 7 | +const bool dev_run = false; |
| 8 | + |
| 9 | +void presolveOffOn(const std::string& message, const HighsLp& lp, Highs& h, |
| 10 | + const HighsInt require_presolved_model_num_col = 0, |
| 11 | + const HighsInt require_presolved_model_num_row = 0, |
| 12 | + const HighsInt require_presolved_model_num_nz = 0); |
| 13 | + |
| 14 | +TEST_CASE("test-col-stuffing", "[highs_test_presolve_rules]") { |
| 15 | + HighsLp lp; |
| 16 | + |
| 17 | + Highs h; |
| 18 | + h.setOptionValue("output_flag", dev_run); |
| 19 | + h.setOptionValue("presolve_rule_test", kPresolveRuleColStuffing); |
| 20 | + const bool lp0 = true; |
| 21 | + const bool lp1 = true; |
| 22 | + const bool lp1a = true; |
| 23 | + const bool lp1b = true; |
| 24 | + |
| 25 | + if (lp0) { |
| 26 | + lp.num_col_ = 3; |
| 27 | + lp.num_row_ = 1; |
| 28 | + lp.sense_ = ObjSense::kMaximize; |
| 29 | + lp.col_cost_ = {1.8, 0.9, 1}; |
| 30 | + lp.col_lower_.assign(lp.num_col_, 0); |
| 31 | + lp.col_upper_.assign(lp.num_col_, 1); |
| 32 | + lp.row_lower_ = {-kHighsInf}; |
| 33 | + lp.row_upper_ = {4}; |
| 34 | + lp.a_matrix_.format_ = MatrixFormat::kRowwise; |
| 35 | + lp.a_matrix_.start_ = {0, lp.num_col_}; |
| 36 | + lp.a_matrix_.index_.resize(lp.num_col_); |
| 37 | + std::iota(lp.a_matrix_.index_.begin(), lp.a_matrix_.index_.end(), 0); |
| 38 | + lp.a_matrix_.value_ = {3, 2, 2}; |
| 39 | + |
| 40 | + for (int k = 0; k < 2; k++) { |
| 41 | + if (dev_run) printf("\n3-variable knapsack: %s\n", k == 0 ? "LP" : "IP"); |
| 42 | + REQUIRE(h.passModel(lp) == HighsStatus::kOk); |
| 43 | + h.setOptionValue("presolve_rule_test", kPresolveRuleColStuffing); |
| 44 | + h.run(); |
| 45 | + if (dev_run) h.writeSolution("", 1); |
| 46 | + lp.integrality_.assign(lp.num_col_, HighsVarType::kInteger); |
| 47 | + } |
| 48 | + lp.clear(); |
| 49 | + } |
| 50 | + |
| 51 | + lp.num_col_ = 2; |
| 52 | + lp.num_row_ = 1; |
| 53 | + lp.sense_ = ObjSense::kMinimize; |
| 54 | + lp.col_lower_.assign(lp.num_col_, 0); |
| 55 | + lp.col_upper_.assign(lp.num_col_, 1); |
| 56 | + lp.row_lower_ = {2.0}; |
| 57 | + lp.row_upper_ = {kHighsInf}; |
| 58 | + lp.a_matrix_.format_ = MatrixFormat::kRowwise; |
| 59 | + lp.a_matrix_.start_ = {0, lp.num_col_}; |
| 60 | + lp.a_matrix_.index_.resize(lp.num_col_); |
| 61 | + std::iota(lp.a_matrix_.index_.begin(), lp.a_matrix_.index_.end(), 0); |
| 62 | + if (lp1) { |
| 63 | + lp.col_cost_.assign(lp.num_col_, 1); |
| 64 | + lp.a_matrix_.value_.assign(lp.num_col_, 1); |
| 65 | + presolveOffOn("Capturing neos-787933 issue", lp, h); |
| 66 | + } |
| 67 | + if (lp1a) { |
| 68 | + lp.col_cost_ = {2, 1}; |
| 69 | + lp.a_matrix_.value_.assign(lp.num_col_, 1); |
| 70 | + presolveOffOn("Variant A neos-787933 issue", lp, h); |
| 71 | + } |
| 72 | + if (lp1b) { |
| 73 | + lp.col_cost_ = {-2, -1}; |
| 74 | + lp.a_matrix_.value_.assign(lp.num_col_, 1); |
| 75 | + presolveOffOn("Variant B neos-787933 issue", lp, h); |
| 76 | + } |
| 77 | + lp.clear(); |
| 78 | + |
| 79 | + h.resetGlobalScheduler(true); |
| 80 | +} |
| 81 | + |
| 82 | +void presolveOffOn(const std::string& message, const HighsLp& lp, Highs& h, |
| 83 | + const HighsInt require_presolved_model_num_col, |
| 84 | + const HighsInt require_presolved_model_num_row, |
| 85 | + const HighsInt require_presolved_model_num_nz) { |
| 86 | + const HighsRunData& run_data = h.getRunData(); |
| 87 | + bool presolve_on = false; |
| 88 | + // If the model reduces to empty, then the output from different |
| 89 | + // solvers cannot be tested |
| 90 | + const bool reduce_to_empty = require_presolved_model_num_col == 0 && |
| 91 | + require_presolved_model_num_row == 0; |
| 92 | + const HighsInt to_k = reduce_to_empty ? 2 : 5; |
| 93 | + for (int k = 0; k < to_k; k++) { |
| 94 | + std::string solver = kSimplexString; |
| 95 | + std::string run_crossover = kHighsOnString; |
| 96 | + bool basis_postsolve = true; |
| 97 | + if (k == 0) { |
| 98 | + // Presolve off - to get the optimal solution to debug |
| 99 | + // presolve |
| 100 | + presolve_on = false; |
| 101 | + } else { |
| 102 | + presolve_on = true; |
| 103 | + if (k == 1) { |
| 104 | + solver = kSimplexString; |
| 105 | + } else if (k == 2) { |
| 106 | + solver = kIpmString; |
| 107 | + } else if (k == 3) { |
| 108 | + solver = kIpmString; |
| 109 | + run_crossover = kHighsOffString; |
| 110 | + basis_postsolve = false; |
| 111 | + } else { |
| 112 | + solver = kHiPdlpString; |
| 113 | + basis_postsolve = false; |
| 114 | + } |
| 115 | + } |
| 116 | + std::string presolve = presolve_on ? kHighsOnString : kHighsOffString; |
| 117 | + h.setOptionValue(kPresolveString, presolve); |
| 118 | + h.setOptionValue(kRunCrossoverString, run_crossover); |
| 119 | + h.setOptionValue(kSolverString, solver); |
| 120 | + if (dev_run) |
| 121 | + printf( |
| 122 | + "\n============\n%s: presolve = %s; solver = %s%s\n============\n\n", |
| 123 | + message.c_str(), presolve.c_str(), solver.c_str(), |
| 124 | + solver == kIpmString ? ("; run_crossover = " + run_crossover).c_str() |
| 125 | + : ""); |
| 126 | + REQUIRE(h.passModel(lp) == HighsStatus::kOk); |
| 127 | + h.run(); |
| 128 | + if (dev_run) h.writeSolution("", 1); |
| 129 | + if (presolve_on) { |
| 130 | + // Ensure that the model is reduced to empty |
| 131 | + REQUIRE(run_data.presolved_model_num_col == |
| 132 | + require_presolved_model_num_col); |
| 133 | + REQUIRE(run_data.presolved_model_num_row == |
| 134 | + require_presolved_model_num_row); |
| 135 | + REQUIRE(run_data.presolved_model_num_nz == |
| 136 | + require_presolved_model_num_nz); |
| 137 | + // Ensure that dual postsolve is correct |
| 138 | + REQUIRE(h.getModelStatus() == HighsModelStatus::kOptimal); |
| 139 | + REQUIRE(h.getInfo().num_primal_infeasibilities == 0); |
| 140 | + REQUIRE(h.getInfo().num_dual_infeasibilities == 0); |
| 141 | + REQUIRE(h.getInfo().simplex_iteration_count == 0); |
| 142 | + // Ensure that any basis postsolve is correct |
| 143 | + if (basis_postsolve) |
| 144 | + REQUIRE(run_data.num_simplex_iterations_after_postsolve == 0); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments