Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

sample2.cpp

00001 // Copyright (C) 2002, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #if defined(_MSC_VER)
00004 // Turn off compiler warning about long names
00005 #  pragma warning(disable:4786)
00006 #endif
00007 
00008 #include <cassert>
00009 #include <iomanip>
00010 
00011 
00012 // For Branch and bound
00013 #include "OsiSolverInterface.hpp"
00014 #include "SbbModel.hpp"
00015 #include "SbbBranchUser.hpp"
00016 #include "SbbCompareUser.hpp"
00017 #include "SbbHeuristicUser.hpp"
00018 #ifdef COIN_USE_CLP
00019 #include "OsiClpSolverInterface.hpp"
00020 #endif
00021 #ifdef COIN_USE_OSL
00022 #include "OsiOslSolverInterface.hpp"
00023 #endif
00024 
00025 // Cuts
00026 
00027 #include "CglGomory.hpp"
00028 #include "CglProbing.hpp"
00029 #include "CglKnapsackCover.hpp"
00030 #include "CglOddHole.hpp"
00031 
00032 // Heuristics
00033 
00034 #include "SbbHeuristic.hpp"
00035 
00036 
00037 // Time
00038 
00039 #include  <time.h>
00040 #if !defined(_MSC_VER)
00041 #include <sys/times.h>
00042 #include <sys/resource.h>
00043 #include <unistd.h>
00044 #endif
00045 static double cpuTime()
00046 {
00047   double cpu_temp;
00048 #if defined(_MSC_VER)
00049   unsigned int ticksnow;        /* clock_t is same as int */
00050   
00051   ticksnow = (unsigned int)clock();
00052   
00053   cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
00054 #else
00055   struct rusage usage;
00056   getrusage(RUSAGE_SELF,&usage);
00057   cpu_temp = usage.ru_utime.tv_sec;
00058   cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
00059 #endif
00060   return cpu_temp;
00061 }
00062 
00063 //#############################################################################
00064 
00065 
00066 /************************************************************************
00067 
00068 This main program reads in an integer model from an mps file.
00069 
00070 It then sets up some Cgl cut generators and calls branch and cut.
00071 
00072 Branching is simple binary branching on integer variables.
00073 
00074 Node selection is depth first until first solution is found and then
00075 based on objective and number of unsatisfied integer variables.
00076 In this example the functionality is the same as default but it is
00077 a user comparison function.
00078 
00079 Variable branching selection is on maximum minimum-of-up-down change
00080 after strong branching on 5 variables closest to 0.5.
00081 
00082 A simple rounding heuristic is used.
00083 
00084 
00085 ************************************************************************/
00086 
00087 // ****** define comparison to choose best next node
00088 
00089 int main (int argc, const char *argv[])
00090 {
00091 
00092   // Define your favorite OsiSolver
00093   
00094 #ifdef COIN_USE_CLP
00095   OsiClpSolverInterface solver1;
00096   //solver1.messageHandler()->setLogLevel(0);
00097   SbbModel model(solver1);
00098   solver1.getModelPtr()->setDualBound(1.0e10);
00099 #endif
00100 #ifdef COIN_USE_OSL
00101   OsiOslSolverInterface solver1;
00102   //solver1.messageHandler()->setLogLevel(0);
00103   SbbModel model(solver1);
00104 #endif
00105   model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
00106 
00107   // Read in model using argv[1]
00108   // and assert that it is a clean model
00109   int numMpsReadErrors = model.solver()->readMps(argv[1],"");
00110   assert(numMpsReadErrors==0);
00111 
00112   // Set up some cut generators and defaults
00113   // Probing first as gets tight bounds on continuous
00114 
00115   CglProbing generator1;
00116   generator1.setUsingObjective(true);
00117   generator1.setMaxPass(3);
00118   generator1.setMaxProbe(100);
00119   generator1.setMaxLook(50);
00120   generator1.setRowCuts(3);
00121 
00122   CglGomory generator2;
00123   // try larger limit
00124   generator2.setLimit(300);
00125 
00126   CglKnapsackCover generator3;
00127 
00128   CglOddHole generator4;
00129   generator4.setMinimumViolation(0.005);
00130   generator4.setMinimumViolationPer(0.00002);
00131   // try larger limit
00132   generator4.setMaximumEntries(200);
00133   
00134   // Add in generators
00135   model.addCutGenerator(&generator1,-1,"Probing");
00136   model.addCutGenerator(&generator2,-1,"Gomory");
00137   model.addCutGenerator(&generator3,-1,"Knapsack");
00138   model.addCutGenerator(&generator4,-1,"OddHole");
00139 
00140   // Allow rounding heuristic
00141 
00142   SbbRounding heuristic1(model);
00143   model.addHeuristic(&heuristic1);
00144 
00145   // And local search when new solution found
00146 
00147   SbbLocalSearch heuristic2(model);
00148   model.addHeuristic(&heuristic2);
00149 
00150   // Redundant definition of default branching (as Default == User)
00151   SbbBranchUserDecision branch;
00152   model.setBranchingMethod(&branch);
00153 
00154   // Definition of node choice
00155   SbbCompareUser compare;
00156   model.setNodeComparison(compare);
00157 
00158   // Do initial solve to continuous
00159   model.initialSolve();
00160 
00161   // Could tune more
00162   model.setMinimumDrop(min(1.0,
00163                              fabs(model.getObjValue())*1.0e-3+1.0e-4));
00164 
00165   if (model.getNumCols()<500)
00166     model.setMaximumCutPassesAtRoot(-100); // always do 100 if possible
00167   else if (model.getNumCols()<5000)
00168     model.setMaximumCutPassesAtRoot(100); // use minimum drop
00169   else
00170     model.setMaximumCutPassesAtRoot(20);
00171   //model.setMaximumCutPasses(5);
00172 
00173   // Switch off strong branching if wanted
00174   // model.setNumberStrong(0);
00175   // Do more strong branching if small
00176   if (model.getNumCols()<5000)
00177     model.setNumberStrong(10);
00178 
00179   model.solver()->setIntParam(OsiMaxNumIterationHotStart,100);
00180 
00181   // If time is given then stop after that number of minutes
00182   if (argc>2) {
00183     int minutes = atoi(argv[2]);
00184     std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl;
00185     assert (minutes>=0);
00186     model.setDblParam(SbbModel::SbbMaximumSeconds,60.0*minutes);
00187   }
00188   // Switch off most output
00189   if (model.getNumCols()<3000) {
00190     model.messageHandler()->setLogLevel(1);
00191     //model.solver()->messageHandler()->setLogLevel(0);
00192   } else {
00193     model.messageHandler()->setLogLevel(2);
00194     model.solver()->messageHandler()->setLogLevel(1);
00195   }
00196   //model.messageHandler()->setLogLevel(2);
00197   //model.solver()->messageHandler()->setLogLevel(2);
00198   //model.setPrintFrequency(50);
00199   
00200   double time1 = cpuTime();
00201 
00202   if (0) {
00203     // integer presolve
00204     SbbModel * model2 = model.integerPresolve();
00205     if (model2) {
00206       // Do complete search
00207   
00208       model2->branchAndBound();
00209       // get back solution
00210       model.originalModel(model2,false);
00211     } else {
00212       // infeasible
00213       exit(1);
00214     }
00215   } else {
00216     // Do complete search
00217   
00218     model.branchAndBound();
00219   }
00220 
00221   std::cout<<argv[1]<<" took "<<cpuTime()-time1<<" seconds, "
00222            <<model.getNodeCount()<<" nodes with objective "
00223            <<model.getObjValue()
00224            <<(!model.status() ? " Finished" : " Not finished")
00225            <<std::endl;
00226 
00227   // Print solution if finished - we can't get names from Osi!
00228 
00229   if (!model.status()&&model.getObjValue()<1.0e50) {
00230     int numberColumns = model.solver()->getNumCols();
00231     
00232     const double * solution = model.solver()->getColSolution();
00233     
00234     int iColumn;
00235     std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
00236     
00237     std::cout<<"--------------------------------------"<<std::endl;
00238     for (iColumn=0;iColumn<numberColumns;iColumn++) {
00239       double value=solution[iColumn];
00240       if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) 
00241         std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
00242     }
00243     std::cout<<"--------------------------------------"<<std::endl;
00244   
00245     std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
00246   }
00247   return 0;
00248 }    

Generated on Wed Dec 3 14:36:18 2003 for Sbb by doxygen 1.3.5