forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cp_constraints_test.cc
120 lines (102 loc) · 4.3 KB
/
cp_constraints_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/sat/cp_constraints.h"
#include <stdint.h>
#include <string>
#include <vector>
#include "absl/types/span.h"
#include "gtest/gtest.h"
#include "ortools/base/logging.h"
#include "ortools/sat/integer.h"
#include "ortools/sat/integer_search.h"
#include "ortools/sat/model.h"
#include "ortools/sat/precedences.h"
#include "ortools/sat/sat_base.h"
#include "ortools/sat/sat_solver.h"
namespace operations_research {
namespace sat {
namespace {
TEST(LiteralXorIsTest, OneVariable) {
Model model;
const BooleanVariable a = model.Add(NewBooleanVariable());
const BooleanVariable b = model.Add(NewBooleanVariable());
model.Add(LiteralXorIs({Literal(a, true)}, true));
model.Add(LiteralXorIs({Literal(b, true)}, false));
SatSolver* solver = model.GetOrCreate<SatSolver>();
EXPECT_TRUE(solver->Propagate());
EXPECT_TRUE(solver->Assignment().LiteralIsTrue(Literal(a, true)));
EXPECT_TRUE(solver->Assignment().LiteralIsFalse(Literal(b, true)));
}
// A simple macro to make the code more readable.
#define EXPECT_BOUNDS_EQ(var, lb, ub) \
EXPECT_EQ(model.Get(LowerBound(var)), lb); \
EXPECT_EQ(model.Get(UpperBound(var)), ub)
TEST(PartialIsOneOfVarTest, MinMaxPropagation) {
Model model;
const IntegerVariable target_var = model.Add(NewIntegerVariable(-10, 20));
std::vector<IntegerVariable> vars;
std::vector<Literal> selectors;
for (int i = 0; i < 10; ++i) {
vars.push_back(model.Add(ConstantIntegerVariable(i)));
selectors.push_back(Literal(model.Add(NewBooleanVariable()), true));
}
model.Add(PartialIsOneOfVar(target_var, vars, selectors));
EXPECT_TRUE(model.GetOrCreate<SatSolver>()->Propagate());
EXPECT_BOUNDS_EQ(target_var, 0, 9);
model.Add(ClauseConstraint({selectors[0].Negated()}));
EXPECT_TRUE(model.GetOrCreate<SatSolver>()->Propagate());
EXPECT_BOUNDS_EQ(target_var, 1, 9);
model.Add(ClauseConstraint({selectors[8].Negated()}));
EXPECT_TRUE(model.GetOrCreate<SatSolver>()->Propagate());
EXPECT_BOUNDS_EQ(target_var, 1, 9);
model.Add(ClauseConstraint({selectors[9].Negated()}));
EXPECT_TRUE(model.GetOrCreate<SatSolver>()->Propagate());
EXPECT_BOUNDS_EQ(target_var, 1, 7);
}
TEST(GreaterThanAtLeastOneOfPropagatorTest, BasicTest) {
for (int i = 0; i < 2; ++i) {
Model model;
// We create a simple model with 3 variables and 2 conditional precedences.
// We only add the GreaterThanAtLeastOneOfPropagator() for i == 1.
const IntegerVariable a = model.Add(NewIntegerVariable(0, 3));
const IntegerVariable b = model.Add(NewIntegerVariable(0, 3));
const IntegerVariable c = model.Add(NewIntegerVariable(0, 3));
const Literal ac = Literal(model.Add(NewBooleanVariable()), true);
const Literal bc = Literal(model.Add(NewBooleanVariable()), true);
model.Add(ConditionalLowerOrEqualWithOffset(a, c, 3, ac));
model.Add(ConditionalLowerOrEqualWithOffset(b, c, 2, bc));
model.Add(ClauseConstraint({ac, bc}));
if (i == 1) {
model.Add(GreaterThanAtLeastOneOf(
c, {a, b}, {IntegerValue(3), IntegerValue(2)}, {ac, bc}, {}));
}
// Test that we do propagate more with the extra propagator.
EXPECT_TRUE(model.GetOrCreate<SatSolver>()->Propagate());
EXPECT_EQ(model.Get(LowerBound(c)), i == 0 ? 0 : 2);
// Test that we find all solutions.
int num_solutions = 0;
while (true) {
const auto status = SolveIntegerProblemWithLazyEncoding(&model);
if (status != SatSolver::Status::FEASIBLE) break;
++num_solutions;
VLOG(1) << model.Get(Value(a)) << " " << model.Get(Value(b)) << " "
<< model.Get(Value(c));
model.Add(ExcludeCurrentSolutionAndBacktrack());
}
EXPECT_EQ(num_solutions, 18);
}
}
#undef EXPECT_BOUNDS_EQ
} // namespace
} // namespace sat
} // namespace operations_research