Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
callback.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <algorithm>
17#include <optional>
18#include <utility>
19#include <vector>
20
21#include "absl/container/flat_hash_set.h"
22#include "absl/status/status.h"
23#include "absl/status/statusor.h"
24#include "absl/strings/string_view.h"
25#include "absl/time/time.h"
26#include "absl/types/span.h"
30#include "ortools/math_opt/callback.pb.h"
35#include "ortools/math_opt/sparse_containers.pb.h"
37
38namespace operations_research {
39namespace math_opt {
40
41std::optional<absl::string_view> Enum<CallbackEvent>::ToOptString(
43 switch (value) {
45 return "presolve";
47 return "simplex";
49 return "mip";
51 return "mip_solution";
53 return "mip_node";
55 return "barrier";
56 }
57 return std::nullopt;
58}
59
60absl::Span<const CallbackEvent> Enum<CallbackEvent>::AllValues() {
61 static constexpr CallbackEvent kCallbackEventValues[] = {
65 };
66 return absl::MakeConstSpan(kCallbackEventValues);
67}
68
70 const absl::Duration runtime)
71 : event(event), runtime(runtime) {}
72
73CallbackData::CallbackData(const ModelStorage* storage,
74 const CallbackDataProto& proto)
75 // iOS 11 does not support .value() hence we use operator* here and CHECK
76 // below that we have a value.
77 : event(*EnumFromProto(proto.event())),
78 presolve_stats(proto.presolve_stats()),
79 simplex_stats(proto.simplex_stats()),
80 barrier_stats(proto.barrier_stats()),
81 mip_stats(proto.mip_stats()) {
82 CHECK(EnumFromProto(proto.event()).has_value());
83 if (proto.has_primal_solution_vector()) {
84 solution = VariableValuesFromProto(storage, proto.primal_solution_vector())
85 .value();
86 }
87 auto maybe_time = util_time::DecodeGoogleApiProto(proto.runtime());
88 CHECK_OK(maybe_time.status());
89 runtime = *maybe_time;
90}
91
93 const ModelStorage* const expected_storage) const {
94 RETURN_IF_ERROR(mip_node_filter.CheckModelStorage(expected_storage))
95 << "invalid mip_node_filter";
96 RETURN_IF_ERROR(mip_solution_filter.CheckModelStorage(expected_storage))
97 << "invalid mip_solution_filter";
98 return absl::OkStatus();
99}
100
101CallbackRegistrationProto CallbackRegistration::Proto() const {
102 CallbackRegistrationProto result;
103 for (const CallbackEvent event : events) {
104 result.add_request_registration(EnumToProto(event));
105 }
106 std::sort(result.mutable_request_registration()->begin(),
107 result.mutable_request_registration()->end());
108 *result.mutable_mip_solution_filter() = mip_solution_filter.Proto();
109 *result.mutable_mip_node_filter() = mip_node_filter.Proto();
110 result.set_add_lazy_constraints(add_lazy_constraints);
111 result.set_add_cuts(add_cuts);
112 return result;
113}
114
116 const ModelStorage* const expected_storage) const {
117 for (const GeneratedLinearConstraint& constraint : new_constraints) {
119 internal::CheckModelStorage(/*storage=*/constraint.storage(),
120 /*expected_storage=*/expected_storage))
121 << "invalid new_constraints";
122 }
124 for (const auto& [v, _] : solution) {
126 /*storage=*/v.storage(), /*expected_storage=*/expected_storage))
127 << "invalid variable " << v << " in suggested_solutions";
128 }
129 }
130 return absl::OkStatus();
131}
132
133CallbackResultProto CallbackResult::Proto() const {
134 CallbackResultProto result;
135 result.set_terminate(terminate);
137 *result.add_suggested_solutions() = VariableValuesToProto(solution);
138 }
139 for (const GeneratedLinearConstraint& constraint : new_constraints) {
140 CallbackResultProto::GeneratedLinearConstraint* constraint_proto =
141 result.add_cuts();
142 constraint_proto->set_is_lazy(constraint.is_lazy);
143 constraint_proto->set_lower_bound(
144 constraint.linear_constraint.lower_bound_minus_offset());
145 constraint_proto->set_upper_bound(
146 constraint.linear_constraint.upper_bound_minus_offset());
147 *constraint_proto->mutable_linear_expression() =
148 VariableValuesToProto(constraint.linear_constraint.expression.terms());
149 }
150 return result;
151}
152
153} // namespace math_opt
154} // namespace operations_research
#define RETURN_IF_ERROR(expr)
CpModelProto proto
The output proto.
int64_t value
double solution
absl::Status CheckModelStorage(const ModelStorage *const storage, const ModelStorage *const expected_storage)
Definition key_types.h:168
absl::flat_hash_map< Variable, V > VariableMap
std::optional< typename EnumProto< P >::Cpp > EnumFromProto(P proto_value)
Definition enums.h:281
absl::StatusOr< VariableMap< double > > VariableValuesFromProto(const ModelStorage *const model, const SparseDoubleVectorProto &vars_proto)
CallbackEvent
The supported events during a solve for callbacks.
Definition callback.h:96
SparseDoubleVectorProto VariableValuesToProto(const VariableMap< double > &variable_values)
Returns the proto equivalent of variable_values.
Enum< E >::Proto EnumToProto(std::optional< E > value)
Definition enums.h:270
In SWIG mode, we don't want anything besides these top-level includes.
inline ::absl::StatusOr< absl::Duration > DecodeGoogleApiProto(const google::protobuf::Duration &proto)
Definition protoutil.h:42
absl::Duration runtime
Time since Solve() was called. Available for all events.
Definition callback.h:208
std::optional< VariableMap< double > > solution
Definition callback.h:205
CallbackData(CallbackEvent event, absl::Duration runtime)
Users will typically not need this function other than for testing.
Definition callback.cc:69
absl::flat_hash_set< CallbackEvent > events
Definition callback.h:162
absl::Status CheckModelStorage(const ModelStorage *expected_storage) const
Definition callback.cc:92
CallbackRegistrationProto Proto() const
Definition callback.cc:101
absl::Status CheckModelStorage(const ModelStorage *expected_storage) const
Definition callback.cc:115
std::vector< GeneratedLinearConstraint > new_constraints
Definition callback.h:265
bool terminate
Stop the solve process and return early. Can be called from any event.
Definition callback.h:261
std::vector< VariableMap< double > > suggested_solutions
A solution or partially defined solution to give to the solver.
Definition callback.h:268
static absl::Span< const E > AllValues()
Returns all possible values of the enum.
static std::optional< absl::string_view > ToOptString(E value)