Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
enforcement.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_H_
15#define ORTOOLS_SAT_ENFORCEMENT_H_
16
17#include <cstdint>
18#include <functional>
19#include <ostream>
20#include <utility>
21#include <vector>
22
23#include "absl/container/inlined_vector.h"
24#include "absl/types/span.h"
26#include "ortools/sat/model.h"
28#include "ortools/util/rev.h"
30
31namespace operations_research {
32namespace sat {
33
35
36// An enforced constraint can be in one of these 4 states.
37// Note that we rely on the integer encoding to take 2 bits for optimization.
39 // One enforcement literal is false.
41 // More than two literals are unassigned.
43 // All enforcement literals are true but one.
45 // All enforcement literals are true.
47};
48
49std::ostream& operator<<(std::ostream& os, const EnforcementStatus& e);
50
51// This is meant as an helper to deal with enforcement for any constraint.
53 public:
54 explicit EnforcementPropagator(Model* model);
55
56 // SatPropagator interface.
57 bool Propagate(Trail* trail) final;
58 void Untrail(const Trail& trail, int trail_index) final;
59
60 // Adds a new constraint to the class and register a callback that will
61 // be called on status change. Note that we also call the callback with the
62 // initial status if different from CANNOT_PROPAGATE when added.
63 //
64 // It is better to not call this for empty enforcement list, but you can. A
65 // negative id means the level zero status will never change, and only the
66 // first call to callback() should be necessary, we don't save it.
67 EnforcementId Register(
68 absl::Span<const Literal> enforcement,
69 std::function<void(EnforcementId, EnforcementStatus)> callback = nullptr);
70
71 // Add the enforcement reason to the given vector.
72 void AddEnforcementReason(EnforcementId id,
73 std::vector<Literal>* reason) const;
74
75 EnforcementStatus Status(EnforcementId id) const {
76 if (id < 0) return EnforcementStatus::IS_ENFORCED;
77 return statuses_[id];
78 }
79
80 // This recomputes the current status by scanning the given list.
81 // It thus has a linear complexity and should not be used in hot loops.
82 EnforcementStatus Status(absl::Span<const Literal> enforcement) const;
83
84 // Recompute the status from the current assignment.
85 // This should only used in DCHECK().
86 EnforcementStatus DebugStatus(EnforcementId id);
87
88 // Returns the enforcement literals of the given id.
89 absl::Span<const Literal> GetEnforcementLiterals(EnforcementId id) const {
90 if (id < 0) return {};
91 return GetSpan(id);
92 }
93
94 private:
95 absl::Span<Literal> GetSpan(EnforcementId id);
96 absl::Span<const Literal> GetSpan(EnforcementId id) const;
97 void ChangeStatus(EnforcementId id, EnforcementStatus new_status);
98
99 // Returns kNoLiteralIndex if nothing need to change or a new literal to
100 // watch. This also calls the registered callback.
101 LiteralIndex ProcessIdOnTrue(Literal watched, EnforcementId id);
102
103 // External classes.
104 const Trail& trail_;
105 const VariablesAssignment& assignment_;
106 RevRepository<int> rev_int_repository_;
107
108 // All enforcement will be copied there, and we will create Span out of this.
109 // Note that we don't store the span so that we are not invalidated on buffer_
110 // resizing.
112 std::vector<Literal> buffer_;
113
116 EnforcementId, std::function<void(EnforcementId, EnforcementStatus)>>
117 callbacks_;
118
119 // Used to restore status and call callback on untrail.
120 std::vector<std::pair<EnforcementId, EnforcementStatus>> untrail_stack_;
121 int rev_stack_size_ = 0;
122 int64_t rev_stamp_ = 0;
123
124 // We use a two watcher scheme.
126 watcher_;
127
128 std::vector<Literal> temp_literals_;
129
130 std::vector<EnforcementId> ids_to_fix_until_next_root_level_;
131
132 friend class EnforcementHelper;
133};
134
135} // namespace sat
136} // namespace operations_research
137
138#endif // ORTOOLS_SAT_ENFORCEMENT_H_
EnforcementStatus Status(EnforcementId id) const
Definition enforcement.h:75
void AddEnforcementReason(EnforcementId id, std::vector< Literal > *reason) const
void Untrail(const Trail &trail, int trail_index) final
EnforcementId Register(absl::Span< const Literal > enforcement, std::function< void(EnforcementId, EnforcementStatus)> callback=nullptr)
absl::Span< const Literal > GetEnforcementLiterals(EnforcementId id) const
Definition enforcement.h:89
EnforcementStatus DebugStatus(EnforcementId id)
SatPropagator(const std::string &name)
Definition sat_base.h:786
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
Definition cp_model.cc:89
OR-Tools root namespace.
#define DEFINE_STRONG_INDEX_TYPE(index_type_name)