source: trunk/Samples/crew.cpp @ 85

Last change on this file since 85 was 85, checked in by forrest, 15 years ago

example

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1// Copyright (C) 2005, 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// For Branch and bound
13#include "OsiClpSolverInterface.hpp"
14#include "CbcModel.hpp"
15#include "CbcBranchActual.hpp"
16#include "CbcBranchUser.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 = "../../Mps/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  // Redundant definition of default branching (as Default == User)
56  CbcBranchUserDecision branch;
57  model.setBranchingMethod(&branch);
58
59  // Definition of node choice
60  CbcCompareUser compare;
61  model.setNodeComparison(compare);
62
63
64  int iColumn;
65  int numberColumns = solver3->getNumCols();
66  /* We are going to add a single follow on object but we
67     want to give low priority to existing integers.
68     As the default priority is 1000 we don't actually need to give
69     integer priorities but it is here to show how.
70  */
71  // Normal integer priorities
72  int * priority = new int [numberColumns];
73  int numberIntegers=0;
74  for (iColumn=0;iColumn<numberColumns;iColumn++) {
75    if (solver3->isInteger(iColumn)) {
76      priority[numberIntegers++]= 100; // low priority
77    }
78  }
79  /* Second parameter is true if we are adding objects,
80     false if integers.  So this does integers */
81  model.passInPriorities(priority,false);
82  delete [] priority;
83  /* Add in objects before we can give priority.
84     In this case just one - but this shows general method
85  */
86  CbcObject ** objects = new CbcObject * [1];
87  objects[0]=new CbcFollowOn(&model);
88  model.addObjects(1,objects);
89  delete objects[0];
90  delete [] objects;
91  // High priority
92  int followPriority=1;
93  model.passInPriorities(&followPriority,true);
94
95  // Do initial solve to continuous
96  model.initialSolve();
97
98  // Do more strong branching if small
99  // Switch off strong branching if wanted
100  model.setNumberStrong(0);
101
102  model.solver()->setIntParam(OsiMaxNumIterationHotStart,100);
103
104  // Switch off most output
105  if (model.getNumCols()<3000) {
106    model.messageHandler()->setLogLevel(1);
107    //model.solver()->messageHandler()->setLogLevel(0);
108  } else {
109    model.messageHandler()->setLogLevel(2);
110    model.solver()->messageHandler()->setLogLevel(1);
111  }
112  //model.setPrintFrequency(50);
113
114  // Do complete search
115  try {
116    model.branchAndBound();
117  }
118  catch (CoinError e) {
119    e.print();
120    if (e.lineNumber()>=0)
121      std::cout<<"This was from a CoinAssert"<<std::endl;
122    exit(0);
123  }
124  //void printHowMany();
125  //printHowMany();
126  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
127           <<model.getNodeCount()<<" nodes with objective "
128           <<model.getObjValue()
129           <<(!model.status() ? " Finished" : " Not finished")
130           <<std::endl;
131
132  // Print solution if finished - we can't get names from Osi!
133
134  if (model.getMinimizationObjValue()<1.0e50) {
135    int numberColumns = model.solver()->getNumCols();
136   
137    const double * solution = model.solver()->getColSolution();
138   
139    int iColumn;
140    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
141   
142    std::cout<<"--------------------------------------"<<std::endl;
143    for (iColumn=0;iColumn<numberColumns;iColumn++) {
144      double value=solution[iColumn];
145      if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) 
146        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
147    }
148    std::cout<<"--------------------------------------"<<std::endl;
149 
150    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
151  }
152  return 0;
153}   
Note: See TracBrowser for help on using the repository browser.