OR-Tools  9.0
math_opt_proto_utils.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 
15 
16 #include <stdint.h>
17 
18 #include <algorithm>
19 #include <functional>
20 
21 #include "absl/base/log_severity.h"
22 #include "absl/container/flat_hash_set.h"
24 #include "ortools/base/logging.h"
25 #include "ortools/math_opt/callback.pb.h"
26 #include "ortools/math_opt/sparse_containers.pb.h"
28 
29 namespace operations_research {
30 namespace math_opt {
31 
32 void RemoveSparseDoubleVectorZeros(SparseDoubleVectorProto& sparse_vector) {
33  CHECK_EQ(sparse_vector.ids_size(), sparse_vector.values_size());
34  // Keep track of the next index that has not yet been used for a non zero
35  // value.
36  int next = 0;
37  for (const auto [id, value] : MakeView(sparse_vector)) {
38  // Se use `!(== 0.0)` here so that we keep NaN values for which both `v ==
39  // 0` and `v != 0` returns false.
40  if (!(value == 0.0)) {
41  sparse_vector.set_ids(next, id);
42  sparse_vector.set_values(next, value);
43  ++next;
44  }
45  }
46  // At the end of the iteration, `next` contains the index of the first unused
47  // index. This means it contains the number of used elements.
48  sparse_vector.mutable_ids()->Truncate(next);
49  sparse_vector.mutable_values()->Truncate(next);
50 }
51 
53  const SparseVectorFilterProto& filter)
54  : filter_(filter) {
55  // We only do this test in non-optimized builds.
56  if (DEBUG_MODE && filter_.filter_by_ids()) {
57  // Checks that input filtered_ids are strictly increasing.
58  // That is: for all i, ids(i) < ids(i+1).
59  // Hence here we fail if there exists i such that ids(i) >= ids(i+1).
60  const auto& ids = filter_.filtered_ids();
61  CHECK(std::adjacent_find(ids.begin(), ids.end(),
62  std::greater_equal<int64_t>()) == ids.end())
63  << "The input filter.filtered_ids must be strictly increasing.";
64  }
65 }
66 
67 absl::flat_hash_set<CallbackEventProto> EventSet(
68  const CallbackRegistrationProto& callback_registration) {
69  // Here we don't use for-range loop since for repeated enum fields, the type
70  // used in C++ is RepeatedField<int>. Using the generated getter instead
71  // guarantees type safety.
72  absl::flat_hash_set<CallbackEventProto> events;
73  for (int i = 0; i < callback_registration.request_registration_size(); ++i) {
74  events.emplace(callback_registration.request_registration(i));
75  }
76  return events;
77 }
78 
79 } // namespace math_opt
80 } // namespace operations_research
#define CHECK(condition)
Definition: base/logging.h:498
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:705
SparseVectorFilterPredicate(const SparseVectorFilterProto &filter)
Block * next
int64_t value
const bool DEBUG_MODE
Definition: macros.h:24
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
absl::flat_hash_set< CallbackEventProto > EventSet(const CallbackRegistrationProto &callback_registration)
void RemoveSparseDoubleVectorZeros(SparseDoubleVectorProto &sparse_vector)
Collection of objects used to extend the Constraint Solver library.