source: trunk/Samples/driver.cpp @ 91

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

reasonable standard driver for clp

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
RevLine 
[91]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 "ClpFactorization.hpp"
8#include "Presolve.hpp"
9#include <iomanip>
10
11// Time
12
13#include  <time.h>
14#if !defined(_MSC_VER)
15#include <sys/times.h>
16#include <sys/resource.h>
17#include <unistd.h>
18#endif
19static double cpuTime()
20{
21  double cpu_temp;
22#if defined(_MSC_VER)
23  unsigned int ticksnow;        /* clock_t is same as int */
24 
25  ticksnow = (unsigned int)clock();
26 
27  cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
28#else
29  struct rusage usage;
30  getrusage(RUSAGE_SELF,&usage);
31  cpu_temp = usage.ru_utime.tv_sec;
32  cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
33#endif
34  return cpu_temp;
35}
36
37int main (int argc, const char *argv[])
38{
39  ClpSimplex  model;
40  int status;
41  // Keep names
42  if (argc<2)
43    status=model.readMps("../../Mps/Sample/p0033.mps",true);
44  else
45    status=model.readMps(argv[1],true);
46  /*
47    This driver show how to do presolve
48  */
49
50  if (status) {
51    fprintf(stderr,"Bad readMps %s\n",argv[1]);
52    fprintf(stdout,"Bad readMps %s\n",argv[1]);
53    exit(1);
54  }
55
56  double time1 = cpuTime();
57  ClpSimplex * model2;
58  Presolve pinfo;
59  int numberPasses=5; // can change this
60  model2 = pinfo.presolvedModel(model,1.0e-8,false,numberPasses);
61  if (!model2) {
62    fprintf(stderr,"Presolve says %s is infeasible with tolerance of %g\n",
63            argv[1],1.0e-8);
64    fprintf(stdout,"Presolve says %s is infeasible with tolerance of %g\n",
65            argv[1],1.0e-8);
66    model2 = pinfo.presolvedModel(model,1.0e-7,false,numberPasses);
67    if (!model2) {
68      fprintf(stderr,"Presolve says %s is infeasible with tolerance of %g\n",
69              argv[1],1.0e-7);
70      fprintf(stdout,"Presolve says %s is infeasible with tolerance of %g\n",
71              argv[1],1.0e-7);
72      exit(2);
73    }
74  }
75  model2->checkSolution();
76  // change from 200
77  model2->factorization()->maximumPivots(100+model2->numberRows()/50);
78  if (argc<3 ||!strstr(argv[2],"primal")) {
79    // faster if bounds tightened
80    int numberInfeasibilities = model2->tightenPrimalBounds();
81    if (numberInfeasibilities)
82      std::cout<<"** Analysis indicates model infeasible"
83               <<std::endl;
84    // up dual bound for safety
85    model2->setDualBound(1.0e10);
86    model2->dual();
87  } else {
88    // up infeasibility cost for safety
89    model2->setInfeasibilityCost(1.0e10);
90    model2->primal();
91  }
92  pinfo.postsolve(true);
93               
94  int numberIterations=model2->numberIterations();;
95  delete model2;
96  printf("Resolving from postsolved model\n");
97  model.primal(1);
98
99  numberIterations += model.numberIterations();;
100  // for running timing tests
101  std::cout<<argv[1]<<" Objective "<<model.objectiveValue()<<" took "<<
102    numberIterations<<" iterations and "<<cpuTime()-time1<<" seconds"<<std::endl;
103
104  std::string modelName;
105  model.getStrParam(ClpProbName,modelName);
106  std::cout<<"Model "<<modelName<<" has "<<model.numberRows()<<" rows and "<<
107    model.numberColumns()<<" columns"<<std::endl;
108
109  // remove this to print solution
110
111  exit(0);
112
113  /*
114    Now to print out solution.  The methods used return modifiable
115    arrays while the alternative names return const pointers -
116    which is of course much more virtuous.
117
118    This version just does non-zero columns
119 
120   */
121#if 0
122  int numberRows = model.numberRows();
123
124  // Alternatively getRowActivity()
125  double * rowPrimal = model.primalRowSolution();
126  // Alternatively getRowPrice()
127  double * rowDual = model.dualRowSolution();
128  // Alternatively getRowLower()
129  double * rowLower = model.rowLower();
130  // Alternatively getRowUpper()
131  double * rowUpper = model.rowUpper();
132  // Alternatively getRowObjCoefficients()
133  double * rowObjective = model.rowObjective();
134   
135  // If we have not kept names (parameter to readMps) this will be 0
136  assert(model.lengthNames());
137
138  // Row names
139  const std::vector<std::string> * rowNames = model.rowNames();
140
141
142  int iRow;
143
144  std::cout<<"                       Primal          Dual         Lower         Upper        (Cost)"
145           <<std::endl;
146
147  for (iRow=0;iRow<numberRows;iRow++) {
148    double value;
149    std::cout<<std::setw(6)<<iRow<<" "<<std::setw(8)<<(*rowNames)[iRow];
150    value = rowPrimal[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 = rowDual[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 = rowLower[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    value = rowUpper[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    if (rowObjective) {
171      value = rowObjective[iRow];
172      if (fabs(value)<1.0e5)
173        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
174      else
175        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
176    }
177    std::cout<<std::endl;
178  }
179#endif
180  std::cout<<"--------------------------------------"<<std::endl;
181
182  // Columns
183
184  int numberColumns = model.numberColumns();
185
186  // Alternatively getColSolution()
187  double * columnPrimal = model.primalColumnSolution();
188  // Alternatively getReducedCost()
189  double * columnDual = model.dualColumnSolution();
190  // Alternatively getColLower()
191  double * columnLower = model.columnLower();
192  // Alternatively getColUpper()
193  double * columnUpper = model.columnUpper();
194  // Alternatively getObjCoefficients()
195  double * columnObjective = model.objective();
196   
197  // If we have not kept names (parameter to readMps) this will be 0
198  assert(model.lengthNames());
199
200  // Column names
201  const std::vector<std::string> * columnNames = model.columnNames();
202
203
204  int iColumn;
205 
206  std::cout<<"                       Primal          Dual         Lower         Upper          Cost"
207           <<std::endl;
208
209  for (iColumn=0;iColumn<numberColumns;iColumn++) {
210    double value;
211    value = columnPrimal[iColumn];
212    if (fabs(value)>1.0e-8) {
213      std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<(*columnNames)[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 = columnDual[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 = columnLower[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 = columnUpper[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      value = columnObjective[iColumn];
234      if (fabs(value)<1.0e5)
235        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
236      else
237        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
238     
239      std::cout<<std::endl;
240    }
241  }
242  std::cout<<"--------------------------------------"<<std::endl;
243
244  return 0;
245}   
Note: See TracBrowser for help on using the repository browser.