source: branches/pre/Samples/driver.cpp @ 180

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

new stuff

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