Changeset 44


Ignore:
Timestamp:
Sep 21, 2006 11:29:23 AM (13 years ago)
Author:
pbonami
Message:

Can add linear cut to nonlinear formulation in Ipopt

Location:
branches/devel/Bonmin/src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/devel/Bonmin/src/Apps/Makefile.am

    r40 r44  
    5858
    5959bonmin_LDADD = \
     60        ../BonminAmplInterface/libamplinterface.la\
    6061        libbonmin.la \
    61         ../BonminAmplInterface/libamplinterface.la\
    6262        $(IPOPTOBJDIR)/src/Apps/AmplSolver/libamplinterface.la \
    6363        $(IPOPTLIBS) \
     
    117117
    118118nodeSolver_LDADD = \
     119        ../BonminAmplInterface/libamplinterface.la \
    119120        libbonmin.la \
    120         ../BonminAmplInterface/libamplinterface.la \
    121121        $(IPOPTOBJDIR)/src/Apps/AmplSolver/libamplinterface.la \
    122122        $(IPOPTLIBS) \
  • branches/devel/Bonmin/src/Apps/Makefile.in

    r40 r44  
    9595        $(OSIOBJDIR)/src/libOsi.la $(CLPOBJDIR)/src/libClp.la \
    9696        $(COINUTILSOBJDIR)/src/libCoinUtils.la $(am__DEPENDENCIES_3)
    97 bonmin_DEPENDENCIES = libbonmin.la \
    98         ../BonminAmplInterface/libamplinterface.la \
     97bonmin_DEPENDENCIES = ../BonminAmplInterface/libamplinterface.la \
     98        libbonmin.la \
    9999        $(IPOPTOBJDIR)/src/Apps/AmplSolver/libamplinterface.la \
    100100        $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) \
     
    102102am_nodeSolver_OBJECTS = nodeSolver.$(OBJEXT)
    103103nodeSolver_OBJECTS = $(am_nodeSolver_OBJECTS)
    104 nodeSolver_DEPENDENCIES = libbonmin.la \
    105         ../BonminAmplInterface/libamplinterface.la \
     104nodeSolver_DEPENDENCIES = ../BonminAmplInterface/libamplinterface.la \
     105        libbonmin.la \
    106106        $(IPOPTOBJDIR)/src/Apps/AmplSolver/libamplinterface.la \
    107107        $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) \
     
    357357bonmin_SOURCES = bonmin.cpp
    358358bonmin_LDADD = \
     359        ../BonminAmplInterface/libamplinterface.la\
    359360        libbonmin.la \
    360         ../BonminAmplInterface/libamplinterface.la\
    361361        $(IPOPTOBJDIR)/src/Apps/AmplSolver/libamplinterface.la \
    362362        $(IPOPTLIBS) \
     
    395395nodeSolver_SOURCES = nodeSolver.cpp
    396396nodeSolver_LDADD = \
     397        ../BonminAmplInterface/libamplinterface.la \
    397398        libbonmin.la \
    398         ../BonminAmplInterface/libamplinterface.la \
    399399        $(IPOPTOBJDIR)/src/Apps/AmplSolver/libamplinterface.la \
    400400        $(IPOPTLIBS) \
  • branches/devel/Bonmin/src/IpoptInterface/IpCbcColReader.cpp

    r38 r44  
    3636  std::ifstream inFile(colFileName.c_str());
    3737  if(!inFile.is_open()) {
    38     std::cerr<<"Error in opening Names file (probably does not exists)";
    3938    return false;
    4039  }
  • branches/devel/Bonmin/src/IpoptInterface/IpoptInterface.cpp

    r26 r44  
    14951495IpoptInterface::getInfinity() const
    14961496{
    1497   return infty_;
     1497  return DBL_MAX;
    14981498}
    14991499
     
    20672067  const double * colUpper = getColUpper();
    20682068  const double * duals = getRowPrice();
    2069   double infty = 1e100 *getInfinity();
     2069  double infty = getInfinity();
     2070  double nlp_infty = infty_;
    20702071 
    20712072  for(int i = 0; i< m ; i++) {
    20722073    if(constTypes_[i] == Ipopt::TMINLP::NON_LINEAR) {
    2073       if(rowLower[i] > -infty && rowUpper[i] < infty && fabs(duals[i]) == 0.)
     2074      if(rowLower[i] > - nlp_infty && rowUpper[i] < nlp_infty && fabs(duals[i]) == 0.)
    20742075      {
    20752076        binding[i] = -1;
     
    20782079      }
    20792080      binding[i] = numBindings;
    2080       if(rowLower[i] > - infty_)
     2081      if(rowLower[i] > - nlp_infty)
    20812082        lb[numBindings] = rowLower[i] - g[i];
    20822083      else
    20832084        lb[numBindings] = - infty;
    2084       if(rowUpper[i] < infty_)
     2085      if(rowUpper[i] < nlp_infty)
    20852086        ub[numBindings] = rowUpper[i] - g[i];
    20862087      else
    2087         ub[numBindings] = infty_;
     2088        ub[numBindings] = infty;
    20882089      if(rowLower[i] > -infty && rowUpper[i] < infty)
    20892090      {
     
    21072108          ub[binding[jRow_[i] - 1]], tiny_, veryTiny_)) {
    21082109        cuts[binding[jRow_[i] - 1]].insert(jCol_[i]-1,jValues_[i]);
    2109         if(lb[binding[jRow_[i] - 1]] > - infty_)
     2110        if(lb[binding[jRow_[i] - 1]] > - infty)
    21102111          lb[binding[jRow_[i] - 1]] += jValues_[i] * getColSolution()[jCol_ [i] - 1];
    2111         if(ub[binding[jRow_[i] - 1]] < infty_)
     2112        if(ub[binding[jRow_[i] - 1]] < infty)
    21122113        ub[binding[jRow_[i] - 1]] += jValues_[i] * getColSolution()[jCol_ [i] - 1];
    21132114      }
     
    21202121
    21212122    newCut.setGloballyValid();
     2123    newCut.setEffectiveness(99.99e99);
    21222124    newCut.setLb(lb[i]);
    21232125    newCut.setUb(ub[i]);
     
    21592161    OsiRowCut newCut;
    21602162    newCut.setGloballyValid();
     2163    newCut.setEffectiveness(99.99e99);
    21612164    newCut.setRow(v);
    21622165    newCut.setLb(-DBL_MAX/*Infinity*/);
     
    22322235  const double * duals = getRowPrice();
    22332236  assert(m==getNumRows());
    2234   double infty = getInfinity() * 1e100;
     2237  double infty = getInfinity();
     2238  double nlp_infty = infty_;
    22352239  for(int i = 0 ; i < m ; i++) {
    22362240    {
    22372241      if(constTypes_[i] == Ipopt::TMINLP::NON_LINEAR) {
    22382242        //If constraint is equality not binding do not add
    2239         if(rowLower[i] > -infty && rowUpper[i] < infty && fabs(duals[i]) == 0.)
     2243        if(rowLower[i] > -nlp_infty && rowUpper[i] < nlp_infty && fabs(duals[i]) == 0.)
    22402244        {
    22412245            binding[i] = -1;
     
    22442248        else
    22452249          binding[i] = numBindings;
    2246         if(rowLower[i] > - infty_)
     2250        if(rowLower[i] > - nlp_infty)
    22472251          rowLow[numBindings] = (rowLower[i] - g[i]) - 1e-07;
    22482252        else
    2249           rowLow[numBindings] = - infty_;
    2250         if(rowUpper[i] < infty_)
     2253          rowLow[numBindings] = - infty;
     2254        if(rowUpper[i] < nlp_infty)
    22512255          rowUp[numBindings] =  (rowUpper[i] - g[i]) + 1e-07;
    22522256        else
    2253           rowUp[numBindings] = infty_;
     2257          rowUp[numBindings] = infty;
    22542258       
    22552259        //If equality or ranged constraint only add one side by looking at sign of dual multiplier
    2256         if(rowLower[i] > -infty && rowUpper[i] < infty)
     2260        if(rowLower[i] > -nlp_infty && rowUpper[i] < nlp_infty)
    22572261        {
    22582262          if(duals[i] >= 0)// <= inequality
     
    24122416  }
    24132417
    2414 si.writeMps("init");
    24152418
    24162419  setWarmStartOptions();
     
    24202423}
    24212424
    2422 
     2425/** Add a collection of linear cuts to problem formulation.*/
     2426void
     2427IpoptInterface::applyRowCuts(int numberCuts, const OsiRowCut * cuts)
     2428{
     2429  const OsiRowCut ** cutsPtrs = new const OsiRowCut*[numberCuts];
     2430  for(int i = 0 ; i < numberCuts ; i++)
     2431  {
     2432    cutsPtrs[i] = &cuts[i];
     2433  }
     2434  tminlp_->addCuts(numberCuts, cutsPtrs);
     2435  delete [] cutsPtrs;
     2436}
     2437
  • branches/devel/Bonmin/src/IpoptInterface/IpoptInterface.hpp

    r26 r44  
    646646  virtual void deleteRows(const int num, const int * rowIndices)
    647647  {
    648     if(num>0)
    649       throw CoinError("Ipopt model does not implement this function.",
    650           "deleteRows","IpoptInterface");
     648     tminlp_->removeCuts(num, rowIndices);
     649  }
     650
     651  void deleteLastRows(int number){
     652  tminlp_->removeLastCuts(number);
    651653  }
    652654
     
    811813  //@}
    812814  /** get NLP constraint violation of current point */
    813   double getConstraintViolation();
     815
     816
     817   /** Add a collection of linear cuts to problem formulation.*/
     818  virtual void applyRowCuts(int numberCuts, const OsiRowCut * cuts);
     819
     820
     821  /** Add a collection of linear cuts to the problem formulation */
     822  virtual void applyRowCuts(int numberCuts, const OsiRowCut ** cuts)
     823  {
     824    tminlp_->addCuts(numberCuts, cuts);
     825  }
     826
     827
     828 double getConstraintViolation();
    814829
    815830//---------------------------------------------------------------------------
     
    834849
    835850
    836   /** We have to keep this but it will throw an error.
     851  /** Add a linear cut to the problem formulation.
    837852  */
    838853  virtual void applyRowCut( const OsiRowCut & rc )
    839854  {
    840     throw SimpleError("Ipopt model does not implement this function.",
    841         "applyRowCut");
     855    const OsiRowCut * cut = &rc;
     856    tminlp_->addCuts(1, &cut);
    842857  }
    843858  /** We have to keep this but it will throw an error.
  • branches/devel/Bonmin/src/IpoptInterface/Makefile.am

    r1 r44  
    2525        IpoptWarmStart.cpp IpoptWarmStart.hpp \
    2626        TMINLP2TNLP.cpp TMINLP2TNLP.hpp \
    27         TMINLP.hpp \
     27        TMINLP.cpp TMINLP.hpp \
    2828        TNLP2FPNLP.cpp TNLP2FPNLP.hpp
    2929
     
    9494        IpoptWarmStart.cppbak IpoptWarmStart.hppbak \
    9595        TMINLP2TNLP.cppbak TMINLP2TNLP.hppbak \
    96         TMINLP.hppbak \
     96        TMINLP.hppbak TMINLP.hppbak \
    9797        TNLP2FPNLP.cppbak TNLP2FPNLP.hppbak
    9898
  • branches/devel/Bonmin/src/IpoptInterface/Makefile.in

    r1 r44  
    6060        IpCbcColReader.lo IpCbcStartPointReader.lo IpoptInterface.lo \
    6161        IpoptInteriorWarmStarter.lo IpoptWarmStart.lo TMINLP2TNLP.lo \
    62         TNLP2FPNLP.lo
     62        TMINLP.lo TNLP2FPNLP.lo
    6363libipoptinterface_la_OBJECTS = $(am_libipoptinterface_la_OBJECTS)
    6464depcomp = $(SHELL) $(top_srcdir)/../depcomp
     
    317317        IpoptWarmStart.cpp IpoptWarmStart.hpp \
    318318        TMINLP2TNLP.cpp TMINLP2TNLP.hpp \
    319         TMINLP.hpp \
     319        TMINLP.cpp TMINLP.hpp \
    320320        TNLP2FPNLP.cpp TNLP2FPNLP.hpp
    321321
     
    377377        IpoptWarmStart.cppbak IpoptWarmStart.hppbak \
    378378        TMINLP2TNLP.cppbak TMINLP2TNLP.hppbak \
    379         TMINLP.hppbak \
     379        TMINLP.hppbak TMINLP.hppbak \
    380380        TNLP2FPNLP.cppbak TNLP2FPNLP.hppbak
    381381
     
    439439@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IpoptInteriorWarmStarter.Plo@am__quote@
    440440@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IpoptWarmStart.Plo@am__quote@
     441@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TMINLP.Plo@am__quote@
    441442@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TMINLP2TNLP.Plo@am__quote@
    442443@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TNLP2FPNLP.Plo@am__quote@
  • branches/devel/Bonmin/src/IpoptInterface/TMINLP.hpp

    r26 r44  
    5555  {
    5656  public:
    57     /**@name Constructors/Destructors */
    58     //@{
    59     TMINLP()
    60     {}
    61     ;
    62 
    63     /** Default destructor */
    64     virtual ~TMINLP()
    65     {}
    66     ;
    67     //@}
     57    friend class TMINLP2TNLP;
    6858
    6959    /** Class to store sos constraints for model */
     
    8979      /** @} */
    9080      /** default constructor. */
    91       SosInfo():num(0), types(NULL), priorities(NULL), numNz(0), starts(NULL),
    92              indices(NULL), weights(NULL)
    93       {}
     81      SosInfo();
    9482      /** Copy constructor.*/
    95       SosInfo(const SosInfo & source):num(source.num), types(NULL), priorities(NULL),
    96           numNz(source.numNz), starts(NULL),indices(NULL),
    97            weights(NULL)
    98       {
    99 
    100         if(num > 0) {
    101           assert(source.types!=NULL);
    102           assert(source.priorities!=NULL);
    103           assert(source.starts!=NULL);
    104           assert(source.indices!=NULL);
    105           assert(source.weights!=NULL);
    106           types = new char[num];
    107           priorities = new int[num];
    108           starts = new int[num + 1];
    109           indices = new int[numNz];
    110           weights = new double[numNz];
    111           for(int i = 0 ; i < num ; i++) {
    112             source.types[i] = types[i];
    113             source.priorities[i] = priorities[i];
    114             source.starts[i] = starts[i];
    115           }
    116           for(int i = 0 ; i < numNz ; i++) {
    117             source.indices[i] = indices[i];
    118             source.weights[i] = weights[i];
    119           }
    120         }
    121         else {
    122           assert(source.types==NULL);
    123           assert(source.priorities==NULL);
    124           assert(source.starts==NULL);
    125           assert(source.indices==NULL);
    126           assert(source.weights==NULL);
    127         }
    128 
    129       }
     83      SosInfo(const SosInfo & source);
     84     
    13085
    13186      /** destructor*/
     
    13792
    13893      /** Reset information */
    139       void gutsOfDestructor()
    140       {
    141         num = 0;
    142         numNz = 0;
    143         if(types) delete [] types;
    144         types = NULL;
    145         if(starts) delete [] starts;
    146         starts = NULL;
    147         if(indices) delete [] indices;
    148         indices = NULL;
    149         if(priorities) delete [] priorities;
    150         priorities = NULL;
    151         if(weights) delete [] weights;
    152         weights = NULL;
    153       }
     94      void gutsOfDestructor();
     95
    15496    };
    15597
     
    210152    };
    211153
     154    /**@name Constructors/Destructors */
     155    //@{
     156    TMINLP();
     157
     158    /** Default destructor */
     159    virtual ~TMINLP()
     160    {}
     161    //@}
     162;
     163
    212164    /**@name methods to gather information about the MINLP */
    213165    //@{
     
    294246    virtual const BranchingInfo * branchingInfo() const = 0;
    295247
    296     virtual const SosInfo * sosConstraints() const = 0;
     248    /** Add some linear cuts to the problem formulation */
     249   void addCuts(int numberCuts, const OsiRowCut ** cuts);
     250 
     251   /** Remove some cuts to the formulation */
     252   void removeCuts(int number ,const int * toRemove);
     253
     254   /** remove the last number cuts.*/
     255   void removeLastCuts(int number);
     256
     257   virtual const SosInfo * sosConstraints() const = 0;
    297258  private:
    298259    /**@name Default Compiler Generated Methods
     
    314275    //@}
    315276
    316 
     277      private:
     278  /** resize arrays for linear cuts */
     279  void resizeLinearCuts(int newNumberCuts, int newNnz);
     280  /** columnindices of linear cuts. */
     281   int * jCol_;
     282  /** rows indices of linear cuts. */
     283   int * iRow_;
     284  /** elements of linear cuts.*/
     285  double * elems_;
     286  /** lower bounds for linear cuts. */
     287  double * lower_;
     288  /** upper bounds for linear cuts. */
     289  double * upper_;
     290  /** number of linear cuts.*/
     291  int nLinearCuts_;
     292  /** number of non-zeroes in linear cuts*/
     293  int linearCutsNnz_;
     294  /** storage size for linear cuts number of cuts.*/
     295  int linearCutsCapacity_;
     296  /** storage size for linear cuts number of nnz.*/
     297  int linearCutsNnzCapacity_;
    317298  };
    318299
  • branches/devel/Bonmin/src/IpoptInterface/TMINLP2TNLP.cpp

    r26 r44  
    227227  void TMINLP2TNLP::setDualsInit(Index n, const Number* duals_init)
    228228  {
    229     DBG_ASSERT(n == m_ + 2 * n_);
     229    DBG_ASSERT(n == m_ );
    230230
    231231    if(!duals_init_)
     
    256256      Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style)
    257257  {
    258     return tminlp_->get_nlp_info(n, m, nnz_jac_g, nnz_h_lag, index_style);
     258    bool return_code = tminlp_->get_nlp_info(n, m, nnz_jac_g, nnz_h_lag, index_style);
     259    m += tminlp_->nLinearCuts_;
     260    nnz_jac_g += tminlp_->linearCutsNnz_;
     261    return return_code;
    259262  }
    260263
     
    264267    IpBlasDcopy(n, x_l_, 1, x_l, 1);
    265268    IpBlasDcopy(n, x_u_, 1, x_u, 1);
    266     IpBlasDcopy(m, g_l_, 1, g_l, 1);
    267     IpBlasDcopy(m, g_u_, 1, g_u, 1);
    268 
     269    IpBlasDcopy(m_, g_l_, 1, g_l, 1);
     270    IpBlasDcopy(m_, g_u_, 1, g_u, 1);
     271    IpBlasDcopy(tminlp_->nLinearCuts_, tminlp_->lower_, 1, &g_l[m_],1);
     272    IpBlasDcopy(tminlp_->nLinearCuts_, tminlp_->upper_, 1, &g_u[m_],1);
    269273    return true;
    270274  }
     
    275279      Number* lambda)
    276280  {
    277     assert(m==m_);
     281    assert(m==m_ + tminlp_->nLinearCuts_);
    278282    assert(n==n_);
    279283    if (init_x == true) {
     
    292296      if(duals_init_ == NULL)
    293297        return false;
    294       IpBlasDcopy(m, duals_init_, 1, lambda, 1);
     298      IpBlasDcopy(m_, duals_init_, 1, lambda, 1);
     299      for(int i = 0 ; i < tminlp_->nLinearCuts_; i++)
     300      {
     301        lambda [i + m_ ] = 0.;
     302      }
    295303    }
    296304
     
    327335      Index m, Number* g)
    328336  {
    329     return tminlp_->eval_g(n, x, new_x, m, g);
     337    int return_code = tminlp_->eval_g(n, x, new_x, m_, g);
     338    // Add the linear cuts
     339    int nnz = 0;
     340    for(int i = 0 ; i < tminlp_->nLinearCuts_ ; i++)
     341    {
     342      int iplusm = m_ + i;
     343      g[iplusm] = 0;
     344      while(tminlp_->iRow_[nnz]==i) { g[iplusm] += tminlp_->elems_[nnz] * x[tminlp_->jCol_[nnz]]; nnz++;}
     345    }
     346    return return_code;
    330347  }
    331348
     
    334351      Index *jCol, Number* values)
    335352  {
    336     return tminlp_->eval_jac_g(n, x, new_x, m, nele_jac, iRow, jCol, values);
     353    int return_code = tminlp_->eval_jac_g(n, x, new_x, m_ , nele_jac - tminlp_->nLinearCuts_,
     354                                          iRow, jCol, values);
     355    bool isFortran = index_style_ == TNLP::FORTRAN_STYLE;
     356    if(iRow != NULL)
     357    {
     358      DBG_ASSERT(iCol != NULL);
     359      DBG_ASSERT(values == NULL)
     360     
     361      int nnz = nele_jac - tminlp_->linearCutsNnz_ ;
     362      for(int i = 0; i < tminlp_->linearCutsNnz_ ; i++ , nnz++)
     363      {
     364         iRow[nnz] = tminlp_->iRow_[i] + m_ + isFortran;
     365         jCol[nnz] = tminlp_->jCol_[i] + isFortran;
     366      }
     367    }
     368    else
     369    {
     370      DBG_ASSERT(iCol == NULL);
     371      DBG_ASSERT(values != NULL);
     372      IpBlasDcopy(tminlp_->linearCutsNnz_ , tminlp_->elems_, 1, &values[nele_jac - tminlp_->linearCutsNnz_],1);
     373    }
     374  return return_code;
    337375  }
    338376
     
    353391  {
    354392    assert(n==n_);
    355     assert(m==m_);
     393    assert(m==m_ + tminlp_->nLinearCuts_);
    356394    if (!x_sol_) {
    357395      x_sol_ = new Number[n];
     
    550588// namespace Ipopt
    551589
    552 
    553 
    554 
    555 
    556 
  • branches/devel/Bonmin/src/OaInterface/IpCbcOACutGenerator.cpp

    r1 r44  
    6060  }
    6161
     62#ifdef 0
     63 //Add tight cuts at LP optimum
     64 int numberCuts = si.getNumRows() - nlp_->getNumRows();
     65 const OsiRowCut ** cuts = new const OsiRowCut*[numberCuts];
     66 int begin = nlp_->getNumRows();
     67 numberCuts = 0;
     68 int end = si.getNumRows();
     69 const double * rowLower = si.getRowLower();
     70 const double * rowUpper = si.getRowUpper();
     71 const CoinPackedMatrix * mat = si.getMatrixByRow();     
     72 const CoinBigIndex * starts = mat->getVectorStarts();
     73 const int * lengths = mat->getVectorLengths();
     74 const double * elements = mat->getElements();
     75 const int * indices = mat->getIndices();
     76 
     77 for(int i = begin ; i < end ; i++, numberCuts++)
     78 {
     79   bool nnzExists=false;
     80   for(int k = starts[i] ; k < starts[i]+lengths[i] ; k++)
     81   {
     82     if(indices[k] == nlp_->getNumCols())
     83     {
     84       nnzExists = true;
     85       char sign = (elements[k]>0.)?'+':'-';
     86       char type='<';
     87       if(rowLower[i]>-1e20) type='>';
     88       std::cout<<"Non zero with sign: "<<sign<<", type: "<<type<<std::endl;
     89     }
     90   }
     91   if(nnzExists)
     92   {
     93      numberCuts--;
     94      continue;
     95   }
     96   else
     97      std::cout<<"No nonzero element"<<std::endl;
     98   int * indsCopy = CoinCopyOfArray(&indices[starts[i]], lengths[i]);
     99   double * elemsCopy = CoinCopyOfArray(&elements[starts[i]], lengths[i]);
     100   cuts[numberCuts] = new OsiRowCut(rowLower[i], rowUpper[i], lengths[i], lengths[i],
     101                           indsCopy, elemsCopy);
     102 }
     103 nlp_->applyRowCuts(numberCuts,cuts);
     104#endif
    62105  //Now solve the NLP get the cuts, reset bounds and get out
    63106
     
    128171    }
    129172  }
     173  nlp_->deleteLastRows(numberCuts);
    130174  delete [] saveColLb;
    131175  delete [] saveColUb;
Note: See TracChangeset for help on using the changeset viewer.