Changeset 126


Ignore:
Timestamp:
Nov 21, 2006 4:27:08 PM (13 years ago)
Author:
pbonami
Message:

Filter with warm starts almost working

Location:
branches/devel/Bonmin/src/Interfaces/Filter
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/devel/Bonmin/src/Interfaces/Filter/BonFilterSolver.cpp

    r123 r126  
    99
    1010#include "BonFilterSolver.hpp"
     11#include "BonFilterWarmStart.hpp"
     12
    1113#include <fstream>
    1214
     
    321323{
    322324  assert(tnlp == cached_->tnlp_);
    323   cached_->ifail = 0;//-1;
     325  cached_->ifail = -1;
    324326  //rescan bounds which may have changed
    325327  assert(cached_->bounds);
     
    584586{ return solverName_;}
    585587
     588bool
     589FilterSolver::setWarmStart(const CoinWarmStart * warm,
     590                           Ipopt::SmartPtr<TMINLP2TNLP> tnlp){
     591  const FilterWarmStart * warmF = dynamic_cast<const FilterWarmStart *> (warm);
     592  CoinCopyN(warmF->array(), warmF->size(), cached_->lws);
     593  for(int i = 0 ; i < 14 ; i ++)
     594    {
     595      cached_->istat[i] = warmF->istat()[i];
     596    }
     597  return 1;
     598}
     599
     600CoinWarmStart *
     601FilterSolver::getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const{
     602  return new FilterWarmStart(cached_->maxiWk, cached_->lws, cached_->istat);
     603}
     604
     605CoinWarmStart *
     606FilterSolver::getEmptyWarmStart() const {
     607  return new FilterWarmStart;}
     608
    586609
    587610
  • branches/devel/Bonmin/src/Interfaces/Filter/BonFilterSolver.hpp

    r123 r126  
    1313#include "BonTNLPSolver.hpp"
    1414
    15 #include "CoinWarmStartBasis.hpp"
    1615namespace Bonmin{
    1716class FilterSolver : public TNLPSolver{
     
    3736};
    3837
     38  /** Fortran type for integer used in filter. */
     39    typedef ipfint fint;
     40  /** Fortran type for double.used in filter */
     41    typedef double real;
     42
     43
    3944  virtual UnsolvedError * newUnsolvedError(int num,
    4045                                           Ipopt::SmartPtr<TMINLP2TNLP> problem,
     
    4348
    4449
    45 
    46     typedef ipfint fint;
    47     typedef double real;
    48 
    4950  ///Default constructor
    5051  FilterSolver();
     
    7172  /// Set the warm start in the solver
    7273  virtual bool setWarmStart(const CoinWarmStart * warm,
    73                             Ipopt::SmartPtr<TMINLP2TNLP> tnlp){
    74    return 1;}
     74                            Ipopt::SmartPtr<TMINLP2TNLP> tnlp);
    7575
    7676  /// Get the warm start form the solver
    77   virtual CoinWarmStart * getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const{
    78    return new CoinWarmStartBasis;}
    79 
    80   virtual CoinWarmStart * getEmptyWarmStart() const {
    81   return new CoinWarmStartBasis;}
     77  virtual CoinWarmStart * getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const;
     78
     79  virtual CoinWarmStart * getEmptyWarmStart() const;
    8280
    8381
  • branches/devel/Bonmin/src/Interfaces/Filter/BonFilterWarmStart.cpp

    r125 r126  
    88// Date : 11/21/2006
    99
     10#include "BonFilterWarmStart.hpp"
     11
     12
     13namespace Bonmin{
     14
     15  CoinWarmStartDiff *
     16  FilterWarmStart::generateDiff(const CoinWarmStart * const oldOne) const {
     17
     18    const FilterWarmStart * old = dynamic_cast<const FilterWarmStart  *>(oldOne);
     19
     20    if(size_ != old->size_){
     21      throw CoinError("Can not make difference for warm starts of differnet sizes",
     22                      "generateDiffs",
     23                      "FilterWarmStart");
     24    }
     25    FilterWarmStartDiff * diff = new FilterWarmStartDiff(size_);
     26
     27    fint lastDiff=-1;
     28    for(fint i = 0 ; i < size_ ; i++){
     29      if(warmArray_[i] != old->warmArray_[i]){
     30        diff->differences.push_back(FilterWarmStartDiff::OneDiff(i, warmArray_[i] - old->warmArray_[i]));
     31        lastDiff = i;
     32      }
     33    }
     34   
     35    std::cout<<"Index of last difference "<<lastDiff<<", size "<<size_<<std::endl;
     36    diff->differences.resize(diff->differences.size());
     37   
     38    for(int i = 0 ; i < 14 ; i++){
     39      diff->istat_[i] = istat_[i];
     40    }
     41    return diff;
     42     
     43  }
     44
     45
     46  void
     47  FilterWarmStart::applyDiff(const CoinWarmStartDiff * diff){
     48   
     49    const FilterWarmStartDiff * diffF = dynamic_cast<const FilterWarmStartDiff  *>(diff);
     50    assert(diffF != NULL);
     51    fint end = diffF->differences.size();
     52    for(fint i = 0 ; i < end ; i++)
     53      {
     54        warmArray_[diffF->differences[i].first] += diffF->differences[i].second;
     55      }
     56
     57    for(int i = 0 ; i < 14 ; i++)
     58      istat_[i] = diffF->istat_[i];
     59  }
     60
     61
     62FilterSolver::fint
     63FilterWarmStart::def_istat[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
     64
     65
     66} /* End namespace Bonmin */
  • branches/devel/Bonmin/src/Interfaces/Filter/BonFilterWarmStart.hpp

    r125 r126  
    1515
    1616#include "CoinWarmStartBasis.hpp"
     17#include "BonFilterSolver.hpp" /* for types */
    1718
     19#include <vector>
    1820namespace Bonmin{
    1921
    20   class BonFilterWarmStart : public CoinWarmStartBasis
     22  /** Warm start for filter interface.
     23  Warm start for filter constists of a (possibly huge) array of integers.
     24  \bug Inheritance from CoinWarmStartBasis is only for compatibility with Cbc
     25  */
     26  class FilterWarmStart : public CoinWarmStartBasis
    2127  {
     28    typedef FilterSolver::fint fint;
     29    typedef FilterSolver::real real;
     30   
     31  public:
     32    /** Default values for istat */
     33    static fint def_istat[14];
     34    /** Constructor */
     35    FilterWarmStart(const fint size = 0, const fint * warmArray = NULL, const fint istat[14] = def_istat):
     36      CoinWarmStartBasis(),
     37      size_(size),
     38      warmArray_(NULL){
     39      if (size_ > 0){
     40        warmArray_ = new fint[size];
     41        if(warmArray != NULL){
     42          CoinCopyN(warmArray, size, warmArray_);
     43        }
     44      }
     45      else if(warmArray != NULL) {
     46        throw CoinError("Array passed but size is 0","FilterWarmStart(const fint, const fint *)","FilterWarmStart");
     47        }
     48      for(int i = 0 ; i < 14 ; i ++)
     49        istat_[i] = istat[i];
     50    }
     51
     52    /** Copy constructor */
     53    FilterWarmStart(const FilterWarmStart & other):
     54      CoinWarmStartBasis(other),
     55      size_(other.size_),
     56      warmArray_(NULL){
     57      if (size_ > 0){
     58        warmArray_ = new fint[size_];
     59        if(other.warmArray_ != NULL){
     60          CoinCopyN(other.warmArray_, size_, warmArray_);
     61        }
     62      }
     63      else if(other.warmArray_ != NULL) {
     64        throw CoinError("Array passed but size is 0","FilterWarmStart(const fint, const fint *)","FilterWarmStart");
     65        }     
     66    }
     67
     68    /** virtual copy */
     69    virtual CoinWarmStart * clone() const
     70    { return new FilterWarmStart(*this);}
     71
     72    /**Set size of the array. */
     73    void setInfo(const fint size = 0, const fint * warmArray = NULL, const fint istat[14] = def_istat){
     74      if(size != size_){
     75        size_ = size;
     76        if(warmArray_) delete [] warmArray_;
     77        warmArray_ = NULL;
     78        if(size > 0)
     79          warmArray_ = new fint[size];
     80      }
     81      else if(size > 0){
     82        assert(warmArray_);
     83      }
     84      if(size <= 0 && warmArray)
     85        throw CoinError("Array passed but size is 0","setInfo(const fint, const fint *)","FilterWarmStart");     
     86      CoinCopyN(warmArray, size_, warmArray_);
     87     
     88      for(int i = 0 ; i < 14 ; i ++)
     89        istat_[i] = istat[i];
     90    }
     91   
     92    /** Destructor. */
     93    virtual ~FilterWarmStart(){
     94      delete [] warmArray_;
     95    }
     96
     97    /** Generate differences.*/
     98    virtual CoinWarmStartDiff * generateDiff(const CoinWarmStart * const other) const;
     99
     100    /** Apply differences. */
     101    virtual void applyDiff(const CoinWarmStartDiff * const cswDiff);
     102
     103
     104    /** Access to array */
     105    const fint * array() const{
     106      return warmArray_;
     107    }
     108
     109    /** Access to size. */
     110    fint size() const {
     111      return size_;}
     112
     113    const fint * istat()const {
     114      return istat_;}
     115  private:
     116    /** Size of the warm start object. */
     117    fint size_;
     118
     119    /** Warm start information */
     120    fint * warmArray_;
     121
     122    /** istat */
     123    fint istat_[14];
     124   
    22125  };
    23126
    24127
    25   class BonFilterWarmStartDiff : public CoinWarmStartBasisDiff
     128  class FilterWarmStartDiff : public CoinWarmStartBasisDiff
    26129  {
     130    typedef FilterSolver::fint fint;
     131    typedef FilterSolver::real real;
     132
     133  public:
     134    FilterWarmStartDiff(fint capacity):
     135      CoinWarmStartBasisDiff(),
     136      differences(0){
     137      differences.reserve(capacity);
     138    }
     139
     140    virtual CoinWarmStartDiff * clone() const{
     141      int size = differences.size();
     142      FilterWarmStartDiff * return_value = new FilterWarmStartDiff(size);
     143      return_value->differences = differences;
     144      for(int i = 0 ; i < 14 ; i++)
     145        {
     146          return_value->istat_[i] = istat_[i];
     147        }
     148      return return_value;
     149    }
     150    friend class FilterWarmStart;
     151  private:
     152    /** One difference is two integers (indice and difference). */
     153    typedef std::pair<fint, fint> OneDiff;
     154    /** Vector of all the differences.*/
     155    std::vector<OneDiff> differences;
     156
     157    /** istat */
     158    fint istat_[14];
     159
    27160  };
    28161
Note: See TracChangeset for help on using the changeset viewer.