From 0ab3f0fabd4428f26060cbedbbb7db5880bf366b Mon Sep 17 00:00:00 2001 From: Pavlo Muts Date: Tue, 22 Jul 2025 10:29:52 +0200 Subject: [PATCH] setSolution() from Highs accepts solution hint --- ortools/math_opt/solvers/highs_solver.cc | 16 ++++++++++++++++ ortools/math_opt/solvers/highs_solver_test.cc | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ortools/math_opt/solvers/highs_solver.cc b/ortools/math_opt/solvers/highs_solver.cc index 25f66f1e7f..5d5bccf7e1 100644 --- a/ortools/math_opt/solvers/highs_solver.cc +++ b/ortools/math_opt/solvers/highs_solver.cc @@ -925,6 +925,22 @@ absl::StatusOr HighsSolver::Solve( return absl::OkStatus(); }; + if (model_parameters.solution_hints_size() > 0) { + // Take the first solution hint and set the solution. + const SolutionHintProto& hint = model_parameters.solution_hints(0); + const int num_vars = highs_->getModel().lp_.num_col_; + // Highs accepts only full solutions, on partial solutions it will + // return an error. + if (hint.variable_values().ids_size() == num_vars) { + HighsSolution sol = HighsSolution(); + sol.col_value.resize(num_vars); + for (const auto [id, val] : MakeView(hint.variable_values())) { + sol.col_value[variable_data_.at(id).index] = val; + } + RETURN_IF_ERROR(ToStatus(highs_->setSolution(sol))); + } + } + RETURN_IF_ERROR(ListInvertedBounds().ToStatus()); // TODO(b/271595607): delete this code once we upgrade HiGHS, if HiGHS does // return a proper infeasibility status for models with empty integer bounds. diff --git a/ortools/math_opt/solvers/highs_solver_test.cc b/ortools/math_opt/solvers/highs_solver_test.cc index ccb276129b..3afa835800 100644 --- a/ortools/math_opt/solvers/highs_solver_test.cc +++ b/ortools/math_opt/solvers/highs_solver_test.cc @@ -164,8 +164,8 @@ INSTANTIATE_TEST_SUITE_P(HighsLpModelSolveParametersTest, /*supports_duals=*/true, /*supports_primal_only_warm_starts=*/false))); -// MIP hint appears to be supported by Highs::setSolution, this is not yet -// implemented. +// Highs::setSolution is implemented, but it only accepts complete solutions. +// The test below generates partial solutuions, so we skip it. GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MipSolutionHintTest); // HiGHS does not support branching priority.