Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_constraints.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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 <algorithm>
17#include <vector>
18
19#include "absl/types/span.h"
20#include "ortools/sat/integer.h"
22#include "ortools/sat/model.h"
25
26namespace operations_research {
27namespace sat {
28
30 bool sum = false;
31 int unassigned_index = -1;
32 for (int i = 0; i < literals_.size(); ++i) {
33 const Literal l = literals_[i];
34 if (trail_->Assignment().LiteralIsFalse(l)) {
35 sum ^= false;
36 } else if (trail_->Assignment().LiteralIsTrue(l)) {
37 sum ^= true;
38 } else {
39 // If we have more than one unassigned literal, we can't deduce anything.
40 if (unassigned_index != -1) return true;
41 unassigned_index = i;
42 }
43 }
44
45 // Propagates?
46 if (unassigned_index != -1) {
47 literal_reason_.clear();
48 for (int i = 0; i < literals_.size(); ++i) {
49 if (i == unassigned_index) continue;
50 const Literal l = literals_[i];
51 literal_reason_.push_back(
52 trail_->Assignment().LiteralIsFalse(l) ? l : l.Negated());
53 }
54 const Literal u = literals_[unassigned_index];
55 integer_trail_->EnqueueLiteral(sum == value_ ? u.Negated() : u,
56 literal_reason_, {});
57 return true;
58 }
59
60 // Ok.
61 if (sum == value_) return true;
62
63 // Conflict.
64 std::vector<Literal>* conflict = trail_->MutableConflict();
65 conflict->clear();
66 for (int i = 0; i < literals_.size(); ++i) {
67 const Literal l = literals_[i];
68 conflict->push_back(trail_->Assignment().LiteralIsFalse(l) ? l
69 : l.Negated());
70 }
71 return false;
72}
73
75 const int id = watcher->Register(this);
76 for (const Literal& l : literals_) {
77 watcher->WatchLiteral(l, id);
78 watcher->WatchLiteral(l.Negated(), id);
79 }
80}
81
83 IntegerVariable target_var, const absl::Span<const AffineExpression> exprs,
84 const absl::Span<const Literal> selectors,
85 const absl::Span<const Literal> enforcements, Model* model)
86 : target_var_(target_var),
87 enforcements_(enforcements.begin(), enforcements.end()),
88 selectors_(selectors.begin(), selectors.end()),
89 exprs_(exprs.begin(), exprs.end()),
90 trail_(model->GetOrCreate<Trail>()),
91 integer_trail_(model->GetOrCreate<IntegerTrail>()) {}
92
94 int id, IntegerValue propagation_slack, IntegerVariable /*var_to_explain*/,
95 int /*trail_index*/, std::vector<Literal>* literals_reason,
96 std::vector<int>* trail_indices_reason) {
97 literals_reason->clear();
98 trail_indices_reason->clear();
99
100 const int first_non_false = id;
101 const IntegerValue target_min = propagation_slack;
102
103 for (const Literal l : enforcements_) {
104 literals_reason->push_back(l.Negated());
105 }
106 for (int i = 0; i < first_non_false; ++i) {
107 // If the level zero bounds is good enough, no reason needed.
108 //
109 // TODO(user): We could also skip this if we already have the reason for
110 // the expression being high enough in the current conflict.
111 if (integer_trail_->LevelZeroLowerBound(exprs_[i]) >= target_min) {
112 continue;
113 }
114
115 literals_reason->push_back(selectors_[i]);
116 }
117 integer_trail_->AddAllGreaterThanConstantReason(
118 absl::MakeSpan(exprs_).subspan(first_non_false), target_min,
119 trail_indices_reason);
120}
121
123 // TODO(user): In case of a conflict, we could push one of them to false if
124 // it is the only one.
125 for (const Literal l : enforcements_) {
126 if (!trail_->Assignment().LiteralIsTrue(l)) return true;
127 }
128
129 // Compute the min of the lower-bound for the still possible variables.
130 // TODO(user): This could be optimized by keeping more info from the last
131 // Propagate() calls.
132 IntegerValue target_min = kMaxIntegerValue;
133 const IntegerValue current_min = integer_trail_->LowerBound(target_var_);
134 const AssignmentView assignment(trail_->Assignment());
135
136 int first_non_false = 0;
137 const int size = exprs_.size();
138 Literal* const selectors = selectors_.data();
139 AffineExpression* const exprs = exprs_.data();
140 for (int i = 0; i < size; ++i) {
141 if (assignment.LiteralIsTrue(selectors[i])) return true;
142
143 // The permutation is needed to have proper lazy reason.
144 if (assignment.LiteralIsFalse(selectors[i])) {
145 if (i != first_non_false) {
146 std::swap(selectors[i], selectors[first_non_false]);
147 std::swap(exprs[i], exprs[first_non_false]);
148 }
149 ++first_non_false;
150 continue;
151 }
152
153 const IntegerValue min = integer_trail_->LowerBound(exprs[i]);
154 if (min < target_min) {
155 target_min = min;
156
157 // Abort if we can't get a better bound.
158 if (target_min <= current_min) return true;
159 }
160 }
161
162 if (target_min == kMaxIntegerValue) {
163 // All false, conflit.
164 *(trail_->MutableConflict()) = selectors_;
165 return false;
166 }
167
168 // Note that we use id/propagation_slack for other purpose.
169 return integer_trail_->EnqueueWithLazyReason(
170 IntegerLiteral::GreaterOrEqual(target_var_, target_min),
171 /*id=*/first_non_false, /*propagation_slack=*/target_min, this);
172}
173
175 GenericLiteralWatcher* watcher) {
176 const int id = watcher->Register(this);
177 for (const Literal l : selectors_) watcher->WatchLiteral(l.Negated(), id);
178 for (const Literal l : enforcements_) watcher->WatchLiteral(l, id);
179 for (const AffineExpression e : exprs_) {
180 if (!e.IsConstant()) {
181 watcher->WatchLowerBound(e, id);
182 }
183 }
184}
185
186} // namespace sat
187} // namespace operations_research
bool LiteralIsFalse(Literal literal) const
Definition sat_base.h:227
bool LiteralIsTrue(Literal literal) const
Definition sat_base.h:231
void RegisterWith(GenericLiteralWatcher *watcher)
void WatchLiteral(Literal l, int id, int watch_index=-1)
Definition integer.h:1428
void WatchLowerBound(IntegerVariable var, int id, int watch_index=-1)
Definition integer.h:1436
int Register(PropagatorInterface *propagator)
Registers a propagator and returns its unique ids.
Definition integer.cc:2327
void Explain(int id, IntegerValue propagation_slack, IntegerVariable var_to_explain, int trail_index, std::vector< Literal > *literals_reason, std::vector< int > *trail_indices_reason) final
For LazyReasonInterface.
GreaterThanAtLeastOneOfPropagator(IntegerVariable target_var, absl::Span< const AffineExpression > exprs, absl::Span< const Literal > selectors, absl::Span< const Literal > enforcements, Model *model)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
In SWIG mode, we don't want anything besides these top-level includes.
static IntegerLiteral GreaterOrEqual(IntegerVariable i, IntegerValue bound)