Ignore:
Timestamp:
Jan 6, 2019 6:17:46 PM (10 months ago)
Author:
unxusr
Message:

formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/examples/nway.cpp

    r1898 r2469  
    66#include <cassert>
    77#include <iomanip>
    8 
    98
    109#include "CoinPragma.hpp"
     
    1716#include "CoinTime.hpp"
    1817
    19 
    2018/************************************************************************
    2119
     
    2523
    2624*************************************************************************/
    27 int main (int argc, const char *argv[])
     25int main(int argc, const char *argv[])
    2826{
    29  
     27
    3028  // Define your favorite OsiSolver
    31  
     29
    3230  OsiClpSolverInterface solver1;
    33  
     31
    3432  // Read in model using argv[1]
    3533  // and assert that it is a clean model
     
    4341  }
    4442#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;
     43  if (argc >= 2)
     44    mpsFileName = argv[1];
     45  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
     46  if (numMpsReadErrors != 0) {
     47    printf("%d errors reading MPS file\n", numMpsReadErrors);
     48    return numMpsReadErrors;
    5149  }
    52  
     50
    5351  int iRow, iColumn;
    5452  int numberColumns = solver1.getNumCols();
    5553  int numberRows = solver1.getNumRows();
    5654  // 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  
     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
    6664  // 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++) {
     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++) {
    7472        int iColumn = column[j];
    75         if (element[j]!=1.0||!solver1.isInteger(iColumn)||mark[iColumn]>=0||columnLower[iColumn]) {
    76           goodRow=false;
     73        if (element[j] != 1.0 || !solver1.isInteger(iColumn) || mark[iColumn] >= 0 || columnLower[iColumn]) {
     74          goodRow = false;
    7775          break;
    7876        }
     
    8078      if (goodRow) {
    8179        // mark all
    82         for (int j=rowStart[iRow];j<rowStart[iRow]+rowLength[iRow];j++) {
     80        for (int j = rowStart[iRow]; j < rowStart[iRow] + rowLength[iRow]; j++) {
    8381          int iColumn = column[j];
    84           mark[iColumn]=numberSOS;
     82          mark[iColumn] = numberSOS;
    8583        }
    8684        numberSOS++;
     
    8886    }
    8987  }
    90   std::cout<<numberSOS<<" SOS"<<std::endl;
     88  std::cout << numberSOS << " SOS" << std::endl;
    9189  if (!numberSOS)
    9290    return 0;
    9391  CbcModel model(solver1);
    9492  // Do sets and priorities
    95   CbcObject ** objects = new CbcObject * [numberSOS];
     93  CbcObject **objects = new CbcObject *[numberSOS];
    9694  int numberIntegers = model.numberIntegers();
    9795  /* model may not have created objects
    9896     If none then create
    9997  */
    100   if (!numberIntegers||!model.numberObjects()) {
     98  if (!numberIntegers || !model.numberObjects()) {
    10199    model.findIntegers(true);
    102100    numberIntegers = model.numberIntegers();
    103101  }
    104   int * priority = new int[numberSOS];
     102  int *priority = new int[numberSOS];
    105103  // Set SOS priorities high
    106   CoinFillN(priority,numberSOS,1);
     104  CoinFillN(priority, numberSOS, 1);
    107105  // 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;
     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;
    114112    }
    115113    // NULL uses 0,1,2 .. as weights
    116     objects[iSOS]= new CbcNWay(&model,n,which,iSOS);
     114    objects[iSOS] = new CbcNWay(&model, n, which, iSOS);
    117115  }
    118   delete [] mark;
    119   delete [] which;
    120   model.addObjects(numberSOS,objects);
    121   for (iColumn=0;iColumn<numberSOS;iColumn++)
     116  delete[] mark;
     117  delete[] which;
     118  model.addObjects(numberSOS, objects);
     119  for (iColumn = 0; iColumn < numberSOS; iColumn++)
    122120    delete objects[iColumn];
    123   delete [] objects;
    124   model.passInPriorities(priority,true);
    125   delete [] priority;
     121  delete[] objects;
     122  model.passInPriorities(priority, true);
     123  delete[] priority;
    126124
    127125  // If time is given then stop after that number of minutes
    128   if (argc>2) {
     126  if (argc > 2) {
    129127    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);
     128    std::cout << "Stopping after " << minutes << " minutes" << std::endl;
     129    assert(minutes >= 0);
     130    model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0 * minutes);
    133131  }
    134132  // Switch off most output
    135   model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
    136   if (model.getNumCols()<3000) {
     133  model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
     134  if (model.getNumCols() < 3000) {
    137135    model.messageHandler()->setLogLevel(1);
    138136    //model.solver()->messageHandler()->setLogLevel(0);
     
    142140  }
    143141  //model.messageHandler()->setLogLevel(1);
    144  
     142
    145143  double time1 = CoinCpuTime();
    146144
    147145  // Do complete search
    148  
     146
    149147  model.branchAndBound();
    150148
    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;
     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;
    156154
    157155  // Print solution - we can't get names from Osi!
    158156
    159   if (model.getMinimizationObjValue()<1.0e50) {
     157  if (model.getMinimizationObjValue() < 1.0e50) {
    160158    int numberColumns = model.solver()->getNumCols();
    161    
    162     const double * solution = model.solver()->getColSolution();
    163    
     159
     160    const double *solution = model.solver()->getColSolution();
     161
    164162    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;
     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;
    172170    }
    173     std::cout<<"--------------------------------------"<<std::endl;
    174  
    175     std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
     171    std::cout << "--------------------------------------" << std::endl;
     172
     173    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
    176174  }
    177175  return 0;
    178 }   
     176}
Note: See TracChangeset for help on using the changeset viewer.