source: trunk/Samples/defaults.cpp @ 50

Last change on this file since 50 was 50, checked in by ladanyi, 17 years ago

devel-1 merged into HEAD

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#include "ClpSimplex.hpp"
5#include "ClpPrimalColumnSteepest.hpp"
6#include "ClpDualRowSteepest.hpp"
7#include <iomanip>
8
9int main (int argc, const char *argv[])
10{
11  ClpSimplex  model;
12  int status;
13  // Keep names
14  if (argc<2)
15    status=model.readMps("../../Mps/Sample/p0033.mps",true);
16  else
17    status=model.readMps(argv[1],true);
18  /*
19    This driver is similar to minimum.cpp, but it
20    sets all parameter values to their defaults.  The purpose of this
21    is to give a list of most of the methods that the end user will need.
22
23    There are also some more methods as for OsiSolverInterface e.g.
24    some loadProblem methods and deleteRows and deleteColumns.
25
26    Often two methods do the same thing, one having a name I like
27    while the other adheres to OsiSolverInterface standards
28  */
29
30  // Use exact devex ( a variant of steepest edge)
31  ClpPrimalColumnSteepest primalSteepest;
32  model.setPrimalColumnPivotAlgorithm(primalSteepest);
33
34
35  int integerValue;
36  double value;
37
38  // Infeasibility cost
39  value = model.infeasibilityCost();
40  std::cout<<"Default value of infeasibility cost is "<<value<<std::endl;
41  model.setInfeasibilityCost(value);
42
43  if (!status) {
44    model.primal();
45  }
46  // Number of rows and columns - also getNumRows, getNumCols
47  std::string modelName;
48  model.getStrParam(ClpProbName,modelName);
49  std::cout<<"Model "<<modelName<<" has "<<model.numberRows()<<" rows and "<<
50    model.numberColumns()<<" columns"<<std::endl;
51
52  /* Some parameters as in OsiSolverParameters.  ObjectiveLimits
53     are not active yet.  dualTolerance, setDualTolerance,
54     primalTolerance and setPrimalTolerance may be used as well */
55
56  model.getDblParam(ClpDualObjectiveLimit,value);
57  std::cout<<"Value of ClpDualObjectiveLimit is "<<value<<std::endl;
58  model.getDblParam(ClpPrimalObjectiveLimit,value);
59  std::cout<<"Value of ClpPrimalObjectiveLimit is "<<value<<std::endl;
60  model.getDblParam(ClpDualTolerance,value);
61  std::cout<<"Value of ClpDualTolerance is "<<value<<std::endl;
62  model.getDblParam(ClpPrimalTolerance,value);
63  std::cout<<"Value of ClpPrimalTolerance is "<<value<<std::endl;
64  model.getDblParam(ClpObjOffset,value);
65  std::cout<<"Value of ClpObjOffset is "<<value<<std::endl;
66
67  // setDblParam(ClpPrimalTolerance) is same as this
68  model.getDblParam(ClpPrimalTolerance,value);
69  model.setPrimalTolerance(value);
70
71  model.setDualTolerance( model.dualTolerance()) ;
72
73  // Other Param stuff
74
75  // Can also use maximumIterations
76  model.getIntParam(ClpMaxNumIteration,integerValue);
77  std::cout<<"Value of ClpMaxNumIteration is "<<integerValue<<std::endl;
78  model.setMaximumIterations(integerValue);
79
80  // Not sure this works yet
81  model.getIntParam(ClpMaxNumIterationHotStart,integerValue);
82  std::cout<<"Value of ClpMaxNumIterationHotStart is "
83           <<integerValue<<std::endl;
84
85  // Can also use getIterationCount and getObjValue
86  /* Status of problem:
87      0 - optimal
88      1 - primal infeasible
89      2 - dual infeasible
90      3 - stopped on iterations etc
91      4 - stopped due to errors
92  */
93  std::cout<<"Model status is "<<model.status()<<" after "
94           <<model.numberIterations()<<" iterations - objective is "
95           <<model.objectiveValue()<<std::endl;
96
97  assert(!model.isAbandoned());
98  assert(model.isProvenOptimal());
99  assert(!model.isProvenPrimalInfeasible());
100  assert(!model.isProvenDualInfeasible());
101  assert(!model.isPrimalObjectiveLimitReached());
102  assert(!model.isDualObjectiveLimitReached());
103  assert(!model.isIterationLimitReached());
104
105
106  // Things to help you determine if optimal
107  assert(model.primalFeasible());
108  assert(!model.numberPrimalInfeasibilities());
109  assert(model.sumPrimalInfeasibilities()<1.0e-7);
110  assert(model.dualFeasible());
111  assert(!model.numberDualInfeasibilities());
112  assert(model.sumDualInfeasibilities()<1.0e-7);
113
114  // Save warm start and set to all slack
115  unsigned char * basis1 = model.statusCopy();
116  model.createStatus();
117
118  // Now create another model and do hot start
119  ClpSimplex model2=model;
120  model2.copyinStatus(basis1);
121  delete [] basis1;
122
123  // Check model has not got basis (should iterate)
124  model.dual();
125 
126  // Can use getObjSense
127  model2.setOptimizationDirection(model.optimizationDirection());
128
129  // Can use scalingFlag() to check if scaling on
130  // But set up scaling
131  model2.scaling();
132
133  // Could play with sparse factorization on/off
134  model2.setSparseFactorization(model.sparseFactorization());
135
136  // Sets row pivot choice algorithm in dual
137  ClpDualRowSteepest dualSteepest;
138  model2.setDualRowPivotAlgorithm(dualSteepest);
139
140  // Dual bound (i.e. dual infeasibility cost)
141  value = model.dualBound();
142  std::cout<<"Default value of dual bound is "<<value<<std::endl;
143  model.setDualBound(value);
144
145  // Do some deafult message handling
146  // To see real use - see OsiOslSolverInterfaceTest.cpp
147  CoinMessageHandler handler;
148  model2.passInMessageHandler(& handler);
149  model2.newLanguage(CoinMessages::us_en);
150
151  //Increase level of detail
152  model2.setLogLevel(4);
153
154  // solve
155  model2.dual();
156  // flip direction twice and solve
157  model2.setOptimizationDirection(-1);
158  model2.dual();
159  model2.setOptimizationDirection(1);
160  //Decrease level of detail
161  model2.setLogLevel(1);
162  model2.dual();
163
164  /*
165    Now for getting at information.  This will not deal with:
166
167    ClpMatrixBase * rowCopy() and ClpMatrixbase * clpMatrix()
168
169    nor with
170
171    double * infeasibilityRay() and double * unboundedRay()
172    (NULL returned if none/wrong)
173    Up to user to use delete [] on these arrays.
174
175   */
176
177
178  /*
179    Now to print out solution.  The methods used return modifiable
180    arrays while the alternative names return const pointers -
181    which is of course much more virtuous
182 
183   */
184
185  int numberRows = model2.numberRows();
186
187  // Alternatively getRowActivity()
188  double * rowPrimal = model2.primalRowSolution();
189  // Alternatively getRowPrice()
190  double * rowDual = model2.dualRowSolution();
191  // Alternatively getRowLower()
192  double * rowLower = model2.rowLower();
193  // Alternatively getRowUpper()
194  double * rowUpper = model2.rowUpper();
195  // Alternatively getRowObjCoefficients()
196  double * rowObjective = model2.rowObjective();
197   
198  // If we have not kept names (parameter to readMps) this will be 0
199  assert(model2.lengthNames());
200
201  // Row names
202  const std::vector<std::string> * rowNames = model2.rowNames();
203
204
205  int iRow;
206
207  std::cout<<"                       Primal          Dual         Lower         Upper        (Cost)"
208           <<std::endl;
209
210  for (iRow=0;iRow<numberRows;iRow++) {
211    double value;
212    std::cout<<std::setw(6)<<iRow<<" "<<std::setw(8)<<(*rowNames)[iRow];
213    value = rowPrimal[iRow];
214    if (fabs(value)<1.0e5)
215      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
216    else
217      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
218    value = rowDual[iRow];
219    if (fabs(value)<1.0e5)
220      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
221    else
222      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
223    value = rowLower[iRow];
224    if (fabs(value)<1.0e5)
225      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
226    else
227      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
228    value = rowUpper[iRow];
229    if (fabs(value)<1.0e5)
230      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
231    else
232      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
233    if (rowObjective) {
234      value = rowObjective[iRow];
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    }
240    std::cout<<std::endl;
241  }
242  std::cout<<"--------------------------------------"<<std::endl;
243
244  // Columns
245
246  int numberColumns = model2.numberColumns();
247
248  // Alternatively getColSolution()
249  double * columnPrimal = model2.primalColumnSolution();
250  // Alternatively getReducedCost()
251  double * columnDual = model2.dualColumnSolution();
252  // Alternatively getColLower()
253  double * columnLower = model2.columnLower();
254  // Alternatively getColUpper()
255  double * columnUpper = model2.columnUpper();
256  // Alternatively getObjCoefficients()
257  double * columnObjective = model2.objective();
258   
259  // If we have not kept names (parameter to readMps) this will be 0
260  assert(model2.lengthNames());
261
262  // Column names
263  const std::vector<std::string> * columnNames = model2.columnNames();
264
265
266  int iColumn;
267 
268  std::cout<<"                       Primal          Dual         Lower         Upper          Cost"
269           <<std::endl;
270
271  for (iColumn=0;iColumn<numberColumns;iColumn++) {
272    double value;
273    std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<(*columnNames)[iColumn];
274    value = columnPrimal[iColumn];
275    if (fabs(value)<1.0e5)
276      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
277    else
278      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
279    value = columnDual[iColumn];
280    if (fabs(value)<1.0e5)
281      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
282    else
283      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
284    value = columnLower[iColumn];
285    if (fabs(value)<1.0e5)
286      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
287    else
288      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
289    value = columnUpper[iColumn];
290    if (fabs(value)<1.0e5)
291      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
292    else
293      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
294    value = columnObjective[iColumn];
295    if (fabs(value)<1.0e5)
296      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
297    else
298      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
299
300    std::cout<<std::endl;
301  }
302  std::cout<<"--------------------------------------"<<std::endl;
303
304  std::cout<<resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
305
306  // Now matrix
307  CoinPackedMatrix * matrix = model2.matrix();
308
309  const double * element = matrix->getElements();
310  const int * row = matrix->getIndices();
311  const int * start = matrix->getVectorStarts();
312  const int * length = matrix->getVectorLengths();
313
314  for (iColumn=0;iColumn<numberColumns;iColumn++) {
315    std::cout<<"Column "<<iColumn;
316    int j;
317    for (j=start[iColumn];j<start[iColumn]+length[iColumn];j++) 
318      std::cout<<" ( "<<row[j]<<", "<<element[j]<<")";
319    std::cout<<std::endl;
320  }
321  return 0;
322}   
Note: See TracBrowser for help on using the repository browser.