source: trunk/Cbc/examples/crew.cpp @ 1173

Last change on this file since 1173 was 1173, checked in by forrest, 10 years ago

add $id

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1/* $Id: crew.cpp 1173 2009-06-04 09:44:10Z forrest $ */
2// Copyright (C) 2005, International Business Machines
3// Corporation and others.  All Rights Reserved.
4#if defined(_MSC_VER)
5// Turn off compiler warning about long names
6#  pragma warning(disable:4786)
7#endif
8
9#include <cassert>
10#include <iomanip>
11
12
13// For Branch and bound
14#include "OsiClpSolverInterface.hpp"
15#include "CbcModel.hpp"
16#include "CbcBranchActual.hpp"
17#include "CbcCompareUser.hpp"
18
19#include  "CoinTime.hpp"
20
21/************************************************************************
22
23This main program reads in an integer model from an mps file.
24It expects it to be unit coefficients and unit rhs.
25
26Branching is follow-on branching plus simple binary branching on integer variables.
27
28*/
29int main (int argc, const char *argv[])
30{
31
32  // Define a Solver for long thin problems
33 
34  OsiClpSolverInterface solver1;
35
36  // Read in model using argv[1]
37  // and assert that it is a clean model
38  std::string mpsFileName = "../../Data/Sample/p0033.mps";
39  if (argc>=2) mpsFileName = argv[1];
40  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
41  assert(numMpsReadErrors==0);
42  double time1 = CoinCpuTime();
43
44  solver1.initialSolve();
45  // Reduce printout
46  solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry);
47
48  OsiSolverInterface * solver2=&solver1;
49  CbcModel model(*solver2);
50  // Point to solver
51  OsiSolverInterface * solver3 = model.solver();
52  OsiClpSolverInterface * osiclp = dynamic_cast< OsiClpSolverInterface*> (solver3);
53  assert (osiclp);
54
55  // Definition of node choice
56  CbcCompareUser compare;
57  model.setNodeComparison(compare);
58
59
60  int iColumn;
61  int numberColumns = solver3->getNumCols();
62  /* We are going to add a single follow on object but we
63     want to give low priority to existing integers.
64     As the default priority is 1000 we don't actually need to give
65     integer priorities but it is here to show how.
66  */
67  // Normal integer priorities
68  int * priority = new int [numberColumns];
69  int numberIntegers=0;
70  for (iColumn=0;iColumn<numberColumns;iColumn++) {
71    if (solver3->isInteger(iColumn)) {
72      priority[numberIntegers++]= 100; // low priority
73    }
74  }
75  /* Second parameter is true if we are adding objects,
76     false if integers.  So this does integers */
77  model.passInPriorities(priority,false);
78  delete [] priority;
79  /* Add in objects before we can give priority.
80     In this case just one - but this shows general method
81  */
82  CbcObject ** objects = new CbcObject * [1];
83  objects[0]=new CbcFollowOn(&model);
84  model.addObjects(1,objects);
85  delete objects[0];
86  delete [] objects;
87  // High priority
88  int followPriority=1;
89  model.passInPriorities(&followPriority,true);
90
91  // Do initial solve to continuous
92  model.initialSolve();
93
94  // Do more strong branching if small
95  // Switch off strong branching if wanted
96  model.setNumberStrong(0);
97
98  model.solver()->setIntParam(OsiMaxNumIterationHotStart,100);
99
100  // Switch off most output
101  if (model.getNumCols()<3000) {
102    model.messageHandler()->setLogLevel(1);
103    //model.solver()->messageHandler()->setLogLevel(0);
104  } else {
105    model.messageHandler()->setLogLevel(2);
106    model.solver()->messageHandler()->setLogLevel(1);
107  }
108  //model.setPrintFrequency(50);
109
110  // Do complete search
111  try {
112    model.branchAndBound();
113  }
114  catch (CoinError e) {
115    e.print();
116    if (e.lineNumber()>=0)
117      std::cout<<"This was from a CoinAssert"<<std::endl;
118    exit(0);
119  }
120  //void printHowMany();
121  //printHowMany();
122  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
123           <<model.getNodeCount()<<" nodes with objective "
124           <<model.getObjValue()
125           <<(!model.status() ? " Finished" : " Not finished")
126           <<std::endl;
127
128  // Print solution if finished - we can't get names from Osi!
129
130  if (model.getMinimizationObjValue()<1.0e50) {
131    int numberColumns = model.solver()->getNumCols();
132   
133    const double * solution = model.solver()->getColSolution();
134   
135    int iColumn;
136    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
137   
138    std::cout<<"--------------------------------------"<<std::endl;
139    for (iColumn=0;iColumn<numberColumns;iColumn++) {
140      double value=solution[iColumn];
141      if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) 
142        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
143    }
144    std::cout<<"--------------------------------------"<<std::endl;
145 
146    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
147  }
148  return 0;
149}   
Note: See TracBrowser for help on using the repository browser.