/home/coin/SVN-release/OS-2.1.0/Couenne/src/main/Couenne.cpp

Go to the documentation of this file.
00001 // $Id: Couenne.cpp 154 2009-06-16 18:52:53Z pbelotti $
00002 //
00003 // (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2006, 2007
00004 // All Rights Reserved.
00005 // This code is published under the Common Public License.
00006 //
00007 // Authors :
00008 // Pietro Belotti, Lehigh University
00009 // Stefan Vigerske, Humboldt University
00010 //
00011 // Date : 07/06/2009
00012 
00013 #include "CouenneConfig.h"
00014 
00015 #include <cstdlib>
00016 
00017 #include "CoinPragma.hpp"
00018 #include "CoinError.hpp"
00019 #include "CoinTime.hpp"
00020 
00021 #include "CouenneUserInterface.hpp"
00022 #ifdef COIN_HAS_ASL
00023 #include "CouenneAmplInterface.hpp"
00024 #endif
00025 #ifdef COIN_HAS_OS
00026 #include "CouenneOSInterface.hpp"
00027 #endif
00028 
00029 #include "BonRegisteredOptions.hpp"
00030 #include "BonCbc.hpp"
00031 
00032 #include "BonCouenneSetup.hpp"
00033 #include "BonCouenneInterface.hpp"
00034 
00035 // for printing of statistics
00036 #include "CbcCutGenerator.hpp"      
00037 #include "CouenneCutGenerator.hpp" 
00038 #include "CouenneProblem.hpp"
00039 // the maximum difference between a printed optimum and a CouNumber
00040 #define PRINTED_PRECISION 1e-5
00041 
00042 using Ipopt::SmartPtr;
00043 
00044 static const int infeasible = 1;
00045 
00046 bool parseCommandLine(int argc, char* argv[], Ipopt::SmartPtr<Ipopt::OptionsList> options) {
00047         assert(IsValid(options));
00048         
00049         if (argc==3 && strcmp(argv[1], "-AMPL")==0)
00050                 options->SetStringValue("nlfile", argv[2]);
00051 
00052         if (argc==3 && strcmp(argv[1], "-OSIL")==0)
00053                 options->SetStringValue("osilfile", argv[2]);
00054 
00055         return true;
00056 }
00057 
00058 int main (int argc, char *argv[]) {
00059   WindowsErrorPopupBlocker();
00060 
00061   double time_start = CoinCpuTime();
00062   
00063         // register options to prepare for parsing the command line
00064         SmartPtr<Bonmin::RegisteredOptions> roptions = new Bonmin::RegisteredOptions();
00065         Bonmin::CouenneSetup::registerAllOptions(roptions);
00066 #ifdef COIN_HAS_ASL
00067         CouenneAmplInterface::registerOptions(roptions);
00068 #endif
00069 #ifdef COIN_HAS_OS
00070         CouenneOSInterface::registerOptions(roptions);
00071 #endif
00072         
00073         SmartPtr<Ipopt::Journalist> jnlst = new Ipopt::Journalist();
00074         // do not add journals yet, maybe the user wants to do so; but what if parsing the command line gives errors?
00075         
00076         SmartPtr<Ipopt::OptionsList> options = new Ipopt::OptionsList(GetRawPtr(roptions), jnlst);
00077         if (!parseCommandLine(argc, argv, options))
00078                 return EXIT_FAILURE;
00079         
00080         
00081 
00082         CouenneUserInterface* userinterface = NULL;
00083         
00084         std::string dummy;
00085 #ifdef COIN_HAS_ASL
00086         if (!userinterface && options->GetStringValue("nlfile", dummy, "")) {
00087                 userinterface = new CouenneAmplInterface(options, jnlst);
00088                 ((CouenneAmplInterface*)userinterface) -> setRegisteredOptions(roptions); // for some reason the TMINLP constructor needs the registered options
00089         }
00090 #endif
00091 #ifdef COIN_HAS_OS
00092         if (!userinterface && options->GetStringValue("osilfile", dummy, "")) {
00093                 userinterface = new CouenneOSInterface();
00094         }
00095 #endif
00096         
00097         if (!userinterface) {
00098                 fprintf(stderr, "Error: No input file given.\n");
00099                 return EXIT_FAILURE;
00100         }
00101         
00102         if (!userinterface->setupJournals())
00103                 return EXIT_FAILURE;
00104         
00105         CouenneProblem* problem = userinterface->getCouenneProblem();
00106         if (!problem)
00107                 return EXIT_FAILURE;
00108         problem->initOptions(options);
00109         
00110         SmartPtr<Bonmin::TMINLP> tminlp = userinterface->getTMINLP();
00111         if (Ipopt::IsNull(tminlp))
00112                 return EXIT_FAILURE;
00113 
00114         try {
00115     Bonmin::Bab bb;
00116     bb.setUsingCouenne (true);
00117 
00118     Bonmin::CouenneSetup couenne;
00119     couenne.setOptionsAndJournalist(roptions, options, jnlst);
00120     if (!couenne.InitializeCouenne (NULL, problem, tminlp))
00121       throw infeasible;
00122 
00123     double timeLimit = 0;
00124     options -> GetNumericValue ("time_limit", timeLimit, "couenne.");
00125     couenne.setDoubleParameter (Bonmin::BabSetupBase::MaxTime, timeLimit - (time_start = (CoinCpuTime () - time_start)));
00126   
00127     if (!userinterface->addBabPlugins(bb))
00128         return EXIT_FAILURE;
00129 
00130     bb (couenne); // do branch and bound
00131     
00132     // retrieve test value to check
00133     double global_opt;
00134     options -> GetNumericValue ("couenne_check", global_opt, "couenne.");
00135 
00136     if (global_opt < COUENNE_INFINITY) { // some value found in couenne.opt
00137       double opt = bb.model (). getBestPossibleObjValue ();
00138 
00139       jnlst -> Printf(Ipopt::J_SUMMARY, J_PROBLEM, "Global Optimum Test on %-40s %s\n", 
00140               problem -> problemName ().c_str (), 
00141               (fabs (opt - global_opt) / 
00142                (1. + CoinMax (fabs (opt), fabs (global_opt))) < PRINTED_PRECISION) ? 
00143               "OK" : "FAILED");
00144 
00145     } else if (couenne.displayStats ()) { // print statistics
00146 
00147       int nr=-1, nt=-1;
00148       double st=-1;
00149 
00150       CouenneCutGenerator* cg = NULL;
00151       if (bb.model (). cutGenerators ())
00152         cg = dynamic_cast <CouenneCutGenerator *>       (bb.model (). cutGenerators () [0] -> generator ());
00153       if (cg) cg -> getStats (nr, nt, st);
00154       else jnlst -> Printf(Ipopt::J_WARNING, J_PROBLEM, "Warning: Could not get pointer to CouenneCutGenerator\n");
00155 
00156         jnlst -> Printf(Ipopt::J_SUMMARY, J_PROBLEM, "Stats: %-15s %4d [var] %4d [int] %4d [con] %4d [aux] "
00157                         "%6d [root] %8d [tot] %6g [sep] %8g [time] %8g [bb] "
00158                         "%20e [lower] %20e [upper] %7d [nodes]\n",// %s %s\n",
00159                         problem -> problemName ().c_str (),
00160                         problem -> nOrigVars   (), 
00161                         problem -> nOrigIntVars(), 
00162                         problem -> nOrigCons   (),
00163                         problem -> nVars       () - problem -> nOrigVars (),
00164                         nr, nt, st, 
00165                         CoinCpuTime () - time_start,
00166                         cg ? (CoinCpuTime () - cg -> rootTime ()) : CoinCpuTime (),
00167                         bb.model (). getBestPossibleObjValue (),
00168                         bb.model (). getObjValue (),
00169                         //bb.bestBound (),
00170                         //bb.bestObj (),
00171                         bb.numNodes ()
00172                         //bb.iterationCount (),
00173                         //status.c_str (), message.c_str ()
00174         );
00175     }    
00176 
00177     if (!userinterface->writeSolution(bb))
00178         return EXIT_FAILURE;
00179         
00180         } catch(Bonmin::TNLPSolver::UnsolvedError *E) {
00181      E->writeDiffFiles();
00182      E->printError(std::cerr);
00183     //There has been a failure to solve a problem with Ipopt.
00184     //And we will output file with information on what has been changed in the problem to make it fail.
00185     //Now depending on what algorithm has been called (B-BB or other) the failed problem may be at different place.
00186     //    const OsiSolverInterface &si1 = (algo > 0) ? nlpSolver : *model.solver();
00187      
00188   } catch(Bonmin::OsiTMINLPInterface::SimpleError &E) {
00189     std::cerr<<E.className()<<"::"<<E.methodName()
00190              <<std::endl
00191              <<E.message()<<std::endl;
00192     
00193   } catch(CoinError &E) {
00194     std::cerr<<E.className()<<"::"<<E.methodName()
00195              <<std::endl
00196              <<E.message()<<std::endl;
00197     
00198   } catch (Ipopt::OPTION_INVALID &E) {
00199         std::cerr<<"Ipopt exception : "<<E.Message()<<std::endl;
00200    
00201   } catch (int generic_error) {
00202     if (generic_error == infeasible)
00203       jnlst->Printf(Ipopt::J_SUMMARY, J_PROBLEM, "problem infeasible\n");
00204   }
00205   
00206   delete userinterface;
00207   
00208   return EXIT_SUCCESS;
00209 }

Generated on Tue Mar 30 03:04:38 2010 by  doxygen 1.4.7