forked from coin-or/Clp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent1.cpp
176 lines (161 loc) · 4.75 KB
/
event1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <cassert>
#include <iomanip>
#include "CoinPragma.hpp"
#include "OsiClpSolverInterface.hpp"
#include "ClpSimplex.hpp"
//#############################################################################
/************************************************************************
This keeps track of iterations using event handler
Coding was thrown together - data should be saved more elagantly
*/
#define SAVE_ITS 3
// Sequence of In variable
int sequenceIn[SAVE_ITS]={-1};
// Direction of In, 1 going up, -1 going down, 0 not a clue
int directionIn[SAVE_ITS]={-1};
// Sequence of Out variable
int sequenceOut[SAVE_ITS]={-1};
// Direction of Out, 1 to upper bound, -1 to lower bound, 0 - superbasic
int directionOut[SAVE_ITS]={-1};
// Pivot Row
int pivot[SAVE_ITS]={-1};
/** This is so user can trap events and do useful stuff.
ClpSimplex model_ is available as well as anything else you care
to pass in
*/
class MyEventHandler : public ClpEventHandler {
public:
/**@name Overrides */
//@{
virtual int event(Event whichEvent);
//@}
/**@name Constructors, destructor etc*/
//@{
/** Default constructor. */
MyEventHandler();
/// Constructor with pointer to model (redundant as setEventHandler does)
MyEventHandler(ClpSimplex *model);
/** Destructor */
virtual ~MyEventHandler();
/** The copy constructor. */
MyEventHandler(const MyEventHandler &rhs);
/// Assignment
MyEventHandler &operator=(const MyEventHandler &rhs);
/// Clone
virtual ClpEventHandler *clone() const;
//@}
protected:
// data goes here
};
//-------------------------------------------------------------------
// Default Constructor
//-------------------------------------------------------------------
MyEventHandler::MyEventHandler()
: ClpEventHandler()
{
}
//-------------------------------------------------------------------
// Copy constructor
//-------------------------------------------------------------------
MyEventHandler::MyEventHandler(const MyEventHandler &rhs)
: ClpEventHandler(rhs)
{
}
// Constructor with pointer to model
MyEventHandler::MyEventHandler(ClpSimplex *model)
: ClpEventHandler(model)
{
}
//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
MyEventHandler::~MyEventHandler()
{
}
//----------------------------------------------------------------
// Assignment operator
//-------------------------------------------------------------------
MyEventHandler &
MyEventHandler::operator=(const MyEventHandler &rhs)
{
if (this != &rhs) {
ClpEventHandler::operator=(rhs);
}
return *this;
}
//-------------------------------------------------------------------
// Clone
//-------------------------------------------------------------------
ClpEventHandler *MyEventHandler::clone() const
{
return new MyEventHandler(*this);
}
int MyEventHandler::event(Event whichEvent)
{
if (whichEvent == endOfIteration) {
// move up
for (int i=SAVE_ITS-2;i>=0;i--) {
sequenceIn[i+1] = sequenceIn[i];
directionIn[i+1] = directionIn[i];
sequenceOut[i+1] = sequenceOut[i];
directionOut[i+1] = directionOut[i];
pivot[i+1] = pivot[i];
}
sequenceIn[0] = model_->sequenceIn();
directionIn[0] = model_->directionIn();
sequenceOut[0] = model_->sequenceOut();
directionOut[0] = model_->directionOut();
pivot[0] = model_->pivotRow();
}
return -1;
}
int main(int argc, const char *argv[])
{
#ifndef OSICLP
// using ClpSimplex
ClpSimplex model;
#else
OsiClpSolverInterface solver1;
#endif
// Read in model using argv[1]
// and assert that it is a clean model
std::string mpsFileName;
if (argc >= 2) {
mpsFileName = argv[1];
#ifndef OSICLP
int numMpsReadErrors = model.readMps(mpsFileName.c_str());
#else
int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
#endif
if (numMpsReadErrors != 0) {
printf("%d errors reading MPS file\n", numMpsReadErrors);
return numMpsReadErrors;
}
} else {
printf("Need mps file\n");
return -1;
}
// allow Clp to track iterations
MyEventHandler clpEventHandler;
#ifndef OSICLP
ClpSimplex * simplex = &model;
#else
// go over to Clp
ClpSimplex * simplex = solver1.getModelPtr();
#endif
simplex->passInEventHandler(&clpEventHandler);
// If tiny problem more output
if (simplex->numberRows()<40)
simplex->setLogLevel(63);
simplex->primal();
/* print last few iterations
then can change basis etc */
printf("last few iterations\n");
int numberIterations = simplex->numberIterations();
for (int i=0;i<SAVE_ITS;i++) {
printf("Iteration %d in %d direction %d out %d direction %d pivotrow %d\n",
numberIterations-i,sequenceIn[i],directionIn[i],
sequenceOut[i],directionOut[i],pivot[i]);
}
return 0;
}