OR-Tools  9.6
gurobi_callback.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 #ifndef OR_TOOLS_MATH_OPT_SOLVERS_GUROBI_CALLBACK_H_
15 #define OR_TOOLS_MATH_OPT_SOLVERS_GUROBI_CALLBACK_H_
16 
17 #include <cstdint>
18 #include <vector>
19 
20 #include "absl/container/flat_hash_set.h"
21 #include "absl/status/status.h"
22 #include "absl/time/time.h"
25 #include "ortools/math_opt/callback.pb.h"
30 #include "ortools/math_opt/sparse_containers.pb.h"
31 
32 namespace operations_research {
33 namespace math_opt {
34 
39  int num_gurobi_vars = 0;
40  // events[i] indicates if we should run user_cb when Gurobi's callback is
41  // triggered with where=i. See:
42  // https://www.gurobi.com/documentation/9.1/refman/cb_codes.html
43  // For list of possible values for where. See also EventToGurobiWhere() below.
44  std::vector<bool> events;
45  const SparseVectorFilterProto& mip_solution_filter;
46  const SparseVectorFilterProto& mip_node_filter;
47  const absl::Time start;
48 };
49 
50 // Converts a set of CallbackEventProto enums to a bit vector indicating which
51 // Gurobi callback events need to run our callback.
52 //
53 // The returned vector will have an entry for each possible value of "where"
54 // that Gurobi's callbacks can stop at, see the table here:
55 // https://www.gurobi.com/documentation/9.1/refman/cb_codes.html
56 std::vector<bool> EventToGurobiWhere(
57  const absl::flat_hash_set<CallbackEventProto>& events);
58 
60  const GurobiCallbackInput& callback_input,
61  MessageCallbackData& message_callback_data,
62  SolveInterrupter* local_interrupter);
63 
64 // Makes the final calls to the message callback with any unfinished line if
65 // necessary. It must be called once at the end of the solve, even when the
66 // solve or one callback failed (in case the last unfinished line contains some
67 // details about that).
68 void GurobiCallbackImplFlush(const GurobiCallbackInput& callback_input,
69  MessageCallbackData& message_callback_data);
70 
71 } // namespace math_opt
72 } // namespace operations_research
73 #endif // OR_TOOLS_MATH_OPT_SOLVERS_GUROBI_CALLBACK_H_
std::function< void(const std::vector< std::string > &)> MessageCallback
std::function< absl::StatusOr< CallbackResultProto >(const CallbackDataProto &)> Callback
GurobiMPCallbackContext * context
void GurobiCallbackImplFlush(const GurobiCallbackInput &callback_input, MessageCallbackData &message_callback_data)
absl::Status GurobiCallbackImpl(const Gurobi::CallbackContext &context, const GurobiCallbackInput &callback_input, MessageCallbackData &message_callback_data, SolveInterrupter *const local_interrupter)
std::vector< bool > EventToGurobiWhere(const absl::flat_hash_set< CallbackEventProto > &events)
Collection of objects used to extend the Constraint Solver library.
const SparseVectorFilterProto & mip_node_filter
SolverInterface::MessageCallback message_cb
const SparseVectorFilterProto & mip_solution_filter
const gtl::linked_hash_map< int64_t, int > & variable_ids