Google OR-Tools v9.11
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-2024 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 OR_TOOLS_SAT_DIFFN_H_
15#define OR_TOOLS_SAT_DIFFN_H_
16
17#include <cstdint>
18#include <functional>
19#include <optional>
20#include <vector>
21
22#include "absl/container/flat_hash_set.h"
23#include "absl/types/span.h"
27#include "ortools/sat/integer.h"
29#include "ortools/sat/model.h"
31#include "ortools/sat/sat_parameters.pb.h"
33#include "ortools/sat/util.h"
34
35namespace operations_research {
36namespace sat {
37
38// Propagates using a box energy reasoning.
40 public:
43 Model* model)
44 : x_(*x),
45 y_(*y),
46 random_(model->GetOrCreate<ModelRandomGenerator>()),
47 shared_stats_(model->GetOrCreate<SharedStatistics>()),
48 orthogonal_packing_checker_(*random_, shared_stats_) {}
49
51
52 bool Propagate() final;
54
55 private:
56 struct Conflict {
57 // The Orthogonal Packing subproblem we used.
58 std::vector<RectangleInRange> items_for_opp;
59 Rectangle rectangle_with_too_much_energy;
60 OrthogonalPackingResult opp_result;
61 };
62 std::optional<Conflict> FindConflict(
63 std::vector<RectangleInRange> active_box_ranges);
64
65 std::vector<RectangleInRange> GeneralizeExplanation(const Conflict& conflict);
66
67 bool BuildAndReportEnergyTooLarge(
68 const std::vector<RectangleInRange>& ranges);
69
70 SchedulingConstraintHelper& x_;
71 SchedulingConstraintHelper& y_;
72 ModelRandomGenerator* random_;
73 SharedStatistics* shared_stats_;
74 OrthogonalPackingInfeasibilityDetector orthogonal_packing_checker_;
75
76 int64_t num_calls_ = 0;
77 int64_t num_conflicts_ = 0;
78 int64_t num_conflicts_two_boxes_ = 0;
79 int64_t num_refined_conflicts_ = 0;
80 int64_t num_conflicts_with_slack_ = 0;
81
86};
87
88// Enforces that the boxes with corners in (x, y), (x + dx, y), (x, y + dy)
89// and (x + dx, y + dy) do not overlap.
90void AddNonOverlappingRectangles(const std::vector<IntervalVariable>& x,
91 const std::vector<IntervalVariable>& y,
92 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.
105 Model* model);
107
108 bool Propagate() final;
109 void Register(int fast_priority, int slow_priority);
110
111 private:
112 bool PropagateOnXWhenOnlyTwoBoxes();
113 bool FindBoxesThatMustOverlapAHorizontalLineAndPropagate(
114 bool fast_propagation, const SchedulingConstraintHelper& x,
116
120
121 GenericLiteralWatcher* watcher_;
122 int fast_id_; // Propagator id of the "fast" version.
123
124 std::vector<IndexedInterval> indexed_boxes_;
125 std::vector<std::vector<int>> events_overlapping_boxes_;
126
127 absl::flat_hash_set<absl::Span<int>> reduced_overlapping_boxes_;
128 std::vector<absl::Span<int>> boxes_to_propagate_;
129 std::vector<absl::Span<int>> disjoint_boxes_;
130 std::vector<int> non_zero_area_boxes_;
131
132 DisjunctiveOverloadChecker overload_checker_;
133 DisjunctiveDetectablePrecedences forward_detectable_precedences_;
134 DisjunctiveDetectablePrecedences backward_detectable_precedences_;
135 DisjunctiveNotLast forward_not_last_;
136 DisjunctiveNotLast backward_not_last_;
137 DisjunctiveEdgeFinding forward_edge_finding_;
138 DisjunctiveEdgeFinding backward_edge_finding_;
139
144};
145
146// Propagator that compares the boxes pairwise.
148 public:
151 : global_x_(*x),
152 global_y_(*y),
153 shared_stats_(model->GetOrCreate<SharedStatistics>()),
154 params_(model->GetOrCreate<SatParameters>()) {}
155
157
158 bool Propagate() final;
159 int RegisterWith(GenericLiteralWatcher* watcher);
160
161 private:
164 delete;
165
166 // Return false if a conflict is found.
167 bool FindRestrictionsAndPropagateConflict(
168 const std::vector<ItemForPairwiseRestriction>& items,
169 std::vector<PairwiseRestriction>* restrictions);
170
171 bool FindRestrictionsAndPropagateConflict(
172 const std::vector<ItemForPairwiseRestriction>& items1,
173 const std::vector<ItemForPairwiseRestriction>& items2,
174 std::vector<PairwiseRestriction>* restrictions);
175
176 bool PropagateTwoBoxes(const PairwiseRestriction& restriction);
177
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<ItemForPairwiseRestriction> non_zero_area_boxes_;
188 std::vector<ItemForPairwiseRestriction> horizontal_zero_area_boxes_;
189 std::vector<ItemForPairwiseRestriction> vertical_zero_area_boxes_;
190 std::vector<ItemForPairwiseRestriction> point_zero_area_boxes_;
191};
192
193} // namespace sat
194} // namespace operations_research
195
196#endif // OR_TOOLS_SAT_DIFFN_H_
IntegerValue y
void Register(int fast_priority, int slow_priority)
Definition diffn.cc:679
NonOverlappingRectanglesDisjunctivePropagator(SchedulingConstraintHelper *x, SchedulingConstraintHelper *y, Model *model)
The slow_propagators select which disjunctive algorithms to propagate.
Definition diffn.cc:661
Propagates using a box energy reasoning.
Definition diffn.h:39
NonOverlappingRectanglesEnergyPropagator(SchedulingConstraintHelper *x, SchedulingConstraintHelper *y, Model *model)
Definition diffn.h:41
Base class for CP like propagators.
Definition integer.h:1414
Propagator that compares the boxes pairwise.
Definition diffn.h:147
RectanglePairwisePropagator(SchedulingConstraintHelper *x, SchedulingConstraintHelper *y, Model *model)
Definition diffn.h:149
Simple class to add statistics by name and print them at the end.
GRBmodel * model
void AddNonOverlappingRectangles(const std::vector< IntervalVariable > &x, const std::vector< IntervalVariable > &y, Model *model)
Definition diffn.cc:174
In SWIG mode, we don't want anything besides these top-level includes.
STL namespace.
const Variable x
Definition qp_tests.cc:127