1 | // Copyright (C) 2004, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | |
---|
4 | |
---|
5 | // This is a simple example to create a model by row |
---|
6 | #include "ClpSimplex.hpp" |
---|
7 | #include "CoinHelperFunctions.hpp" |
---|
8 | #include "CoinTime.hpp" |
---|
9 | #include "CoinBuild.hpp" |
---|
10 | #include "CoinModel.hpp" |
---|
11 | #include <iomanip> |
---|
12 | #include <cassert> |
---|
13 | |
---|
14 | int main (int argc, const char *argv[]) |
---|
15 | { |
---|
16 | try { |
---|
17 | // Empty model |
---|
18 | ClpSimplex model; |
---|
19 | |
---|
20 | // Objective - just nonzeros |
---|
21 | int objIndex[]={0,2}; |
---|
22 | double objValue[]={1.0,4.0}; |
---|
23 | // Upper bounds - as dense vector |
---|
24 | double upper[]={2.0,COIN_DBL_MAX,4.0}; |
---|
25 | |
---|
26 | // Create space for 3 columns |
---|
27 | model.resize(0,3); |
---|
28 | // Fill in |
---|
29 | int i; |
---|
30 | // Virtuous way |
---|
31 | // First objective |
---|
32 | for (i=0;i<2;i++) |
---|
33 | model.setObjectiveCoefficient(objIndex[i],objValue[i]); |
---|
34 | // Now bounds (lower will be zero by default but do again) |
---|
35 | for (i=0;i<3;i++) { |
---|
36 | model.setColumnLower(i,0.0); |
---|
37 | model.setColumnUpper(i,upper[i]); |
---|
38 | } |
---|
39 | /* |
---|
40 | We could also have done in non-virtuous way e.g. |
---|
41 | double * objective = model.objective(); |
---|
42 | and then set directly |
---|
43 | */ |
---|
44 | // Faster to add rows all at once - but this is easier to show |
---|
45 | // Now add row 1 as >= 2.0 |
---|
46 | int row1Index[] = {0,2}; |
---|
47 | double row1Value[]={1.0,1.0}; |
---|
48 | model.addRow(2,row1Index,row1Value, |
---|
49 | 2.0,COIN_DBL_MAX); |
---|
50 | // Now add row 2 as == 1.0 |
---|
51 | int row2Index[] = {0,1,2}; |
---|
52 | double row2Value[]={1.0,-5.0,1.0}; |
---|
53 | model.addRow(3,row2Index,row2Value, |
---|
54 | 1.0,1.0); |
---|
55 | // solve |
---|
56 | model.dual(); |
---|
57 | |
---|
58 | /* |
---|
59 | Adding one row at a time has a significant overhead so let's |
---|
60 | try a more complicated but faster way |
---|
61 | |
---|
62 | First time adding in 10000 rows one by one |
---|
63 | */ |
---|
64 | model.allSlackBasis(); |
---|
65 | ClpSimplex modelSave=model; |
---|
66 | double time1 = CoinCpuTime(); |
---|
67 | int k; |
---|
68 | for ( k=0;k<10000;k++) { |
---|
69 | int row2Index[] = {0,1,2}; |
---|
70 | double row2Value[]={1.0,-5.0,1.0}; |
---|
71 | model.addRow(3,row2Index,row2Value, |
---|
72 | 1.0,1.0); |
---|
73 | } |
---|
74 | printf("Time for 10000 addRow is %g\n",CoinCpuTime()-time1); |
---|
75 | model.dual(); |
---|
76 | model=modelSave; |
---|
77 | // Now use build |
---|
78 | CoinBuild buildObject; |
---|
79 | time1 = CoinCpuTime(); |
---|
80 | for ( k=0;k<10000;k++) { |
---|
81 | int row2Index[] = {0,1,2}; |
---|
82 | double row2Value[]={1.0,-5.0,1.0}; |
---|
83 | buildObject.addRow(3,row2Index,row2Value, |
---|
84 | 1.0,1.0); |
---|
85 | } |
---|
86 | model.addRows(buildObject); |
---|
87 | printf("Time for 10000 addRow using CoinBuild is %g\n",CoinCpuTime()-time1); |
---|
88 | model.dual(); |
---|
89 | model=modelSave; |
---|
90 | int del[]={0,1,2}; |
---|
91 | model.deleteRows(2,del); |
---|
92 | // Now use build +-1 |
---|
93 | CoinBuild buildObject2; |
---|
94 | time1 = CoinCpuTime(); |
---|
95 | for ( k=0;k<10000;k++) { |
---|
96 | int row2Index[] = {0,1,2}; |
---|
97 | double row2Value[]={1.0,-1.0,1.0}; |
---|
98 | buildObject2.addRow(3,row2Index,row2Value, |
---|
99 | 1.0,1.0); |
---|
100 | } |
---|
101 | model.addRows(buildObject2,true); |
---|
102 | printf("Time for 10000 addRow using CoinBuild+-1 is %g\n",CoinCpuTime()-time1); |
---|
103 | model.dual(); |
---|
104 | model=modelSave; |
---|
105 | model.deleteRows(2,del); |
---|
106 | // Now use build +-1 |
---|
107 | CoinModel modelObject2; |
---|
108 | time1 = CoinCpuTime(); |
---|
109 | for ( k=0;k<10000;k++) { |
---|
110 | int row2Index[] = {0,1,2}; |
---|
111 | double row2Value[]={1.0,-1.0,1.0}; |
---|
112 | modelObject2.addRow(3,row2Index,row2Value, |
---|
113 | 1.0,1.0); |
---|
114 | } |
---|
115 | model.addRows(modelObject2,true); |
---|
116 | printf("Time for 10000 addRow using CoinModel+-1 is %g\n",CoinCpuTime()-time1); |
---|
117 | model.dual(); |
---|
118 | model=ClpSimplex(); |
---|
119 | // Now use build +-1 |
---|
120 | CoinModel modelObject3; |
---|
121 | time1 = CoinCpuTime(); |
---|
122 | for ( k=0;k<10000;k++) { |
---|
123 | int row2Index[] = {0,1,2}; |
---|
124 | double row2Value[]={1.0,-1.0,1.0}; |
---|
125 | modelObject3.addRow(3,row2Index,row2Value, |
---|
126 | 1.0,1.0); |
---|
127 | } |
---|
128 | model.loadProblem(modelObject3,true); |
---|
129 | printf("Time for 10000 addRow using CoinModel load +-1 is %g\n",CoinCpuTime()-time1); |
---|
130 | model.writeMps("xx.mps"); |
---|
131 | model.dual(); |
---|
132 | model=modelSave; |
---|
133 | // Now use model |
---|
134 | CoinModel modelObject; |
---|
135 | time1 = CoinCpuTime(); |
---|
136 | for ( k=0;k<10000;k++) { |
---|
137 | int row2Index[] = {0,1,2}; |
---|
138 | double row2Value[]={1.0,-5.0,1.0}; |
---|
139 | modelObject.addRow(3,row2Index,row2Value, |
---|
140 | 1.0,1.0); |
---|
141 | } |
---|
142 | model.addRows(modelObject); |
---|
143 | printf("Time for 10000 addRow using CoinModel is %g\n",CoinCpuTime()-time1); |
---|
144 | model.dual(); |
---|
145 | // Print column solution |
---|
146 | int numberColumns = model.numberColumns(); |
---|
147 | |
---|
148 | // Alternatively getColSolution() |
---|
149 | double * columnPrimal = model.primalColumnSolution(); |
---|
150 | // Alternatively getReducedCost() |
---|
151 | double * columnDual = model.dualColumnSolution(); |
---|
152 | // Alternatively getColLower() |
---|
153 | double * columnLower = model.columnLower(); |
---|
154 | // Alternatively getColUpper() |
---|
155 | double * columnUpper = model.columnUpper(); |
---|
156 | // Alternatively getObjCoefficients() |
---|
157 | double * columnObjective = model.objective(); |
---|
158 | |
---|
159 | int iColumn; |
---|
160 | |
---|
161 | std::cout<<" Primal Dual Lower Upper Cost" |
---|
162 | <<std::endl; |
---|
163 | |
---|
164 | for (iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
165 | double value; |
---|
166 | std::cout<<std::setw(6)<<iColumn<<" "; |
---|
167 | value = columnPrimal[iColumn]; |
---|
168 | if (fabs(value)<1.0e5) |
---|
169 | std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value; |
---|
170 | else |
---|
171 | std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value; |
---|
172 | value = columnDual[iColumn]; |
---|
173 | if (fabs(value)<1.0e5) |
---|
174 | std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value; |
---|
175 | else |
---|
176 | std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value; |
---|
177 | value = columnLower[iColumn]; |
---|
178 | if (fabs(value)<1.0e5) |
---|
179 | std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value; |
---|
180 | else |
---|
181 | std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value; |
---|
182 | value = columnUpper[iColumn]; |
---|
183 | if (fabs(value)<1.0e5) |
---|
184 | std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value; |
---|
185 | else |
---|
186 | std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value; |
---|
187 | value = columnObjective[iColumn]; |
---|
188 | if (fabs(value)<1.0e5) |
---|
189 | std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value; |
---|
190 | else |
---|
191 | std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value; |
---|
192 | |
---|
193 | std::cout<<std::endl; |
---|
194 | } |
---|
195 | std::cout<<"--------------------------------------"<<std::endl; |
---|
196 | // Test CoinAssert |
---|
197 | std::cout<<"If Clp compiled with -g below should give assert, if with -O1 or COIN_ASSERT CoinError"<<std::endl; |
---|
198 | model=modelSave; |
---|
199 | model.deleteRows(2,del); |
---|
200 | // Deliberate error |
---|
201 | model.deleteColumns(1,del+2); |
---|
202 | // Now use build +-1 |
---|
203 | CoinBuild buildObject3; |
---|
204 | time1 = CoinCpuTime(); |
---|
205 | for ( k=0;k<10000;k++) { |
---|
206 | int row2Index[] = {0,1,2}; |
---|
207 | double row2Value[]={1.0,-1.0,1.0}; |
---|
208 | buildObject3.addRow(3,row2Index,row2Value, |
---|
209 | 1.0,1.0); |
---|
210 | } |
---|
211 | model.addRows(buildObject3,true); |
---|
212 | } |
---|
213 | catch (CoinError e) { |
---|
214 | e.print(); |
---|
215 | if (e.lineNumber()>=0) |
---|
216 | std::cout<<"This was from a CoinAssert"<<std::endl; |
---|
217 | } |
---|
218 | return 0; |
---|
219 | } |
---|