Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
2d_distances_propagator.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_2D_DISTANCES_PROPAGATOR_H_
15#define ORTOOLS_SAT_2D_DISTANCES_PROPAGATOR_H_
16
17#include <cstdint>
18#include <utility>
19#include <variant>
20#include <vector>
21
22#include "absl/container/fixed_array.h"
23#include "absl/container/flat_hash_map.h"
24#include "ortools/sat/integer.h"
26#include "ortools/sat/model.h"
31
32namespace operations_research {
33namespace sat {
34
35// This class implements a propagator for non_overlap_2d constraints that uses
36// the Linear2Bounds to detect precedences between pairs of boxes and
37// detect a conflict if the precedences implies an overlap between the two
38// boxes. For doing this efficiently, it keeps track of pairs of boxes that have
39// non-fixed precedences in the Linear2Bounds and only check those in the
40// propagation.
42 public:
44
45 ~Precedences2DPropagator() override;
46
47 bool Propagate() final;
49
50 private:
51 void CollectNewPairsOfBoxesWithNonTrivialDistance();
52 void UpdateVarLookups();
53 IntegerValue UpperBound(
54 std::variant<LinearExpression2, LinearExpression2Index> linear2) const;
55 void AddOrUpdateDataForPairOfBoxes(int box1, int box2);
56
57 struct PairData {
58 // The condition must be true if ub(linear2) < ub.
59 struct Condition {
60 // If the expression is in the Linear2Indices it is represented by its
61 // index, otherwise it is represented by the expression itself.
62 std::variant<LinearExpression2, LinearExpression2Index> linear2;
63 IntegerValue ub;
64 };
65
66 absl::FixedArray<Literal, 4> pair_presence_literals;
67 int box1;
68 int box2;
69 // start1_before_end2[0==x, 1==y][0=start_1_end_2, 1=start_2_end_1]
70 Condition start_before_end[2][2];
71 };
72 absl::flat_hash_map<std::pair<int, int>, int> non_trivial_pairs_index_;
73 std::vector<PairData> pair_data_;
74 struct VarUsage {
75 // boxes[0=x, 1=y][0=start, 1=end]
76 std::vector<int> boxes[2][2];
77 };
78
79 absl::flat_hash_map<IntegerVariable, VarUsage> var_to_box_and_coeffs_;
80
82 Linear2Bounds* linear2_bounds_;
83 Linear2Watcher* linear2_watcher_;
84 SharedStatistics* shared_stats_;
85 Linear2Indices* lin2_indices_;
86 Trail* trail_;
87 IntegerTrail* integer_trail_;
88
89 int last_helper_inprocessing_count_ = -1;
90 int num_known_linear2_ = 0;
91 int64_t last_linear2_timestamp_ = -1;
92
93 int64_t num_conflicts_ = 0;
94 int64_t num_calls_ = 0;
95
97 Precedences2DPropagator& operator=(const Precedences2DPropagator&) = delete;
98};
99
100} // namespace sat
101} // namespace operations_research
102
103#endif // ORTOOLS_SAT_2D_DISTANCES_PROPAGATOR_H_
Precedences2DPropagator(NoOverlap2DConstraintHelper *helper, Model *model)
OR-Tools root namespace.
std::variant< LinearExpression2, LinearExpression2Index > linear2