forked from coin-or/Cbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample4.cpp
234 lines (189 loc) · 6.7 KB
/
sample4.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
// Copyright (C) 2004, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
#include "CbcConfig.h"
#include "CoinPragma.hpp"
#include <cassert>
#include <iomanip>
// For Branch and bound
#include "OsiSolverInterface.hpp"
#include "CbcModel.hpp"
#include "CbcBranchUser.hpp"
#include "CbcBranchActual.hpp"
#include "CbcCompareUser.hpp"
#include "CoinTime.hpp"
#include "OsiClpSolverInterface.hpp"
//#############################################################################
/************************************************************************
This main program reads in an SOS model (rgn) from an mps file.
It then solves it three ways :-
a) As normal
b) SOS 1
c) SOS 2 (so answer will be different)
************************************************************************/
int main(int argc, const char *argv[])
{
// Define your favorite OsiSolver
OsiClpSolverInterface solver1;
//solver1.messageHandler()->setLogLevel(0);
CbcModel model(solver1);
model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
// Read in rgn.mps
std::string mpsFileName;
#if defined(MIPLIB3DIR)
mpsFileName = MIPLIB3DIR "/rgn";
#else
if (argc < 2) {
fprintf(stderr, "Do not know where to find miplib3 MPS files.\n");
exit(1);
}
#endif
if (argc >= 2)
mpsFileName = argv[1];
int numMpsReadErrors = model.solver()->readMps(mpsFileName.c_str(), "");
if (numMpsReadErrors != 0) {
printf("%d errors reading MPS file\n", numMpsReadErrors);
return numMpsReadErrors;
}
// Definition of node choice
CbcCompareUser compare;
compare.setWeight(0.0);
model.setNodeComparison(compare);
// Reduce output
model.messageHandler()->setLogLevel(1);
// Get branching messages
model.messageHandler()->setLogLevel(3);
// Do initial solve to continuous
model.initialSolve();
// Save model
CbcModel model2 = model;
int numberColumns = model.getNumCols();
int numberIntegers = 0;
int *integerVariable = new int[numberColumns];
int i;
for (i = 0; i < numberColumns; i++) {
if (model.isInteger(i)) {
integerVariable[numberIntegers++] = i;
}
}
if (numberColumns != 180 || numberIntegers != 100) {
printf("Incorrect model for example\n");
exit(1);
}
double time1 = CoinCpuTime();
model.branchAndBound();
std::cout << "rgn.mps"
<< " took " << CoinCpuTime() - time1 << " seconds, "
<< model.getNodeCount() << " nodes with objective "
<< model.getObjValue()
<< (!model.status() ? " Finished" : " Not finished")
<< std::endl;
const double *solution = model.solver()->getColSolution();
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
std::cout << "--------------------------------------" << std::endl;
for (i = 0; i < numberIntegers; i++) {
int iColumn = integerVariable[i];
double value = solution[iColumn];
if (fabs(value) > 1.0e-7)
std::cout << std::setw(6) << iColumn << " " << value << std::endl;
}
std::cout << "--------------------------------------" << std::endl;
std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
// Restore model
model = model2;
// Convert slacks to variables
CoinBigIndex start[5] = { 0, 1, 2, 3, 4 };
int row[4] = { 0, 1, 2, 3 };
double element[4] = { 1.0, 1.0, 1.0, 1.0 };
double up[4] = { 1.0, 1.0, 1.0, 1.0 };
model.solver()->addCols(4, start, row, element, NULL, up, NULL);
// Now use SOS1
int numberSets = 4;
int which[104];
double weights[104];
int starts[5];
// load
int n = 0;
starts[0] = 0;
for (int iSet = 0; iSet < 4; iSet++) {
for (int i = 0; i < 25; i++) {
weights[n] = i + 1.0;
which[n] = iSet * 25 + i;
n++;
}
// slack - make sure first branch is on slack
weights[n] = 1000.0;
which[n] = 180 + iSet;
n++;
starts[iSet + 1] = n;
}
for (i = 0; i < numberIntegers; i++) {
int iColumn = integerVariable[i];
// Stop being integer
model.solver()->setContinuous(iColumn);
}
// save model in this state
CbcModel modelSOS = model;
CbcObject **objects = new CbcObject *[numberSets];
for (i = 0; i < numberSets; i++) {
objects[i] = new CbcSOS(&model, starts[i + 1] - starts[i], which + starts[i],
weights, i);
}
model.addObjects(numberSets, objects);
for (i = 0; i < numberSets; i++)
delete objects[i];
delete[] objects;
time1 = CoinCpuTime();
model.branchAndBound();
std::cout << "rgn.mps"
<< " took " << CoinCpuTime() - time1 << " seconds, "
<< model.getNodeCount() << " nodes with objective "
<< model.getObjValue()
<< (!model.status() ? " Finished" : " Not finished")
<< std::endl;
solution = model.solver()->getColSolution();
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
std::cout << "--------------------------------------" << std::endl;
for (i = 0; i < numberIntegers; i++) {
int iColumn = integerVariable[i];
double value = solution[iColumn];
if (fabs(value) > 1.0e-7)
std::cout << std::setw(6) << iColumn << " " << value << std::endl;
}
std::cout << "--------------------------------------" << std::endl;
std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
// Restore SOS model
model = modelSOS;
// Now use SOS2
objects = new CbcObject *[numberSets];
for (i = 0; i < numberSets; i++) {
objects[i] = new CbcSOS(&model, starts[i + 1] - starts[i], which + starts[i],
weights, i, 2);
}
model.addObjects(numberSets, objects);
for (i = 0; i < numberSets; i++)
delete objects[i];
delete[] objects;
time1 = CoinCpuTime();
model.branchAndBound();
std::cout << "rgn.mps"
<< " took " << CoinCpuTime() - time1 << " seconds, "
<< model.getNodeCount() << " nodes with objective "
<< model.getObjValue()
<< (!model.status() ? " Finished" : " Not finished")
<< std::endl;
solution = model.solver()->getColSolution();
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
std::cout << "--------------------------------------" << std::endl;
for (i = 0; i < numberIntegers; i++) {
int iColumn = integerVariable[i];
double value = solution[iColumn];
if (fabs(value) > 1.0e-7)
std::cout << std::setw(6) << iColumn << " " << value
<< std::endl;
}
std::cout << "--------------------------------------" << std::endl;
std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
delete[] integerVariable;
return 0;
}