source: trunk/Cbc/examples/nway.cpp @ 1468

Last change on this file since 1468 was 1468, checked in by stefan, 11 years ago

do not require CbcConfig?.h in example to decide whether sample or miplib3 is present - do this in makefile

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 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 "CbcModel.hpp"
14#include "CbcBranchActual.hpp"
15#include "OsiClpSolverInterface.hpp"
16
17// Time
18#include "CoinTime.hpp"
19
20
21/************************************************************************
22
23This main program reads in an integer model from an mps file.
24
25It then tries to find SOS structure and solves using N-way variables
26
27*************************************************************************/
28int main (int argc, const char *argv[])
29{
30 
31  // Define your favorite OsiSolver
32 
33  OsiClpSolverInterface solver1;
34 
35  // Read in model using argv[1]
36  // and assert that it is a clean model
37  std::string mpsFileName;
38#if defined(MIPLIB3DIR)
39  mpsFileName = MIPLIB3DIR "/10teams";
40#else
41  if (argc < 2) {
42    fprintf(stderr, "Do not know where to find miplib3 MPS files.\n");
43    exit(1);
44  }
45#endif
46  if (argc>=2) mpsFileName = argv[1];
47  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
48  assert(numMpsReadErrors==0);
49 
50  int iRow, iColumn;
51  int numberColumns = solver1.getNumCols();
52  int numberRows = solver1.getNumRows();
53  // get row copy
54  const CoinPackedMatrix * matrix = solver1.getMatrixByRow();
55  const double * element = matrix->getElements();
56  const int * column = matrix->getIndices();
57  const CoinBigIndex * rowStart = matrix->getVectorStarts();
58  const int * rowLength = matrix->getVectorLengths();
59  const double * rowLower = solver1.getRowLower();
60  const double * rowUpper = solver1.getRowUpper();
61  const double * columnLower = solver1.getColLower();
62 
63  // Look for possible SOS
64  int numberSOS=0;
65  int * mark = new int[numberColumns];
66  CoinFillN(mark,numberColumns,-1);
67  for (iRow=0;iRow<numberRows;iRow++) {
68    if (rowLower[iRow]==1.0&&rowUpper[iRow]==1.0) {
69      bool goodRow=true;
70      for (int j=rowStart[iRow];j<rowStart[iRow]+rowLength[iRow];j++) {
71        int iColumn = column[j];
72        if (element[j]!=1.0||!solver1.isInteger(iColumn)||mark[iColumn]>=0||columnLower[iColumn]) {
73          goodRow=false;
74          break;
75        }
76      }
77      if (goodRow) {
78        // mark all
79        for (int j=rowStart[iRow];j<rowStart[iRow]+rowLength[iRow];j++) {
80          int iColumn = column[j];
81          mark[iColumn]=numberSOS;
82        }
83        numberSOS++;
84      }
85    }
86  }
87  std::cout<<numberSOS<<" SOS"<<std::endl;
88  if (!numberSOS)
89    return 0;
90  CbcModel model(solver1);
91  // Do sets and priorities
92  CbcObject ** objects = new CbcObject * [numberSOS];
93  int numberIntegers = model.numberIntegers();
94  /* model may not have created objects
95     If none then create
96  */
97  if (!numberIntegers||!model.numberObjects()) {
98    model.findIntegers(true);
99    numberIntegers = model.numberIntegers();
100  }
101  int * priority = new int[numberSOS];
102  // Set SOS priorities high
103  CoinFillN(priority,numberSOS,1);
104  // Set up SOS
105  int * which = new int[numberColumns];
106  for (int iSOS =0;iSOS<numberSOS;iSOS++) {
107    int n=0;
108    for (iColumn=0;iColumn<numberColumns;iColumn++) {
109      if (mark[iColumn]==iSOS)
110        which[n++]=iColumn;
111    }
112    // NULL uses 0,1,2 .. as weights
113    objects[iSOS]= new CbcNWay(&model,n,which,iSOS);
114  }
115  delete [] mark;
116  delete [] which;
117  model.addObjects(numberSOS,objects);
118  for (iColumn=0;iColumn<numberSOS;iColumn++)
119    delete objects[iColumn];
120  delete [] objects;
121  model.passInPriorities(priority,true);
122  delete [] priority;
123
124  // If time is given then stop after that number of minutes
125  if (argc>2) {
126    int minutes = atoi(argv[2]);
127    std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl;
128    assert (minutes>=0);
129    model.setDblParam(CbcModel::CbcMaximumSeconds,60.0*minutes);
130  }
131  // Switch off most output
132  model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
133  if (model.getNumCols()<3000) {
134    model.messageHandler()->setLogLevel(1);
135    //model.solver()->messageHandler()->setLogLevel(0);
136  } else {
137    model.messageHandler()->setLogLevel(2);
138    //model.solver()->messageHandler()->setLogLevel(1);
139  }
140  //model.messageHandler()->setLogLevel(1);
141 
142  double time1 = CoinCpuTime();
143
144  // Do complete search
145 
146  model.branchAndBound();
147
148  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
149           <<model.getNodeCount()<<" nodes with objective "
150           <<model.getObjValue()
151           <<(!model.status() ? " Finished" : " Not finished")
152           <<std::endl;
153
154  // Print solution - we can't get names from Osi!
155
156  if (model.getMinimizationObjValue()<1.0e50) {
157    int numberColumns = model.solver()->getNumCols();
158   
159    const double * solution = model.solver()->getColSolution();
160   
161    int iColumn;
162    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
163   
164    std::cout<<"--------------------------------------"<<std::endl;
165    for (iColumn=0;iColumn<numberColumns;iColumn++) {
166      double value=solution[iColumn];
167      if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) 
168        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
169    }
170    std::cout<<"--------------------------------------"<<std::endl;
171 
172    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
173  }
174  return 0;
175}   
Note: See TracBrowser for help on using the repository browser.