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

formatting

File:
1 edited

Legend:

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

    r1898 r2469  
    66#include <cassert>
    77#include <iomanip>
    8 
    98
    109#include "CoinPragma.hpp"
     
    2726#include "CbcHeuristic.hpp"
    2827
    29 
    3028// Time
    3129#include "CoinTime.hpp"
    32 
    3330
    3431/************************************************************************
     
    5653************************************************************************/
    5754
    58 int main (int argc, const char *argv[])
     55int main(int argc, const char *argv[])
    5956{
    6057
    6158  // Define a Solver which inherits from OsiClpsolverInterface -> OsiSolverInterface
    62  
     59
    6360  ClpQuadInterface solver1;
    6461
    6562  // Read in model using argv[1]
    6663  if (argc <= 1) {
    67      printf("using %s <modelfile>\n", argv[0]);
    68      return 1;
     64    printf("using %s <modelfile>\n", argv[0]);
     65    return 1;
    6966  }
    7067  // must use clp to get a quadratic model
    71   ClpSimplex * clp = solver1.getModelPtr();
     68  ClpSimplex *clp = solver1.getModelPtr();
    7269  int numMpsReadErrors = clp->readMps(argv[1]);
    7370  // and assert that it is a clean model
    74   if( numMpsReadErrors != 0 )
    75   {
    76      printf("%d errors reading MPS file\n", numMpsReadErrors);
    77      return numMpsReadErrors;
    78   }
    79 
    80   // This clones solver
     71  if (numMpsReadErrors != 0) {
     72    printf("%d errors reading MPS file\n", numMpsReadErrors);
     73    return numMpsReadErrors;
     74  }
     75
     76  // This clones solver
    8177  CbcModel model(solver1);
    8278  // But now model doesn't know about integers!
    83   const char * integerInfo = clp->integerInformation();
     79  const char *integerInfo = clp->integerInformation();
    8480  int numberColumns = clp->numberColumns();
    8581  // and point to solver
    86   OsiSolverInterface * solver2 = model.solver();
    87   for (int iColumn=0;iColumn<numberColumns;iColumn++) {
    88     if (integerInfo[iColumn]) 
     82  OsiSolverInterface *solver2 = model.solver();
     83  for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
     84    if (integerInfo[iColumn])
    8985      solver2->setInteger(iColumn);
    9086  }
     
    9288  // Within branch and cut it is better (at present) to switch off all objectives
    9389
    94   ClpQuadInterface * osiclp = dynamic_cast< ClpQuadInterface*> (solver2);
    95   assert (osiclp);
     90  ClpQuadInterface *osiclp = dynamic_cast< ClpQuadInterface * >(solver2);
     91  assert(osiclp);
    9692  // Set fake objective so Cbc not confused
    9793  osiclp->initialize();
    98   solver2->setHintParam(OsiDoReducePrint,true,OsiHintTry);
     94  solver2->setHintParam(OsiDoReducePrint, true, OsiHintTry);
    9995
    10096  // Set up some cut generators and defaults
     
    111107  // Add in generators
    112108  // Only some generators work (and even then try without first)
    113   model.addCutGenerator(&generator1,1,"Probing");
     109  model.addCutGenerator(&generator1, 1, "Probing");
    114110  // Allow rounding heuristic
    115111
     
    117113  // do not add yet as don't know how to deal with quadratic objective
    118114  //model.addHeuristic(&heuristic1);
    119 
    120115
    121116  // Redundant definition of default branching (as Default == User)
     
    129124  model.setNodeComparison(compare);
    130125
    131 
    132126  // Do initial solve to continuous
    133127  model.initialSolve();
     
    135129  // Could tune more
    136130  model.setMinimumDrop(CoinMin(1.0,
    137                              fabs(model.getMinimizationObjValue())*1.0e-3+1.0e-4));
     131    fabs(model.getMinimizationObjValue()) * 1.0e-3 + 1.0e-4));
    138132
    139133  model.setMaximumCutPassesAtRoot(0);
     
    142136  // Switch off strong branching if wanted
    143137  //model.setNumberStrong(5);
    144  
    145   model.solver()->setIntParam(OsiMaxNumIterationHotStart,10000);
     138
     139  model.solver()->setIntParam(OsiMaxNumIterationHotStart, 10000);
    146140
    147141  // If time is given then stop after that number of minutes
    148   if (argc>2) {
     142  if (argc > 2) {
    149143    int minutes = atoi(argv[2]);
    150     std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl;
    151     assert (minutes>=0);
    152     model.setDblParam(CbcModel::CbcMaximumSeconds,60.0*minutes);
     144    std::cout << "Stopping after " << minutes << " minutes" << std::endl;
     145    assert(minutes >= 0);
     146    model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0 * minutes);
    153147  }
    154148  // Switch off most output
    155   if (model.getNumCols()<3000) {
     149  if (model.getNumCols() < 3000) {
    156150    model.messageHandler()->setLogLevel(1);
    157151    //model.solver()->messageHandler()->setLogLevel(0);
     
    161155  }
    162156  model.setPrintFrequency(50);
    163  
     157
    164158  double time1 = CoinCpuTime();
    165159
    166160  // Do complete search
    167  
     161
    168162  model.branchAndBound();
    169163
    170   std::cout<<argv[1]<<" took "<<CoinCpuTime()-time1<<" seconds, "
    171            <<model.getNodeCount()<<" nodes with objective "
    172            <<model.getObjValue()
    173            <<(!model.status() ? " Finished" : " Not finished")
    174            <<std::endl;
     164  std::cout << argv[1] << " took " << CoinCpuTime() - time1 << " seconds, "
     165            << model.getNodeCount() << " nodes with objective "
     166            << model.getObjValue()
     167            << (!model.status() ? " Finished" : " Not finished")
     168            << std::endl;
    175169
    176170  // Print more statistics
    177   std::cout<<"Cuts at root node changed objective from "<<model.getContinuousObjective()
    178            <<" to "<<model.rootObjectiveAfterCuts()<<std::endl;
     171  std::cout << "Cuts at root node changed objective from " << model.getContinuousObjective()
     172            << " to " << model.rootObjectiveAfterCuts() << std::endl;
    179173
    180174  int numberGenerators = model.numberCutGenerators();
    181   for (int iGenerator=0;iGenerator<numberGenerators;iGenerator++) {
    182     CbcCutGenerator * generator = model.cutGenerator(iGenerator);
    183     std::cout<<generator->cutGeneratorName()<<" was tried "
    184              <<generator->numberTimesEntered()<<" times and created "
    185              <<generator->numberCutsInTotal()<<" cuts of which "
    186              <<generator->numberCutsActive()<<" were active after adding rounds of cuts"
    187              <<std::endl;
     175  for (int iGenerator = 0; iGenerator < numberGenerators; iGenerator++) {
     176    CbcCutGenerator *generator = model.cutGenerator(iGenerator);
     177    std::cout << generator->cutGeneratorName() << " was tried "
     178              << generator->numberTimesEntered() << " times and created "
     179              << generator->numberCutsInTotal() << " cuts of which "
     180              << generator->numberCutsActive() << " were active after adding rounds of cuts"
     181              << std::endl;
    188182  }
    189183  // Print solution if finished - we can't get names from Osi!
    190184
    191   if (!model.status()&&model.getMinimizationObjValue()<1.0e50) {
     185  if (!model.status() && model.getMinimizationObjValue() < 1.0e50) {
    192186    int numberColumns = model.solver()->getNumCols();
    193    
    194     const double * solution = model.solver()->getColSolution();
    195    
     187
     188    const double *solution = model.solver()->getColSolution();
     189
    196190    int iColumn;
    197     std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
    198    
    199     std::cout<<"--------------------------------------"<<std::endl;
    200     for (iColumn=0;iColumn<numberColumns;iColumn++) {
    201       double value=solution[iColumn];
    202       if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn))
    203         std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
     191    std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
     192
     193    std::cout << "--------------------------------------" << std::endl;
     194    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
     195      double value = solution[iColumn];
     196      if (fabs(value) > 1.0e-7 && model.solver()->isInteger(iColumn))
     197        std::cout << std::setw(6) << iColumn << " " << value << std::endl;
    204198    }
    205     std::cout<<"--------------------------------------"<<std::endl;
    206  
    207     std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
     199    std::cout << "--------------------------------------" << std::endl;
     200
     201    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
    208202  }
    209203  return 0;
    210 }   
     204}
Note: See TracChangeset for help on using the changeset viewer.