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

Last change on this file since 1464 was 1464, checked in by stefan, 9 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: 6.4 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
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  /*  This example does not look to find the correct order.  SOS are much more
92      powerful if there is a genuine order e.g. size or time.
93
94      There are two pieces of code here.
95      1) Leave integrality conditions and add SOS as extra.  They should have
96      a higher priority.
97      2) Take off integrality conditions and do as SOS of type 2.  This is artificial
98      in this case.
99  */
100  //#define SOS2
101#ifndef SOS2
102  CbcModel model(solver1);
103  // Do sets and priorities
104  CbcObject ** objects = new CbcObject * [numberSOS];
105  int numberIntegers = model.numberIntegers();
106  /* model may not have created objects
107     If none then create
108  */
109  if (!numberIntegers||!model.numberObjects()) {
110    model.findIntegers(true);
111    numberIntegers = model.numberIntegers();
112  }
113  int * priority = new int[numberSOS];
114  // Set SOS priorities high
115  CoinFillN(priority,numberSOS,1);
116  // Set up SOS
117  int * which = new int[numberColumns];
118  for (int iSOS =0;iSOS<numberSOS;iSOS++) {
119    int n=0;
120    for (iColumn=0;iColumn<numberColumns;iColumn++) {
121      if (mark[iColumn]==iSOS)
122        which[n++]=iColumn;
123    }
124    // NULL uses 0,1,2 .. as weights
125    objects[iSOS]= new CbcSOS(&model,n,which,NULL,iSOS,1);
126  }
127#else
128  // take off integers
129    for (iColumn=0;iColumn<numberColumns;iColumn++) 
130      solver1.setContinuous(iColumn);
131  CbcModel model(solver1);
132  // Do sets and priorities
133  CbcObject ** objects = new CbcObject * [numberSOS];
134  // Set up SOS
135  int * which = new int[numberColumns];
136  for (int iSOS =0;iSOS<numberSOS;iSOS++) {
137    int n=0;
138    for (iColumn=0;iColumn<numberColumns;iColumn++) {
139      if (mark[iColumn]==iSOS)
140        which[n++]=iColumn;
141    }
142    // NULL uses 0,1,2 .. as weights
143    objects[iSOS]= new CbcSOS(&model,n,which,NULL,iSOS,2);
144  }
145#endif
146  delete [] mark;
147  delete [] which;
148  model.addObjects(numberSOS,objects);
149  for (iColumn=0;iColumn<numberSOS;iColumn++)
150    delete objects[iColumn];
151  delete [] objects;
152#ifndef SOS2
153  model.passInPriorities(priority,true);
154  delete [] priority;
155#endif
156
157  // If time is given then stop after that number of minutes
158  if (argc>2) {
159    int minutes = atoi(argv[2]);
160    std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl;
161    assert (minutes>=0);
162    model.setDblParam(CbcModel::CbcMaximumSeconds,60.0*minutes);
163  }
164  // Switch off most output
165  model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
166  if (model.getNumCols()<3000) {
167    model.messageHandler()->setLogLevel(1);
168    //model.solver()->messageHandler()->setLogLevel(0);
169  } else {
170    model.messageHandler()->setLogLevel(2);
171    model.solver()->messageHandler()->setLogLevel(1);
172  }
173  model.messageHandler()->setLogLevel(1);
174 
175  double time1 = CoinCpuTime();
176
177  // Do complete search
178 
179  model.branchAndBound();
180
181  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
182           <<model.getNodeCount()<<" nodes with objective "
183           <<model.getObjValue()
184           <<(!model.status() ? " Finished" : " Not finished")
185           <<std::endl;
186
187  // Print solution - we can't get names from Osi!
188
189  if (model.getMinimizationObjValue()<1.0e50) {
190    int numberColumns = model.solver()->getNumCols();
191   
192    const double * solution = model.solver()->getColSolution();
193   
194    int iColumn;
195    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
196   
197    std::cout<<"--------------------------------------"<<std::endl;
198    for (iColumn=0;iColumn<numberColumns;iColumn++) {
199      double value=solution[iColumn];
200#ifndef SOS2
201      if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) 
202        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
203#else
204      if (fabs(value)>1.0e-7) 
205        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
206#endif
207    }
208    std::cout<<"--------------------------------------"<<std::endl;
209 
210    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
211  }
212  return 0;
213}   
Note: See TracBrowser for help on using the repository browser.