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

Last change on this file since 1898 was 1898, checked in by stefan, 6 years ago

fixup examples

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