OR-Tools  9.2
gscip_solver_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_GSCIP_SOLVER_CALLBACK_H_
15#define OR_TOOLS_MATH_OPT_SOLVERS_GSCIP_SOLVER_CALLBACK_H_
16
17#include <memory>
18#include <optional>
19
20#include "absl/base/thread_annotations.h"
21#include "absl/status/status.h"
22#include "absl/strings/string_view.h"
23#include "absl/synchronization/mutex.h"
24#include "absl/time/time.h"
25#include "scip/type_scip.h"
26#include "ortools/gscip/gscip.h"
27#include "ortools/math_opt/callback.pb.h"
30
31namespace operations_research {
32namespace math_opt {
33
34// Handler for user callbacks for GScipSolver.
35//
36// It deals with solve interruption when the user request it or when an error
37// occurs during the call of the user callback. Any such error is returned by
38// Flush().
40 public:
41 // Returns a non null handler if needed (there are supported events that we
42 // register to).
43 //
44 // The caller will also have to use MessageHandler() when calling
45 // GScip::Solve() when the result is not nullptr.
46 //
47 // At the end of the solve, Flush() must be called (when everything else
48 // succeeded) to make the final user callback calls and return the first error
49 // that occurred when calling the user callback.
50 static std::unique_ptr<GScipSolverCallbackHandler> RegisterIfNeeded(
51 const CallbackRegistrationProto& callback_registration,
52 SolverInterface::Callback callback, absl::Time solve_start, SCIP* scip);
53
56 delete;
57
58 // Returns the handler to pass to GScip::Solve().
60
61 // Makes any last pending calls and returns the first error that occurred
62 // while calling the user callback. Returns OkStatus if no error has occurred.
63 absl::Status Flush();
64
65 private:
67 absl::Time solve_start, SCIP* scip);
68
69 // Updates message_callback_data_ and makes the necessary calls to the user
70 // callback if necessary. This method has the expected signature for a
71 // GScipMessageHandler.
72 void MessageCallback(GScipMessageType type, absl::string_view message);
73
74 // Makes a call to the user callback, updating the status_ and interrupting
75 // the solve if needed (in case of error or if requested by the user).
76 //
77 // This function will ignores calls when status_ is not ok. It returns the
78 // result of the call of the callback when the call has successfully been made
79 // and the user has not requested the termination of the solve.
80 //
81 // This function will hold the callback_mutex_ while making the call to the
82 // user callback to serialize calls.
83 absl::optional<CallbackResultProto> CallUserCallback(
84 const CallbackDataProto& callback_data)
85 ABSL_LOCKS_EXCLUDED(callback_mutex_);
86
87 // The user callback. Should be called via CallUserCallback().
88 const SolverInterface::Callback callback_;
89
90 // Start time of the solve.
91 const absl::Time solve_start_;
92
93 // The SCIP solver, used for interruptions.
94 SCIP* const scip_;
95
96 // Mutex serializing calls to the user callback and the access to status_.
97 absl::Mutex callback_mutex_;
98
99 // The first error status returned by the user callback.
100 absl::Status status_ ABSL_GUARDED_BY(callback_mutex_);
101
102 // Mutex serializing access to message_callback_data_ and the serialization of
103 // calls to the user callback for CALLBACK_EVENT_MESSAGE events.
104 absl::Mutex message_mutex_;
105
106 // The buffer used to generate the message events.
107 MessageCallbackData message_callback_data_ ABSL_GUARDED_BY(message_mutex_);
108};
109
110} // namespace math_opt
111} // namespace operations_research
112
113#endif // OR_TOOLS_MATH_OPT_SOLVERS_GSCIP_SOLVER_CALLBACK_H_
GScipSolverCallbackHandler & operator=(const GScipSolverCallbackHandler &)=delete
static std::unique_ptr< GScipSolverCallbackHandler > RegisterIfNeeded(const CallbackRegistrationProto &callback_registration, SolverInterface::Callback callback, absl::Time solve_start, SCIP *scip)
GScipSolverCallbackHandler(const GScipSolverCallbackHandler &)=delete
std::function< absl::StatusOr< CallbackResultProto >(const CallbackDataProto &)> Callback
MPCallback * callback
Collection of objects used to extend the Constraint Solver library.
std::function< void(GScipMessageType type, absl::string_view message)> GScipMessageHandler
std::string message
Definition: trace.cc:398