Skip to content

Commit

Permalink
add some data
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Mar 26, 2024
1 parent b834322 commit 79962c7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 73 deletions.
133 changes: 62 additions & 71 deletions ilpy/impl/solvers/ScipEventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,60 @@ typedef void PyObject;
// TODO: narrow these down to the ones we actually need
const char* getEventTypeName(SCIP_EVENTTYPE eventtype) {
switch (eventtype) {
case SCIP_EVENTTYPE_DISABLED: return "SCIP_EVENTTYPE_DISABLED";
case SCIP_EVENTTYPE_VARADDED: return "SCIP_EVENTTYPE_VARADDED";
case SCIP_EVENTTYPE_VARDELETED: return "SCIP_EVENTTYPE_VARDELETED";
case SCIP_EVENTTYPE_VARFIXED: return "SCIP_EVENTTYPE_VARFIXED";
case SCIP_EVENTTYPE_VARUNLOCKED: return "SCIP_EVENTTYPE_VARUNLOCKED";
case SCIP_EVENTTYPE_OBJCHANGED: return "SCIP_EVENTTYPE_OBJCHANGED";
case SCIP_EVENTTYPE_GLBCHANGED: return "SCIP_EVENTTYPE_GLBCHANGED";
case SCIP_EVENTTYPE_GUBCHANGED: return "SCIP_EVENTTYPE_GUBCHANGED";
case SCIP_EVENTTYPE_LBTIGHTENED: return "SCIP_EVENTTYPE_LBTIGHTENED";
case SCIP_EVENTTYPE_LBRELAXED: return "SCIP_EVENTTYPE_LBRELAXED";
case SCIP_EVENTTYPE_UBTIGHTENED: return "SCIP_EVENTTYPE_UBTIGHTENED";
case SCIP_EVENTTYPE_UBRELAXED: return "SCIP_EVENTTYPE_UBRELAXED";
case SCIP_EVENTTYPE_GHOLEADDED: return "SCIP_EVENTTYPE_GHOLEADDED";
case SCIP_EVENTTYPE_GHOLEREMOVED: return "SCIP_EVENTTYPE_GHOLEREMOVED";
case SCIP_EVENTTYPE_LHOLEADDED: return "SCIP_EVENTTYPE_LHOLEADDED";
case SCIP_EVENTTYPE_LHOLEREMOVED: return "SCIP_EVENTTYPE_LHOLEREMOVED";
case SCIP_EVENTTYPE_IMPLADDED: return "SCIP_EVENTTYPE_IMPLADDED";
case SCIP_EVENTTYPE_TYPECHANGED: return "SCIP_EVENTTYPE_TYPECHANGED";
case SCIP_EVENTTYPE_PRESOLVEROUND: return "SCIP_EVENTTYPE_PRESOLVEROUND";
case SCIP_EVENTTYPE_NODEFOCUSED: return "SCIP_EVENTTYPE_NODEFOCUSED";
case SCIP_EVENTTYPE_NODEFEASIBLE: return "SCIP_EVENTTYPE_NODEFEASIBLE";
case SCIP_EVENTTYPE_NODEINFEASIBLE: return "SCIP_EVENTTYPE_NODEINFEASIBLE";
case SCIP_EVENTTYPE_NODEBRANCHED: return "SCIP_EVENTTYPE_NODEBRANCHED";
case SCIP_EVENTTYPE_NODEDELETE: return "SCIP_EVENTTYPE_NODEDELETE";
case SCIP_EVENTTYPE_FIRSTLPSOLVED: return "SCIP_EVENTTYPE_FIRSTLPSOLVED";
case SCIP_EVENTTYPE_LPSOLVED: return "SCIP_EVENTTYPE_LPSOLVED";
case SCIP_EVENTTYPE_POORSOLFOUND: return "SCIP_EVENTTYPE_POORSOLFOUND";
case SCIP_EVENTTYPE_BESTSOLFOUND: return "SCIP_EVENTTYPE_BESTSOLFOUND";
case SCIP_EVENTTYPE_ROWADDEDSEPA: return "SCIP_EVENTTYPE_ROWADDEDSEPA";
case SCIP_EVENTTYPE_ROWDELETEDSEPA: return "SCIP_EVENTTYPE_ROWDELETEDSEPA";
case SCIP_EVENTTYPE_ROWADDEDLP: return "SCIP_EVENTTYPE_ROWADDEDLP";
case SCIP_EVENTTYPE_ROWDELETEDLP: return "SCIP_EVENTTYPE_ROWDELETEDLP";
case SCIP_EVENTTYPE_ROWCOEFCHANGED: return "SCIP_EVENTTYPE_ROWCOEFCHANGED";
case SCIP_EVENTTYPE_ROWCONSTCHANGED: return "SCIP_EVENTTYPE_ROWCONSTCHANGED";
case SCIP_EVENTTYPE_ROWSIDECHANGED: return "SCIP_EVENTTYPE_ROWSIDECHANGED";
case SCIP_EVENTTYPE_SYNC: return "SCIP_EVENTTYPE_SYNC";
case SCIP_EVENTTYPE_GBDCHANGED: return "SCIP_EVENTTYPE_GBDCHANGED";
case SCIP_EVENTTYPE_LBCHANGED: return "SCIP_EVENTTYPE_LBCHANGED";
case SCIP_EVENTTYPE_UBCHANGED: return "SCIP_EVENTTYPE_UBCHANGED";
case SCIP_EVENTTYPE_BOUNDTIGHTENED: return "SCIP_EVENTTYPE_BOUNDTIGHTENED";
case SCIP_EVENTTYPE_BOUNDRELAXED: return "SCIP_EVENTTYPE_BOUNDRELAXED";
case SCIP_EVENTTYPE_BOUNDCHANGED: return "SCIP_EVENTTYPE_BOUNDCHANGED";
case SCIP_EVENTTYPE_GHOLECHANGED: return "SCIP_EVENTTYPE_GHOLECHANGED";
case SCIP_EVENTTYPE_LHOLECHANGED: return "SCIP_EVENTTYPE_LHOLECHANGED";
case SCIP_EVENTTYPE_HOLECHANGED: return "SCIP_EVENTTYPE_HOLECHANGED";
case SCIP_EVENTTYPE_DOMCHANGED: return "SCIP_EVENTTYPE_DOMCHANGED";
case SCIP_EVENTTYPE_VARCHANGED: return "SCIP_EVENTTYPE_VARCHANGED";
case SCIP_EVENTTYPE_VAREVENT: return "SCIP_EVENTTYPE_VAREVENT";
case SCIP_EVENTTYPE_NODESOLVED: return "SCIP_EVENTTYPE_NODESOLVED";
case SCIP_EVENTTYPE_NODEEVENT: return "SCIP_EVENTTYPE_NODEEVENT";
case SCIP_EVENTTYPE_LPEVENT: return "SCIP_EVENTTYPE_LPEVENT";
case SCIP_EVENTTYPE_SOLFOUND: return "SCIP_EVENTTYPE_SOLFOUND";
case SCIP_EVENTTYPE_ROWCHANGED: return "SCIP_EVENTTYPE_ROWCHANGED";
case SCIP_EVENTTYPE_ROWEVENT: return "SCIP_EVENTTYPE_ROWEVENT";
case SCIP_EVENTTYPE_DISABLED: return "DISABLED";

Check warning on line 14 in ilpy/impl/solvers/ScipEventHandler.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/ScipEventHandler.h#L14

Added line #L14 was not covered by tests
case SCIP_EVENTTYPE_VARADDED: return "VARADDED";
case SCIP_EVENTTYPE_VARDELETED: return "VARDELETED";
case SCIP_EVENTTYPE_VARFIXED: return "VARFIXED";
case SCIP_EVENTTYPE_VARUNLOCKED: return "VARUNLOCKED";
case SCIP_EVENTTYPE_OBJCHANGED: return "OBJCHANGED";
case SCIP_EVENTTYPE_GLBCHANGED: return "GLBCHANGED";
case SCIP_EVENTTYPE_GUBCHANGED: return "GUBCHANGED";
case SCIP_EVENTTYPE_LBTIGHTENED: return "LBTIGHTENED";
case SCIP_EVENTTYPE_LBRELAXED: return "LBRELAXED";
case SCIP_EVENTTYPE_UBTIGHTENED: return "UBTIGHTENED";
case SCIP_EVENTTYPE_UBRELAXED: return "UBRELAXED";
case SCIP_EVENTTYPE_GHOLEADDED: return "GHOLEADDED";
case SCIP_EVENTTYPE_GHOLEREMOVED: return "GHOLEREMOVED";
case SCIP_EVENTTYPE_LHOLEADDED: return "LHOLEADDED";
case SCIP_EVENTTYPE_LHOLEREMOVED: return "LHOLEREMOVED";
case SCIP_EVENTTYPE_IMPLADDED: return "IMPLADDED";
case SCIP_EVENTTYPE_TYPECHANGED: return "TYPECHANGED";

Check warning on line 31 in ilpy/impl/solvers/ScipEventHandler.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/ScipEventHandler.h#L16-L31

Added lines #L16 - L31 were not covered by tests
case SCIP_EVENTTYPE_PRESOLVEROUND: return "PRESOLVEROUND";
case SCIP_EVENTTYPE_NODEFOCUSED: return "NODEFOCUSED";
case SCIP_EVENTTYPE_NODEFEASIBLE: return "NODEFEASIBLE";
case SCIP_EVENTTYPE_NODEINFEASIBLE: return "NODEINFEASIBLE";
case SCIP_EVENTTYPE_NODEBRANCHED: return "NODEBRANCHED";
case SCIP_EVENTTYPE_NODEDELETE: return "NODEDELETE";
case SCIP_EVENTTYPE_FIRSTLPSOLVED: return "FIRSTLPSOLVED";
case SCIP_EVENTTYPE_LPSOLVED: return "LPSOLVED";
case SCIP_EVENTTYPE_POORSOLFOUND: return "POORSOLFOUND";
case SCIP_EVENTTYPE_BESTSOLFOUND: return "BESTSOLFOUND";
case SCIP_EVENTTYPE_ROWADDEDSEPA: return "ROWADDEDSEPA";
case SCIP_EVENTTYPE_ROWDELETEDSEPA: return "ROWDELETEDSEPA";
case SCIP_EVENTTYPE_ROWADDEDLP: return "ROWADDEDLP";
case SCIP_EVENTTYPE_ROWDELETEDLP: return "ROWDELETEDLP";
case SCIP_EVENTTYPE_ROWCOEFCHANGED: return "ROWCOEFCHANGED";
case SCIP_EVENTTYPE_ROWCONSTCHANGED: return "ROWCONSTCHANGED";
case SCIP_EVENTTYPE_ROWSIDECHANGED: return "ROWSIDECHANGED";
case SCIP_EVENTTYPE_SYNC: return "SYNC";
case SCIP_EVENTTYPE_GBDCHANGED: return "GBDCHANGED";
case SCIP_EVENTTYPE_LBCHANGED: return "LBCHANGED";
case SCIP_EVENTTYPE_UBCHANGED: return "UBCHANGED";
case SCIP_EVENTTYPE_BOUNDTIGHTENED: return "BOUNDTIGHTENED";
case SCIP_EVENTTYPE_BOUNDRELAXED: return "BOUNDRELAXED";
case SCIP_EVENTTYPE_BOUNDCHANGED: return "BOUNDCHANGED";
case SCIP_EVENTTYPE_GHOLECHANGED: return "GHOLECHANGED";
case SCIP_EVENTTYPE_LHOLECHANGED: return "LHOLECHANGED";
case SCIP_EVENTTYPE_HOLECHANGED: return "HOLECHANGED";
case SCIP_EVENTTYPE_DOMCHANGED: return "DOMCHANGED";
case SCIP_EVENTTYPE_VARCHANGED: return "VARCHANGED";
case SCIP_EVENTTYPE_VAREVENT: return "VAREVENT";
case SCIP_EVENTTYPE_NODESOLVED: return "NODESOLVED";
case SCIP_EVENTTYPE_NODEEVENT: return "NODEEVENT";
case SCIP_EVENTTYPE_LPEVENT: return "LPEVENT";
case SCIP_EVENTTYPE_SOLFOUND: return "SOLFOUND";
case SCIP_EVENTTYPE_ROWCHANGED: return "ROWCHANGED";
case SCIP_EVENTTYPE_ROWEVENT: return "ROWEVENT";

Check warning on line 67 in ilpy/impl/solvers/ScipEventHandler.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/ScipEventHandler.h#L46-L67

Added lines #L46 - L67 were not covered by tests
// Add other cases here
default: return "SCIP_EVENTTYPE_UNKNOWN";

Check warning on line 69 in ilpy/impl/solvers/ScipEventHandler.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/ScipEventHandler.h#L69

Added line #L69 was not covered by tests
}
Expand Down Expand Up @@ -112,23 +112,14 @@ class EventhdlrNewSol : public scip::ObjEventhdlr {
virtual SCIP_DECL_EVENTEXEC(scip_exec) {
std::string eventTypeName = getEventTypeName(SCIPeventGetType(event));

PyObject* callback = _backend->getEventCallback();
if (callback != nullptr) {
if (PyCallable_Check(callback)) {
PyObject* pyEventTypeName = PyUnicode_FromString(eventTypeName.c_str());
PyObject* payload = PyDict_New();
PyDict_SetItemString(payload, "event_type", pyEventTypeName);
PyObject* result = PyObject_CallFunctionObjArgs(callback, payload, nullptr);
if (result == nullptr) {
PyErr_Print();
}
Py_XDECREF(result);
Py_DECREF(payload);
Py_DECREF(pyEventTypeName);
} else {
throw std::runtime_error("Callback is not callable");
}
}
std::map<std::string, std::string> payload;
payload["event_type"] = eventTypeName;
payload["gap"] = std::to_string(SCIPgetGap(scip));
payload["nodes"] = std::to_string(SCIPgetNNodes(scip));
payload["lp_iterations"] = std::to_string(SCIPgetNLPIterations(scip));
payload["dualbound"] = std::to_string(SCIPgetDualbound(scip));
payload["primalbound"] = std::to_string(SCIPgetPrimalbound(scip));
_backend->emitEvent(payload);

return SCIP_OKAY;
}
Expand Down
24 changes: 22 additions & 2 deletions ilpy/impl/solvers/SolverBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,28 @@ class SolverBackend {
* Get the event callback function or nullptr if no callback is set.
*
*/
PyObject* getEventCallback() const {
return _callback;
void emitEvent(const std::map<std::string, std::string>& payload) const {
if (_callback == nullptr) {
return;
}
if (PyCallable_Check(_callback)) {
PyObject* dict = PyDict_New();

Check warning on line 134 in ilpy/impl/solvers/SolverBackend.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/SolverBackend.h#L134

Added line #L134 was not covered by tests
for (const auto& pair : payload) {
PyObject* pyKey = PyUnicode_FromString(pair.first.c_str());
PyObject* pyValue = PyUnicode_FromString(pair.second.c_str());
PyDict_SetItem(dict, pyKey, pyValue);
Py_DECREF(pyKey);
Py_DECREF(pyValue);

Check warning on line 140 in ilpy/impl/solvers/SolverBackend.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/SolverBackend.h#L136-L140

Added lines #L136 - L140 were not covered by tests
}
PyObject* result = PyObject_CallFunctionObjArgs(_callback, dict, nullptr);

Check warning on line 142 in ilpy/impl/solvers/SolverBackend.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/SolverBackend.h#L142

Added line #L142 was not covered by tests
if (result == nullptr) {
PyErr_Print();

Check warning on line 144 in ilpy/impl/solvers/SolverBackend.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/SolverBackend.h#L144

Added line #L144 was not covered by tests
}
Py_XDECREF(result);
Py_DECREF(dict);
} else {

Check warning on line 148 in ilpy/impl/solvers/SolverBackend.h

View check run for this annotation

Codecov / codecov/patch

ilpy/impl/solvers/SolverBackend.h#L146-L148

Added lines #L146 - L148 were not covered by tests
throw std::runtime_error("Callback is not callable");
}
}

/**
Expand Down

0 comments on commit 79962c7

Please sign in to comment.