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

Last change on this file since 1898 was 1898, checked in by stefan, 5 years ago

fixup examples

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