source: trunk/Cbc/examples/driver3.cpp @ 697

Last change on this file since 697 was 697, checked in by forrest, 12 years ago

add example of using standalone solver as function

File size: 4.3 KB
Line 
1// Copyright (C) 2007, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#if defined(_MSC_VER)
4// Turn off compiler warning about long names
5#  pragma warning(disable:4786)
6#endif
7
8#include <cassert>
9#include <iomanip>
10
11
12#include "CbcModel.hpp"
13#include "OsiClpSolverInterface.hpp"
14#include "CbcBranchDynamic.hpp"
15
16#include  "CoinTime.hpp"
17
18//#############################################################################
19
20
21/************************************************************************
22
23This main program shows how to take advantage of the standalone cbc in your program.
24It should perform very nearly the same as cbc 
25First it reads in an integer model from an mps file and saves and strips off integer information.
26Then it initializes the integer model with cbc defaults
27Then it puts back integers - here you could do anything and also set parameters
28Then it calls CbcMain1 passing all parameters apart from first
29Finally it prints solution
30
31************************************************************************/
32
33int main (int argc, const char *argv[])
34{
35
36  OsiClpSolverInterface solver1;
37  //#define USE_OSI_NAMES
38#ifdef USE_OSI_NAMES
39  // Say we are keeping names (a bit slower this way)
40  solver1.setIntParam(OsiNameDiscipline,1);
41#endif
42  // Read in model using argv[1]
43  // and assert that it is a clean model
44  std::string mpsFileName = "../../Data/Sample/p0033.mps";
45  if (argc>=2) mpsFileName = argv[1];
46  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
47  assert(numMpsReadErrors==0);
48
49  // Strip off integer information and save
50  int numberColumns = solver1.getNumCols();
51  char * integer = new char[numberColumns];
52  int i;
53  for (i=0;i<numberColumns;i++) {
54    if (solver1.isInteger(i)) {
55      integer[i]=1;
56      solver1.setContinuous(i);
57    } else {
58      integer[i]=0;
59    }
60  }
61  // Pass to Cbc initialize defaults
62  CbcModel model(solver1);   
63  CbcMain0(model);
64
65  // Solve just to show there are no integers
66  model.branchAndBound();
67  // Set cutoff etc back in model and solver
68  model.resetModel();
69  // Solver was cloned so get it
70  OsiSolverInterface * solver = model.solver();
71  // Put back integers.  Here the user could do anything really
72#define ADD_DIRECTLY
73#ifndef ADD_DIRECTLY
74  for (i=0;i<numberColumns;i++) {
75    if (integer[i])
76      solver->setInteger(i);
77  }
78#else
79  CbcObject ** objects = new CbcObject * [ numberColumns];
80  int n=0;
81  for (i=0;i<numberColumns;i++) {
82    if (integer[i]) {
83      CbcSimpleIntegerDynamicPseudoCost * newObject =
84        new CbcSimpleIntegerDynamicPseudoCost(&model,i);
85      objects[n++]=newObject;
86    }
87  }
88  model.addObjects(n,objects);
89  for (i=0;i<n;i++)
90    delete objects[i];
91  delete [] objects;
92#endif
93  delete [] integer;
94  /* Now go into code for standalone solver
95     Could copy arguments and add -quit at end to be safe
96     but this will do
97  */
98  if (argc>2) {
99    CbcMain1(argc-1,argv+1,model);
100  } else {
101    const char * argv2[]={"driver3","-solve","-quit"};
102    CbcMain1(3,argv2,model);
103  }
104
105  // Print solution if finished (could get from model.bestSolution() as well
106
107  if (solver->getObjValue()*solver->getObjSense()<1.0e50) {
108   
109    const double * solution = solver->getColSolution();
110   
111    int iColumn;
112    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
113   
114    std::cout<<"--------------------------------------"<<std::endl;
115#ifdef USE_OSI_NAMES
116   
117    for (iColumn=0;iColumn<numberColumns;iColumn++) {
118      double value=solution[iColumn];
119      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
120        std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver->getColName(iColumn)
121                 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
122    }
123#else
124    // names may not be in current solver - use original
125   
126    for (iColumn=0;iColumn<numberColumns;iColumn++) {
127      double value=solution[iColumn];
128      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
129        std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver1.getModelPtr()->columnName(iColumn)
130                 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
131    }
132#endif
133    std::cout<<"--------------------------------------"<<std::endl;
134 
135    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
136  }
137  return 0;
138}   
Note: See TracBrowser for help on using the repository browser.