source: trunk/Clp/examples/addRows.cpp @ 1369

Last change on this file since 1369 was 1153, checked in by forrest, 12 years ago

modify example

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
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
14int 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    model.writeMps("b.mps");
146    // Method using least memory - but most complicated
147    time1 = CoinCpuTime();
148    // Assumes we know exact size of model and matrix
149    // Empty model
150    ClpSimplex  model2;
151    {
152      // Create space for 3 columns and 10000 rows
153      int numberRows=10000;
154      int numberColumns=3;
155      // This is fully dense - but would not normally be so
156      int numberElements = numberRows*numberColumns;
157      // Arrays will be set to default values
158      model2.resize(numberRows,numberColumns);
159      double * elements = new double [numberElements];
160      CoinBigIndex * starts = new CoinBigIndex [numberColumns+1];
161      int * rows = new int [numberElements];;
162      int * lengths = new int[numberColumns];
163      // Now fill in - totally unsafe but ....
164      // no need as defaults to 0.0 double * columnLower = model2.columnLower();
165      double * columnUpper = model2.columnUpper();
166      double * objective = model2.objective();
167      double * rowLower = model2.rowLower();
168      double * rowUpper = model2.rowUpper();
169      // Columns - objective was packed
170      for (k=0;k<2;k++) {
171        int iColumn = objIndex[k];
172        objective[iColumn]=objValue[k];
173      }
174      for (k=0;k<numberColumns;k++)
175        columnUpper[k]=upper[k];
176      // Rows
177      for (k=0;k<numberRows;k++) {
178        rowLower[k]=1.0;
179        rowUpper[k]=1.0;
180      }
181      // Now elements
182      double row2Value[]={1.0,-5.0,1.0};
183      CoinBigIndex put=0;
184      for (k=0;k<numberColumns;k++) {
185        starts[k]=put;
186        lengths[k]=numberRows;
187        double value=row2Value[k];
188        for (int i=0;i<numberRows;i++) {
189          rows[put]=i;
190          elements[put]=value;
191          put++;
192        }
193      }
194      starts[numberColumns]=put;
195      // assign to matrix
196      CoinPackedMatrix * matrix = new CoinPackedMatrix(true,0.0,0.0);
197      matrix->assignMatrix(true,numberRows,numberColumns,numberElements,
198                           elements,rows,starts,lengths);
199      ClpPackedMatrix * clpMatrix = new ClpPackedMatrix(matrix);
200      model2.replaceMatrix(clpMatrix,true);
201      printf("Time for 10000 addRow using hand written code is %g\n",CoinCpuTime()-time1);
202      // If matrix is really big could switch off creation of row copy
203      // model2.setSpecialOptions(256);
204    }
205    model2.dual();
206    model2.writeMps("a.mps");
207    // Print column solution
208    int numberColumns = model.numberColumns();
209   
210    // Alternatively getColSolution()
211    double * columnPrimal = model.primalColumnSolution();
212    // Alternatively getReducedCost()
213    double * columnDual = model.dualColumnSolution();
214    // Alternatively getColLower()
215    double * columnLower = model.columnLower();
216    // Alternatively getColUpper()
217    double * columnUpper = model.columnUpper();
218    // Alternatively getObjCoefficients()
219    double * columnObjective = model.objective();
220   
221    int iColumn;
222   
223    std::cout<<"               Primal          Dual         Lower         Upper          Cost"
224             <<std::endl;
225   
226    for (iColumn=0;iColumn<numberColumns;iColumn++) {
227      double value;
228      std::cout<<std::setw(6)<<iColumn<<" ";
229      value = columnPrimal[iColumn];
230      if (fabs(value)<1.0e5)
231        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
232      else
233        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
234      value = columnDual[iColumn];
235      if (fabs(value)<1.0e5)
236        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
237      else
238        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
239      value = columnLower[iColumn];
240      if (fabs(value)<1.0e5)
241        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
242      else
243        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
244      value = columnUpper[iColumn];
245      if (fabs(value)<1.0e5)
246        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
247      else
248        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
249      value = columnObjective[iColumn];
250      if (fabs(value)<1.0e5)
251        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
252      else
253        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
254     
255      std::cout<<std::endl;
256    }
257    std::cout<<"--------------------------------------"<<std::endl;
258    // Test CoinAssert
259    std::cout<<"If Clp compiled with -g below should give assert, if with -O1 or COIN_ASSERT CoinError"<<std::endl;
260    model=modelSave;
261    model.deleteRows(2,del);
262    // Deliberate error
263    model.deleteColumns(1,del+2);
264    // Now use build +-1
265    CoinBuild buildObject3;
266    time1 = CoinCpuTime();
267    for ( k=0;k<10000;k++) {
268      int row2Index[] = {0,1,2};
269      double row2Value[]={1.0,-1.0,1.0};
270      buildObject3.addRow(3,row2Index,row2Value,
271                          1.0,1.0);
272    }
273    model.addRows(buildObject3,true);
274  }
275  catch (CoinError e) {
276    e.print();
277    if (e.lineNumber()>=0)
278      std::cout<<"This was from a CoinAssert"<<std::endl;
279  }
280  return 0;
281}   
Note: See TracBrowser for help on using the repository browser.