Go to the documentation of this file.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 {
00067 return static_cast<int> (solutions_.size());
00068 }
00069
00071 inline bool hasKnowledge() const
00072 { return solutions_.empty() ? false : true; }
00073
00076
00077
00078
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
00095
00096
00097
00098
00099
00100
00101
00102
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
00108
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
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
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
00152
00153
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
00164
00165
00166
00167
00168
00169
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
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