source: trunk/Samples/driver.cpp @ 384

Last change on this file since 384 was 384, checked in by forrest, 15 years ago

add barrier

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1// Copyright (C) 2002,2003 International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#include "ClpSimplex.hpp"
5#include <iomanip>
6
7int main (int argc, const char *argv[])
8{
9  ClpSimplex  model;
10  int status;
11  // Keep names when reading an mps file
12  if (argc<2)
13    status=model.readMps("../../Mps/Sample/p0033.mps",true);
14  else
15    status=model.readMps(argv[1],true);
16
17  if (status) {
18    fprintf(stderr,"Bad readMps %s\n",argv[1]);
19    fprintf(stdout,"Bad readMps %s\n",argv[1]);
20    exit(1);
21  }
22#ifdef STYLE1
23  if (argc<3 ||!strstr(argv[2],"primal")) {
24    // Use the dual algorithm unless user said "primal"
25    model.initialDualSolve();
26  } else {
27    model.initialPrimalSolve();
28  }
29#else
30  ClpSolve solvectl;
31
32
33  if (argc<3 ||(!strstr(argv[2],"primal")&&!strstr(argv[2],"barrier"))) {
34    // Use the dual algorithm unless user said "primal" or "barrier"
35    std::cout << std::endl << " Solve using Dual: " << std::endl;
36    solvectl.setSolveType(ClpSolve::useDual);
37    solvectl.setPresolveType(ClpSolve::presolveOn);
38    model.initialSolve(solvectl);
39  } else if (strstr(argv[2],"barrier")) {
40    // Use the barrier algorithm if user said "barrier"
41    std::cout << std::endl << " Solve using Barrier: " << std::endl;
42    solvectl.setSolveType(ClpSolve::useBarrier);
43    solvectl.setPresolveType(ClpSolve::presolveOn);
44    model.initialSolve(solvectl);
45  } else {
46    std::cout << std::endl << " Solve using Primal: " << std::endl;
47    solvectl.setSolveType(ClpSolve::usePrimal);
48    solvectl.setPresolveType(ClpSolve::presolveOn);
49    model.initialSolve(solvectl);
50  }
51#endif
52  std::string modelName;
53  model.getStrParam(ClpProbName,modelName);
54  std::cout<<"Model "<<modelName<<" has "<<model.numberRows()<<" rows and "<<
55    model.numberColumns()<<" columns"<<std::endl;
56
57  // remove this to print solution
58
59  exit(0);
60
61  /*
62    Now to print out solution.  The methods used return modifiable
63    arrays while the alternative names return const pointers -
64    which is of course much more virtuous.
65
66    This version just does non-zero columns
67 
68   */
69#if 0
70  int numberRows = model.numberRows();
71
72  // Alternatively getRowActivity()
73  double * rowPrimal = model.primalRowSolution();
74  // Alternatively getRowPrice()
75  double * rowDual = model.dualRowSolution();
76  // Alternatively getRowLower()
77  double * rowLower = model.rowLower();
78  // Alternatively getRowUpper()
79  double * rowUpper = model.rowUpper();
80  // Alternatively getRowObjCoefficients()
81  double * rowObjective = model.rowObjective();
82   
83  // If we have not kept names (parameter to readMps) this will be 0
84  assert(model.lengthNames());
85
86  // Row names
87  const std::vector<std::string> * rowNames = model.rowNames();
88
89
90  int iRow;
91
92  std::cout<<"                       Primal          Dual         Lower         Upper        (Cost)"
93           <<std::endl;
94
95  for (iRow=0;iRow<numberRows;iRow++) {
96    double value;
97    std::cout<<std::setw(6)<<iRow<<" "<<std::setw(8)<<(*rowNames)[iRow];
98    value = rowPrimal[iRow];
99    if (fabs(value)<1.0e5)
100      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
101    else
102      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
103    value = rowDual[iRow];
104    if (fabs(value)<1.0e5)
105      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
106    else
107      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
108    value = rowLower[iRow];
109    if (fabs(value)<1.0e5)
110      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
111    else
112      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
113    value = rowUpper[iRow];
114    if (fabs(value)<1.0e5)
115      std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
116    else
117      std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
118    if (rowObjective) {
119      value = rowObjective[iRow];
120      if (fabs(value)<1.0e5)
121        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
122      else
123        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
124    }
125    std::cout<<std::endl;
126  }
127#endif
128  std::cout<<"--------------------------------------"<<std::endl;
129
130  // Columns
131
132  int numberColumns = model.numberColumns();
133
134  // Alternatively getColSolution()
135  double * columnPrimal = model.primalColumnSolution();
136  // Alternatively getReducedCost()
137  double * columnDual = model.dualColumnSolution();
138  // Alternatively getColLower()
139  double * columnLower = model.columnLower();
140  // Alternatively getColUpper()
141  double * columnUpper = model.columnUpper();
142  // Alternatively getObjCoefficients()
143  double * columnObjective = model.objective();
144   
145  // If we have not kept names (parameter to readMps) this will be 0
146  assert(model.lengthNames());
147
148  // Column names
149  const std::vector<std::string> * columnNames = model.columnNames();
150
151
152  int iColumn;
153 
154  std::cout<<"                       Primal          Dual         Lower         Upper          Cost"
155           <<std::endl;
156
157  for (iColumn=0;iColumn<numberColumns;iColumn++) {
158    double value;
159    value = columnPrimal[iColumn];
160    if (fabs(value)>1.0e-8) {
161      std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<(*columnNames)[iColumn];
162      if (fabs(value)<1.0e5)
163        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
164      else
165        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
166      value = columnDual[iColumn];
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      value = columnLower[iColumn];
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      value = columnUpper[iColumn];
177      if (fabs(value)<1.0e5)
178        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
179      else
180        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
181      value = columnObjective[iColumn];
182      if (fabs(value)<1.0e5)
183        std::cout<<setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14)<<value;
184      else
185        std::cout<<setiosflags(std::ios::scientific)<<std::setw(14)<<value;
186     
187      std::cout<<std::endl;
188    }
189  }
190  std::cout<<"--------------------------------------"<<std::endl;
191
192  return 0;
193}   
Note: See TracBrowser for help on using the repository browser.