Skip to content

Commit

Permalink
Merge pull request #1972 from ERGO-Code/enhance-check-solution
Browse files Browse the repository at this point in the history
Enhance check solution
  • Loading branch information
jajhall authored Oct 16, 2024
2 parents 73c136f + 66aaa97 commit 3255be8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
47 changes: 44 additions & 3 deletions check/TestCheckSolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,17 @@ void runWriteReadCheckSolution(Highs& highs, const std::string model,
HighsStatus return_status;
std::string solution_file;
HighsModelStatus status = HighsModelStatus::kNotset;
if (dev_run) printf("\nSolving model %s from scratch\n", model.c_str());
run_status = highs.run();
REQUIRE(run_status == HighsStatus::kOk);

status = highs.getModelStatus();
REQUIRE(status == require_model_status);

solution_file = model + ".sol";
if (dev_run)
printf("Writing solution in style %d to %s\n", int(write_solution_style),
solution_file.c_str());
if (dev_run) return_status = highs.writeSolution("", write_solution_style);
return_status = highs.writeSolution(solution_file, write_solution_style);
REQUIRE(return_status == HighsStatus::kOk);
Expand All @@ -418,6 +422,7 @@ void runWriteReadCheckSolution(Highs& highs, const std::string model,

// primalDualInfeasible1Lp has no values in the solution file so,
// after it's read, HiGHS::solution.value_valid is false
if (dev_run) printf("Reading solution from %s\n", solution_file.c_str());
return_status = highs.readSolution(solution_file);
if (value_valid) {
REQUIRE(return_status == HighsStatus::kOk);
Expand All @@ -431,6 +436,13 @@ void runWriteReadCheckSolution(Highs& highs, const std::string model,
} else {
REQUIRE(return_status == HighsStatus::kError);
}
if (dev_run) printf("Solving model from solution read from file\n");
run_status = highs.run();
REQUIRE(run_status == HighsStatus::kOk);

status = highs.getModelStatus();
REQUIRE(status == require_model_status);

std::remove(solution_file.c_str());
}

Expand All @@ -440,7 +452,7 @@ void runSetLpSolution(const std::string model) {
std::string model_file =
std::string(HIGHS_DIR) + "/check/instances/" + model + ".mps";
const HighsInfo& info = highs.getInfo();
if (dev_run) printf("\nSolving from scratch\n");
if (dev_run) printf("\nSolving %s from scratch\n", model.c_str());
highs.setOptionValue("output_flag", dev_run);
if (dev_run) highs.setOptionValue("log_dev_level", 1);

Expand All @@ -460,8 +472,8 @@ void runSetLpSolution(const std::string model) {
REQUIRE(return_status == HighsStatus::kOk);

highs.run();
// Use a reduction in iteration count as a anity check that starting
// from the optimal solution has worked
// Use a reduction in iteration count as a sanity check that
// starting from the optimal solution has worked
HighsInt simplex_iteration_count1 = info.simplex_iteration_count;
if (dev_run)
printf(
Expand All @@ -470,4 +482,33 @@ void runSetLpSolution(const std::string model) {
model.c_str(), (int)simplex_iteration_count0,
(int)simplex_iteration_count1);
REQUIRE(simplex_iteration_count1 < simplex_iteration_count0);

// Now write a sparse solution, and read it in to hot start
HighsInt write_solution_style = kSolutionStyleSparse;
std::string solution_file = model + ".sol";
if (dev_run) printf("Writing sparse solution to %s\n", solution_file.c_str());
if (dev_run) return_status = highs.writeSolution("");
return_status = highs.writeSolution(solution_file, write_solution_style);
REQUIRE(return_status == HighsStatus::kOk);

highs.clear();
highs.setOptionValue("output_flag", dev_run);
if (dev_run) highs.setOptionValue("log_dev_level", 1);

highs.readModel(model_file);
if (dev_run)
printf("Reading sparse solution from %s\n", solution_file.c_str());
return_status = highs.readSolution(solution_file);
REQUIRE(return_status == HighsStatus::kOk);

if (dev_run) printf("Solving model from sparse solution read from file\n");
HighsStatus run_status = highs.run();
REQUIRE(run_status == HighsStatus::kOk);

HighsModelStatus status = highs.getModelStatus();
REQUIRE(status == HighsModelStatus::kOptimal);

highs.clear();

std::remove(solution_file.c_str());
}
1 change: 1 addition & 0 deletions src/lp_data/HighsLpUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,7 @@ HighsStatus readSolutionFile(const std::string filename,
if (style == kSolutionStyleSparse) assert(sparse);
if (sparse) {
num_col = -num_col;
assert(num_col <= lp_num_col);
} else {
if (num_col != lp_num_col) {
highsLogUser(log_options, HighsLogType::kError,
Expand Down

0 comments on commit 3255be8

Please sign in to comment.