Ignore:
Timestamp:
May 16, 2013 4:44:48 PM (7 years ago)
Author:
pbelotti
Message:

replaced some iterators with const_iterator for compiling issues in MSVS, as well as drand48().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Couenne/src/main/BonCouenne.cpp

    r943 r955  
    4949#include "CouenneJournalist.hpp"
    5050
     51#include "CouenneExprClone.hpp"
     52#include "CouenneExprGroup.hpp"
     53#include "CouenneExprAbs.hpp"
     54#include "CouenneExprConst.hpp"
     55#include "CouenneExprCos.hpp"
     56#include "CouenneExprDiv.hpp"
     57#include "CouenneExprExp.hpp"
     58#include "CouenneExprInv.hpp"
     59#include "CouenneExprLog.hpp"
     60//#include "CouenneExprMax.hpp"
     61//#include "CouenneExprMin.hpp"
     62#include "CouenneExprMul.hpp"
     63#include "CouenneExprOpp.hpp"
     64#include "CouenneExprPow.hpp"
     65#include "CouenneExprSin.hpp"
     66#include "CouenneExprSub.hpp"
     67#include "CouenneExprSum.hpp"
     68#include "CouenneExprVar.hpp"
     69
    5170#ifdef COIN_HAS_NTY
    52 #include "Nauty.h"
    53 
    54 // FIXME: horrible global variables. Brrr.
     71// FIXME: horrible global variable. Brrr.
    5572#include "CouenneBranchingObject.hpp"
    5673#endif
     
    7390#endif
    7491
     92class MyTMINLP : public Bonmin::TMINLP {
     93   virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g,
     94       Ipopt::Index& nnz_h_lag, Ipopt::TNLP::IndexStyleEnum& index_style) {
     95      n = 2; m = 1; nnz_jac_g = 0; nnz_h_lag = 0; index_style = Ipopt::TNLP::C_STYLE;
     96   };
     97
     98   virtual bool get_variables_types(Ipopt::Index n, VariableType* var_types) {};
     99
     100   virtual bool get_variables_linearity(Ipopt::Index n,
     101                 Ipopt::TNLP::LinearityType* var_types) {};
     102
     103   virtual bool get_constraints_linearity(Ipopt::Index m,
     104                 Ipopt::TNLP::LinearityType* const_types) {};
     105
     106   virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u,
     107       Ipopt::Index m, Ipopt::Number* g_l, Ipopt::Number* g_u) {};
     108
     109   virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x,
     110                                   bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U,
     111       Ipopt::Index m, bool init_lambda,
     112       Ipopt::Number* lambda) {};
     113
     114   virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
     115       Ipopt::Number& obj_value) {};
     116
     117   virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
     118       Ipopt::Number* grad_f) {};
     119
     120   virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
     121       Ipopt::Index m, Ipopt::Number* g) {};
     122
     123   virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
     124       Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index* iRow,
     125       Ipopt::Index *jCol, Ipopt::Number* values) {};
     126
     127   virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
     128       Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda,
     129       bool new_lambda, Ipopt::Index nele_hess,
     130       Ipopt::Index* iRow, Ipopt::Index* jCol, Ipopt::Number* values) {};
     131
     132   virtual void finalize_solution(TMINLP::SolverReturn status,
     133                                  Ipopt::Index n, const Ipopt::Number* x, Ipopt::Number obj_value) {};
     134
     135   virtual const BranchingInfo * branchingInfo() const {};
     136
     137   virtual const SosInfo * sosConstraints() const {};
     138};
     139
    75140//#define FM_FRES
    76141
     
    90155  char * pbName = NULL;
    91156
    92   bool infeasible = false;
     157  const int infeasible = 1;
    93158
    94159  try {
    95160
    96161    CouenneBab bb;
     162    //    bb.setUsingCouenne (true);
    97163
    98164    CouenneProblem *p = NULL;
    99165    CouenneInterface *ci = NULL;
    100166
    101 #if 0
     167    SmartPtr<Bonmin::TMINLP> tminlp;
     168
     169#if 1
    102170    //ci = new CouenneInterface;
    103171    p = new CouenneProblem;
     
    105173    p -> addVariable (false, p -> domain ());
    106174    p -> addVariable (false, p -> domain ());
    107     p -> addVariable (false, p -> domain ());
    108     p -> addVariable (false, p -> domain ());
    109 
    110     p -> addObjective    (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (2))), "min");
    111     p -> addLEConstraint (new exprSum (new exprClone (p->Var (0)), new exprClone (p->Var (2))), new exprConst (1));
    112     p -> addEQConstraint (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (2))), new exprConst (1));
    113     p -> addEQConstraint (new exprSum (new exprClone (p->Var (1)), new exprClone (p->Var (3))), new exprConst (1));
    114     p -> addEQConstraint (new exprSum (new exprClone (p->Var (2)), new exprClone (p->Var (3))), new exprConst (1));
     175
     176    double x_[2] = {0.,0.};
     177    double lb_[2] = {-COUENNE_INFINITY,-COUENNE_INFINITY};
     178    double ub_[2] = {COUENNE_INFINITY,COUENNE_INFINITY};
     179
     180    p->domain()->push(2, x_, lb_, ub_);
     181
     182    expression* nl[1] = { new exprOpp(new exprMul(new exprSum(new exprOpp(new exprPow(new exprClone(p->Var(1)), new exprConst(2.0))), new exprClone(p->Var(1))), new exprConst(100.0))) };
     183    exprGroup::lincoeff lin(1, std::pair<exprVar*, double>(p->Var(0), 100.0));
     184
     185    p -> addObjective    (new exprConst(1), "min");
     186    p -> addEQConstraint (new exprGroup(0, lin, nl, 1), new exprConst (0));
     187
     188    p->print();
     189
     190    tminlp = new MyTMINLP();
    115191#endif
    116192
    117193    CouenneSetup couenne;
     194
     195    if (!couenne.InitializeCouenne (argv, p, tminlp, ci, &bb))
     196      throw infeasible;
     197
     198    //////////////////////////////
    118199    CouenneCutGenerator *cg = NULL;
    119     ConstJnlstPtr jnlst;
    120     CouenneProblem *prob = NULL;
    121 
    122     infeasible = !(couenne.InitializeCouenne (argv, p, NULL, ci, &bb));
    123200
    124201    // there is only one CouenneCutGenerator object; scan array until
     
    128205
    129206      for (std::list <Bonmin::BabSetupBase::CuttingMethod>::iterator
    130              i  = couenne.cutGenerators () . begin ();
     207                   i  = couenne.cutGenerators () . begin ();
    131208           !cg && (i != couenne.cutGenerators () . end ());
    132209           ++i)
     
    143220      cg -> setBabPtr (&bb);
    144221
    145     else if (!infeasible) {
    146 
     222    else {
    147223      printf ("main(): ### ERROR: Can not get CouenneCutGenerator\n");
    148       exit (-1);
     224      exit (1);
    149225    }
    150226
    151227    // initial printout
    152228
    153     jnlst = couenne. couennePtr () -> Jnlst ();
    154     prob  = couenne. couennePtr () -> Problem ();
     229    ConstJnlstPtr jnlst = couenne. couennePtr () -> Jnlst ();
     230
     231    CouenneProblem *prob = couenne. couennePtr () -> Problem ();
    155232
    156233    bb. setProblem (prob);
    157 
    158     int retcomp = 2; // unspecified.
    159234
    160235    jnlst -> Printf (J_ERROR, J_COUENNE, "\
     
    187262    //////////////////////////////////
    188263
    189 #ifdef COIN_HAS_NTY
    190     double symmGroupSize = prob -> orbitalBranching () ? prob -> getNtyInfo () -> getGroupSize () : -1;
    191 #endif
    192 
    193     if (!infeasible)
    194       bb (couenne); // do branch and bound
     264    bb (couenne); // do branch and bound
    195265
    196266#ifdef COIN_HAS_NTY
     
    210280
    211281#if defined (FM_TRACE_OPTSOL) || defined (FM_FRES)
    212     double cbcLb = (infeasible ? -COIN_DBL_MAX : bb.model (). getBestPossibleObjValue ());
     282    double cbcLb = bb.model ().getBestPossibleObjValue();
    213283    double printObj = 0;
    214284    bool foundSol = false;
     
    225295
    226296    if(cp != NULL) {
    227       double cbcObjVal = infeasible ? COIN_DBL_MAX : bb.model().getObjValue();
    228       int modelNvars = prob -> nVars ();//bb.model().getNumCols();
     297      double cbcObjVal = bb.model().getObjValue();
     298      int modelNvars = bb.model().getNumCols();
    229299
    230300      CouenneRecordBestSol *rs = cp->getRecordBestSol();
    231       const double *cbcSol = infeasible ? NULL : bb.model().getColSolution();
     301      const double *cbcSol = bb.model().getColSolution();
    232302      double *modCbcSol = new double[modelNvars];
    233303      double modCbcSolVal= 1e100, modCbcSolMaxViol = 0;
     
    240310      }
    241311
    242       // round cbcSol's integer coordinates
    243 
    244       // for (int i=0; i<modelNvars; i++)
    245       //        if (cp -> Var (i) -> isDefinedInteger ())
    246       //          cbcSol [i] = COUENNE_round (cbcSol [i]);
    247 
    248       if (cbcObjVal < 1e49 && !infeasible) {
     312      if(cbcObjVal < 1e49) {
    249313
    250314#ifdef FM_CHECKNLP2
     
    271335      bool couenneSolIsFeas = false;
    272336
    273       // round couenneSol's integer coordinates
    274 
    275       // for (int i=0; i<modelNvars; i++)
    276       //        if (cp -> Var (i) -> isDefinedInteger ())
    277       //          couenneSol [i] = COUENNE_round (couenneSol [i]);
    278 
    279337      if(couenneSol != NULL) {
    280338#ifdef FM_CHECKNLP2
    281         int cMS = rs->getCardModSol();
    282339        couenneSolIsFeas = cp->checkNLP2(couenneSol, 0, false,
    283340                                         false, true,
    284341                                         cp->getFeasTol());
     342        int cMS = rs->getCardModSol();
    285343        CoinCopyN(rs->getModSol(cMS), cMS, modCouenneSol);
    286344        modCouenneSolVal = rs->getModSolVal();
    287345        modCouenneSolMaxViol = rs->getModSolMaxViol();
    288346#else /* not FM_CHECKNLP2 */
     347        couenneSolIsFeas = cp->checkNLP(couenneSol, modCouenneSolVal, true);
    289348        int cMS = cp->nVars();
    290         couenneSolIsFeas = cp->checkNLP(couenneSol, modCouenneSolVal, true);
    291349        CoinCopyN(couenneSol, cMS, modCouenneSol);
    292350        modCouenneSolMaxViol = cp->getFeasTol();
     
    295353      }
    296354
    297       retcomp = rs -> compareAndSave (modCbcSol,     modCbcSolVal,     modCbcSolMaxViol,     cbcSolIsFeas,
    298                                       modCouenneSol, modCouenneSolVal, modCouenneSolMaxViol, couenneSolIsFeas,
    299                                       modelNvars, cp->getFeasTol());
    300 
    301       // switch (retcomp) {
    302       // case -1: printf("No solution found\n"); break;
    303       // case 0: printf("Best solution found by Cbc. Value: %10.4f. Tolerance: %10g\n", modCbcSolVal, modCbcSolMaxViol); break;
    304       // case 1: //printf("Best solution found by Couenne  Value: %10.4f  Tolerance: %10g\n", modCouenneSolVal, modCouenneSolMaxViol); break;
    305       // default: break; // never happens
    306       // }
     355      int retcomp = rs->compareAndSave(modCbcSol, modCbcSolVal,
     356                                       modCbcSolMaxViol, cbcSolIsFeas,
     357                                       modCouenneSol, modCouenneSolVal,
     358                                       modCouenneSolMaxViol, couenneSolIsFeas,
     359                                       modelNvars, cp->getFeasTol());
     360      switch (retcomp) {
     361      case -1: printf("No solution found\n"); break;
     362      case 0: printf("Best solution found by Cbc. Value: %10.4f. Tolerance: %10g\n", modCbcSolVal, modCbcSolMaxViol); break;
     363      case 1: //printf("Best solution found by Couenne  Value: %10.4f  Tolerance: %10g\n", modCouenneSolVal, modCouenneSolMaxViol); break;
     364      default: break; // never happens
     365      }
    307366
    308367      if(rs->getHasSol()) {
     
    350409        fprintf(f_res, "         *");
    351410      }
    352       fprintf(f_res, " %10d %10.4f\n", infeasible ? 0 : bb.numNodes (),
     411      fprintf(f_res, " %10d %10.4f\n", bb.numNodes (),
    353412              CoinCpuTime () - time_start);
    354413      fclose(f_res);
     
    361420
    362421    double
    363       ub = infeasible ?  COIN_DBL_MAX : bb. model (). getObjValue (),
    364       lb = infeasible ? -COIN_DBL_MAX : bb. model (). getBestPossibleObjValue ();
     422      ub = bb. model (). getObjValue (),
     423      lb = bb. model (). getBestPossibleObjValue ();
    365424
    366425    if (cp -> getRecordBestSol () &&
    367         cp       -> getRecordBestSol () -> getHasSol () &&
    368         (ub > cp -> getRecordBestSol () -> getVal    ()))   
    369       ub = cp -> getRecordBestSol () -> getVal ();
    370 
    371     if (false || //(fabs (lb) > COUENNE_INFINITY / 1e4) ||
    372         (lb > ub))
    373       lb = ub;
     426        (ub > cp -> getRecordBestSol () -> getVal ())) ub = cp -> getRecordBestSol () -> getVal ();
     427
     428    if ((fabs (lb) > COUENNE_INFINITY / 1e4) ||
     429        (lb > ub))                                     lb = ub;
    374430
    375431    char
    376       *gapstr = new char [40],
    377       *lbstr  = new char [40],
    378       *ubstr  = new char [40];
    379 
    380     // CouenneSolverInterface <OsiClpSolverInterface> *csi = dynamic_cast <CouenneSolverInterface *> (couenne.continuousSolver ());
    381 
    382     // double rootLB = csi -> rootLB ();
     432      *gapstr = new char [80],
     433      *lbstr  = new char [80],
     434      *ubstr  = new char [80];
    383435
    384436    sprintf (lbstr,  "%10g",     lb);
     
    386438    sprintf (gapstr, "%.2f%%", fabs (100. * (ub - lb) / (1. + fabs (lb))));
    387439
    388     if (!infeasible)
    389       jnlst -> Printf (J_ERROR, J_COUENNE, "\n\
     440    jnlst -> Printf (J_ERROR, J_COUENNE, "\n\
    390441Linearization cuts added at root node:   %8d\n\
    391 Linearization cuts added in total:       %8d  (separation time: %gs)\n",
    392                        nr, nt, st);
    393 
    394     else jnlst -> Printf (J_ERROR, J_COUENNE, "Problem infeasible\n");
    395 
    396     jnlst -> Printf (J_NONE, J_COUENNE, "\
    397 Total time:                              %8gs (%gs in branch-and-bound)\n\
     442Linearization cuts added in total:       %8d  (separation time: %gs)\n\
     443Total solving time:                      %8gs (%gs in branch-and-bound)\n\
    398444Lower bound:                           %s\n\
    399445Upper bound:                           %s  (gap: %s)\n\
    400446Branch-and-bound nodes:                  %8d\n",
     447                     nr, nt, st,
    401448                     CoinCpuTime () - time_start,
    402449                     cg ? (CoinCpuTime () - CoinMax (time_start, cg -> rootTime ())) : CoinCpuTime () - time_start,
    403                      ((lb <= -8.9999e12) ||
    404                                        infeasible ||          (fabs (lb)             > COUENNE_INFINITY/1e4)) ? "      -inf" : lbstr,
    405                      ((retcomp < 0) || infeasible ||                           (ub   > COUENNE_INFINITY/1e4)) ? "       inf" : ubstr,
    406                      (                 infeasible || (CoinMax (fabs (lb), fabs (ub)) > COUENNE_INFINITY/1e4)) ? "--"        : gapstr,
    407                      infeasible ? 0 : bb.numNodes ());
    408 
    409     // if (fabs (ub - bb. model (). getObjValue ()) > COUENNE_EPS * fabs (ub))
    410     //   jnlst -> Printf (J_ERROR, J_COUENNE,
    411     //                 "Warning: upper bounds differ between Couenne and Cbc. Saving Couenne's (more reliable).\n");
     450                     (lb < -9e12) ||
     451                     (lb > COUENNE_INFINITY/1e4) ? "      -inf" : lbstr,
     452                     (ub > COUENNE_INFINITY/1e4) ? "       inf" : ubstr,
     453                     (ub > COUENNE_INFINITY/1e4) ? "--" : gapstr,
     454                     bb.numNodes ());
     455
     456    if (fabs (ub - bb. model (). getObjValue ()) > COUENNE_EPS * fabs (ub))
     457      jnlst -> Printf (J_ERROR, J_COUENNE,
     458                       "Warning: upper bounds differ between Couenne and Cbc. Saving Couenne's (more reliable).\n");
    412459
    413460    delete [] lbstr;
     
    417464    if (global_opt < COUENNE_INFINITY) { // some value found in couenne.opt
    418465
    419       double opt = infeasible ? -COIN_DBL_MAX : bb.model (). getBestPossibleObjValue ();
     466      double opt = bb.model (). getBestPossibleObjValue ();
    420467
    421468      printf ("Global Optimum Test on %-40s %s\n",
     
    435482        printf ("Stats: %-15s %4d [var] %4d [int] %4d [con] %4d [aux] "
    436483                "%6d [root] %8d [tot] %6g [sep] %8g [time] %8g [bb] "
    437 #ifdef COIN_HAS_NTY
    438                 "%20e [lower] %20e [upper] %7d [nodes] %.0g [sg] %d [sgc]\n",
    439 #else
    440                 "%20e [lower] %20e [upper] %7d [nodes]\n",
    441 #endif
     484                "%20e [lower] %20e [upper] %7d [nodes]\n",// %s %s\n",
    442485                cp ? cp -> problemName (). c_str () : "unknown",
    443486                (cp) ? cp -> nOrigVars     () : -1,
     
    453496                //bb.bestBound (),
    454497                //bb.bestObj (),
    455                 infeasible ? 0 : bb.numNodes ()
    456 #ifdef COIN_HAS_NTY
    457                 ,symmGroupSize
    458                 ,CouenneBranchingObject::nSGcomputations
    459 #endif
    460 );
     498                bb.numNodes ());
    461499                //bb.iterationCount ());
    462500                //status.c_str (), message.c_str ());
     
    487525   std::cerr<<"Ipopt exception : "<<E.Message()<<std::endl;
    488526  }
    489   // catch (int generic_error) {
    490   //   // if (generic_error == infeasible)
    491   //   //   printf ("problem infeasible\n");
    492   // }
     527  catch (int generic_error) {
     528    if (generic_error == infeasible)
     529      printf ("problem infeasible\n");
     530  }
    493531
    494532//  catch(...) {
Note: See TracChangeset for help on using the changeset viewer.