source: branches/devel/Cbc/examples/dynamic.cpp @ 409

Last change on this file since 409 was 409, checked in by forrest, 13 years ago

now that I understand .am

File size: 3.4 KB
Line 
1// Copyright (C) 2006, 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 "OsiSolverInterface.hpp"
14#include "CbcModel.hpp"
15#include "ClpDynamicInterface.hpp"
16// Time
17#include "CoinTime.hpp"
18
19
20/************************************************************************
21
22This main program reads in mkc from an mps file.
23
24It then decomposes problem - subproblems are multi-color knapsack
25
26This is to try and let Cbc use a dynamic model
27
28************************************************************************/
29
30int main (int argc, const char *argv[])
31{
32
33  // Define a Solver which inherits from OsiClpsolverInterface -> OsiSolverInterface
34 
35  ClpDynamicInterface solver1;
36
37  // Read in model using argv[1]
38  // and assert that it is a clean model
39  std::string mpsFileName = "../../Data/miplib3/mkc";
40  if (argc>=2) mpsFileName = argv[1];
41  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
42  assert(numMpsReadErrors==0);
43
44  // This clones solver
45  CbcModel model(solver1);
46  OsiSolverInterface * solver2 = model.solver();
47  ClpDynamicInterface * osiclp = dynamic_cast< ClpDynamicInterface*> (solver2);
48  assert (osiclp);
49  // Setup data
50  int numberRows = osiclp->getNumRows();
51  int * block = new int[numberRows];
52  int i;
53  for (i=0;i<numberRows;i++)
54    block[i]=-2;
55  if (numberRows==67) {
56    // ltw
57    for (i=0;i<58;i++)
58      block[i]=-1;
59    block[66]=-1;
60  } else if (numberRows==3411) {
61    // mkc
62    block[0]=-1;
63    block[1]=-1;
64    for (i=26;i<465;i++)
65      block[i]=-1;
66  } else {
67    printf("unknown model\n");
68    exit(1);;
69  }
70
71  // Now that we know master rows let solver decompose it
72  osiclp->initialize(block,&model);
73  delete [] block;
74
75  // Allow fake heuristic to move serendipity solutions across
76  CbcHeuristicDynamic heuristic2(model);
77  model.addHeuristic(&heuristic2);
78
79  // Do initial solve to continuous
80  model.initialSolve();
81
82  // Switch off strong branching and trust
83  model.setNumberStrong(0);
84  model.setNumberBeforeTrust(0);
85  model.messageHandler()->setLogLevel(2);
86  model.solver()->messageHandler()->setLogLevel(1);
87 
88  double time1 = CoinCpuTime();
89
90  // Do complete search
91 
92  model.branchAndBound();
93
94  std::cout<<argv[1]<<" took "<<CoinCpuTime()-time1<<" seconds, "
95           <<model.getNodeCount()<<" nodes with objective "
96           <<model.getObjValue()
97           <<(!model.status() ? " Finished" : " Not finished")
98           <<std::endl;
99  // Print solution if finished - we can't get names from Osi!
100
101  if (!model.status()&&model.getMinimizationObjValue()<1.0e50) {
102    int numberColumns = model.solver()->getNumCols();
103   
104    const double * solution = model.solver()->getColSolution();
105   
106    int iColumn;
107    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
108   
109    std::cout<<"--------------------------------------"<<std::endl;
110    for (iColumn=0;iColumn<numberColumns;iColumn++) {
111      double value=solution[iColumn];
112      if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) 
113        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
114    }
115    std::cout<<"--------------------------------------"<<std::endl;
116 
117    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
118  }
119  return 0;
120}   
Note: See TracBrowser for help on using the repository browser.