OR-Tools  9.1
gurobi_callback.h
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
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"
24#include "ortools/math_opt/callback.pb.h"
27#include "ortools/math_opt/sparse_containers.pb.h"
28
30
31namespace operations_research {
32namespace math_opt {
33
38 // events[i] indicates if we should run user_cb when Gurobi's callback is
39 // triggered with where=i. See:
40 // https://www.gurobi.com/documentation/9.1/refman/cb_codes.html
41 // For list of possible values for where. See also EventToGurobiWhere() below.
42 std::vector<bool> events;
43 const SparseVectorFilterProto& mip_solution_filter;
44 const SparseVectorFilterProto& mip_node_filter;
45 const absl::Time start;
46};
47
48// Converts a set of CallbackEventProto enums to a bit vector indicating which
49// Gurobi callback events need to run our callback.
50//
51// The returned vector will have an entry for each possible value of "where"
52// that Gurobi's callbacks can stop at, see the table here:
53// https://www.gurobi.com/documentation/9.1/refman/cb_codes.html
54std::vector<bool> EventToGurobiWhere(
55 const absl::flat_hash_set<CallbackEventProto>& events);
56
57absl::Status GurobiCallbackImpl(GRBmodel* grb_model, void* cbdata, int where,
58 const GurobiCallbackInput& callback_input,
59 MessageCallbackData& message_callback_data);
60
61// Makes the final calls to the user callback with any buffered event if
62// necessary. It must be called once at the end of the solve, and only if all
63// previous callbacks succeeded (and the solve succeeded).
64absl::Status GurobiCallbackImplFlush(
65 const GurobiCallbackInput& callback_input,
66 MessageCallbackData& message_callback_data);
67
68} // namespace math_opt
69} // namespace operations_research
70#endif // OR_TOOLS_MATH_OPT_SOLVERS_GUROBI_CALLBACK_H_
std::function< absl::StatusOr< CallbackResultProto >(const CallbackDataProto &)> Callback
struct _GRBmodel GRBmodel
Definition: environment.h:24
int where
std::vector< bool > EventToGurobiWhere(const absl::flat_hash_set< CallbackEventProto > &events)
absl::Status GurobiCallbackImpl(GRBmodel *grb_model, void *cbdata, int where, const GurobiCallbackInput &callback_input, MessageCallbackData &message_callback_data)
absl::Status GurobiCallbackImplFlush(const GurobiCallbackInput &callback_input, MessageCallbackData &message_callback_data)
Collection of objects used to extend the Constraint Solver library.
const SparseVectorFilterProto & mip_node_filter
const SparseVectorFilterProto & mip_solution_filter
const gtl::linked_hash_map< int64_t, int > & variable_ids