00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00035
00036 private:
00037 AlpsSolutionPool(const AlpsSolutionPool&);
00038 AlpsSolutionPool& operator=(const AlpsSolutionPool&);
00039
00040 private:
00041
00042 std::multimap< double, AlpsSolution* > solutions_;
00043 int maxNumSolutions_;
00044
00045
00046
00047
00048
00049
00050
00051
00052
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
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
00075
00076
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
00093
00094
00095
00096
00097
00098
00099
00100
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
00106
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
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
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
00150
00151
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
00162
00163
00164
00165
00166
00167
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
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