21#include "absl/status/status.h"
22#include "absl/strings/string_view.h"
23#include "gtest/gtest.h"
33 const bool supports_soc_constraints,
34 const bool supports_incremental_add_and_deletes)
35 : solver_type(solver_type),
37 supports_soc_constraints(supports_soc_constraints),
38 supports_incremental_add_and_deletes(
39 supports_incremental_add_and_deletes) {}
45 <<
", supports_soc_constraints: "
47 <<
", supports_incremental_add_and_deletes: "
54using ::testing::AnyOf;
55using ::testing::HasSubstr;
56using ::testing::status::IsOkAndHolds;
57using ::testing::status::StatusIs;
62constexpr absl::string_view kNoSocSupportMessage =
63 "This test is disabled as the solver does not support second-order cone "
71TEST_P(SimpleSecondOrderConeTest, CanBuildSecondOrderConeModel) {
73 const Variable
x =
model.AddContinuousVariable(0.0, 1.0,
"x");
74 model.AddSecondOrderConeConstraint({
x}, 2 *
x);
75 if (GetParam().supports_soc_constraints) {
79 StatusIs(AnyOf(absl::StatusCode::kInvalidArgument,
80 absl::StatusCode::kUnimplemented),
81 HasSubstr(
"second-order cone constraints")));
93TEST_P(SimpleSecondOrderConeTest, SolveSimpleSocModel) {
94 if (!GetParam().supports_soc_constraints) {
95 GTEST_SKIP() << kNoSocSupportMessage;
98 const Variable
x =
model.AddContinuousVariable(0.0, 1.0,
"x");
99 const Variable
y =
model.AddContinuousVariable(0.0, 1.0,
"y");
100 const Variable z =
model.AddContinuousVariable(0.0, 1.0,
"z");
102 model.AddSecondOrderConeConstraint({
x, 2.0 *
y, 3.0 * z}, 1.0);
105 7.0 / 6.0, {{
x, 6.0 / 7.0}, {
y, 3.0 / 14.0}, {z, 2.0 / 21.0}},
116TEST_P(SimpleSecondOrderConeTest, SolveMultipleSocConstraintModel) {
117 if (!GetParam().supports_soc_constraints) {
118 GTEST_SKIP() << kNoSocSupportMessage;
121 const Variable
x =
model.AddContinuousVariable(0.0, 1.0,
"x");
122 const Variable
y =
model.AddContinuousVariable(0.0, 1.0,
"y");
124 model.AddSecondOrderConeConstraint({
x, 2.0 *
y}, 2.0 *
x + 3.0);
125 model.AddSecondOrderConeConstraint({2.0 *
x,
y}, 2.0 *
y + 3.0);
138TEST_P(SimpleSecondOrderConeTest, SolveModelWithSocAndLinearConstraints) {
139 if (!GetParam().supports_soc_constraints) {
140 GTEST_SKIP() << kNoSocSupportMessage;
143 const Variable
x =
model.AddVariable(
"x");
144 const Variable
y =
model.AddVariable(
"y");
146 model.AddLinearConstraint(
x -
y <= 1.0);
147 model.AddSecondOrderConeConstraint({
x,
y}, 2.0);
148 const double sqrt_of_seven = std::sqrt(7.0);
152 (sqrt_of_seven + 1.0) / 2.0,
153 {{
x, (sqrt_of_seven + 1.0) / 2.0}, {
y, (sqrt_of_seven - 1.0) / 2.0}},
171TEST_P(IncrementalSecondOrderConeTest, LinearToSecondOrderConeUpdate) {
173 const Variable
x =
model.AddContinuousVariable(0.0, 1.0,
"x");
174 const Variable
y =
model.AddContinuousVariable(0.0, 1.0,
"y");
175 model.AddLinearConstraint(
x + 0.5 *
y <= 1.0);
181 ASSERT_THAT(solver->Solve({.parameters = GetParam().parameters}),
184 model.AddSecondOrderConeConstraint({
x,
y}, std::sqrt(0.5));
186 if (!GetParam().supports_soc_constraints) {
195 StatusIs(AnyOf(absl::StatusCode::kInvalidArgument,
196 absl::StatusCode::kUnimplemented),
197 AllOf(HasSubstr(
"second-order cone constraint"),
199 Not(HasSubstr(
"update failed")),
201 HasSubstr(
"solver re-creation failed"))));
206 IsOkAndHolds(GetParam().supports_incremental_add_and_deletes
209 EXPECT_THAT(solver->SolveWithoutUpdate({.parameters = GetParam().parameters}),
231TEST_P(IncrementalSecondOrderConeTest, UpdateDeletesSecondOrderConeConstraint) {
232 if (!GetParam().supports_soc_constraints) {
233 GTEST_SKIP() << kNoSocSupportMessage;
236 const Variable
x =
model.AddContinuousVariable(0.0, 1.0,
"x");
237 const Variable
y =
model.AddContinuousVariable(0.0, 1.0,
"y");
238 model.AddLinearConstraint(
x + 0.5 *
y <= 1.0);
239 const SecondOrderConeConstraint
c =
240 model.AddSecondOrderConeConstraint({
x,
y}, std::sqrt(0.5));
246 ASSERT_THAT(solver->Solve({.parameters = GetParam().parameters}),
250 model.DeleteSecondOrderConeConstraint(
c);
253 IsOkAndHolds(GetParam().supports_incremental_add_and_deletes
256 EXPECT_THAT(solver->SolveWithoutUpdate({.parameters = GetParam().parameters}),
275TEST_P(IncrementalSecondOrderConeTest, UpdateDeletesUpperBoundingVariable) {
276 if (!GetParam().supports_soc_constraints) {
277 GTEST_SKIP() << kNoSocSupportMessage;
280 const Variable
x =
model.AddContinuousVariable(0.0, 1.0,
"x");
281 const Variable
y =
model.AddContinuousVariable(0.0, 1.0,
"y");
282 model.AddSecondOrderConeConstraint({
x},
y);
288 ASSERT_THAT(solver->Solve({.parameters = GetParam().parameters}),
294 IsOkAndHolds(GetParam().supports_incremental_add_and_deletes
297 EXPECT_THAT(solver->SolveWithoutUpdate({.parameters = GetParam().parameters}),
317TEST_P(IncrementalSecondOrderConeTest,
318 UpdateDeletesVariableInUpperBoundingExpression) {
319 if (!GetParam().supports_soc_constraints) {
320 GTEST_SKIP() << kNoSocSupportMessage;
323 const Variable
x =
model.AddContinuousVariable(0.0, 2.0,
"x");
324 const Variable
y =
model.AddContinuousVariable(0.0, 1.0,
"y");
325 model.AddSecondOrderConeConstraint({
x},
y + 1.0);
331 ASSERT_THAT(solver->Solve({.parameters = GetParam().parameters}),
337 IsOkAndHolds(GetParam().supports_incremental_add_and_deletes
340 EXPECT_THAT(solver->SolveWithoutUpdate({.parameters = GetParam().parameters}),
360TEST_P(IncrementalSecondOrderConeTest, UpdateDeletesVariableThatIsAnArgument) {
361 if (!GetParam().supports_soc_constraints) {
362 GTEST_SKIP() << kNoSocSupportMessage;
365 const Variable
x =
model.AddContinuousVariable(1.0, 1.0,
"x");
366 const Variable
y =
model.AddContinuousVariable(0.0, 1.0,
"y");
367 model.AddSecondOrderConeConstraint({
x},
y);
373 ASSERT_THAT(solver->Solve({.parameters = GetParam().parameters}),
379 IsOkAndHolds(GetParam().supports_incremental_add_and_deletes
382 EXPECT_THAT(solver->SolveWithoutUpdate({.parameters = GetParam().parameters}),
402TEST_P(IncrementalSecondOrderConeTest, UpdateDeletesVariableInAnArgument) {
403 if (!GetParam().supports_soc_constraints) {
404 GTEST_SKIP() << kNoSocSupportMessage;
407 const Variable
x =
model.AddContinuousVariable(1.0, 1.0,
"x");
408 const Variable
y =
model.AddContinuousVariable(0.0, 2.0,
"y");
409 model.AddSecondOrderConeConstraint({
x + 1.0},
y);
415 ASSERT_THAT(solver->Solve({.parameters = GetParam().parameters}),
421 IsOkAndHolds(GetParam().supports_incremental_add_and_deletes
424 EXPECT_THAT(solver->SolveWithoutUpdate({.parameters = GetParam().parameters}),
An object oriented wrapper for quadratic constraints in ModelStorage.
Matcher< SolveResult > IsOptimalWithSolution(const double expected_objective, const VariableMap< double > expected_variable_values, const double tolerance)
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)
SolverType
The solvers supported by MathOpt.
<=x<=1 IncrementalMipTest::IncrementalMipTest() :model_("incremental_solve_test"), x_(model_.AddContinuousVariable(0.0, 1.0, "x")), y_(model_.AddIntegerVariable(0.0, 2.0, "y")), c_(model_.AddLinearConstraint(0<=x_+y_<=1.5, "c")) { model_.Maximize(3.0 *x_+2.0 *y_+0.1);solver_=NewIncrementalSolver(&model_, TestedSolver()).value();const SolveResult first_solve=solver_->Solve().value();CHECK(first_solve.has_primal_feasible_solution());CHECK_LE(std::abs(first_solve.objective_value() - 3.6), kTolerance)<< first_solve.objective_value();} namespace { TEST_P(SimpleMipTest, OneVarMax) { Model model;const Variable x=model.AddVariable(0.0, 4.0, false, "x");model.Maximize(2.0 *x);ASSERT_OK_AND_ASSIGN(const SolveResult result, Solve(model, GetParam().solver_type));ASSERT_THAT(result, IsOptimal(8.0));EXPECT_THAT(result.variable_values(), IsNear({{x, 4.0}}));} TEST_P(SimpleMipTest, OneVarMin) { Model model;const Variable x=model.AddVariable(-2.4, 4.0, false, "x");model.Minimize(2.0 *x);ASSERT_OK_AND_ASSIGN(const SolveResult result, Solve(model, GetParam().solver_type));ASSERT_THAT(result, IsOptimal(-4.8));EXPECT_THAT(result.variable_values(), IsNear({{x, -2.4}}));} TEST_P(SimpleMipTest, OneIntegerVar) { Model model;const Variable x=model.AddVariable(0.0, 4.5, true, "x");model.Maximize(2.0 *x);ASSERT_OK_AND_ASSIGN(const SolveResult result, Solve(model, GetParam().solver_type));ASSERT_THAT(result, IsOptimal(8.0));EXPECT_THAT(result.variable_values(), IsNear({{x, 4.0}}));} TEST_P(SimpleMipTest, SimpleLinearConstraint) { Model model;const Variable x=model.AddBinaryVariable("x");const Variable y=model.AddBinaryVariable("y");model.Maximize(2.0 *x+y);model.AddLinearConstraint(0.0<=x+y<=1.5, "c");ASSERT_OK_AND_ASSIGN(const SolveResult result, Solve(model, GetParam().solver_type));ASSERT_THAT(result, IsOptimal(2.0));EXPECT_THAT(result.variable_values(), IsNear({{x, 1}, {y, 0}}));} TEST_P(SimpleMipTest, Unbounded) { Model model;const Variable x=model.AddVariable(0.0, kInf, true, "x");model.Maximize(2.0 *x);ASSERT_OK_AND_ASSIGN(const SolveResult result, Solve(model, GetParam().solver_type));if(GetParam().report_unboundness_correctly) { ASSERT_THAT(result, TerminatesWithOneOf({TerminationReason::kUnbounded, TerminationReason::kInfeasibleOrUnbounded}));} else { ASSERT_THAT(result, TerminatesWith(TerminationReason::kOtherError));} } TEST_P(SimpleMipTest, Infeasible) { Model model;const Variable x=model.AddVariable(0.0, 3.0, true, "x");model.Maximize(2.0 *x);model.AddLinearConstraint(x >=4.0);ASSERT_OK_AND_ASSIGN(const SolveResult result, Solve(model, GetParam().solver_type));ASSERT_THAT(result, TerminatesWith(TerminationReason::kInfeasible));} TEST_P(SimpleMipTest, FractionalBoundsContainNoInteger) { if(GetParam().solver_type==SolverType::kGurobi) { GTEST_SKIP()<< "TODO(b/272298816): Gurobi bindings are broken here.";} Model model;const Variable x=model.AddIntegerVariable(0.5, 0.6, "x");model.Maximize(x);EXPECT_THAT(Solve(model, GetParam().solver_type), IsOkAndHolds(TerminatesWith(TerminationReason::kInfeasible)));} TEST_P(IncrementalMipTest, EmptyUpdate) { ASSERT_THAT(solver_->Update(), IsOkAndHolds(DidUpdate()));ASSERT_OK_AND_ASSIGN(const SolveResult result, solver_->SolveWithoutUpdate());ASSERT_THAT(result, IsOptimal(3.6));EXPECT_THAT(result.variable_values(), IsNear({{x_, 0.5}, {y_, 1.0}}));} TEST_P(IncrementalMipTest, MakeContinuous) { model_.set_continuous(y_);ASSERT_THAT(solver_->Update(), IsOkAndHolds(DidUpdate()));ASSERT_OK_AND_ASSIGN(const SolveResult result, solver_->SolveWithoutUpdate());ASSERT_THAT(result, IsOptimal(4.1));EXPECT_THAT(result.variable_values(), IsNear({{x_, 1.0}, {y_, 0.5}}));} TEST_P(IncrementalMipTest, DISABLED_MakeContinuousWithNonIntegralBounds) { solver_.reset();Model model("bounds");const Variable x=model.AddIntegerVariable(0.5, 1.5, "x");model.Maximize(x);ASSERT_OK_AND_ASSIGN(const auto solver, NewIncrementalSolver(&model, TestedSolver()));ASSERT_THAT(solver->Solve(), IsOkAndHolds(IsOptimal(1.0)));model.set_continuous(x);ASSERT_THAT(solver->Update(), IsOkAndHolds(DidUpdate()));ASSERT_THAT(solver-> IsOkAndHolds(IsOptimal(1.5)))
ASSERT_THAT(solver->Update(), IsOkAndHolds(DidUpdate()))
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
absl::StatusOr< std::unique_ptr< IncrementalSolver > > NewIncrementalSolver(Model *model, SolverType solver_type, SolverInitArguments arguments)
Matcher< UpdateResult > DidUpdate()
Actual UpdateResult.did_update is true.
constexpr double kTolerance
std::string ProtobufShortDebugString(const P &message)
internal::StatusIsMatcher StatusIs(CodeMatcher code_matcher, MessageMatcher message_matcher)
#define EXPECT_OK(expression)
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr)
SolverType solver_type
The tested solver.
bool supports_incremental_add_and_deletes
SecondOrderConeTestParameters(SolverType solver_type, SolveParameters parameters, bool supports_soc_constraints, bool supports_incremental_add_and_deletes)
SolveParameters parameters
bool supports_soc_constraints
True if the solver supports second-order cone constraints.
SolveParametersProto Proto() const