Implement setting of partial solution, activate corresponding tests

This commit is contained in:
Pavlo Muts
2025-07-22 19:29:26 +02:00
parent 97636fdec7
commit fd1443e820
2 changed files with 21 additions and 14 deletions

View File

@@ -14,7 +14,6 @@
// Unimplemented features:
// * Quadratic objective
// * TODO(b/272767311): initial basis, more precise returned basis.
// * Starting solution
// * TODO(b/271104776): Returning rays
#include "ortools/math_opt/solvers/highs_solver.h"
@@ -928,17 +927,16 @@ absl::StatusOr<SolveResultProto> HighsSolver::Solve(
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)));
HighsInt num_entries = hint.variable_values().ids_size();
std::vector<HighsInt> index(num_entries);
std::vector<double> value(num_entries);
size_t i = 0;
for (const auto [id, val] : MakeView(hint.variable_values())) {
index[i] = variable_data_.at(id).index;
value[i] = val;
++i;
}
RETURN_IF_ERROR(ToStatus(highs_->setSolution(num_entries, index.data(), value.data())));
}
RETURN_IF_ERROR(ListInvertedBounds().ToStatus());

View File

@@ -164,9 +164,18 @@ INSTANTIATE_TEST_SUITE_P(HighsLpModelSolveParametersTest,
/*supports_duals=*/true,
/*supports_primal_only_warm_starts=*/true)));
// 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);
SolutionHintTestParams MakeHighsSolutionHintParams() {
SolveParameters solve_params;
solve_params.presolve = Emphasis::kOff;
(*solve_params.highs.mutable_int_options())["mip_max_nodes"] = 0;
std::string hint_message_regex =
"Attempting to find feasible solution by "
"solving MIP for user-supplied values of";
return SolutionHintTestParams(SolverType::kHighs, solve_params, std::nullopt,
hint_message_regex);
}
INSTANTIATE_TEST_SUITE_P(HighsSolutionHintTest, MipSolutionHintTest,
Values(MakeHighsSolutionHintParams()));
// HiGHS does not support branching priority.
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BranchPrioritiesTest);