Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
2d_try_edge_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 OR_TOOLS_SAT_2D_TRY_EDGE_PROPAGATOR_H_
15#define OR_TOOLS_SAT_2D_TRY_EDGE_PROPAGATOR_H_
16
17#include <cstdint>
18#include <optional>
19#include <utility>
20#include <vector>
21
23#include "ortools/sat/integer.h"
25#include "ortools/sat/model.h"
28#include "ortools/sat/util.h"
29#include "ortools/util/bitset.h"
30
31namespace operations_research {
32namespace sat {
33
34// Propagator that for each boxes participating in a no_overlap_2d constraint
35// try to find the leftmost valid position that is compatible with all the
36// other boxes. If none is found, it will propagate a conflict. Otherwise, if
37// it is different from the current x_min, it will propagate the new x_min.
39 Model* model,
40 GenericLiteralWatcher* watcher,
41 int priority);
42
43// Exposed for testing.
45 public:
46 TryEdgeRectanglePropagator(bool x_is_forward_after_swap,
47 bool y_is_forward_after_swap, bool swap_x_and_y,
48 NoOverlap2DConstraintHelper* helper, Model* model)
49 : helper_(*helper),
50 shared_stats_(model->GetOrCreate<SharedStatistics>()),
51 x_is_forward_after_swap_(x_is_forward_after_swap),
52 y_is_forward_after_swap_(y_is_forward_after_swap),
53 swap_x_and_y_(swap_x_and_y) {}
54
56
57 bool Propagate() final;
59
60 protected:
61 // placed_boxes_ is a list that is only meaningful for indices for which
62 // is_in_cache_[box_index] is true. After applying this condition,
63 // placed_boxes_ contains a list of boxes placed at their current x_min and
64 // that does not overlap with the mandatory region of any other box in
65 // placed_boxes_. In other words, there is no point on looking for any
66 // propagation for this heuristic between boxes that are already in
67 // placed_boxes_.
69 std::vector<bool> is_in_cache_;
70
73 std::vector<bool> is_active_;
75
76 std::vector<int> changed_item_;
77 std::vector<int> changed_mandatory_;
78
79 virtual bool ExplainAndPropagate(
80 const std::vector<std::pair<int, std::optional<IntegerValue>>>&
81 found_propagations);
82
83 // This function assumes that a propagation is found and the box with index
84 // `box_index` cannot be placed to the left of new_x_min. It returns a list of
85 // indices of boxes that defines a subproblem where the propagation is still
86 // valid, including `box_index` itself.
87 std::vector<int> GetMinimumProblemWithPropagation(int box_index,
88 IntegerValue new_x_min);
89
90 private:
91 void PopulateActiveBoxRanges();
92
94 SharedStatistics* shared_stats_;
95 bool x_is_forward_after_swap_;
96 bool y_is_forward_after_swap_;
97 bool swap_x_and_y_;
98 std::vector<IntegerValue> cached_y_hint_;
99
100 std::vector<IntegerValue> potential_x_positions_;
101 std::vector<IntegerValue> potential_y_positions_;
102
103 int64_t num_conflicts_ = 0;
104 int64_t num_propagations_ = 0;
105 int64_t num_calls_ = 0;
106
107 CompactVectorVector<int> conflicts_per_x_and_y_;
108 bool CanPlace(int box_index,
109 const std::pair<IntegerValue, IntegerValue>& position,
110 CompactVectorVector<int>* with_conflicts = nullptr) const;
111
114 delete;
115};
116
117} // namespace sat
118} // namespace operations_research
119
120#endif // OR_TOOLS_SAT_2D_TRY_EDGE_PROPAGATOR_H_
Simple class to add statistics by name and print them at the end.
std::vector< int > GetMinimumProblemWithPropagation(int box_index, IntegerValue new_x_min)
TryEdgeRectanglePropagator(bool x_is_forward_after_swap, bool y_is_forward_after_swap, bool swap_x_and_y, NoOverlap2DConstraintHelper *helper, Model *model)
virtual bool ExplainAndPropagate(const std::vector< std::pair< int, std::optional< IntegerValue > > > &found_propagations)
void CreateAndRegisterTryEdgePropagator(NoOverlap2DConstraintHelper *helper, Model *model, GenericLiteralWatcher *watcher, int priority)
In SWIG mode, we don't want anything besides these top-level includes.
STL namespace.