OR-Tools  9.1
sat/util.cc
Go to the documentation of this file.
1 // Copyright 2010-2021 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 #include "ortools/sat/util.h"
15 
16 #include <algorithm>
17 #include <cmath>
18 #include <cstdint>
19 
20 #include "ortools/base/stl_util.h"
21 
22 namespace operations_research {
23 namespace sat {
24 
25 int MoveOneUnprocessedLiteralLast(const std::set<LiteralIndex>& processed,
26  int relevant_prefix_size,
27  std::vector<Literal>* literals) {
28  if (literals->empty()) return -1;
29  if (!gtl::ContainsKey(processed, literals->back().Index())) {
30  return std::min<int>(relevant_prefix_size, literals->size());
31  }
32 
33  // To get O(n log n) size of suffixes, we will first process the last n/2
34  // literals, we then move all of them first and process the n/2 literals left.
35  // We use the same algorithm recursively. The sum of the suffixes' size S(n)
36  // is thus S(n/2) + n + S(n/2). That gives us the correct complexity. The code
37  // below simulates one step of this algorithm and is made to be "robust" when
38  // from one call to the next, some literals have been removed (but the order
39  // of literals is preserved).
40  int num_processed = 0;
41  int num_not_processed = 0;
42  int target_prefix_size = literals->size() - 1;
43  for (int i = literals->size() - 1; i >= 0; i--) {
44  if (gtl::ContainsKey(processed, (*literals)[i].Index())) {
45  ++num_processed;
46  } else {
47  ++num_not_processed;
48  target_prefix_size = i;
49  }
50  if (num_not_processed >= num_processed) break;
51  }
52  if (num_not_processed == 0) return -1;
53  target_prefix_size = std::min(target_prefix_size, relevant_prefix_size);
54 
55  // Once a prefix size has been decided, it is always better to
56  // enqueue the literal already processed first.
57  std::stable_partition(literals->begin() + target_prefix_size, literals->end(),
58  [&processed](Literal l) {
59  return gtl::ContainsKey(processed, l.Index());
60  });
61  return target_prefix_size;
62 }
63 
64 void IncrementalAverage::Reset(double reset_value) {
65  num_records_ = 0;
66  average_ = reset_value;
67 }
68 
69 void IncrementalAverage::AddData(double new_record) {
70  num_records_++;
71  average_ += (new_record - average_) / num_records_;
72 }
73 
74 void ExponentialMovingAverage::AddData(double new_record) {
75  num_records_++;
76  average_ = (num_records_ == 1)
77  ? new_record
78  : (new_record + decaying_factor_ * (average_ - new_record));
79 }
80 
81 void Percentile::AddRecord(double record) {
82  records_.push_front(record);
83  if (records_.size() > record_limit_) {
84  records_.pop_back();
85  }
86 }
87 
88 double Percentile::GetPercentile(double percent) {
89  CHECK_GT(records_.size(), 0);
90  CHECK_LE(percent, 100.0);
91  CHECK_GE(percent, 0.0);
92  std::vector<double> sorted_records(records_.begin(), records_.end());
93  std::sort(sorted_records.begin(), sorted_records.end());
94  const int num_records = sorted_records.size();
95 
96  const double percentile_rank =
97  static_cast<double>(num_records) * percent / 100.0 - 0.5;
98  if (percentile_rank <= 0) {
99  return sorted_records.front();
100  } else if (percentile_rank >= num_records - 1) {
101  return sorted_records.back();
102  }
103  // Interpolate.
104  DCHECK_GE(num_records, 2);
105  DCHECK_LT(percentile_rank, num_records - 1);
106  const int lower_rank = static_cast<int>(std::floor(percentile_rank));
107  DCHECK_LT(lower_rank, num_records - 1);
108  return sorted_records[lower_rank] +
109  (percentile_rank - lower_rank) *
110  (sorted_records[lower_rank + 1] - sorted_records[lower_rank]);
111 }
112 
113 void CompressTuples(absl::Span<const int64_t> domain_sizes, int64_t any_value,
114  std::vector<std::vector<int64_t>>* tuples) {
115  if (tuples->empty()) return;
116 
117  // Remove duplicates if any.
119 
120  const int num_vars = (*tuples)[0].size();
121 
122  std::vector<int> to_remove;
123  std::vector<int64_t> tuple_minus_var_i(num_vars - 1);
124  for (int i = 0; i < num_vars; ++i) {
125  const int domain_size = domain_sizes[i];
126  if (domain_size == 1) continue;
127  absl::flat_hash_map<const std::vector<int64_t>, std::vector<int>>
128  masked_tuples_to_indices;
129  for (int t = 0; t < tuples->size(); ++t) {
130  int out = 0;
131  for (int j = 0; j < num_vars; ++j) {
132  if (i == j) continue;
133  tuple_minus_var_i[out++] = (*tuples)[t][j];
134  }
135  masked_tuples_to_indices[tuple_minus_var_i].push_back(t);
136  }
137  to_remove.clear();
138  for (const auto& it : masked_tuples_to_indices) {
139  if (it.second.size() != domain_size) continue;
140  (*tuples)[it.second.front()][i] = any_value;
141  to_remove.insert(to_remove.end(), it.second.begin() + 1, it.second.end());
142  }
143  std::sort(to_remove.begin(), to_remove.end(), std::greater<int>());
144  for (const int t : to_remove) {
145  (*tuples)[t] = tuples->back();
146  tuples->pop_back();
147  }
148  }
149 }
150 
151 } // namespace sat
152 } // namespace operations_research
int64_t min
Definition: alldiff_cst.cc:139
#define CHECK_GE(val1, val2)
Definition: base/logging.h:702
void AddRecord(double record)
Definition: sat/util.cc:81
#define CHECK_GT(val1, val2)
Definition: base/logging.h:703
void STLSortAndRemoveDuplicates(T *v, const LessFunc &less_func)
Definition: stl_util.h:58
#define CHECK_LE(val1, val2)
Definition: base/logging.h:700
bool ContainsKey(const Collection &collection, const Key &key)
Definition: map_util.h:200
#define DCHECK_GE(val1, val2)
Definition: base/logging.h:890
Collection of objects used to extend the Constraint Solver library.
void CompressTuples(absl::Span< const int64_t > domain_sizes, int64_t any_value, std::vector< std::vector< int64_t >> *tuples)
Definition: sat/util.cc:113
int MoveOneUnprocessedLiteralLast(const std::set< LiteralIndex > &processed, int relevant_prefix_size, std::vector< Literal > *literals)
Definition: sat/util.cc:25
double GetPercentile(double percent)
Definition: sat/util.cc:88
#define DCHECK_LT(val1, val2)
Definition: base/logging.h:889