Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
enforcement_helper.h
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
14#ifndef ORTOOLS_SAT_ENFORCEMENT_HELPER_H_
15#define ORTOOLS_SAT_ENFORCEMENT_HELPER_H_
16
17#include <vector>
18
19#include "absl/base/attributes.h"
20#include "absl/types/span.h"
22#include "ortools/sat/integer.h"
24#include "ortools/sat/model.h"
26
27namespace operations_research {
28namespace sat {
29
30// This is meant as an helper to deal with enforcement for any integer based
31// constraint. It wraps some IntegerTrail functions while making sure the
32// enforcement literals are properly added to the propagation reason.
34 public:
35 explicit EnforcementHelper(Model* model);
36
37 // Calls `Register` with a callback calling
38 // `watcher->CallOnNextPropagate(literal_watcher_id)` if a propagation might
39 // be possible.
40 EnforcementId Register(absl::Span<const Literal> enforcement_literals,
41 GenericLiteralWatcher* watcher,
42 int literal_watcher_id);
43
44 // Add the enforcement reason to the given vector.
45 void AddEnforcementReason(EnforcementId id,
46 std::vector<Literal>* reason) const {
47 enforcement_propagator_.AddEnforcementReason(id, reason);
48 }
49
50 // Try to propagate when the enforced constraint is not satisfiable.
51 // This is currently in O(enforcement_size).
52 ABSL_MUST_USE_RESULT bool PropagateWhenFalse(
53 EnforcementId id, absl::Span<const Literal> literal_reason,
54 absl::Span<const IntegerLiteral> integer_reason);
55
56 ABSL_MUST_USE_RESULT bool Enqueue(
57 EnforcementId id, IntegerLiteral i_lit,
58 absl::Span<const Literal> literal_reason,
59 absl::Span<const IntegerLiteral> integer_reason);
60
61 ABSL_MUST_USE_RESULT bool SafeEnqueue(
62 EnforcementId id, IntegerLiteral i_lit,
63 absl::Span<const IntegerLiteral> integer_reason);
64
65 ABSL_MUST_USE_RESULT bool ConditionalEnqueue(
66 EnforcementId id, Literal lit, IntegerLiteral i_lit,
67 absl::Span<const Literal> literal_reason,
68 absl::Span<const IntegerLiteral> integer_reason);
69
70 ABSL_MUST_USE_RESULT bool EnqueueLiteral(
71 EnforcementId id, Literal literal,
72 absl::Span<const Literal> literal_reason,
73 absl::Span<const IntegerLiteral> integer_reason);
74
75 bool ReportConflict(EnforcementId id,
76 absl::Span<const IntegerLiteral> integer_reason) {
77 return ReportConflict(id, /*literal_reason=*/{}, integer_reason);
78 }
79
80 bool ReportConflict(EnforcementId id,
81 absl::Span<const Literal> literal_reason,
82 absl::Span<const IntegerLiteral> integer_reason);
83
84 EnforcementStatus Status(EnforcementId id) const {
85 return enforcement_propagator_.Status(id);
86 }
87
88 // Returns the enforcement literals of the given id.
89 absl::Span<const Literal> GetEnforcementLiterals(EnforcementId id) const {
90 return enforcement_propagator_.GetEnforcementLiterals(id);
91 }
92
93 private:
94 EnforcementPropagator& enforcement_propagator_;
95 const VariablesAssignment& assignment_;
96 IntegerTrail* integer_trail_;
97 std::vector<Literal> temp_reason_;
98 std::vector<IntegerLiteral> temp_integer_reason_;
99};
100
101} // namespace sat
102} // namespace operations_research
103
104#endif // ORTOOLS_SAT_ENFORCEMENT_HELPER_H_
absl::Span< const Literal > GetEnforcementLiterals(EnforcementId id) const
bool ReportConflict(EnforcementId id, absl::Span< const IntegerLiteral > integer_reason)
ABSL_MUST_USE_RESULT bool EnqueueLiteral(EnforcementId id, Literal literal, absl::Span< const Literal > literal_reason, absl::Span< const IntegerLiteral > integer_reason)
ABSL_MUST_USE_RESULT bool Enqueue(EnforcementId id, IntegerLiteral i_lit, absl::Span< const Literal > literal_reason, absl::Span< const IntegerLiteral > integer_reason)
EnforcementId Register(absl::Span< const Literal > enforcement_literals, GenericLiteralWatcher *watcher, int literal_watcher_id)
ABSL_MUST_USE_RESULT bool ConditionalEnqueue(EnforcementId id, Literal lit, IntegerLiteral i_lit, absl::Span< const Literal > literal_reason, absl::Span< const IntegerLiteral > integer_reason)
ABSL_MUST_USE_RESULT bool PropagateWhenFalse(EnforcementId id, absl::Span< const Literal > literal_reason, absl::Span< const IntegerLiteral > integer_reason)
EnforcementStatus Status(EnforcementId id) const
void AddEnforcementReason(EnforcementId id, std::vector< Literal > *reason) const
ABSL_MUST_USE_RESULT bool SafeEnqueue(EnforcementId id, IntegerLiteral i_lit, absl::Span< const IntegerLiteral > integer_reason)
OR-Tools root namespace.