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