forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path2d_mandatory_overlap_propagator.cc
106 lines (93 loc) · 3.63 KB
/
2d_mandatory_overlap_propagator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/sat/2d_mandatory_overlap_propagator.h"
#include <cstdint>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "absl/types/span.h"
#include "ortools/base/logging.h"
#include "ortools/sat/diffn_util.h"
#include "ortools/sat/integer.h"
#include "ortools/sat/model.h"
#include "ortools/sat/no_overlap_2d_helper.h"
#include "ortools/sat/scheduling_helpers.h"
namespace operations_research {
namespace sat {
int MandatoryOverlapPropagator::RegisterWith(GenericLiteralWatcher* watcher) {
const int id = watcher->Register(this);
helper_.WatchAllBoxes(id);
return id;
}
MandatoryOverlapPropagator::~MandatoryOverlapPropagator() {
if (!VLOG_IS_ON(1)) return;
std::vector<std::pair<std::string, int64_t>> stats;
stats.push_back({"MandatoryOverlapPropagator/called_with_zero_area",
num_calls_zero_area_});
stats.push_back({"MandatoryOverlapPropagator/called_without_zero_area",
num_calls_nonzero_area_});
stats.push_back({"MandatoryOverlapPropagator/conflicts", num_conflicts_});
shared_stats_->AddStats(stats);
}
bool MandatoryOverlapPropagator::Propagate() {
if (!helper_.SynchronizeAndSetDirection(true, true, false)) return false;
mandatory_regions_.clear();
mandatory_regions_index_.clear();
bool has_zero_area_boxes = false;
absl::Span<const TaskTime> tasks =
helper_.x_helper().TaskByIncreasingNegatedStartMax();
for (int i = tasks.size() - 1; i >= 0; --i) {
const int b = tasks[i].task_index;
if (!helper_.IsPresent(b)) continue;
const ItemWithVariableSize item = helper_.GetItemWithVariableSize(b);
if (item.x.start_max > item.x.end_min ||
item.y.start_max > item.y.end_min) {
continue;
}
mandatory_regions_.push_back({.x_min = item.x.start_max,
.x_max = item.x.end_min,
.y_min = item.y.start_max,
.y_max = item.y.end_min});
mandatory_regions_index_.push_back(b);
if (mandatory_regions_.back().SizeX() == 0 ||
mandatory_regions_.back().SizeY() == 0) {
has_zero_area_boxes = true;
}
}
std::optional<std::pair<int, int>> conflict;
if (has_zero_area_boxes) {
num_calls_zero_area_++;
conflict = FindOneIntersectionIfPresentWithZeroArea(mandatory_regions_);
} else {
num_calls_nonzero_area_++;
conflict = FindOneIntersectionIfPresent(mandatory_regions_);
}
if (conflict.has_value()) {
num_conflicts_++;
return helper_.ReportConflictFromTwoBoxes(
mandatory_regions_index_[conflict->first],
mandatory_regions_index_[conflict->second]);
}
return true;
}
void CreateAndRegisterMandatoryOverlapPropagator(
NoOverlap2DConstraintHelper* helper, Model* model,
GenericLiteralWatcher* watcher, int priority) {
MandatoryOverlapPropagator* propagator =
new MandatoryOverlapPropagator(helper, model);
watcher->SetPropagatorPriority(propagator->RegisterWith(watcher), priority);
model->TakeOwnership(propagator);
}
} // namespace sat
} // namespace operations_research