OR-Tools  9.0
gscip_event_handler.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 "absl/status/status.h"
17 #include "absl/strings/string_view.h"
18 #include "ortools/base/logging.h"
19 #include "ortools/gscip/gscip.h"
21 #include "scip/def.h"
22 #include "scip/scip.h"
23 #include "scip/scip_event.h"
24 #include "scip/type_event.h"
25 
29 };
30 
31 // SCIP callback implementation
32 
33 static SCIP_DECL_EVENTEXEC(EventExec) {
34  VLOG(3) << "EventExec";
35  CHECK_NE(scip, nullptr);
36  CHECK_NE(eventhdlr, nullptr);
37  CHECK_NE(event, nullptr);
38 
39  SCIP_EVENTHDLRDATA* event_handler_data = SCIPeventhdlrGetData(eventhdlr);
40  CHECK_NE(event_handler_data, nullptr);
41  operations_research::GScipEventHandler* handler = event_handler_data->handler;
43  event_handler_data->gscip, SCIPeventGetType(event)));
44 
45  return SCIP_OKAY;
46 }
47 
48 static SCIP_DECL_EVENTINIT(EventInit) {
49  VLOG(3) << "EventInit";
50  CHECK_NE(scip, nullptr);
51  CHECK_NE(eventhdlr, nullptr);
52 
53  SCIP_EVENTHDLRDATA* event_handler_data = SCIPeventhdlrGetData(eventhdlr);
54  CHECK_NE(event_handler_data, nullptr);
55  operations_research::GScipEventHandler* handler = event_handler_data->handler;
56  for (const SCIP_EVENTTYPE event_type :
58  SCIP_CALL(SCIPcatchEvent(scip, event_type, eventhdlr, nullptr, nullptr));
59  }
60 
61  return SCIP_OKAY;
62 }
63 
64 static SCIP_DECL_EVENTFREE(EventFree) {
65  VLOG(3) << "EventFree";
66  CHECK_NE(scip, nullptr);
67  CHECK_NE(eventhdlr, nullptr);
68 
69  SCIP_EVENTHDLRDATA* event_handler_data = SCIPeventhdlrGetData(eventhdlr);
70  CHECK_NE(event_handler_data, nullptr);
71  delete event_handler_data;
72  SCIPeventhdlrSetData(eventhdlr, nullptr);
73  return SCIP_OKAY;
74 }
75 
76 namespace operations_research {
77 
79  // event_handler_data is freed in EventFree.
80  SCIP_EVENTHDLRDATA* event_handler_data = new SCIP_EVENTHDLRDATA;
81  event_handler_data->gscip = scip;
82  event_handler_data->handler = handler;
83  SCIP_EVENTHDLR* event_handler = nullptr;
84  CHECK_OK(SCIP_TO_STATUS(SCIPincludeEventhdlrBasic(
85  scip->scip(), &event_handler, handler->description().name.c_str(),
86  handler->description().description.c_str(), EventExec,
87  event_handler_data)));
88  CHECK_NE(event_handler, nullptr);
90  SCIPsetEventhdlrInit(scip->scip(), event_handler, EventInit)));
92  SCIPsetEventhdlrFree(scip->scip(), event_handler, EventFree)));
93 }
94 
95 } // namespace operations_research
#define CHECK_OK(x)
Definition: base/logging.h:42
#define CHECK_NE(val1, val2)
Definition: base/logging.h:706
#define VLOG(verboselevel)
Definition: base/logging.h:986
const GScipEventHandlerDescription & description() const
virtual void Execute(GScipEventHandlerContext context)=0
static SCIP_DECL_EVENTFREE(EventFree)
static SCIP_DECL_EVENTEXEC(EventExec)
static SCIP_DECL_EVENTINIT(EventInit)
Collection of objects used to extend the Constraint Solver library.
void RegisterGScipEventHandler(GScip *scip, GScipEventHandler *handler)
#define SCIP_TO_STATUS(x)
operations_research::GScip * gscip
operations_research::GScipEventHandler * handler
std::vector< SCIP_EVENTTYPE > events_to_catch_from_start