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

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

merge split branch into trunk; fix some examples

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