Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
diffn.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_DIFFN_H_
15#define ORTOOLS_SAT_DIFFN_H_
16
17#include <cstdint>
18#include <optional>
19#include <vector>
20
21#include "absl/container/flat_hash_set.h"
22#include "absl/types/span.h"
26#include "ortools/sat/integer.h"
28#include "ortools/sat/model.h"
34#include "ortools/sat/util.h"
35#include "ortools/util/bitset.h"
37
38namespace operations_research {
39namespace sat {
40
41// Propagates using a box energy reasoning.
43 public:
45 Model* model)
46 : helper_(*helper),
47 random_(model->GetOrCreate<ModelRandomGenerator>()),
48 shared_stats_(model->GetOrCreate<SharedStatistics>()),
49 orthogonal_packing_checker_(*random_, shared_stats_) {}
50
52
53 bool Propagate() final;
55
56 private:
57 struct Conflict {
58 // The Orthogonal Packing subproblem we used.
59 std::vector<RectangleInRange> items_for_opp;
60 Rectangle rectangle_with_too_much_energy;
61 OrthogonalPackingResult opp_result;
62 };
63 std::optional<Conflict> FindConflict(
64 std::vector<RectangleInRange> active_box_ranges);
65
66 std::vector<RectangleInRange> GeneralizeExplanation(const Conflict& conflict);
67
68 bool BuildAndReportEnergyTooLarge(absl::Span<const RectangleInRange> ranges);
69
70 NoOverlap2DConstraintHelper& helper_;
71 ModelRandomGenerator* random_;
72 SharedStatistics* shared_stats_;
73 OrthogonalPackingInfeasibilityDetector orthogonal_packing_checker_;
74
75 int64_t num_calls_ = 0;
76 int64_t num_conflicts_ = 0;
77 int64_t num_conflicts_two_boxes_ = 0;
78 int64_t num_refined_conflicts_ = 0;
79 int64_t num_conflicts_with_slack_ = 0;
80
85};
86
87// Enforces that the boxes with corners in (x, y), (x + dx, y), (x, y + dy)
88// and (x + dx, y + dy) do not overlap.
90 const std::vector<Literal>& enforcement_literals,
91 const std::vector<IntervalVariable>& x,
92 const std::vector<IntervalVariable>& y, Model* model);
93
94// Non overlapping rectangles. This includes box with zero-areas.
95// The following is forbidden:
96// - a point box inside a box with a non zero area
97// - a line box overlapping a box with a non zero area
98// - one vertical line box crossing an horizontal line box.
100 : public PropagatorInterface {
101 public:
102 // The slow_propagators select which disjunctive algorithms to propagate.
104 NoOverlap2DConstraintHelper* helper, Model* model);
106
107 bool Propagate() final;
108 void Register(int fast_priority, int slow_priority);
109
110 private:
111 bool FindBoxesThatMustOverlapAHorizontalLineAndPropagate(
112 bool fast_propagation, absl::Span<const int> boxes);
113
116
117 GenericLiteralWatcher* watcher_;
118 TimeLimit* time_limit_;
119 int fast_id_; // Propagator id of the "fast" version.
120
121 // Temporary data.
122 std::vector<IndexedInterval> indexed_boxes_;
123 std::vector<Rectangle> rectangles_;
124 std::vector<int> order_;
125 CompactVectorVector<int> events_overlapping_boxes_;
126
127 // List of box that are fully fixed in the current dive, and for which we
128 // know they are no conflict between them.
129 bool rev_is_in_dive_ = false;
130 Bitset64<int> already_checked_fixed_boxes_;
131 int last_helper_inprocessing_count_ = -1;
132
133 absl::flat_hash_set<absl::Span<const int>> reduced_overlapping_boxes_;
134 std::vector<absl::Span<const int>> boxes_to_propagate_;
135 std::vector<absl::Span<const int>> disjoint_boxes_;
136 std::vector<int> non_zero_area_boxes_;
137
138 DisjunctiveOverloadChecker overload_checker_;
139 DisjunctiveDetectablePrecedences forward_detectable_precedences_;
140 DisjunctiveDetectablePrecedences backward_detectable_precedences_;
141 DisjunctiveNotLast forward_not_last_;
142 DisjunctiveNotLast backward_not_last_;
143 DisjunctiveEdgeFinding forward_edge_finding_;
144 DisjunctiveEdgeFinding backward_edge_finding_;
145 DisjunctiveWithTwoItems disjunctive_with_two_items_;
146
151};
152
153// Propagator that compares the boxes pairwise.
155 public:
157 : helper_(helper),
158 shared_stats_(model->GetOrCreate<SharedStatistics>()),
159 params_(model->GetOrCreate<SatParameters>()) {}
160
162
163 bool Propagate() final;
164 int RegisterWith(GenericLiteralWatcher* watcher);
165
166 private:
169 delete;
170
171 // Return false if a conflict is found.
172 bool FindRestrictionsAndPropagateConflict(
173 absl::Span<const ItemWithVariableSize> items1,
174 absl::Span<const ItemWithVariableSize> items2,
175 std::vector<PairwiseRestriction>* restrictions);
176
177 bool PropagateTwoBoxes(const PairwiseRestriction& restriction);
178
180 SharedStatistics* shared_stats_;
181 const SatParameters* params_;
182
183 int64_t num_calls_ = 0;
184 int64_t num_pairwise_conflicts_ = 0;
185 int64_t num_pairwise_propagations_ = 0;
186
187 std::vector<ItemWithVariableSize> fixed_non_zero_area_boxes_;
188 std::vector<ItemWithVariableSize> non_fixed_non_zero_area_boxes_;
189 std::vector<ItemWithVariableSize> horizontal_zero_area_boxes_;
190 std::vector<ItemWithVariableSize> vertical_zero_area_boxes_;
191 std::vector<ItemWithVariableSize> point_zero_area_boxes_;
192};
193
194} // namespace sat
195} // namespace operations_research
196
197#endif // ORTOOLS_SAT_DIFFN_H_
Definition model.h:345
NonOverlappingRectanglesDisjunctivePropagator(NoOverlap2DConstraintHelper *helper, Model *model)
Definition diffn.cc:708
void Register(int fast_priority, int slow_priority)
Definition diffn.cc:726
NonOverlappingRectanglesEnergyPropagator(NoOverlap2DConstraintHelper *helper, Model *model)
Definition diffn.h:44
RectanglePairwisePropagator(NoOverlap2DConstraintHelper *helper, Model *model)
Definition diffn.h:156
void AddNonOverlappingRectangles(const std::vector< Literal > &enforcement_literals, const std::vector< IntervalVariable > &x, const std::vector< IntervalVariable > &y, Model *model)
Definition diffn.cc:199
OR-Tools root namespace.
STL namespace.