BCESolve
bcegurobicallback.hpp
1// This file is part of the BCESolve library for games of incomplete
2// information
3// Copyright (C) 2022 Benjamin A. Brooks
4//
5// BCESolve free software: you can redistribute it and/or modify it
6// under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// BCESolve is distributed in the hope that it will be useful, but
11// WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see
17// <http://www.gnu.org/licenses/>.
18//
19// Benjamin A. Brooks
20// ben@benjaminbrooks.net
21// Chicago, IL
22
23#include "gurobi_c++.h"
24#include <QObject>
25
26using namespace std;
27
29class BCEGurobiCallback: public QObject, public GRBCallback
30{
31 Q_OBJECT
32
33private:
38
39public:
40
43 cancelFlag(false),isFullOutput(true)
44 {}
45
47 void setFullOutput(bool toggle) {
48 isFullOutput = toggle;
49 }
50
51public slots:
52
55 cancelFlag = true;
56 }
57
58protected:
59
61 void callback () {
62 try {
63 if (where == GRB_CB_BARRIER) {
64 if (cancelFlag) {
65 abort();
66 }
67 }
68 else if (where == GRB_CB_SIMPLEX) {
69 if (cancelFlag) {
70 abort();
71 }
72 }
73 else if (where == GRB_CB_MESSAGE) {
74 if (isFullOutput) {
75 string algorithmOutput = getStringInfo(GRB_CB_MSG_STRING);
76 // Will be redirected to log file by bcelogstream.
77 cout << algorithmOutput << endl;
78 }
79 }
80 }
81 catch (GRBException e) {
82 cout << "======= GRB ERROR =======" << endl;
83 cout << "GRB Error Code: " << e.getErrorCode() << endl;
84 cout << e.getMessage() << endl;
85 }
86 catch (...) {
87 cout << "GRBCallback error, unrelated to GRB error codes." << endl;
88 }
89 }
90
91};
Callback object to communicate with the gurobi solver.
Definition: bcegurobicallback.hpp:30
void setCancelFlagTrue()
Sets cancel flag to 1.
Definition: bcegurobicallback.hpp:54
void setFullOutput(bool toggle)
Controls how much information is printed to the log tab.
Definition: bcegurobicallback.hpp:47
bool isFullOutput
Contains whether solver is currently running Simplex or Barrier.
Definition: bcegurobicallback.hpp:37
bool cancelFlag
Flags if user has cancelled the solve ("true" if cancel has been hit).
Definition: bcegurobicallback.hpp:35
BCEGurobiCallback()
Constructor.
Definition: bcegurobicallback.hpp:42
void callback()
Sends algorithm output to the log file.
Definition: bcegurobicallback.hpp:61