-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathmodify.cpp
117 lines (97 loc) · 3.6 KB
/
modify.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
// Copyright (C) 2007, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
/*
This example shows the creation of a model from arrays, solution
and then changes to objective and adding a row
*/
#include "CbcModel.hpp"
// Using as solver
#include "OsiClpSolverInterface.hpp"
int main(int argc, const char *argv[])
{
// model is as exmip1.mps from Data/samples
int numberRows = 5;
int numberColumns = 8;
int numberElements = 14;
// matrix data - column ordered
CoinBigIndex start[9] = { 0, 2, 4, 6, 8, 10, 11, 12, 14 };
int length[8] = { 2, 2, 2, 2, 2, 1, 1, 2 };
int rows[14] = { 0, 4, 0, 1, 1, 2, 0, 3, 0, 4, 2, 3, 0, 4 };
double elements[14] = { 3, 5.6, 1, 2, 1.1, 1, -2, 2.8, -1, 1, 1, -1.2, -1, 1.9 };
CoinPackedMatrix matrix(true, numberRows, numberColumns, numberElements, elements, rows, start, length);
// rim data
double objective[8] = { 1, 0, 0, 0, 2, 0, 0, -1 };
double rowLower[5] = { 2.5, -COIN_DBL_MAX, 4, 1.8, 3 };
double rowUpper[5] = { COIN_DBL_MAX, 2.1, 4, 5, 15 };
double colLower[8] = { 2.5, 0, 0, 0, 0.5, 0, 0, 0 };
double colUpper[8] = { COIN_DBL_MAX, 4.1, 1, 1, 4, COIN_DBL_MAX, COIN_DBL_MAX, 4.3 };
OsiClpSolverInterface solver1;
// load problem
solver1.loadProblem(matrix, colLower, colUpper, objective,
rowLower, rowUpper);
// mark integer
solver1.setInteger(2);
solver1.setInteger(3);
// Solve
solver1.initialSolve();
// Pass data and solver to CbcModel
CbcModel model(solver1);
// reduce printout
model.setLogLevel(1);
model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
// Do complete search
model.branchAndBound();
/* Print solution. CbcModel clones solver so we
need to get current copy */
const double *solution = model.solver()->getColSolution();
int i;
for (i = 0; i < numberColumns; i++) {
double value = solution[i];
if (fabs(value) > 1.0e-7 && model.solver()->isInteger(i))
printf("i %d has value %g\n", i, value);
else if (fabs(value) > 1.0e-7)
printf("c %d has value %g\n", i, value);
}
// Change objective
solver1.setObjCoeff(0, -100.0);
// Now model has too much information e.g. best solution
// simplest is to start again
// Pass data and solver to CbcModel
model = CbcModel(solver1);
// reduce printout
model.setLogLevel(1);
model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
// Do complete search
model.branchAndBound();
solution = model.solver()->getColSolution();
for (i = 0; i < numberColumns; i++) {
double value = solution[i];
if (fabs(value) > 1.0e-7 && model.solver()->isInteger(i))
printf("i %d has value %g\n", i, value);
else if (fabs(value) > 1.0e-7)
printf("c %d has value %g\n", i, value);
}
// Add constraint
int column[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
double element2[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };
solver1.addRow(8, column, element2, 7.8, COIN_DBL_MAX);
// Now model has too much information e.g. best solution
// simplest is to start again
// Pass data and solver to CbcModel
model = CbcModel(solver1);
// reduce printout
model.setLogLevel(1);
model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
// Do complete search
model.branchAndBound();
solution = model.solver()->getColSolution();
for (i = 0; i < numberColumns; i++) {
double value = solution[i];
if (fabs(value) > 1.0e-7 && model.solver()->isInteger(i))
printf("i %d has value %g\n", i, value);
else if (fabs(value) > 1.0e-7)
printf("c %d has value %g\n", i, value);
}
return 0;
}