source: branches/devel-1/Samples/defaults.cpp @ 10

Last change on this file since 10 was 10, checked in by forrest, 17 years ago

Slightly better example

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