CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
HallOfFame.hh
Go to the documentation of this file.
1 #ifndef CASM_HallOfFame
2 #define CASM_HallOfFame
3 
4 #include <set>
5 
6 #include "casm/misc/CASM_math.hh"
7 
8 namespace CASM {
9 
18  template<typename ObjectType, typename Metric, typename ObjectCompare = std::less<ObjectType> >
19  class HallOfFame {
20 
21  public:
22 
23  typedef std::pair<double, ObjectType> PairType;
24 
27  class Compare {
28 
29  public:
30 
31  Compare(ObjectCompare _obj_compare, double _score_tol) :
32  m_score_compare(_score_tol), m_obj_compare(_obj_compare) {}
33 
34  bool operator()(const PairType &A, const PairType &B) const {
35 
36  if(m_score_compare(A.first, B.first)) {
37  return true;
38  }
39  if(m_score_compare(B.first, A.first)) {
40  return false;
41  }
42  return m_obj_compare(A.second, B.second);
43  }
44 
45  private:
46 
48  ObjectCompare m_obj_compare;
49 
50  };
51 
52  typedef std::set<PairType, Compare> ContainerType;
53  typedef typename ContainerType::const_iterator const_iterator;
54  typedef typename ContainerType::const_reverse_iterator const_reverse_iterator;
55  typedef Index size_type;
56 
58  struct InsertResult {
59 
61 
62  InsertResult(const_iterator _pos, bool _success, double _score, bool _excluded, const_iterator _excluded_pos) :
63  pos(_pos), success(_success), score(_score), excluded(_excluded), excluded_pos(_excluded_pos) {}
64 
65  InsertResult(std::pair<const_iterator, bool> _res, double _score, bool _excluded, const_iterator _excluded_pos) :
66  pos(_res.first), success(_res.second), score(_score), excluded(_excluded), excluded_pos(_excluded_pos) {}
67 
68  const_iterator pos;
69  bool success;
70  double score;
71  bool excluded;
72  const_iterator excluded_pos;
73  };
74 
82  HallOfFame(Metric _metric, ObjectCompare _obj_compare = std::less<ObjectType>(), Index _max_size = 100, double _score_tol = TOL):
83  m_metric(_metric),
84  m_obj_compare(_obj_compare),
85  m_score_compare(_score_tol),
86  m_halloffame(Compare(m_obj_compare, _score_tol)),
87  m_check_exclude(false),
88  m_exclude(Compare(m_obj_compare, _score_tol)),
89  m_max_size(_max_size) {}
90 
92  void exclude(const ObjectType &obj) {
93  m_exclude.insert(PairType(m_metric(obj), obj));
94  m_check_exclude = true;
95  }
96 
98  template<typename ObjectIterator>
99  void exclude(ObjectIterator begin, ObjectIterator end) {
100  for(auto it = begin; it != end; ++it) {
101  exclude(*it);
102  }
103  }
104 
105  void clear_excluded() {
106  m_exclude.clear();
107  }
108 
109  Metric metric() const {
110  return m_metric;
111  }
112 
113  bool empty() const {
114  return m_halloffame.empty();
115  }
116 
117  size_type size() const {
118  return m_halloffame.size();
119  }
120 
121  size_type max_size() const {
122  return m_max_size;
123  }
124 
129  InsertResult insert(const ObjectType &obj) {
130 
131  auto excluded_pos = m_exclude.end();
132  if(m_max_size <= 0) {
133  return InsertResult(m_halloffame.end(), false, std::numeric_limits<double>::quiet_NaN(), false, excluded_pos);
134  }
135 
136  double score = m_metric(obj);
137 
138  // if score is not good enough for hall of fame, do not insert
139  if(m_halloffame.size() == m_max_size && m_score_compare(m_halloffame.rbegin()->first, score)) {
140  return InsertResult(m_halloffame.end(), false, score, false, excluded_pos);
141  }
142 
143  PairType test(score, obj);
144 
145  // insert
146  auto res = m_halloffame.insert(test);
147 
148  // if not successful because it already exists in the hall of fame
149  if(!res.second) {
150  return InsertResult(res, score, false, excluded_pos);
151  }
152 
153  // if successful, check if in exclude list
154  if(m_check_exclude) {
155  excluded_pos = m_exclude.find(test);
156  if(excluded_pos != m_exclude.end()) {
157  m_halloffame.erase(res.first);
158  return InsertResult(m_halloffame.end(), false, score, true, excluded_pos);
159  }
160  }
161 
162  // remove any extras
163  if(m_halloffame.size() > m_max_size) {
164  m_halloffame.erase(std::prev(m_halloffame.end()));
165  }
166 
167  return InsertResult(res, score, false, excluded_pos);
168  }
169 
170  void clear() {
171  m_halloffame.clear();
172  }
173 
174  const_iterator begin() const {
175  return m_halloffame.begin();
176  }
177 
178  const_iterator cbegin() const {
179  return m_halloffame.cbegin();
180  }
181 
182  const_iterator end() const {
183  return m_halloffame.end();
184  }
185 
186  const_iterator cend() const {
187  return m_halloffame.cend();
188  }
189 
190  const_reverse_iterator rbegin() const {
191  return m_halloffame.rbegin();
192  }
193 
194  const_reverse_iterator crbegin() const {
195  return m_halloffame.crbegin();
196  }
197 
198  const_reverse_iterator rend() const {
199  return m_halloffame.rend();
200  }
201 
202  const_reverse_iterator crend() const {
203  return m_halloffame.crend();
204  }
205 
206  std::pair<const_iterator, const_iterator> equal_range(double key) const {
207  return m_halloffame.equal_range(key);
208  }
209 
210  size_type count(double key) const {
211  return m_halloffame.count(key);
212  }
213 
214  const_iterator find(double key) const {
215  return m_halloffame.find(key);
216  }
217 
218  const_iterator lower_bound(double key) const {
219  return m_halloffame.lower_bound(key);
220  }
221 
222  const_iterator upper_bound(double key) const {
223  return m_halloffame.upper_bound(key);
224  }
225 
226 
227  private:
228 
229  Metric m_metric;
230  ObjectCompare m_obj_compare;
232  ContainerType m_halloffame;
234  ContainerType m_exclude;
236 
237  };
238 
239 }
240 
241 #endif
ContainerType::const_iterator const_iterator
Definition: HallOfFame.hh:53
const_reverse_iterator rend() const
Definition: HallOfFame.hh:198
void exclude(const ObjectType &obj)
Add an object that should not be included in the hall of fame.
Definition: HallOfFame.hh:92
FloatCompare m_score_compare
Definition: HallOfFame.hh:231
HallOfFame::const_iterator const_iterator
Definition: HallOfFame.hh:60
bool operator()(const PairType &A, const PairType &B) const
Definition: HallOfFame.hh:34
Definition: Common.hh:7
std::set< PairType, Compare > ContainerType
Definition: HallOfFame.hh:52
size_type count(double key) const
Definition: HallOfFame.hh:210
const_iterator find(double key) const
Definition: HallOfFame.hh:214
Results data structure for HallOfFame::insert.
Definition: HallOfFame.hh:58
ContainerType m_halloffame
Definition: HallOfFame.hh:232
A container for storing best scoring objects.
Definition: HallOfFame.hh:19
Main CASM namespace.
Definition: complete.cpp:8
const double TOL
ObjectCompare m_obj_compare
Definition: HallOfFame.hh:48
size_type size() const
Definition: HallOfFame.hh:117
std::pair< const_iterator, const_iterator > equal_range(double key) const
Definition: HallOfFame.hh:206
ObjectCompare m_obj_compare
Definition: HallOfFame.hh:230
const_iterator begin() const
Definition: HallOfFame.hh:174
InsertResult(std::pair< const_iterator, bool > _res, double _score, bool _excluded, const_iterator _excluded_pos)
Definition: HallOfFame.hh:65
HallOfFame(Metric _metric, ObjectCompare _obj_compare=std::less< ObjectType >(), Index _max_size=100, double _score_tol=TOL)
Constructor.
Definition: HallOfFame.hh:82
const_iterator cbegin() const
Definition: HallOfFame.hh:178
EigenIndex Index
For long integer indexing:
ContainerType::const_reverse_iterator const_reverse_iterator
Definition: HallOfFame.hh:54
Compare(ObjectCompare _obj_compare, double _score_tol)
Definition: HallOfFame.hh:31
const_reverse_iterator rbegin() const
Definition: HallOfFame.hh:190
size_type max_size() const
Definition: HallOfFame.hh:121
const_iterator cend() const
Definition: HallOfFame.hh:186
bool empty() const
Definition: HallOfFame.hh:113
ContainerType m_exclude
Definition: HallOfFame.hh:234
InsertResult insert(const ObjectType &obj)
Insert object in HallOfFame.
Definition: HallOfFame.hh:129
Metric metric() const
Definition: HallOfFame.hh:109
const_iterator lower_bound(double key) const
Definition: HallOfFame.hh:218
void clear_excluded()
Definition: HallOfFame.hh:105
const_reverse_iterator crbegin() const
Definition: HallOfFame.hh:194
const_iterator upper_bound(double key) const
Definition: HallOfFame.hh:222
const_iterator end() const
Definition: HallOfFame.hh:182
InsertResult(const_iterator _pos, bool _success, double _score, bool _excluded, const_iterator _excluded_pos)
Definition: HallOfFame.hh:62
void exclude(ObjectIterator begin, ObjectIterator end)
Add objects that should not be included in the hall of fame.
Definition: HallOfFame.hh:99
FloatCompare m_score_compare
Definition: HallOfFame.hh:47
std::pair< double, ObjectType > PairType
Definition: HallOfFame.hh:23
const_reverse_iterator crend() const
Definition: HallOfFame.hh:202