-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathqmip2.cpp
242 lines (208 loc) · 6.32 KB
/
qmip2.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (C) 2006, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
#include <cassert>
#include "CoinPragma.hpp"
// For Branch and bound
#include "OsiClpSolverInterface.hpp"
#include "CbcModel.hpp"
#include "CbcCutGenerator.hpp"
#include "CoinHelperFunctions.hpp"
#include "CbcStrategy.hpp"
// Need stored cuts
#include "CglStored.hpp"
// For saying about solution validity
#include "OsiAuxInfo.hpp"
// Time
#include "CoinTime.hpp"
// Class to disallow strong branching solutions
#include "CbcFeasibilityBase.hpp"
class CbcFeasibilityNoStrong : public CbcFeasibilityBase {
public:
// Default Constructor
CbcFeasibilityNoStrong() {}
virtual ~CbcFeasibilityNoStrong() {}
// Copy constructor
CbcFeasibilityNoStrong(const CbcFeasibilityNoStrong &rhs) {}
// Assignment operator
CbcFeasibilityNoStrong &operator=(const CbcFeasibilityNoStrong &rhs)
{
return *this;
}
/// Clone
virtual CbcFeasibilityBase *clone() const
{
return new CbcFeasibilityNoStrong();
}
/**
On input mode:
0 - called after a solve but before any cuts
-1 - called after strong branching
Returns :
0 - no opinion
-1 pretend infeasible
1 pretend integer solution
*/
virtual int feasible(CbcModel *model, int mode)
{
return mode;
}
};
/************************************************************************
This main program solves the following 0-1 problem:
min -x0 - 2x1 - 3x2 - 4x3
subject to
x0 + x1 + x2 + x3 <= 2
and quadratic constraints with positive random numbers
It does it creating extra yij variables and constraints xi + xj -1 <= yij
and putting quadratic elements on y
The extra constraints are treated as stored cuts.
This is to show how to keep branching even if we have a solution
************************************************************************/
int main(int argc, const char *argv[])
{
// Define a Solver which inherits from OsiClpsolverInterface -> OsiSolverInterface
OsiClpSolverInterface solver1;
int nX = 4;
int nY = (nX * (nX - 1) / 2);
// All columns
double *obj = new double[nX + nY];
double *clo = new double[nX + nY];
double *cup = new double[nX + nY];
int i;
for (i = 0; i < nX; i++) {
obj[i] = -(i + 1);
clo[i] = 0.0;
cup[i] = 1.0;
}
for (i = nX; i < nX + nY; i++) {
obj[i] = 0.0;
clo[i] = 0.0;
cup[i] = 1.0;
}
// Just ordinary rows
int nRow = 1 + nX;
double *rlo = new double[nRow];
double *rup = new double[nRow];
for (i = 0; i < nRow; i++) {
rlo[i] = -COIN_DBL_MAX;
rup[i] = 1.0;
}
// and first row
rup[0] = nX / 2.0;
// Matrix
int nEl = nX + nX * nX;
int *row = new int[nEl];
int *col = new int[nEl];
double *el = new double[nEl];
// X
nEl = 0;
// May need scale to make plausible
double scaleFactor = 1.0;
for (i = 0; i < nX; i++) {
row[nEl] = 0;
col[nEl] = i;
el[nEl++] = 1.0;
// and diagonal
row[nEl] = i + 1;
col[nEl] = i;
double value = CoinDrand48() * scaleFactor;
// make reasonable (so multiples of 0.000001)
value *= 1.0e6;
int iValue = (int)(value + 1.0);
value = iValue;
value *= 1.0e-6;
el[nEl++] = value;
}
// Y
nY = nX;
// And stored cuts
CglStored stored;
double cutEls[3] = { 1.0, 1.0, -1.0 };
int cutIndices[3];
for (i = 0; i < nX; i++) {
cutIndices[0] = i;
for (int j = i + 1; j < nX; j++) {
cutIndices[1] = j;
cutIndices[2] = nY;
// add cut
stored.addCut(-COIN_DBL_MAX, 1.0, 3, cutIndices, cutEls);
row[nEl] = i + 1;
col[nEl] = nY;
double value = CoinDrand48() * scaleFactor;
// multiply to make ones with most negative objective violated
// make reasonable (so multiples of 0.000001)
value *= 1.0e6 + 1.0e6 * j;
int iValue = (int)(value + 1.0);
value = iValue;
value *= 1.0e-6;
el[nEl++] = value;
// and other
if (i != j) {
row[nEl] = j + 1;
col[nEl] = nY;
el[nEl++] = value;
}
nY++;
}
}
// Create model
CoinPackedMatrix matrix(true, row, col, el, nEl);
solver1.loadProblem(matrix, clo, cup, obj, rlo, rup);
delete[] obj;
delete[] clo;
delete[] cup;
delete[] rlo;
delete[] rup;
delete[] row;
delete[] col;
delete[] el;
// Integers
for (i = 0; i < nX; i++)
solver1.setInteger(i);
// Reduce printout
solver1.setHintParam(OsiDoReducePrint, true, OsiHintTry);
// This clones solver
CbcModel model(solver1);
// Add stored cuts (making sure at all depths)
model.addCutGenerator(&stored, 1, "Stored", true, false, false, -100, 1, -1);
/* You need the next few lines -
a) so that cut generator will always be called again if it generated cuts
b) it is known that matrix is not enough to define problem so do cuts even
if it looks integer feasible at continuous optimum.
c) a solution found by strong branching will be ignored.
d) don't recompute a solution once found
*/
// Make sure cut generator called correctly (a)
model.cutGenerator(0)->setMustCallAgain(true);
// Say cuts needed at continuous (b)
OsiBabSolver oddCuts;
oddCuts.setSolverType(4);
model.passInSolverCharacteristics(&oddCuts);
// Say no to all solutions by strong branching (c)
CbcFeasibilityNoStrong noStrong;
model.setProblemFeasibility(noStrong);
// Say don't recompute solution d)
model.setSpecialOptions(4);
double time1 = CoinCpuTime();
// Do complete search
model.branchAndBound();
std::cout << argv[1] << " took " << CoinCpuTime() - time1 << " seconds, "
<< model.getNodeCount() << " nodes with objective "
<< model.getObjValue()
<< (!model.status() ? " Finished" : " Not finished")
<< std::endl;
// Print solution if finished - we can't get names from Osi!
if (!model.status() && model.getMinimizationObjValue() < 1.0e50) {
int numberColumns = model.solver()->getNumCols();
//const double * solution = model.bestSolution();
const double *solution = model.solver()->getColSolution();
int iColumn;
for (iColumn = 0; iColumn < numberColumns; iColumn++) {
double value = solution[iColumn];
if (fabs(value) > 1.0e-7 && model.solver()->isInteger(iColumn))
printf("Column %d has value %g\n", iColumn, value);
}
}
return 0;
}