/home/coin/SVN-release/CoinAll-1.1.0/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 Common 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-2007, 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 { return static_cast<int> (solutions_.size()); }
00067 
00069     inline bool hasKnowledge() const 
00070         { return solutions_.empty() ? false : true; }
00071 
00074 //    inline std::pair<AlpsKnowledge*, double> getKnowledge() const {
00075 //      return std::make_pair(solutions_.begin()->second.get(),
00076 //                            solutions_.begin()->first);
00077 //    }
00078     inline std::pair<AlpsKnowledge*, double> getKnowledge() const {
00079         return std::make_pair(static_cast<AlpsKnowledge *>
00080                               (solutions_.begin()->second),
00081                               solutions_.begin()->first);
00082     }
00083 
00085     inline void popKnowledge() {
00086         throw CoinError("Can not call popKnowledge()",
00087                         "popKnowledge()", "AlpsSolutionPool");
00088     }
00089 
00092     //   void addSolution(const AlpsSolution* sol, double priority) {
00093     //   CoinSharedPtr<const AlpsSolution> ssol(sol);
00094     //   addSolution(ssol, priority);
00095     // }
00096 
00097 //  inline void addKnowledge(AlpsKnowledge* sol, double priority=0) {
00098 //      CoinSharedPtr<AlpsSolution> 
00099 //          ssol( dynamic_cast<AlpsSolution*>(sol) );
00100 //      addSolution(ssol, priority);
00101 //  }
00102     inline void addKnowledge(AlpsKnowledge* sol, double priority) {
00103         std::pair<const double, AlpsSolution*> ps(priority, dynamic_cast<AlpsSolution*>(sol));
00104         solutions_.insert(ps);
00105         //solutions_.insert(std::make_pair(priority, 
00106 //                                       (AlpsSolution*)sol));
00107         if ((maxNumSolutions_ > 0) && 
00108             (static_cast<int>(solutions_.size()) > maxNumSolutions_)) {
00109             std::multimap< double, AlpsSolution* >::iterator si = 
00110                 solutions_.end();
00111             --si;
00112             AlpsSolution* sol = si->second;
00113             solutions_.erase(si);
00114             delete sol;
00115         }
00116     }
00117 
00119     inline int getMaxNumKnowledges() const { return maxNumSolutions_; }
00120 
00122 //  inline void setMaxNumKnowledges(int maxsols) {
00123 //      if (maxsols > 0) {
00124 //          if (solutions_.size() > maxsols) {
00125 //              std::multimap< double, CoinSharedPtr<AlpsSolution> >::
00126 //                  iterator si = solutions_.begin();
00127 //              for (int i = 0; i < maxsols; ++i)
00128 //                  ++si;
00129 //              solutions_.erase(si, solutions_.end());
00130 //          }
00131 //      }
00132 //      maxNumSolutions_ = maxsols;
00133 //  }
00134     inline void setMaxNumKnowledges(int maxsols) {
00135         if (maxsols > 0) {
00136             if (static_cast<int>(solutions_.size()) > maxsols) {
00137                 std::multimap<double, AlpsSolution*>::
00138                     iterator si = solutions_.begin();
00139                 for (int i = 0; i < maxsols; ++i)
00140                     ++si;
00141                 solutions_.erase(si, solutions_.end());
00142             }
00143         }
00144         maxNumSolutions_ = maxsols;
00145     }
00146 
00149 //    inline std::pair<AlpsKnowledge*, double> getBestKnowledge() const {
00150 //      return std::make_pair(solutions_.begin()->second.get(),
00151 //                            solutions_.begin()->first);
00152 //    }
00153     inline std::pair<AlpsKnowledge*, double> getBestKnowledge() const {
00154         return std::make_pair(static_cast<AlpsKnowledge *>
00155                               (solutions_.begin()->second),
00156                               solutions_.begin()->first);
00157     }
00158 
00161 //  inline void getAllKnowledges
00162 //      (std::vector<std::pair<AlpsKnowledge*, double> >& sols) const {
00163 //      sols.reserve(sols.size() + solutions_.size());
00164 //      std::multimap< double, CoinSharedPtr<AlpsSolution> >::
00165 //          const_iterator si;
00166 //      for (si = solutions_.begin(); si != solutions_.end(); ++si) {
00167 //          sols.push_back(std::make_pair(si->second.get(), si->first));
00168 //      }
00169 //  }
00170     inline void getAllKnowledges
00171         (std::vector<std::pair<AlpsKnowledge*, double> >& sols) const {
00172         sols.reserve(sols.size() + solutions_.size());
00173         std::multimap<double, AlpsSolution*>::const_iterator si;
00174         for (si = solutions_.begin(); si != solutions_.end(); ++si) {
00175             sols.push_back(std::make_pair(static_cast<AlpsKnowledge *>
00176                                           (si->second), si->first));
00177         }
00178     }
00179     
00181     void clean() {
00182         while (!solutions_.empty()) {
00183             std::multimap< double, AlpsSolution* >::iterator si = 
00184                 solutions_.end();
00185             --si;
00186             AlpsSolution* sol = si->second;
00187             solutions_.erase(si);
00188             delete sol;
00189             sol = NULL;
00190         }
00191         
00192         //for_each(solutions_.begin(), solutions_.end(), DeletePtrObject());
00193     }
00194 };
00195 
00196 
00197 
00198 #define AlpsSolutionInterface(ref)                                      \
00199 int getNumSolutions() const {                                           \
00200    (ref).getNumSolutions();                                             \
00201 }                                                                       \
00202 int getMaxNumSolutions() const {                                        \
00203    return (ref).getMaxNumSolutions();                                   \
00204 }                                                                       \
00205 void setMaxNumSolutions(int num) {                                      \
00206    (ref).setMaxNumSolutions(num);                                       \
00207 }                                                                       \
00208 bool hasSolution() const {                                              \
00209    return (ref).hasSolution();                                          \
00210 }                                                                       \
00211 std::pair<const AlpsSolution*, double> getBestSolution() const {        \
00212    return (ref).getBestSolution();                                      \
00213 }                                                                       \
00214 void getAllSolutions                                                    \
00215    (std::vector<std::pair<const AlpsSolution*, double> >& sols) {       \
00216    return (ref).getAllSolutions(sols);                                  \
00217 }                                                                       \
00218 void addSolution(const AlpsSolution* sol, double priority) {            \
00219    (ref).addSolution(sol, priority);                                    \
00220 }
00221 
00222 #endif

Generated on Sun Nov 14 14:06:28 2010 for Coin-All by  doxygen 1.4.7