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

formatting

File:
1 edited

Legend:

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

    r2101 r2469  
    55
    66#include <cassert>
    7 #include <iomanip>
    8 
     7#include <iomanip>
    98
    109#include "CoinPragma.hpp"
     
    2221
    2322//#############################################################################
    24 
    2523
    2624/************************************************************************
     
    6462   or you can check solver status.
    6563*/
    66 /* Return non-zero to return quickly */   
    67 static int callBack(CbcModel * model, int whereFrom)
    68 {
    69   int returnCode=0;
     64/* Return non-zero to return quickly */
     65static int callBack(CbcModel *model, int whereFrom)
     66{
     67  int returnCode = 0;
    7068  switch (whereFrom) {
    7169  case 1:
    7270  case 2:
    73     if (!model->status()&&model->secondaryStatus())
    74       returnCode=1;
     71    if (!model->status() && model->secondaryStatus())
     72      returnCode = 1;
    7573    break;
    76   case 3:
    77     {
    78       // Add in some diving heuristics with different options
    79       CbcHeuristicDiveCoefficient heuristicDC(*model);
    80       heuristicDC.setHeuristicName("DiveCoefficient");
    81       // do if no solution
    82       heuristicDC.setWhen(3);
    83       // 150 passes and fix general integers
    84       heuristicDC.setMaxIterations(151);
    85       // make sure can do as many simplex iterations as wanted
    86       heuristicDC.setMaxSimplexIterations(COIN_INT_MAX);
    87       heuristicDC.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
    88       model->addHeuristic(&heuristicDC) ;
    89       CbcHeuristicDiveFractional heuristicDF(*model);
    90       heuristicDF.setHeuristicName("DiveFractional");
    91       // do if no solution
    92       heuristicDF.setWhen(3);
    93       // 150 passes and don't fix general integers
    94       heuristicDF.setMaxIterations(150);
    95       // make sure can do as many simplex iterations as wanted
    96       heuristicDF.setMaxSimplexIterations(COIN_INT_MAX);
    97       heuristicDF.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
    98       model->addHeuristic(&heuristicDF) ;
    99       CbcHeuristicDiveGuided heuristicDG(*model);
    100       heuristicDG.setHeuristicName("DiveGuided");
    101       // do if no solution
    102       heuristicDG.setWhen(3);
    103       // 200 passes and fix general integers
    104       heuristicDG.setMaxIterations(201);
    105       // make sure can do as many simplex iterations as wanted
    106       heuristicDG.setMaxSimplexIterations(COIN_INT_MAX);
    107       heuristicDG.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
    108       model->addHeuristic(&heuristicDG) ;
    109       CbcHeuristicDiveVectorLength heuristicDV(*model);
    110       heuristicDV.setHeuristicName("DiveVectorLength");
    111       // do if no solution
    112       heuristicDV.setWhen(3);
    113       // 150 passes and fix general integers
    114       heuristicDV.setMaxIterations(151);
    115       // make sure can do as many simplex iterations as wanted
    116       heuristicDV.setMaxSimplexIterations(COIN_INT_MAX);
    117       heuristicDV.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
    118       model->addHeuristic(&heuristicDV) ;
    119       // Second version!
    120       CbcHeuristicDiveVectorLength heuristicDV2(*model);
    121       heuristicDV2.setHeuristicName("DiveVectorLength");
    122       // do if no solution
    123       heuristicDV2.setWhen(3);
    124       // 300 passes and don't fix general integers
    125       heuristicDV2.setMaxIterations(300);
    126       // fix fewer
    127       heuristicDV2.setPercentageToFix(0.05);
    128       // make sure can do as many simplex iterations as wanted
    129       heuristicDV2.setMaxSimplexIterations(COIN_INT_MAX);
    130       heuristicDV2.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
    131       model->addHeuristic(&heuristicDV2) ;
    132       CbcHeuristicDivePseudoCost heuristicDP(*model);
    133       heuristicDP.setHeuristicName("DivePseudoCost");
    134       // do if no solution
    135       heuristicDP.setWhen(3);
    136       // 100 passes and don't fix general integers
    137       heuristicDP.setMaxIterations(100);
    138       // make sure can do as many simplex iterations as wanted
    139       heuristicDP.setMaxSimplexIterations(COIN_INT_MAX);
    140       heuristicDP.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
    141       model->addHeuristic(&heuristicDP) ;
    142       CbcHeuristicDiveLineSearch heuristicDL(*model);
    143       heuristicDL.setHeuristicName("DiveLineSearch");
    144       // do if no solution
    145       heuristicDL.setWhen(3);
    146       // 150 passes and fix general integers
    147       heuristicDL.setMaxIterations(151);
    148       // make sure can do as many simplex iterations as wanted
    149       heuristicDL.setMaxSimplexIterations(COIN_INT_MAX);
    150       heuristicDL.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
    151       model->addHeuristic(&heuristicDL) ;
    152       //CbcCompareUser compare;
    153       //model->setNodeComparison(compare);
    154     }
    155     break;
     74  case 3: {
     75    // Add in some diving heuristics with different options
     76    CbcHeuristicDiveCoefficient heuristicDC(*model);
     77    heuristicDC.setHeuristicName("DiveCoefficient");
     78    // do if no solution
     79    heuristicDC.setWhen(3);
     80    // 150 passes and fix general integers
     81    heuristicDC.setMaxIterations(151);
     82    // make sure can do as many simplex iterations as wanted
     83    heuristicDC.setMaxSimplexIterations(COIN_INT_MAX);
     84    heuristicDC.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
     85    model->addHeuristic(&heuristicDC);
     86    CbcHeuristicDiveFractional heuristicDF(*model);
     87    heuristicDF.setHeuristicName("DiveFractional");
     88    // do if no solution
     89    heuristicDF.setWhen(3);
     90    // 150 passes and don't fix general integers
     91    heuristicDF.setMaxIterations(150);
     92    // make sure can do as many simplex iterations as wanted
     93    heuristicDF.setMaxSimplexIterations(COIN_INT_MAX);
     94    heuristicDF.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
     95    model->addHeuristic(&heuristicDF);
     96    CbcHeuristicDiveGuided heuristicDG(*model);
     97    heuristicDG.setHeuristicName("DiveGuided");
     98    // do if no solution
     99    heuristicDG.setWhen(3);
     100    // 200 passes and fix general integers
     101    heuristicDG.setMaxIterations(201);
     102    // make sure can do as many simplex iterations as wanted
     103    heuristicDG.setMaxSimplexIterations(COIN_INT_MAX);
     104    heuristicDG.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
     105    model->addHeuristic(&heuristicDG);
     106    CbcHeuristicDiveVectorLength heuristicDV(*model);
     107    heuristicDV.setHeuristicName("DiveVectorLength");
     108    // do if no solution
     109    heuristicDV.setWhen(3);
     110    // 150 passes and fix general integers
     111    heuristicDV.setMaxIterations(151);
     112    // make sure can do as many simplex iterations as wanted
     113    heuristicDV.setMaxSimplexIterations(COIN_INT_MAX);
     114    heuristicDV.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
     115    model->addHeuristic(&heuristicDV);
     116    // Second version!
     117    CbcHeuristicDiveVectorLength heuristicDV2(*model);
     118    heuristicDV2.setHeuristicName("DiveVectorLength");
     119    // do if no solution
     120    heuristicDV2.setWhen(3);
     121    // 300 passes and don't fix general integers
     122    heuristicDV2.setMaxIterations(300);
     123    // fix fewer
     124    heuristicDV2.setPercentageToFix(0.05);
     125    // make sure can do as many simplex iterations as wanted
     126    heuristicDV2.setMaxSimplexIterations(COIN_INT_MAX);
     127    heuristicDV2.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
     128    model->addHeuristic(&heuristicDV2);
     129    CbcHeuristicDivePseudoCost heuristicDP(*model);
     130    heuristicDP.setHeuristicName("DivePseudoCost");
     131    // do if no solution
     132    heuristicDP.setWhen(3);
     133    // 100 passes and don't fix general integers
     134    heuristicDP.setMaxIterations(100);
     135    // make sure can do as many simplex iterations as wanted
     136    heuristicDP.setMaxSimplexIterations(COIN_INT_MAX);
     137    heuristicDP.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
     138    model->addHeuristic(&heuristicDP);
     139    CbcHeuristicDiveLineSearch heuristicDL(*model);
     140    heuristicDL.setHeuristicName("DiveLineSearch");
     141    // do if no solution
     142    heuristicDL.setWhen(3);
     143    // 150 passes and fix general integers
     144    heuristicDL.setMaxIterations(151);
     145    // make sure can do as many simplex iterations as wanted
     146    heuristicDL.setMaxSimplexIterations(COIN_INT_MAX);
     147    heuristicDL.setMaxSimplexIterationsAtRoot(COIN_INT_MAX);
     148    model->addHeuristic(&heuristicDL);
     149    //CbcCompareUser compare;
     150    //model->setNodeComparison(compare);
     151  } break;
    156152  case 4:
    157153    // If not good enough could skip postprocessing
    158     break; 
     154    break;
    159155  case 5:
    160156    break;
     
    172168
    173169class MyEventHandler3 : public CbcEventHandler {
    174  
     170
    175171public:
    176172  /**@name Overrides */
     
    184180  MyEventHandler3();
    185181  /// Constructor with pointer to model (redundant as setEventHandler does)
    186   MyEventHandler3(CbcModel * model);
     182  MyEventHandler3(CbcModel *model);
    187183  /** Destructor */
    188184  virtual ~MyEventHandler3();
    189185  /** The copy constructor. */
    190   MyEventHandler3(const MyEventHandler3 & rhs);
     186  MyEventHandler3(const MyEventHandler3 &rhs);
    191187  /// Assignment
    192   MyEventHandler3& operator=(const MyEventHandler3 & rhs);
     188  MyEventHandler3 &operator=(const MyEventHandler3 &rhs);
    193189  /// Clone
    194   virtual CbcEventHandler * clone() const ;
     190  virtual CbcEventHandler *clone() const;
    195191  //@}
    196    
    197    
     192
    198193protected:
    199194  // data goes here
    200195};
    201196//-------------------------------------------------------------------
    202 // Default Constructor 
    203 //-------------------------------------------------------------------
    204 MyEventHandler3::MyEventHandler3 ()
     197// Default Constructor
     198//-------------------------------------------------------------------
     199MyEventHandler3::MyEventHandler3()
    205200  : CbcEventHandler()
    206201{
     
    208203
    209204//-------------------------------------------------------------------
    210 // Copy constructor 
    211 //-------------------------------------------------------------------
    212 MyEventHandler3::MyEventHandler3 (const MyEventHandler3 & rhs)
    213 : CbcEventHandler(rhs)
    214 { 
     205// Copy constructor
     206//-------------------------------------------------------------------
     207MyEventHandler3::MyEventHandler3(const MyEventHandler3 &rhs)
     208  : CbcEventHandler(rhs)
     209{
    215210}
    216211
    217212// Constructor with pointer to model
    218 MyEventHandler3::MyEventHandler3(CbcModel * model)
     213MyEventHandler3::MyEventHandler3(CbcModel *model)
    219214  : CbcEventHandler(model)
    220215{
     
    222217
    223218//-------------------------------------------------------------------
    224 // Destructor 
    225 //-------------------------------------------------------------------
    226 MyEventHandler3::~MyEventHandler3 ()
     219// Destructor
     220//-------------------------------------------------------------------
     221MyEventHandler3::~MyEventHandler3()
    227222{
    228223}
    229224
    230225//----------------------------------------------------------------
    231 // Assignment operator 
     226// Assignment operator
    232227//-------------------------------------------------------------------
    233228MyEventHandler3 &
    234 MyEventHandler3::operator=(const MyEventHandler3& rhs)
     229MyEventHandler3::operator=(const MyEventHandler3 &rhs)
    235230{
    236231  if (this != &rhs) {
     
    242237// Clone
    243238//-------------------------------------------------------------------
    244 CbcEventHandler * MyEventHandler3::clone() const
     239CbcEventHandler *MyEventHandler3::clone() const
    245240{
    246241  return new MyEventHandler3(*this);
    247242}
    248243
    249 CbcEventHandler::CbcAction 
     244CbcEventHandler::CbcAction
    250245MyEventHandler3::event(CbcEvent whichEvent)
    251246{
    252247  // If in sub tree carry on
    253248  if (!model_->parentModel()) {
    254     if (whichEvent==solution||whichEvent==heuristicSolution) {
     249    if (whichEvent == solution || whichEvent == heuristicSolution) {
    255250#ifdef STOP_EARLY
    256251      return stop; // say finished
     
    258253      // If preprocessing was done solution will be to processed model
    259254      int numberColumns = model_->getNumCols();
    260       const double * bestSolution = model_->bestSolution();
    261       assert (bestSolution);
    262       printf("value of solution is %g\n",model_->getObjValue());
    263       for (int i=0;i<numberColumns;i++) {
    264         if (fabs(bestSolution[i])>1.0e-8)
    265           printf("%d %g\n",i,bestSolution[i]);
     255      const double *bestSolution = model_->bestSolution();
     256      assert(bestSolution);
     257      printf("value of solution is %g\n", model_->getObjValue());
     258      for (int i = 0; i < numberColumns; i++) {
     259        if (fabs(bestSolution[i]) > 1.0e-8)
     260          printf("%d %g\n", i, bestSolution[i]);
    266261      }
    267262      return noAction; // carry on
     
    271266    }
    272267  } else {
    273       return noAction; // carry on
    274   }
    275 }
    276 
    277 int main (int argc, const char *argv[])
     268    return noAction; // carry on
     269  }
     270}
     271
     272int main(int argc, const char *argv[])
    278273{
    279274
     
    282277#ifdef USE_OSI_NAMES
    283278  // Say we are keeping names (a bit slower this way)
    284   solver1.setIntParam(OsiNameDiscipline,1);
     279  solver1.setIntParam(OsiNameDiscipline, 1);
    285280#endif
    286281  // Read in model using argv[1]
     
    295290  }
    296291#endif
    297   if (argc>=2) mpsFileName = argv[1];
    298   int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
    299   if( numMpsReadErrors != 0 )
    300   {
    301      printf("%d errors reading MPS file\n", numMpsReadErrors);
    302      return numMpsReadErrors;
     292  if (argc >= 2)
     293    mpsFileName = argv[1];
     294  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
     295  if (numMpsReadErrors != 0) {
     296    printf("%d errors reading MPS file\n", numMpsReadErrors);
     297    return numMpsReadErrors;
    303298  }
    304299  // Tell solver to return fast if presolve or initial solve infeasible
     
    309304   */
    310305  // Messy code below copied from CbcSolver.cpp
    311   // Pass to Cbc initialize defaults 
     306  // Pass to Cbc initialize defaults
    312307  CbcModel modelA(solver1);
    313   CbcModel * model = &modelA;
     308  CbcModel *model = &modelA;
    314309  CbcMain0(modelA);
    315310  // Event handler
     
    320315     but this will do
    321316  */
    322   if (argc>2) {
    323     CbcMain1(argc-1,argv+1,modelA,callBack);
     317  if (argc > 2) {
     318    CbcMain1(argc - 1, argv + 1, modelA, callBack);
    324319  } else {
    325     const char * argv2[]={"driver4","-solve","-quit"};
    326     CbcMain1(3,argv2,modelA,callBack);
     320    const char *argv2[] = { "driver4", "-solve", "-quit" };
     321    CbcMain1(3, argv2, modelA, callBack);
    327322  }
    328323  // Solver was cloned so get current copy
    329   OsiSolverInterface * solver = model->solver();
     324  OsiSolverInterface *solver = model->solver();
    330325  // Print solution if finished (could get from model->bestSolution() as well
    331326
    332327  if (model->bestSolution()) {
    333    
    334     const double * solution = solver->getColSolution();
    335    
     328
     329    const double *solution = solver->getColSolution();
     330
    336331    int iColumn;
    337332    int numberColumns = solver->getNumCols();
    338     std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
    339    
    340     std::cout<<"--------------------------------------"<<std::endl;
     333    std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
     334
     335    std::cout << "--------------------------------------" << std::endl;
    341336#ifdef USE_OSI_NAMES
    342    
    343     for (iColumn=0;iColumn<numberColumns;iColumn++) {
    344       double value=solution[iColumn];
    345       if (fabs(value)>1.0e-7&&solver->isInteger(iColumn))
    346         std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver->getColName(iColumn)
    347                  <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
     337
     338    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
     339      double value = solution[iColumn];
     340      if (fabs(value) > 1.0e-7 && solver->isInteger(iColumn))
     341        std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver->getColName(iColumn)
     342                  << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl;
    348343    }
    349344#else
    350345    // names may not be in current solver - use original
    351    
    352     for (iColumn=0;iColumn<numberColumns;iColumn++) {
    353       double value=solution[iColumn];
    354       if (fabs(value)>1.0e-7&&solver->isInteger(iColumn))
    355         std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver1.getModelPtr()->columnName(iColumn)
    356                  <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
     346
     347    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
     348      double value = solution[iColumn];
     349      if (fabs(value) > 1.0e-7 && solver->isInteger(iColumn))
     350        std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver1.getModelPtr()->columnName(iColumn)
     351                  << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl;
    357352    }
    358353#endif
    359     std::cout<<"--------------------------------------"<<std::endl;
    360  
    361     std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
     354    std::cout << "--------------------------------------" << std::endl;
     355
     356    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
    362357  } else {
    363     std::cout<<" No solution!"<<std::endl;
     358    std::cout << " No solution!" << std::endl;
    364359  }
    365360  return 0;
    366 }   
     361}
Note: See TracChangeset for help on using the changeset viewer.