source: branches/pre/Samples/driver2.cpp @ 222

Last change on this file since 222 was 222, checked in by forrest, 16 years ago

Start of mini sprint

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#include "ClpSimplex.hpp"
5#include "ClpPresolve.hpp"
6#include "CoinHelperFunctions.hpp"
7#include "CoinTime.hpp"
8#include "ClpDualRowSteepest.hpp"
9#include "ClpPrimalColumnSteepest.hpp"
10#include <iomanip>
11
12int main (int argc, const char *argv[])
13{
14  ClpSimplex  model;
15  int status;
16  // Keep names when reading an mps file
17  if (argc<2)
18    status=model.readMps("../../Mps/Sample/p0033.mps",true);
19  else
20    status=model.readMps(argv[1],true);
21
22  if (status) {
23    fprintf(stderr,"Bad readMps %s\n",argv[1]);
24    fprintf(stdout,"Bad readMps %s\n",argv[1]);
25    exit(1);
26  }
27
28  double time1 = CoinCpuTime();
29  /*
30    This driver show how to do presolve.
31  */
32  ClpSimplex * model2;
33  ClpPresolve pinfo;
34  int numberPasses=5; // can change this
35  /* Use a tolerance of 1.0e-8 for feasibility, treat problem as
36     not being integer, do "numberpasses" passes and throw away names
37     in presolved model */
38  model2 = pinfo.presolvedModel(model,1.0e-8,false,numberPasses,false);
39  if (!model2) {
40    fprintf(stderr,"ClpPresolve says %s is infeasible with tolerance of %g\n",
41            argv[1],1.0e-8);
42    fprintf(stdout,"ClpPresolve says %s is infeasible with tolerance of %g\n",
43            argv[1],1.0e-8);
44    // model was infeasible - maybe try again with looser tolerances
45    model2 = pinfo.presolvedModel(model,1.0e-7,false,numberPasses,false);
46    if (!model2) {
47      fprintf(stderr,"ClpPresolve says %s is infeasible with tolerance of %g\n",
48              argv[1],1.0e-7);
49      fprintf(stdout,"ClpPresolve says %s is infeasible with tolerance of %g\n",
50              argv[1],1.0e-7);
51      exit(2);
52    }
53  }
54  // change factorization frequency from 200
55  model2->setFactorizationFrequency(100+model2->numberRows()/50);
56  if (argc<3 ||!strstr(argv[2],"primal")) {
57    // Use the dual algorithm unless user said "primal"
58    /* faster if bounds tightened as then dual can flip variables
59        to other bound to stay dual feasible.  We can trash the bounds as
60        this model is going to be thrown away
61    */
62    int numberInfeasibilities = model2->tightenPrimalBounds();
63    if (numberInfeasibilities)
64      std::cout<<"** Analysis indicates model infeasible"
65               <<std::endl;
66    model2->crash(1000.0,2);
67    ClpDualRowSteepest steep(1);
68    model2->setDualRowPivotAlgorithm(steep);
69    model2->dual();
70  } else {
71    ClpPrimalColumnSteepest steep(1);
72    model2->setPrimalColumnPivotAlgorithm(steep);
73    model2->primal();
74  }
75  pinfo.postsolve(true);
76
77  int numberIterations=model2->numberIterations();;
78  delete model2;
79  /* After this postsolve model should be optimal.
80     We can use checkSolution and test feasibility */
81  model.checkSolution();
82  if (model.numberDualInfeasibilities()||
83      model.numberPrimalInfeasibilities()) 
84    printf("%g dual %g(%d) Primal %g(%d)\n",
85           model.objectiveValue(),
86           model.sumDualInfeasibilities(),
87           model.numberDualInfeasibilities(),
88           model.sumPrimalInfeasibilities(),
89           model.numberPrimalInfeasibilities());
90  // But resolve for safety
91  model.primal(1);
92
93  numberIterations += model.numberIterations();;
94  // for running timing tests
95  std::cout<<argv[1]<<" Objective "<<model.objectiveValue()<<" took "<<
96    numberIterations<<" iterations and "<<
97    CoinCpuTime()-time1<<" seconds"<<std::endl;
98
99  std::string modelName;
100  model.getStrParam(ClpProbName,modelName);
101  std::cout<<"Model "<<modelName<<" has "<<model.numberRows()<<" rows and "<<
102    model.numberColumns()<<" columns"<<std::endl;
103
104  // remove this to print solution
105
106  exit(0);
107
108  /*
109    Now to print out solution.  The methods used return modifiable
110    arrays while the alternative names return const pointers -
111    which is of course much more virtuous.
112
113    This version just does non-zero columns
114 
115   */
116#if 0
117  int numberRows = model.numberRows();
118
119  // Alternatively getRowActivity()
120  double * rowPrimal = model.primalRowSolution();
121  // Alternatively getRowPrice()
122  double * rowDual = model.dualRowSolution();
123  // Alternatively getRowLower()
124  double * rowLower = model.rowLower();
125  // Alternatively getRowUpper()
126  double * rowUpper = model.rowUpper();
127  // Alternatively getRowObjCoefficients()
128  double * rowObjective = model.rowObjective();
129   
130  // If we have not kept names (parameter to readMps) this will be 0
131  assert(model.lengthNames());
132
133  // Row names
134  const std::vector<std::string> * rowNames = model.rowNames();
135
136
137  int iRow;
138
139  std::cout<<"                       Primal          Dual         Lower         Upper        (Cost)"
140           <<std::endl;
141
142  for (iRow=0;iRow<numberRows;iRow++) {
143    double value;
144    std::cout<<std::setw(6)<<iRow<<" "<<std::setw(8)<<(*rowNames)[iRow];
145    value = rowPrimal[iRow];
146    if (fabs(value)<1.0e5)
147      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
148    else
149      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
150    value = rowDual[iRow];
151    if (fabs(value)<1.0e5)
152      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
153    else
154      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
155    value = rowLower[iRow];
156    if (fabs(value)<1.0e5)
157      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
158    else
159      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
160    value = rowUpper[iRow];
161    if (fabs(value)<1.0e5)
162      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
163    else
164      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
165    if (rowObjective) {
166      value = rowObjective[iRow];
167      if (fabs(value)<1.0e5)
168        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
169      else
170        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
171    }
172    std::cout<<std::endl;
173  }
174#endif
175  std::cout<<"--------------------------------------"<<std::endl;
176
177  // Columns
178
179  int numberColumns = model.numberColumns();
180
181  // Alternatively getColSolution()
182  double * columnPrimal = model.primalColumnSolution();
183  // Alternatively getReducedCost()
184  double * columnDual = model.dualColumnSolution();
185  // Alternatively getColLower()
186  double * columnLower = model.columnLower();
187  // Alternatively getColUpper()
188  double * columnUpper = model.columnUpper();
189  // Alternatively getObjCoefficients()
190  double * columnObjective = model.objective();
191   
192  // If we have not kept names (parameter to readMps) this will be 0
193  assert(model.lengthNames());
194
195  // Column names
196  const std::vector<std::string> * columnNames = model.columnNames();
197
198
199  int iColumn;
200 
201  std::cout<<"                       Primal          Dual         Lower         Upper          Cost"
202           <<std::endl;
203
204  for (iColumn=0;iColumn<numberColumns;iColumn++) {
205    double value;
206    value = columnPrimal[iColumn];
207    if (fabs(value)>1.0e-8) {
208      std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<(*columnNames)[iColumn];
209      if (fabs(value)<1.0e5)
210        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
211      else
212        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
213      value = columnDual[iColumn];
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 = columnLower[iColumn];
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 = columnUpper[iColumn];
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 = columnObjective[iColumn];
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     
234      std::cout<<std::endl;
235    }
236  }
237  std::cout<<"--------------------------------------"<<std::endl;
238
239  return 0;
240}   
Note: See TracBrowser for help on using the repository browser.