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

Last change on this file since 739 was 333, checked in by andreasw, 14 years ago

finished examples subdir

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