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

Last change on this file since 1854 was 1854, checked in by stefan, 6 years ago

fix svn keywords property

  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1// $Id: driver3.cpp 1854 2013-01-28 00:02:55Z 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#if defined(_MSC_VER)
7// Turn off compiler warning about long names
8#  pragma warning(disable:4786)
9#endif
10
11#include <cassert>
12#include <iomanip>
13
14#include "CbcModel.hpp"
15#include "OsiClpSolverInterface.hpp"
16#include "CbcBranchDynamic.hpp"
17
18#include  "CoinTime.hpp"
19
20//#############################################################################
21
22
23/************************************************************************
24
25This main program shows how to take advantage of the standalone cbc in your program.
26It should perform very nearly the same as cbc 
27First it reads in an integer model from an mps file and saves and strips off integer information.
28Then it initializes the integer model with cbc defaults
29Then it puts back integers - here you could do anything and also set parameters
30Then it calls CbcMain1 passing all parameters apart from first
31Finally it prints solution
32
33************************************************************************/
34
35int main (int argc, const char *argv[])
36{
37
38  OsiClpSolverInterface solver1;
39  //#define USE_OSI_NAMES
40#ifdef USE_OSI_NAMES
41  // Say we are keeping names (a bit slower this way)
42  solver1.setIntParam(OsiNameDiscipline,1);
43#endif
44  // Read in model using argv[1]
45  // and assert that it is a clean model
46  std::string mpsFileName;
47#if defined(SAMPLEDIR)
48  mpsFileName = SAMPLEDIR "/p0033.mps";
49#else
50  if (argc < 2) {
51    fprintf(stderr, "Do not know where to find sample MPS files.\n");
52    exit(1);
53  }
54#endif
55  if (argc>=2) mpsFileName = argv[1];
56  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
57  assert(numMpsReadErrors==0);
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.