- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

AlpsSolutionPool.h
Go to the documentation of this file.
1 /*===========================================================================*
2 * This file is part of the Abstract Library for Parallel Search (ALPS). *
3 * *
4 * ALPS is distributed under the Eclipse Public License as part of the *
5 * COIN-OR repository (http://www.coin-or.org). *
6 * *
7 * Authors: *
8 * *
9 * Yan Xu, Lehigh University *
10 * Ted Ralphs, Lehigh University *
11 * *
12 * Conceptual Design: *
13 * *
14 * Yan Xu, Lehigh University *
15 * Ted Ralphs, Lehigh University *
16 * Laszlo Ladanyi, IBM T.J. Watson Research Center *
17 * Matthew Saltzman, Clemson University *
18 * *
19 * *
20 * Copyright (C) 2001-2013, Lehigh University, Yan Xu, and Ted Ralphs. *
21 *===========================================================================*/
22
23 #ifndef AlpsSolutionPool_h_
24 #define AlpsSolutionPool_h_
25
26 #include "AlpsKnowledgePool.h"
27 #include "AlpsSolution.h"
28
29 //#############################################################################
30
33 class AlpsSolutionPool : public AlpsKnowledgePool {
34 // *FIXME* ? : we do want to allow solutions with the same priority, but do
35 // *FIXME* ? : we want to allow identical solutions?
36 private:
37 AlpsSolutionPool(const AlpsSolutionPool&);
38 AlpsSolutionPool& operator=(const AlpsSolutionPool&);
39
40 private:
41 // std::multimap< double, CoinSharedPtr<AlpsSolution> > solutions_;
42 std::multimap< double, AlpsSolution* > solutions_;
43 int maxNumSolutions_;
44
45 // inline void addSolution(CoinSharedPtr<AlpsSolution> sol,
46 // double priority) {
47 // solutions_.insert(std::make_pair(priority, sol));
48 // if (maxNumSolutions_ > 0 && solutions_.size() > maxNumSolutions_) {
49 // std::multimap< double, CoinSharedPtr<AlpsSolution> >::
50 // iterator si = solutions_.end();
51 // --si;
52 // solutions_.erase(si);
53 // }
54 // }
55
56 public:
57 AlpsSolutionPool(int maxsols = 1) : maxNumSolutions_(maxsols) {}
58 virtual ~AlpsSolutionPool() {
59 if (! solutions_.empty()) {
60 clean();
61 }
62 }
63
65 // int getNumSolutions() const { return solutions_.size(); }
66 inline int getNumKnowledges() const {
67 return static_cast<int> (solutions_.size());
68 }
69
71 inline bool hasKnowledge() const
72 { return solutions_.empty() ? false : true; }
73
76 // inline std::pair<AlpsKnowledge*, double> getKnowledge() const {
77 // return std::make_pair(solutions_.begin()->second.get(),
78 // solutions_.begin()->first);
79 // }
80 inline std::pair<AlpsKnowledge*, double> getKnowledge() const {
81 return std::make_pair(static_cast<AlpsKnowledge *>
82 (solutions_.begin()->second),
83 solutions_.begin()->first);
84 }
85
87 inline void popKnowledge() {
88 throw CoinError("Can not call popKnowledge()",
89 "popKnowledge()", "AlpsSolutionPool");
90 }
91
94 // void addSolution(const AlpsSolution* sol, double priority) {
95 // CoinSharedPtr<const AlpsSolution> ssol(sol);
96 // addSolution(ssol, priority);
97 // }
98
99 // inline void addKnowledge(AlpsKnowledge* sol, double priority=0) {
100 // CoinSharedPtr<AlpsSolution>
101 // ssol( dynamic_cast<AlpsSolution*>(sol) );
102 // addSolution(ssol, priority);
103 // }
104 inline void addKnowledge(AlpsKnowledge* sol, double priority) {
105 std::pair<const double, AlpsSolution*> ps(priority, dynamic_cast<AlpsSolution*>(sol));
106 solutions_.insert(ps);
107 //solutions_.insert(std::make_pair(priority,
108 // (AlpsSolution*)sol));
109 if ((maxNumSolutions_ > 0) &&
110 (static_cast<int>(solutions_.size()) > maxNumSolutions_)) {
111 std::multimap< double, AlpsSolution* >::iterator si =
112 solutions_.end();
113 --si;
114 AlpsSolution* sol = si->second;
115 solutions_.erase(si);
116 delete sol;
117 }
118 }
119
121 inline int getMaxNumKnowledges() const { return maxNumSolutions_; }
122
124 // inline void setMaxNumKnowledges(int maxsols) {
125 // if (maxsols > 0) {
126 // if (solutions_.size() > maxsols) {
127 // std::multimap< double, CoinSharedPtr<AlpsSolution> >::
128 // iterator si = solutions_.begin();
129 // for (int i = 0; i < maxsols; ++i)
130 // ++si;
131 // solutions_.erase(si, solutions_.end());
132 // }
133 // }
134 // maxNumSolutions_ = maxsols;
135 // }
136 inline void setMaxNumKnowledges(int maxsols) {
137 if (maxsols > 0) {
138 if (static_cast<int>(solutions_.size()) > maxsols) {
139 std::multimap<double, AlpsSolution*>::
140 iterator si = solutions_.begin();
141 for (int i = 0; i < maxsols; ++i)
142 ++si;
143 solutions_.erase(si, solutions_.end());
144 }
145 }
146 maxNumSolutions_ = maxsols;
147 }
148
151 // inline std::pair<AlpsKnowledge*, double> getBestKnowledge() const {
152 // return std::make_pair(solutions_.begin()->second.get(),
153 // solutions_.begin()->first);
154 // }
155 inline std::pair<AlpsKnowledge*, double> getBestKnowledge() const {
156 return std::make_pair(static_cast<AlpsKnowledge *>
157 (solutions_.begin()->second),
158 solutions_.begin()->first);
159 }
160
163 // inline void getAllKnowledges
164 // (std::vector<std::pair<AlpsKnowledge*, double> >& sols) const {
165 // sols.reserve(sols.size() + solutions_.size());
166 // std::multimap< double, CoinSharedPtr<AlpsSolution> >::
167 // const_iterator si;
168 // for (si = solutions_.begin(); si != solutions_.end(); ++si) {
169 // sols.push_back(std::make_pair(si->second.get(), si->first));
170 // }
171 // }
172 inline void getAllKnowledges
173 (std::vector<std::pair<AlpsKnowledge*, double> >& sols) const {
174 sols.reserve(sols.size() + solutions_.size());
175 std::multimap<double, AlpsSolution*>::const_iterator si;
176 for (si = solutions_.begin(); si != solutions_.end(); ++si) {
177 sols.push_back(std::make_pair(static_cast<AlpsKnowledge *>
178 (si->second), si->first));
179 }
180 }
181
183 void clean() {
184 while (!solutions_.empty()) {
185 std::multimap< double, AlpsSolution* >::iterator si =
186 solutions_.end();
187 --si;
188 AlpsSolution* sol = si->second;
189 solutions_.erase(si);
190 delete sol;
191 sol = NULL;
192 }
193
194 //for_each(solutions_.begin(), solutions_.end(), DeletePtrObject());
195 }
196 };
197
198
199
200 #define AlpsSolutionInterface(ref) \
201 int getNumSolutions() const { \
202 (ref).getNumSolutions(); \
203 } \
204 int getMaxNumSolutions() const { \
205 return (ref).getMaxNumSolutions(); \
206 } \
207 void setMaxNumSolutions(int num) { \
208 (ref).setMaxNumSolutions(num); \
209 } \
210 bool hasSolution() const { \
211 return (ref).hasSolution(); \
212 } \
213 std::pair<const AlpsSolution*, double> getBestSolution() const { \
214 return (ref).getBestSolution(); \
215 } \
216 void getAllSolutions \
217 (std::vector<std::pair<const AlpsSolution*, double> >& sols) { \
218 return (ref).getAllSolutions(sols); \
219 } \
220 void addSolution(const AlpsSolution* sol, double priority) { \
221 (ref).addSolution(sol, priority); \
222 }
223
224 #endif
AlpsSolutionPool::setMaxNumKnowledges
void setMaxNumKnowledges(int maxsols)
reset the maximum number of solutions
Definition: AlpsSolutionPool.h:136
AlpsSolutionPool::getNumKnowledges
int getNumKnowledges() const
query the current number of solutions
Definition: AlpsSolutionPool.h:66
AlpsSolutionPool::operator=
AlpsSolutionPool & operator=(const AlpsSolutionPool &)
AlpsSolutionPool::addKnowledge
void addKnowledge(AlpsKnowledge *sol, double priority)
Append the solution to the end of the vector of solutions.
Definition: AlpsSolutionPool.h:104
AlpsSolutionPool::~AlpsSolutionPool
virtual ~AlpsSolutionPool()
Definition: AlpsSolutionPool.h:58
AlpsSolutionPool::getBestKnowledge
std::pair< AlpsKnowledge *, double > getBestKnowledge() const
Return the best solution.
Definition: AlpsSolutionPool.h:155
AlpsKnowledgePool.h
AlpsSolutionPool::solutions_
std::multimap< double, AlpsSolution * > solutions_
Definition: AlpsSolutionPool.h:42
AlpsSolutionPool::getMaxNumKnowledges
int getMaxNumKnowledges() const
query the maximum number of solutions
Definition: AlpsSolutionPool.h:121
AlpsSolutionPool
In the solution pool we assume that the lower the priority value the more desirable the solution is...
Definition: AlpsSolutionPool.h:33
AlpsSolutionPool::popKnowledge
void popKnowledge()
Remove a solution from the pool.
Definition: AlpsSolutionPool.h:87
AlpsKnowledgePool
Definition: AlpsKnowledgePool.h:36
AlpsKnowledge
The abstract base class of any user-defined class that Alps has to know about in order to encode/deco...
Definition: AlpsKnowledge.h:51
AlpsSolutionPool::maxNumSolutions_
int maxNumSolutions_
Definition: AlpsSolutionPool.h:43
CoinError
Error Class thrown by an exception.
Definition: CoinError.hpp:42
AlpsSolutionPool::getKnowledge
std::pair< AlpsKnowledge *, double > getKnowledge() const
Get a solution from solution pool, doesn&#39;t remove it from the pool.
Definition: AlpsSolutionPool.h:80
AlpsSolutionPool::hasKnowledge
bool hasKnowledge() const
return true if there are any solution stored in the solution pool
Definition: AlpsSolutionPool.h:71
AlpsSolutionPool::clean
void clean()
Delete all the solutions in pool.
Definition: AlpsSolutionPool.h:183
AlpsSolutionPool::getAllKnowledges
void getAllKnowledges(std::vector< std::pair< AlpsKnowledge *, double > > &sols) const
Return all the solutions of the solution pool in the provided argument vector.
Definition: AlpsSolutionPool.h:173
AlpsSolution
Definition: AlpsSolution.h:35
AlpsSolutionPool::AlpsSolutionPool
AlpsSolutionPool(int maxsols=1)
Definition: AlpsSolutionPool.h:57
AlpsSolution.h
AlpsSolutionPool::AlpsSolutionPool
AlpsSolutionPool(const AlpsSolutionPool &)