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

Last change on this file since 1468 was 1468, checked in by stefan, 9 years ago

do not require CbcConfig?.h in example to decide whether sample or miplib3 is present - do this in makefile

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