1 | // Copyright (C) 2007, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | /* |
---|
4 | This example shows the creation of a model from arrays, solution |
---|
5 | and then changes to objective and adding a row |
---|
6 | */ |
---|
7 | |
---|
8 | #include "CbcModel.hpp" |
---|
9 | |
---|
10 | // Using as solver |
---|
11 | #include "OsiClpSolverInterface.hpp" |
---|
12 | |
---|
13 | int main (int argc, const char *argv[]) |
---|
14 | { |
---|
15 | // model is as exmip1.mps from Data/samples |
---|
16 | int numberRows=5; |
---|
17 | int numberColumns=8; |
---|
18 | int numberElements=14; |
---|
19 | // matrix data - column ordered |
---|
20 | CoinBigIndex start[9]={0,2,4,6,8,10,11,12,14}; |
---|
21 | int length[8]={2,2,2,2,2,1,1,2}; |
---|
22 | int rows[14]={0,4,0,1,1,2,0,3,0,4,2,3,0,4}; |
---|
23 | double elements[14]={3,5.6,1,2,1.1,1,-2,2.8,-1,1,1,-1.2,-1,1.9}; |
---|
24 | CoinPackedMatrix matrix(true,numberRows,numberColumns,numberElements,elements,rows,start,length); |
---|
25 | |
---|
26 | // rim data |
---|
27 | double objective[8]={1,0,0,0,2,0,0,-1}; |
---|
28 | double rowLower[5]={2.5,-COIN_DBL_MAX,4,1.8,3}; |
---|
29 | double rowUpper[5]={COIN_DBL_MAX,2.1,4,5,15}; |
---|
30 | double colLower[8]={2.5,0,0,0,0.5,0,0,0}; |
---|
31 | double colUpper[8]={COIN_DBL_MAX,4.1,1,1,4,COIN_DBL_MAX,COIN_DBL_MAX,4.3}; |
---|
32 | OsiClpSolverInterface solver1; |
---|
33 | // load problem |
---|
34 | solver1.loadProblem(matrix,colLower,colUpper,objective, |
---|
35 | rowLower,rowUpper); |
---|
36 | // mark integer |
---|
37 | solver1.setInteger(2); |
---|
38 | solver1.setInteger(3); |
---|
39 | |
---|
40 | // Solve |
---|
41 | solver1.initialSolve(); |
---|
42 | |
---|
43 | // Pass data and solver to CbcModel |
---|
44 | CbcModel model(solver1); |
---|
45 | |
---|
46 | // reduce printout |
---|
47 | model.setLogLevel(1); |
---|
48 | model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry); |
---|
49 | // Do complete search |
---|
50 | model.branchAndBound(); |
---|
51 | /* Print solution. CbcModel clones solver so we |
---|
52 | need to get current copy */ |
---|
53 | |
---|
54 | const double * solution = model.solver()->getColSolution(); |
---|
55 | int i; |
---|
56 | |
---|
57 | for (i=0;i<numberColumns;i++) { |
---|
58 | double value=solution[i]; |
---|
59 | if (fabs(value)>1.0e-7&&model.solver()->isInteger(i)) |
---|
60 | printf("i %d has value %g\n",i,value); |
---|
61 | else if (fabs(value)>1.0e-7) |
---|
62 | printf("c %d has value %g\n",i,value); |
---|
63 | } |
---|
64 | |
---|
65 | // Change objective |
---|
66 | solver1.setObjCoeff(0,-100.0); |
---|
67 | |
---|
68 | // Now model has too much information e.g. best solution |
---|
69 | // simplest is to start again |
---|
70 | // Pass data and solver to CbcModel |
---|
71 | model = CbcModel(solver1); |
---|
72 | |
---|
73 | // reduce printout |
---|
74 | model.setLogLevel(1); |
---|
75 | model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry); |
---|
76 | // Do complete search |
---|
77 | model.branchAndBound(); |
---|
78 | |
---|
79 | solution = model.solver()->getColSolution(); |
---|
80 | |
---|
81 | for (i=0;i<numberColumns;i++) { |
---|
82 | double value=solution[i]; |
---|
83 | if (fabs(value)>1.0e-7&&model.solver()->isInteger(i)) |
---|
84 | printf("i %d has value %g\n",i,value); |
---|
85 | else if (fabs(value)>1.0e-7) |
---|
86 | printf("c %d has value %g\n",i,value); |
---|
87 | } |
---|
88 | |
---|
89 | // Add constraint |
---|
90 | int column[8]={0,1,2,3,4,5,6,7}; |
---|
91 | double element2[8] ={1,1,1,1,1,1,1,1}; |
---|
92 | solver1.addRow(8,column,element2,7.8,COIN_DBL_MAX); |
---|
93 | |
---|
94 | // Now model has too much information e.g. best solution |
---|
95 | // simplest is to start again |
---|
96 | // Pass data and solver to CbcModel |
---|
97 | model = CbcModel(solver1); |
---|
98 | |
---|
99 | // reduce printout |
---|
100 | model.setLogLevel(1); |
---|
101 | model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry); |
---|
102 | // Do complete search |
---|
103 | model.branchAndBound(); |
---|
104 | |
---|
105 | solution = model.solver()->getColSolution(); |
---|
106 | |
---|
107 | for (i=0;i<numberColumns;i++) { |
---|
108 | double value=solution[i]; |
---|
109 | if (fabs(value)>1.0e-7&&model.solver()->isInteger(i)) |
---|
110 | printf("i %d has value %g\n",i,value); |
---|
111 | else if (fabs(value)>1.0e-7) |
---|
112 | printf("c %d has value %g\n",i,value); |
---|
113 | } |
---|
114 | return 0; |
---|
115 | } |
---|