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

Go to the documentation of this file.
00001 // $Id: BonCouenne.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, Carnegie Mellon University,
00009 // Pierre Bonami, International Business Machines Corporation
00010 //
00011 // Date : 12/19/2006
00012 
00013 
00014 #if defined(_MSC_VER)
00015 // Turn off compiler warning about long names
00016 #  pragma warning(disable:4786)
00017 #endif
00018 #include <iomanip>
00019 #include <fstream>
00020 
00021 #include <stdlib.h>
00022 
00023 #include "CoinTime.hpp"
00024 #include "CoinError.hpp"
00025 #include "BonCouenneInterface.hpp"
00026 
00027 #include "BonCouenneSetup.hpp"
00028 
00029 #include "BonCbc.hpp"
00030 #ifdef COIN_HAS_FILTERSQP
00031 #include "BonFilterSolver.hpp"
00032 #endif
00033 
00034 #include "CbcCutGenerator.hpp"
00035 #include "CouenneProblem.hpp"
00036 #include "CouenneCutGenerator.hpp"
00037 
00038 using namespace Bonmin;
00039 
00040 // the maximum difference between a printed optimum and a CouNumber
00041 #define PRINTED_PRECISION 1e-5
00042 
00043 #include "exprVar.hpp"
00044 #include "exprConst.hpp"
00045 #include "exprSum.hpp"
00046 #include "exprClone.hpp"
00047 #include "CouenneProblemElem.hpp"
00048 #include "CouenneProblem.hpp"
00049 
00050 int main (int argc, char *argv[]) {
00051 
00052   //WindowsErrorPopupBlocker();
00053   using namespace Ipopt;
00054 
00055   char * pbName = NULL;
00056   double time_start = CoinCpuTime();
00057 
00058   const int infeasible = 1;
00059 
00060   try {
00061 
00062     Bab bb;
00063     bb.setUsingCouenne (true);
00064 
00065     CouenneProblem *p = NULL;
00066     CouenneInterface *ci = NULL;
00067 
00068 #if 0
00069     ci = new CouenneInterface;
00070     p = new CouenneProblem;
00071 
00072     p -> addVariable (false, p -> domain ());
00073     p -> addVariable (false, p -> domain ());
00074     p -> addVariable (false, p -> domain ());
00075     p -> addVariable (false, p -> domain ());
00076 
00077     p -> addObjective    (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (2))), "min");
00078     p -> addLEConstraint (new exprSum (new exprClone (p->Var (0)), new exprClone (p->Var (2))), 
00079                           new exprConst (1));
00080     p -> addEQConstraint (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (2))), 
00081                           new exprConst (1));
00082     p -> addEQConstraint (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (3))), 
00083                           new exprConst (1));
00084     p -> addEQConstraint (new exprSum (new exprClone (p->Var (2)), new exprClone (p->Var (3))), 
00085                           new exprConst (1));
00086 #endif
00087 
00088     CouenneSetup couenne;
00089     if (!couenne.InitializeCouenne (argv, p, ci))
00090       throw infeasible;
00091 
00092 #if 0
00093     CouenneFeasibility feasibility;
00094     bb.model().setProblemFeasibility (feasibility);
00095 #endif
00096 
00098     double timeLimit = 0;
00099     couenne.options () -> GetNumericValue ("time_limit", timeLimit, "couenne.");
00100     couenne.setDoubleParameter (BabSetupBase::MaxTime, 
00101                                 timeLimit - (time_start = (CoinCpuTime () - time_start)));
00102 
00104 
00105     bb (couenne); // do branch and bound
00106 
00108 
00109     std::cout.precision (10);
00110 
00112     CouenneCutGenerator *cg = NULL;
00113 
00114     if (bb.model (). cutGenerators ())
00115       cg = dynamic_cast <CouenneCutGenerator *> 
00116         (bb.model (). cutGenerators () [0] -> generator ());
00117 
00119     int nr=-1, nt=-1;
00120     double st=-1;
00121 
00122     if (cg) cg -> getStats (nr, nt, st);
00123     else printf ("Warning, could not get pointer to CouenneCutGenerator\n");
00124 
00125     CouenneProblem *cp = cg ? cg -> Problem () : NULL;
00126 
00127     // retrieve test value to check
00128     double global_opt;
00129     couenne.options () -> GetNumericValue ("couenne_check", global_opt, "couenne.");
00130 
00131     if (global_opt < COUENNE_INFINITY) { // some value found in couenne.opt
00132 
00133       double opt = bb.model (). getBestPossibleObjValue ();
00134 
00135       printf ("Global Optimum Test on %-40s %s\n", 
00136               cp ? cp -> problemName ().c_str () : "unknown", 
00137               (fabs (opt - global_opt) / 
00138                (1. + CoinMax (fabs (opt), fabs (global_opt))) < PRINTED_PRECISION) ? 
00139               (const char *) "OK" : (const char *) "FAILED");
00140               //opt, global_opt,
00141               //fabs (opt - global_opt));
00142 
00143     } else // good old statistics
00144 
00145     if (couenne.displayStats ()) { // print statistics
00146 
00147       // CAUTION: assuming first cut generator is our CouenneCutGenerator
00148 
00149       if (cg && !cp) printf ("Warning, could not get pointer to problem\n");
00150       else
00151         printf ("Stats: %-15s %4d [var] %4d [int] %4d [con] %4d [aux] "
00152                 "%6d [root] %8d [tot] %6g [sep] %8g [time] %8g [bb] "
00153                 "%20e [lower] %20e [upper] %7d [nodes]\n",// %s %s\n",
00154                 cp ? cp -> problemName ().c_str () : "unknown",
00155                 (cp) ? cp -> nOrigVars     () : -1, 
00156                 (cp) ? cp -> nOrigIntVars  () : -1, 
00157                 (cp) ? cp -> nOrigCons     () : -1,
00158                 (cp) ? (cp -> nVars     () - 
00159                         cp -> nOrigVars ()): -1,
00160                 nr, nt, st, 
00161                 CoinCpuTime () - time_start,
00162                 cg ? (CoinCpuTime () - cg -> rootTime ()) : CoinCpuTime (),
00163                 bb.model (). getBestPossibleObjValue (),
00164                 bb.model (). getObjValue (),
00165                 //bb.bestBound (),
00166                 //bb.bestObj (),
00167                 bb.numNodes ());
00168                 //bb.iterationCount ());
00169                 //status.c_str (), message.c_str ());
00170     }
00171 
00172 //    nlp_and_solver -> writeAmplSolFile (message, bb.bestSolution (), NULL);
00173   }
00174   catch(TNLPSolver::UnsolvedError *E) {
00175      E->writeDiffFiles();
00176      E->printError(std::cerr);
00177     //There has been a failure to solve a problem with Ipopt.
00178     //And we will output file with information on what has been changed in the problem to make it fail.
00179     //Now depending on what algorithm has been called (B-BB or other) the failed problem may be at different place.
00180     //    const OsiSolverInterface &si1 = (algo > 0) ? nlpSolver : *model.solver();
00181   }
00182   catch(OsiTMINLPInterface::SimpleError &E) {
00183     std::cerr<<E.className()<<"::"<<E.methodName()
00184              <<std::endl
00185              <<E.message()<<std::endl;
00186   }
00187   catch(CoinError &E) {
00188     std::cerr<<E.className()<<"::"<<E.methodName()
00189              <<std::endl
00190              <<E.message()<<std::endl;
00191   }
00192   catch (Ipopt::OPTION_INVALID &E)
00193   {
00194    std::cerr<<"Ipopt exception : "<<E.Message()<<std::endl;
00195   }
00196   catch (int generic_error) {
00197     if (generic_error == infeasible)
00198       printf ("problem infeasible\n");
00199   }
00200 
00201 //  catch(...) {
00202 //    std::cerr<<pbName<<" unrecognized excpetion"<<std::endl;
00203 //    std::cerr<<pbName<<"\t Finished \t exception"<<std::endl;
00204 //    throw;
00205 //  }
00206 
00207   delete [] pbName;
00208   return 0;
00209 }

Generated on Mon Aug 3 03:02:16 2009 by  doxygen 1.4.7