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

formatting

File:
1 edited

Legend:

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

    r1899 r2469  
    55
    66#include <cassert>
    7 #include <iomanip>
    8 
     7#include <iomanip>
    98
    109#include "CoinPragma.hpp"
     
    1615
    1716//#############################################################################
    18 
    1917
    2018/************************************************************************
     
    6260   or you can check solver status.
    6361*/
    64 /* Return non-zero to return quickly */   
    65 static int callBack(CbcModel * model, int whereFrom)
    66 {
    67   int returnCode=0;
     62/* Return non-zero to return quickly */
     63static int callBack(CbcModel *model, int whereFrom)
     64{
     65  int returnCode = 0;
    6866  switch (whereFrom) {
    6967  case 1:
    7068  case 2:
    71     if (!model->status()&&model->secondaryStatus())
    72       returnCode=1;
     69    if (!model->status() && model->secondaryStatus())
     70      returnCode = 1;
    7371    break;
    74   case 3:
    75     {
    76       //CbcCompareUser compare;
    77       //model->setNodeComparison(compare);
    78     }
    79     break;
     72  case 3: {
     73    //CbcCompareUser compare;
     74    //model->setNodeComparison(compare);
     75  } break;
    8076  case 4:
    8177    // If not good enough could skip postprocessing
     
    8985}
    9086#include "CbcEventHandler.hpp"
    91 static int cancelAsap=0;
     87static int cancelAsap = 0;
    9288/*
    9389  0 - not yet in Cbc
     
    9591  2 - ending Cbc
    9692*/
    97 static int statusOfCbc=0;
     93static int statusOfCbc = 0;
    9894#include "CoinSignal.hpp"
    99 static CoinSighandler_t saveSignal = static_cast<CoinSighandler_t> (0);
     95static CoinSighandler_t saveSignal = static_cast< CoinSighandler_t >(0);
    10096
    10197extern "C" {
    102      static void
     98static void
    10399#if defined(_MSC_VER)
    104      __cdecl
     100  __cdecl
    105101#endif // _MSC_VER
    106      signal_handler(int /*whichSignal*/)
    107      {
    108        cancelAsap=3;
    109        return;
    110      }
     102  signal_handler(int /*whichSignal*/)
     103{
     104  cancelAsap = 3;
     105  return;
     106}
    111107}
    112108/** This is so user can trap events and do useful stuff. 
     
    117113
    118114class MyEventHandler3 : public CbcEventHandler {
    119  
     115
    120116public:
    121117  /**@name Overrides */
     
    129125  MyEventHandler3();
    130126  /// Constructor with pointer to model (redundant as setEventHandler does)
    131   MyEventHandler3(CbcModel * model);
     127  MyEventHandler3(CbcModel *model);
    132128  /** Destructor */
    133129  virtual ~MyEventHandler3();
    134130  /** The copy constructor. */
    135   MyEventHandler3(const MyEventHandler3 & rhs);
     131  MyEventHandler3(const MyEventHandler3 &rhs);
    136132  /// Assignment
    137   MyEventHandler3& operator=(const MyEventHandler3 & rhs);
     133  MyEventHandler3 &operator=(const MyEventHandler3 &rhs);
    138134  /// Clone
    139   virtual CbcEventHandler * clone() const ;
     135  virtual CbcEventHandler *clone() const;
    140136  //@}
    141    
    142    
     137
    143138protected:
    144139  // data goes here
    145140};
    146141//-------------------------------------------------------------------
    147 // Default Constructor 
    148 //-------------------------------------------------------------------
    149 MyEventHandler3::MyEventHandler3 ()
     142// Default Constructor
     143//-------------------------------------------------------------------
     144MyEventHandler3::MyEventHandler3()
    150145  : CbcEventHandler()
    151146{
     
    153148
    154149//-------------------------------------------------------------------
    155 // Copy constructor 
    156 //-------------------------------------------------------------------
    157 MyEventHandler3::MyEventHandler3 (const MyEventHandler3 & rhs)
    158 : CbcEventHandler(rhs)
    159 { 
     150// Copy constructor
     151//-------------------------------------------------------------------
     152MyEventHandler3::MyEventHandler3(const MyEventHandler3 &rhs)
     153  : CbcEventHandler(rhs)
     154{
    160155}
    161156
    162157// Constructor with pointer to model
    163 MyEventHandler3::MyEventHandler3(CbcModel * model)
     158MyEventHandler3::MyEventHandler3(CbcModel *model)
    164159  : CbcEventHandler(model)
    165160{
     
    167162
    168163//-------------------------------------------------------------------
    169 // Destructor 
    170 //-------------------------------------------------------------------
    171 MyEventHandler3::~MyEventHandler3 ()
     164// Destructor
     165//-------------------------------------------------------------------
     166MyEventHandler3::~MyEventHandler3()
    172167{
    173168}
    174169
    175170//----------------------------------------------------------------
    176 // Assignment operator 
     171// Assignment operator
    177172//-------------------------------------------------------------------
    178173MyEventHandler3 &
    179 MyEventHandler3::operator=(const MyEventHandler3& rhs)
     174MyEventHandler3::operator=(const MyEventHandler3 &rhs)
    180175{
    181176  if (this != &rhs) {
     
    187182// Clone
    188183//-------------------------------------------------------------------
    189 CbcEventHandler * MyEventHandler3::clone() const
     184CbcEventHandler *MyEventHandler3::clone() const
    190185{
    191186  return new MyEventHandler3(*this);
    192187}
    193188
    194 CbcEventHandler::CbcAction 
     189CbcEventHandler::CbcAction
    195190MyEventHandler3::event(CbcEvent whichEvent)
    196191{
    197   if(!statusOfCbc) {
     192  if (!statusOfCbc) {
    198193    // override signal handler
    199194    // register signal handler
    200195    saveSignal = signal(SIGINT, signal_handler);
    201     statusOfCbc=1;
    202   }
    203   if ( (cancelAsap&2)!=0 ) {
     196    statusOfCbc = 1;
     197  }
     198  if ((cancelAsap & 2) != 0) {
    204199    printf("Cbc got cancel\n");
    205200    // switch off Clp cancel
     
    209204  // If in sub tree carry on
    210205  if (!model_->parentModel()) {
    211     if (whichEvent==endSearch&&statusOfCbc==1) {
     206    if (whichEvent == endSearch && statusOfCbc == 1) {
    212207      // switch off cancel
    213       cancelAsap=0;
     208      cancelAsap = 0;
    214209      // restore signal handler
    215210      signal(SIGINT, saveSignal);
    216       statusOfCbc=2;
     211      statusOfCbc = 2;
    217212    }
    218     if (whichEvent==solution||whichEvent==heuristicSolution) {
     213    if (whichEvent == solution || whichEvent == heuristicSolution) {
    219214#ifdef STOP_EARLY
    220215      return stop; // say finished
     
    223218      // If preprocessing was done solution will be to processed model
    224219      int numberColumns = model_->getNumCols();
    225       const double * bestSolution = model_->bestSolution();
    226       assert (bestSolution);
    227       printf("value of solution is %g\n",model_->getObjValue());
    228       for (int i=0;i<numberColumns;i++) {
    229         if (fabs(bestSolution[i])>1.0e-8)
    230           printf("%d %g\n",i,bestSolution[i]);
     220      const double *bestSolution = model_->bestSolution();
     221      assert(bestSolution);
     222      printf("value of solution is %g\n", model_->getObjValue());
     223      for (int i = 0; i < numberColumns; i++) {
     224        if (fabs(bestSolution[i]) > 1.0e-8)
     225          printf("%d %g\n", i, bestSolution[i]);
    231226      }
    232227#endif
     
    237232    }
    238233  } else {
    239       return noAction; // carry on
     234    return noAction; // carry on
    240235  }
    241236}
     
    247242
    248243class MyEventHandler4 : public ClpEventHandler {
    249  
     244
    250245public:
    251246  /**@name Overrides */
     
    259254  MyEventHandler4();
    260255  /// Constructor with pointer to model (redundant as setEventHandler does)
    261   MyEventHandler4(ClpSimplex * model);
     256  MyEventHandler4(ClpSimplex *model);
    262257  /** Destructor */
    263258  virtual ~MyEventHandler4();
    264259  /** The copy constructor. */
    265   MyEventHandler4(const MyEventHandler4 & rhs);
     260  MyEventHandler4(const MyEventHandler4 &rhs);
    266261  /// Assignment
    267   MyEventHandler4& operator=(const MyEventHandler4 & rhs);
     262  MyEventHandler4 &operator=(const MyEventHandler4 &rhs);
    268263  /// Clone
    269   virtual ClpEventHandler * clone() const ;
     264  virtual ClpEventHandler *clone() const;
    270265  //@}
    271    
    272    
     266
    273267protected:
    274268  // data goes here
    275269};
    276270//-------------------------------------------------------------------
    277 // Default Constructor 
    278 //-------------------------------------------------------------------
    279 MyEventHandler4::MyEventHandler4 ()
     271// Default Constructor
     272//-------------------------------------------------------------------
     273MyEventHandler4::MyEventHandler4()
    280274  : ClpEventHandler()
    281275{
     
    283277
    284278//-------------------------------------------------------------------
    285 // Copy constructor 
    286 //-------------------------------------------------------------------
    287 MyEventHandler4::MyEventHandler4 (const MyEventHandler4 & rhs)
    288 : ClpEventHandler(rhs)
    289 { 
     279// Copy constructor
     280//-------------------------------------------------------------------
     281MyEventHandler4::MyEventHandler4(const MyEventHandler4 &rhs)
     282  : ClpEventHandler(rhs)
     283{
    290284}
    291285
    292286// Constructor with pointer to model
    293 MyEventHandler4::MyEventHandler4(ClpSimplex * model)
     287MyEventHandler4::MyEventHandler4(ClpSimplex *model)
    294288  : ClpEventHandler(model)
    295289{
     
    297291
    298292//-------------------------------------------------------------------
    299 // Destructor 
    300 //-------------------------------------------------------------------
    301 MyEventHandler4::~MyEventHandler4 ()
     293// Destructor
     294//-------------------------------------------------------------------
     295MyEventHandler4::~MyEventHandler4()
    302296{
    303297}
    304298
    305299//----------------------------------------------------------------
    306 // Assignment operator 
     300// Assignment operator
    307301//-------------------------------------------------------------------
    308302MyEventHandler4 &
    309 MyEventHandler4::operator=(const MyEventHandler4& rhs)
     303MyEventHandler4::operator=(const MyEventHandler4 &rhs)
    310304{
    311305  if (this != &rhs) {
     
    317311// Clone
    318312//-------------------------------------------------------------------
    319 ClpEventHandler * MyEventHandler4::clone() const
     313ClpEventHandler *MyEventHandler4::clone() const
    320314{
    321315  return new MyEventHandler4(*this);
    322316}
    323317
    324 int
    325 MyEventHandler4::event(Event whichEvent)
    326 {
    327   if ( (cancelAsap&1)!=0 ) {
     318int MyEventHandler4::event(Event whichEvent)
     319{
     320  if ((cancelAsap & 1) != 0) {
    328321    printf("Clp got cancel\n");
    329322    return 5;
     
    333326}
    334327
    335 int main (int argc, const char *argv[])
     328int main(int argc, const char *argv[])
    336329{
    337330
     
    340333#ifdef USE_OSI_NAMES
    341334  // Say we are keeping names (a bit slower this way)
    342   solver1.setIntParam(OsiNameDiscipline,1);
     335  solver1.setIntParam(OsiNameDiscipline, 1);
    343336#endif
    344337  // Read in model using argv[1]
     
    353346  }
    354347#endif
    355   if (argc>=2) mpsFileName = argv[1];
    356   int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
    357   if( numMpsReadErrors != 0 )
    358   {
    359      printf("%d errors reading MPS file\n", numMpsReadErrors);
    360      return numMpsReadErrors;
     348  if (argc >= 2)
     349    mpsFileName = argv[1];
     350  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
     351  if (numMpsReadErrors != 0) {
     352    printf("%d errors reading MPS file\n", numMpsReadErrors);
     353    return numMpsReadErrors;
    361354  }
    362355  // Tell solver to return fast if presolve or initial solve infeasible
     
    370363   */
    371364  // Messy code below copied from CbcSolver.cpp
    372 #if NEW_STYLE_SOLVER==0
    373   // Pass to Cbc initialize defaults 
     365#if NEW_STYLE_SOLVER == 0
     366  // Pass to Cbc initialize defaults
    374367  CbcModel modelA(solver1);
    375   CbcModel * model = &modelA;
     368  CbcModel *model = &modelA;
    376369  CbcMain0(modelA);
    377370  // Event handler
     
    382375     but this will do
    383376  */
    384   if (argc>2) {
    385     CbcMain1(argc-1,argv+1,modelA,callBack);
     377  if (argc > 2) {
     378    CbcMain1(argc - 1, argv + 1, modelA, callBack);
    386379  } else {
    387     const char * argv2[]={"driver4","-solve","-quit"};
    388     CbcMain1(3,argv2,modelA,callBack);
     380    const char *argv2[] = { "driver4", "-solve", "-quit" };
     381    CbcMain1(3, argv2, modelA, callBack);
    389382  }
    390383#else
     
    394387  // Event handler
    395388  MyEventHandler3 eventHandler;
    396   CbcModel * model = control.model();
     389  CbcModel *model = control.model();
    397390  model->passInEventHandler(&eventHandler);
    398   control.solve (argc-1, argv+1, 1);
     391  control.solve(argc - 1, argv + 1, 1);
    399392#endif
    400393  // Solver was cloned so get current copy
    401   OsiSolverInterface * solver = model->solver();
     394  OsiSolverInterface *solver = model->solver();
    402395  // Print solution if finished (could get from model->bestSolution() as well
    403396
    404397  if (model->bestSolution()) {
    405    
    406     const double * solution = solver->getColSolution();
    407    
     398
     399    const double *solution = solver->getColSolution();
     400
    408401    int iColumn;
    409402    int numberColumns = solver->getNumCols();
    410     std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
    411    
    412     std::cout<<"--------------------------------------"<<std::endl;
     403    std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
     404
     405    std::cout << "--------------------------------------" << std::endl;
    413406#ifdef USE_OSI_NAMES
    414    
    415     for (iColumn=0;iColumn<numberColumns;iColumn++) {
    416       double value=solution[iColumn];
    417       if (fabs(value)>1.0e-7&&solver->isInteger(iColumn))
    418         std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver->getColName(iColumn)
    419                  <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
     407
     408    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
     409      double value = solution[iColumn];
     410      if (fabs(value) > 1.0e-7 && solver->isInteger(iColumn))
     411        std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver->getColName(iColumn)
     412                  << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl;
    420413    }
    421414#else
    422415    // names may not be in current solver - use original
    423    
    424     for (iColumn=0;iColumn<numberColumns;iColumn++) {
    425       double value=solution[iColumn];
    426       if (fabs(value)>1.0e-7&&solver->isInteger(iColumn))
    427         std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver1.getModelPtr()->columnName(iColumn)
    428                  <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
     416
     417    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
     418      double value = solution[iColumn];
     419      if (fabs(value) > 1.0e-7 && solver->isInteger(iColumn))
     420        std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver1.getModelPtr()->columnName(iColumn)
     421                  << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl;
    429422    }
    430423#endif
    431     std::cout<<"--------------------------------------"<<std::endl;
    432  
    433     std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
     424    std::cout << "--------------------------------------" << std::endl;
     425
     426    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
    434427  } else {
    435     std::cout<<" No solution!"<<std::endl;
     428    std::cout << " No solution!" << std::endl;
    436429  }
    437430  return 0;
    438 }   
     431}
Note: See TracChangeset for help on using the changeset viewer.