/usr/src/RPM/BUILD/CoinAlps-1.4.4/Alps/src/AlpsSolutionPool.h
Go to the documentation of this file.
00001 /*===========================================================================*
00002  * This file is part of the Abstract Library for Parallel Search (ALPS).     *
00003  *                                                                           *
00004  * ALPS is distributed under the Eclipse Public License as part of the       *
00005  * COIN-OR repository (http://www.coin-or.org).                              *
00006  *                                                                           *
00007  * Authors:                                                                  *
00008  *                                                                           *
00009  *          Yan Xu, Lehigh University                                        *
00010  *          Ted Ralphs, Lehigh University                                    *
00011  *                                                                           *
00012  * Conceptual Design:                                                        *
00013  *                                                                           *
00014  *          Yan Xu, Lehigh University                                        *
00015  *          Ted Ralphs, Lehigh University                                    *
00016  *          Laszlo Ladanyi, IBM T.J. Watson Research Center                  *
00017  *          Matthew Saltzman, Clemson University                             *
00018  *                                                                           * 
00019  *                                                                           *
00020  * Copyright (C) 2001-2013, Lehigh University, Yan Xu, and Ted Ralphs.       *
00021  *===========================================================================*/
00022 
00023 #ifndef AlpsSolutionPool_h_
00024 #define AlpsSolutionPool_h_
00025 
00026 #include "AlpsKnowledgePool.h"
00027 #include "AlpsSolution.h"
00028 
00029 //#############################################################################
00030 
00033 class AlpsSolutionPool : public AlpsKnowledgePool {
00034     // *FIXME* ? : we do want to allow solutions with the same priority, but do
00035     // *FIXME* ? : we want to allow identical solutions?
00036  private:
00037     AlpsSolutionPool(const AlpsSolutionPool&);
00038     AlpsSolutionPool& operator=(const AlpsSolutionPool&);
00039 
00040  private:
00041 //    std::multimap< double, CoinSharedPtr<AlpsSolution> > solutions_;
00042     std::multimap< double, AlpsSolution* > solutions_;
00043     int maxNumSolutions_;
00044 
00045 //    inline void addSolution(CoinSharedPtr<AlpsSolution> sol, 
00046 //                          double priority) {
00047 //      solutions_.insert(std::make_pair(priority, sol));
00048 //      if (maxNumSolutions_ > 0 && solutions_.size() > maxNumSolutions_) {
00049 //          std::multimap< double, CoinSharedPtr<AlpsSolution> >::
00050 //              iterator si = solutions_.end();
00051 //          --si;
00052 //          solutions_.erase(si);
00053 //      }
00054 //    }
00055 
00056  public:
00057     AlpsSolutionPool(int maxsols = 1) : maxNumSolutions_(maxsols) {}
00058     virtual ~AlpsSolutionPool() {
00059         if (! solutions_.empty()) {
00060             clean();
00061         }
00062     }
00063 
00065     //   int getNumSolutions() const { return solutions_.size(); }
00066     inline int getNumKnowledges() const { 
00067         return static_cast<int> (solutions_.size()); 
00068     }
00069 
00071     inline bool hasKnowledge() const 
00072         { return solutions_.empty() ? false : true; }
00073 
00076 //    inline std::pair<AlpsKnowledge*, double> getKnowledge() const {
00077 //      return std::make_pair(solutions_.begin()->second.get(),
00078 //                            solutions_.begin()->first);
00079 //    }
00080     inline std::pair<AlpsKnowledge*, double> getKnowledge() const {
00081         return std::make_pair(static_cast<AlpsKnowledge *>
00082                               (solutions_.begin()->second),
00083                               solutions_.begin()->first);
00084     }
00085 
00087     inline void popKnowledge() {
00088         throw CoinError("Can not call popKnowledge()",
00089                         "popKnowledge()", "AlpsSolutionPool");
00090     }
00091 
00094     //   void addSolution(const AlpsSolution* sol, double priority) {
00095     //   CoinSharedPtr<const AlpsSolution> ssol(sol);
00096     //   addSolution(ssol, priority);
00097     // }
00098 
00099 //  inline void addKnowledge(AlpsKnowledge* sol, double priority=0) {
00100 //      CoinSharedPtr<AlpsSolution> 
00101 //          ssol( dynamic_cast<AlpsSolution*>(sol) );
00102 //      addSolution(ssol, priority);
00103 //  }
00104     inline void addKnowledge(AlpsKnowledge* sol, double priority) {
00105         std::pair<const double, AlpsSolution*> ps(priority, dynamic_cast<AlpsSolution*>(sol));
00106         solutions_.insert(ps);
00107         //solutions_.insert(std::make_pair(priority, 
00108 //                                       (AlpsSolution*)sol));
00109         if ((maxNumSolutions_ > 0) && 
00110             (static_cast<int>(solutions_.size()) > maxNumSolutions_)) {
00111             std::multimap< double, AlpsSolution* >::iterator si = 
00112                 solutions_.end();
00113             --si;
00114             AlpsSolution* sol = si->second;
00115             solutions_.erase(si);
00116             delete sol;
00117         }
00118     }
00119 
00121     inline int getMaxNumKnowledges() const { return maxNumSolutions_; }
00122 
00124 //  inline void setMaxNumKnowledges(int maxsols) {
00125 //      if (maxsols > 0) {
00126 //          if (solutions_.size() > maxsols) {
00127 //              std::multimap< double, CoinSharedPtr<AlpsSolution> >::
00128 //                  iterator si = solutions_.begin();
00129 //              for (int i = 0; i < maxsols; ++i)
00130 //                  ++si;
00131 //              solutions_.erase(si, solutions_.end());
00132 //          }
00133 //      }
00134 //      maxNumSolutions_ = maxsols;
00135 //  }
00136     inline void setMaxNumKnowledges(int maxsols) {
00137         if (maxsols > 0) {
00138             if (static_cast<int>(solutions_.size()) > maxsols) {
00139                 std::multimap<double, AlpsSolution*>::
00140                     iterator si = solutions_.begin();
00141                 for (int i = 0; i < maxsols; ++i)
00142                     ++si;
00143                 solutions_.erase(si, solutions_.end());
00144             }
00145         }
00146         maxNumSolutions_ = maxsols;
00147     }
00148 
00151 //    inline std::pair<AlpsKnowledge*, double> getBestKnowledge() const {
00152 //      return std::make_pair(solutions_.begin()->second.get(),
00153 //                            solutions_.begin()->first);
00154 //    }
00155     inline std::pair<AlpsKnowledge*, double> getBestKnowledge() const {
00156         return std::make_pair(static_cast<AlpsKnowledge *>
00157                               (solutions_.begin()->second),
00158                               solutions_.begin()->first);
00159     }
00160 
00163 //  inline void getAllKnowledges
00164 //      (std::vector<std::pair<AlpsKnowledge*, double> >& sols) const {
00165 //      sols.reserve(sols.size() + solutions_.size());
00166 //      std::multimap< double, CoinSharedPtr<AlpsSolution> >::
00167 //          const_iterator si;
00168 //      for (si = solutions_.begin(); si != solutions_.end(); ++si) {
00169 //          sols.push_back(std::make_pair(si->second.get(), si->first));
00170 //      }
00171 //  }
00172     inline void getAllKnowledges
00173         (std::vector<std::pair<AlpsKnowledge*, double> >& sols) const {
00174         sols.reserve(sols.size() + solutions_.size());
00175         std::multimap<double, AlpsSolution*>::const_iterator si;
00176         for (si = solutions_.begin(); si != solutions_.end(); ++si) {
00177             sols.push_back(std::make_pair(static_cast<AlpsKnowledge *>
00178                                           (si->second), si->first));
00179         }
00180     }
00181     
00183     void clean() {
00184         while (!solutions_.empty()) {
00185             std::multimap< double, AlpsSolution* >::iterator si = 
00186                 solutions_.end();
00187             --si;
00188             AlpsSolution* sol = si->second;
00189             solutions_.erase(si);
00190             delete sol;
00191             sol = NULL;
00192         }
00193         
00194         //for_each(solutions_.begin(), solutions_.end(), DeletePtrObject());
00195     }
00196 };
00197 
00198 
00199 
00200 #define AlpsSolutionInterface(ref)                                      \
00201 int getNumSolutions() const {                                           \
00202    (ref).getNumSolutions();                                             \
00203 }                                                                       \
00204 int getMaxNumSolutions() const {                                        \
00205    return (ref).getMaxNumSolutions();                                   \
00206 }                                                                       \
00207 void setMaxNumSolutions(int num) {                                      \
00208    (ref).setMaxNumSolutions(num);                                       \
00209 }                                                                       \
00210 bool hasSolution() const {                                              \
00211    return (ref).hasSolution();                                          \
00212 }                                                                       \
00213 std::pair<const AlpsSolution*, double> getBestSolution() const {        \
00214    return (ref).getBestSolution();                                      \
00215 }                                                                       \
00216 void getAllSolutions                                                    \
00217    (std::vector<std::pair<const AlpsSolution*, double> >& sols) {       \
00218    return (ref).getAllSolutions(sols);                                  \
00219 }                                                                       \
00220 void addSolution(const AlpsSolution* sol, double priority) {            \
00221    (ref).addSolution(sol, priority);                                    \
00222 }
00223 
00224 #endif