Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ip_multiple_solutions_tests.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cstdint>
17#include <optional>
18#include <ostream>
19
20#include "absl/strings/str_cat.h"
21#include "gtest/gtest.h"
22#include "ortools/base/gmock.h"
26
28
29std::ostream& operator<<(std::ostream& out,
30 const IpMultipleSolutionsTestParams& params) {
31 out << "{ solver_type: " << params.solver_type << " ensure_hint_in_pool: "
33 return out;
34}
35
36namespace {
37
38TEST_P(IpMultipleSolutionsTest, FindTwoSolutionsUsingHint) {
39 ModelSolveParameters model_parameters;
40
41 Model model("Solution Hint MIP");
42
43 Variable x1 = model.AddBinaryVariable("x1");
44 Variable x2 = model.AddBinaryVariable("x2");
45 model.AddLinearConstraint(x1 + x2 == 1);
46 model.Maximize(x1 + 3 * x2);
47
48 // Two feasible solutions [0, 1] and [1, 0]. Hint the worse one.
50 hint.variable_values = {{x1, 1.0}, {x2, 0.0}};
51 model_parameters.solution_hints.emplace_back(hint);
52
53 const Solution expected1 = {
54 .primal_solution = std::make_optional<PrimalSolution>(
55 {.variable_values = {{x1, 0.0}, {x2, 1.0}},
56 .objective_value = 3.0,
57 .feasibility_status = SolutionStatus::kFeasible})};
58
59 const Solution expected2 = {
60 .primal_solution = std::make_optional<PrimalSolution>(
61 {.variable_values = {{x1, 1.0}, {x2, 0.0}},
62 .objective_value = 1.0,
63 .feasibility_status = SolutionStatus::kFeasible})};
64
65 for (int32_t solution_pool_size : {1, 2}) {
66 SCOPED_TRACE(absl::StrCat("Solution pool size: ", solution_pool_size));
67 SolveArguments args = {.parameters = GetParam().ensure_hint_in_pool,
68 .model_parameters = model_parameters};
69 args.parameters.solution_pool_size = solution_pool_size;
70 ASSERT_OK_AND_ASSIGN(const SolveResult result,
71 Solve(model, GetParam().solver_type, args));
72 EXPECT_THAT(result, IsOptimal(3.0));
73 if (solution_pool_size == 1) {
74 EXPECT_THAT(result.solutions, ElementsAre(IsNear(expected1)));
75 } else {
76 // Gurobi does not guarantee that all solution pool entries are feasible,
77 // so we also accept undetermined feasibility status.
78 EXPECT_THAT(result.solutions,
79 ElementsAre(IsNear(expected1),
80 IsNear(expected2, {.allow_undetermined = true})));
81 }
82 }
83}
84
85} // namespace
86
87} // namespace operations_research::math_opt
GRBmodel * model
const Variable x2
const Variable x1
std::optional< ModelSolveParameters::SolutionHint > hint
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
EXPECT_THAT(ComputeInfeasibleSubsystem(model, GetParam().solver_type), IsOkAndHolds(IsInfeasible(true, ModelSubset{ .variable_bounds={{x, ModelSubset::Bounds{.lower=false,.upper=true}}},.linear_constraints={ {c, ModelSubset::Bounds{.lower=true,.upper=false}}}})))
TEST_P(InfeasibleSubsystemTest, CanComputeInfeasibleSubsystem)
absl::StatusOr< SolveResult > Solve(const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolverInitArguments &init_args)
Definition solve.cc:62
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
@ kFeasible
Solver claims the solution is feasible.
Matcher< VariableMap< double > > IsNear(VariableMap< double > expected, const double tolerance)
Definition matchers.cc:221
Matcher< SolveResult > IsOptimal(const std::optional< double > expected_primal_objective, const double tolerance)
Definition matchers.cc:762
std::string ProtobufShortDebugString(const P &message)
Definition proto_utils.h:41
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr)