Skip to content

Commit a3fc200

Browse files
authored
Merge pull request #4737 from pavlomuts/highs_solution_hint
Pass solution hint to HiGHS from mathopt
2 parents 13e2a14 + fd1443e commit a3fc200

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

ortools/math_opt/solvers/highs_solver.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// Unimplemented features:
1515
// * Quadratic objective
1616
// * TODO(b/272767311): initial basis, more precise returned basis.
17-
// * Starting solution
1817
// * TODO(b/271104776): Returning rays
1918

2019
#include "ortools/math_opt/solvers/highs_solver.h"
@@ -925,6 +924,21 @@ absl::StatusOr<SolveResultProto> HighsSolver::Solve(
925924
return absl::OkStatus();
926925
};
927926

927+
if (model_parameters.solution_hints_size() > 0) {
928+
// Take the first solution hint and set the solution.
929+
const SolutionHintProto& hint = model_parameters.solution_hints(0);
930+
HighsInt num_entries = hint.variable_values().ids_size();
931+
std::vector<HighsInt> index(num_entries);
932+
std::vector<double> value(num_entries);
933+
size_t i = 0;
934+
for (const auto [id, val] : MakeView(hint.variable_values())) {
935+
index[i] = variable_data_.at(id).index;
936+
value[i] = val;
937+
++i;
938+
}
939+
RETURN_IF_ERROR(ToStatus(highs_->setSolution(num_entries, index.data(), value.data())));
940+
}
941+
928942
RETURN_IF_ERROR(ListInvertedBounds().ToStatus());
929943
// TODO(b/271595607): delete this code once we upgrade HiGHS, if HiGHS does
930944
// return a proper infeasibility status for models with empty integer bounds.

ortools/math_opt/solvers/highs_solver_test.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,20 @@ INSTANTIATE_TEST_SUITE_P(HighsLpModelSolveParametersTest,
162162
Values(LpModelSolveParametersTestParameters(
163163
SolverType::kHighs, /*exact_zeros=*/true,
164164
/*supports_duals=*/true,
165-
/*supports_primal_only_warm_starts=*/false)));
166-
167-
// MIP hint appears to be supported by Highs::setSolution, this is not yet
168-
// implemented.
169-
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MipSolutionHintTest);
165+
/*supports_primal_only_warm_starts=*/true)));
166+
167+
SolutionHintTestParams MakeHighsSolutionHintParams() {
168+
SolveParameters solve_params;
169+
solve_params.presolve = Emphasis::kOff;
170+
(*solve_params.highs.mutable_int_options())["mip_max_nodes"] = 0;
171+
std::string hint_message_regex =
172+
"Attempting to find feasible solution by "
173+
"solving MIP for user-supplied values of";
174+
return SolutionHintTestParams(SolverType::kHighs, solve_params, std::nullopt,
175+
hint_message_regex);
176+
}
177+
INSTANTIATE_TEST_SUITE_P(HighsSolutionHintTest, MipSolutionHintTest,
178+
Values(MakeHighsSolutionHintParams()));
170179

171180
// HiGHS does not support branching priority.
172181
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BranchPrioritiesTest);

0 commit comments

Comments
 (0)