OR-Tools  9.3
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/container/flat_hash_set.h"
24#include "ortools/math_opt/callback.pb.h"
26#include "ortools/math_opt/result.pb.h"
27#include "ortools/math_opt/sparse_containers.pb.h"
28
29namespace operations_research {
30namespace math_opt {
31
32void 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
67absl::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
79TerminationProto TerminateForLimit(const LimitProto limit, const bool feasible,
80 const absl::string_view detail) {
81 TerminationProto result;
82 if (feasible) {
83 result.set_reason(TERMINATION_REASON_FEASIBLE);
84 } else {
85 result.set_reason(TERMINATION_REASON_NO_SOLUTION_FOUND);
86 }
87 result.set_limit(limit);
88 if (!detail.empty()) {
89 result.set_detail(std::string(detail));
90 }
91 return result;
92}
93
94TerminationProto FeasibleTermination(const LimitProto limit,
95 const absl::string_view detail) {
96 return TerminateForLimit(limit, /*feasible=*/true, detail);
97}
98
99TerminationProto NoSolutionFoundTermination(const LimitProto limit,
100 const absl::string_view detail) {
101 return TerminateForLimit(limit, /*feasible=*/false, detail);
102}
103
104TerminationProto TerminateForReason(const TerminationReasonProto reason,
105 const absl::string_view detail) {
106 TerminationProto result;
107 result.set_reason(reason);
108 if (!detail.empty()) {
109 result.set_detail(std::string(detail));
110 }
111 return result;
112}
113
114} // namespace math_opt
115} // namespace operations_research
#define CHECK(condition)
Definition: base/logging.h:495
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:703
SparseVectorFilterPredicate(const SparseVectorFilterProto &filter)
Block * next
int64_t value
const bool DEBUG_MODE
Definition: macros.h:24
TerminationProto FeasibleTermination(const LimitProto limit, const absl::string_view detail)
TerminationProto TerminateForLimit(const LimitProto limit, const bool feasible, const absl::string_view detail)
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
TerminationProto NoSolutionFoundTermination(const LimitProto limit, const absl::string_view detail)
TerminationProto TerminateForReason(const TerminationReasonProto reason, const absl::string_view detail)
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.