CASM  1.1.0
A Clusters Approach to Statistical Mechanics
HallOfFame.hh
Go to the documentation of this file.
1 #ifndef CASM_HallOfFame
2 #define CASM_HallOfFame
3 
4 #include <limits>
5 #include <set>
6 
7 #include "casm/misc/CASM_math.hh"
8 
9 namespace CASM {
10 
19 template <typename ObjectType, typename Metric,
20  typename ObjectCompare = std::less<ObjectType> >
21 class HallOfFame {
22  public:
23  typedef std::pair<double, ObjectType> PairType;
24 
27  class Compare {
28  public:
29  Compare(ObjectCompare _obj_compare, double _score_tol)
30  : m_score_compare(_score_tol), m_obj_compare(_obj_compare) {}
31 
32  bool operator()(const PairType &A, const PairType &B) const {
33  if (m_score_compare(A.first, B.first)) {
34  return true;
35  }
36  if (m_score_compare(B.first, A.first)) {
37  return false;
38  }
39  return m_obj_compare(A.second, B.second);
40  }
41 
42  private:
44  ObjectCompare m_obj_compare;
45  };
46 
47  typedef std::set<PairType, Compare> ContainerType;
48  typedef typename ContainerType::const_iterator const_iterator;
49  typedef typename ContainerType::const_reverse_iterator const_reverse_iterator;
50  typedef Index size_type;
51 
53  struct InsertResult {
55 
56  InsertResult(const_iterator _pos, bool _success, double _score,
57  bool _excluded, const_iterator _excluded_pos)
58  : pos(_pos),
59  success(_success),
60  score(_score),
61  excluded(_excluded),
62  excluded_pos(_excluded_pos) {}
63 
64  InsertResult(std::pair<const_iterator, bool> _res, double _score,
65  bool _excluded, const_iterator _excluded_pos)
66  : pos(_res.first),
67  success(_res.second),
68  score(_score),
69  excluded(_excluded),
70  excluded_pos(_excluded_pos) {}
71 
73  bool success;
74  double score;
75  bool excluded;
77  };
78 
87  HallOfFame(Metric _metric,
88  ObjectCompare _obj_compare = std::less<ObjectType>(),
89  Index _max_size = 100, double _score_tol = TOL)
90  : m_metric(_metric),
91  m_obj_compare(_obj_compare),
92  m_score_compare(_score_tol),
93  m_halloffame(Compare(m_obj_compare, _score_tol)),
94  m_check_exclude(false),
95  m_exclude(Compare(m_obj_compare, _score_tol)),
96  m_max_size(_max_size) {}
97 
99  void exclude(const ObjectType &obj) {
100  m_exclude.insert(PairType(m_metric(obj), obj));
101  m_check_exclude = true;
102  }
103 
105  template <typename ObjectIterator>
106  void exclude(ObjectIterator begin, ObjectIterator end) {
107  for (auto it = begin; it != end; ++it) {
108  exclude(*it);
109  }
110  }
111 
112  void clear_excluded() { m_exclude.clear(); }
113 
114  Metric metric() const { return m_metric; }
115 
116  bool empty() const { return m_halloffame.empty(); }
117 
118  size_type size() const { return m_halloffame.size(); }
119 
120  size_type max_size() const { return m_max_size; }
121 
126  InsertResult insert(const ObjectType &obj) {
127  auto excluded_pos = m_exclude.end();
128  if (m_max_size <= 0) {
129  return InsertResult(m_halloffame.end(), false,
130  std::numeric_limits<double>::quiet_NaN(), false,
131  excluded_pos);
132  }
133 
134  double score = m_metric(obj);
135 
136  // if score is not good enough for hall of fame, do not insert
137  if (m_halloffame.size() == m_max_size &&
138  m_score_compare(m_halloffame.rbegin()->first, score)) {
139  return InsertResult(m_halloffame.end(), false, score, false,
140  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,
159  excluded_pos);
160  }
161  }
162 
163  // remove any extras
164  if (m_halloffame.size() > m_max_size) {
165  m_halloffame.erase(std::prev(m_halloffame.end()));
166  }
167 
168  return InsertResult(res, score, false, excluded_pos);
169  }
170 
171  void clear() { m_halloffame.clear(); }
172 
173  const_iterator begin() const { return m_halloffame.begin(); }
174 
175  const_iterator cbegin() const { return m_halloffame.cbegin(); }
176 
177  const_iterator end() const { return m_halloffame.end(); }
178 
179  const_iterator cend() const { return m_halloffame.cend(); }
180 
181  const_reverse_iterator rbegin() const { return m_halloffame.rbegin(); }
182 
183  const_reverse_iterator crbegin() const { return m_halloffame.crbegin(); }
184 
185  const_reverse_iterator rend() const { return m_halloffame.rend(); }
186 
187  const_reverse_iterator crend() const { return m_halloffame.crend(); }
188 
189  std::pair<const_iterator, const_iterator> equal_range(double key) const {
190  return m_halloffame.equal_range(key);
191  }
192 
193  size_type count(double key) const { return m_halloffame.count(key); }
194 
195  const_iterator find(double key) const { return m_halloffame.find(key); }
196 
197  const_iterator lower_bound(double key) const {
198  return m_halloffame.lower_bound(key);
199  }
200 
201  const_iterator upper_bound(double key) const {
202  return m_halloffame.upper_bound(key);
203  }
204 
205  private:
206  Metric m_metric;
207  ObjectCompare m_obj_compare;
213 };
214 
215 } // namespace CASM
216 
217 #endif
Compare(ObjectCompare _obj_compare, double _score_tol)
Definition: HallOfFame.hh:29
ObjectCompare m_obj_compare
Definition: HallOfFame.hh:44
FloatCompare m_score_compare
Definition: HallOfFame.hh:43
bool operator()(const PairType &A, const PairType &B) const
Definition: HallOfFame.hh:32
A container for storing best scoring objects.
Definition: HallOfFame.hh:21
const_iterator cend() const
Definition: HallOfFame.hh:179
FloatCompare m_score_compare
Definition: HallOfFame.hh:208
bool empty() const
Definition: HallOfFame.hh:116
std::pair< double, ObjectType > PairType
Definition: HallOfFame.hh:23
std::pair< const_iterator, const_iterator > equal_range(double key) const
Definition: HallOfFame.hh:189
void clear_excluded()
Definition: HallOfFame.hh:112
void exclude(const ObjectType &obj)
Add an object that should not be included in the hall of fame.
Definition: HallOfFame.hh:99
InsertResult insert(const ObjectType &obj)
Insert object in HallOfFame.
Definition: HallOfFame.hh:126
const_iterator find(double key) const
Definition: HallOfFame.hh:195
ObjectCompare m_obj_compare
Definition: HallOfFame.hh:207
size_type max_size() const
Definition: HallOfFame.hh:120
const_reverse_iterator rbegin() const
Definition: HallOfFame.hh:181
Metric metric() const
Definition: HallOfFame.hh:114
void exclude(ObjectIterator begin, ObjectIterator end)
Add objects that should not be included in the hall of fame.
Definition: HallOfFame.hh:106
HallOfFame(Metric _metric, ObjectCompare _obj_compare=std::less< ObjectType >(), Index _max_size=100, double _score_tol=TOL)
Constructor.
Definition: HallOfFame.hh:87
const_reverse_iterator rend() const
Definition: HallOfFame.hh:185
ContainerType m_exclude
Definition: HallOfFame.hh:211
const_iterator begin() const
Definition: HallOfFame.hh:173
size_type count(double key) const
Definition: HallOfFame.hh:193
ContainerType m_halloffame
Definition: HallOfFame.hh:209
const_reverse_iterator crbegin() const
Definition: HallOfFame.hh:183
const_iterator end() const
Definition: HallOfFame.hh:177
ContainerType::const_iterator const_iterator
Definition: HallOfFame.hh:48
const_iterator cbegin() const
Definition: HallOfFame.hh:175
const_iterator lower_bound(double key) const
Definition: HallOfFame.hh:197
size_type size() const
Definition: HallOfFame.hh:118
const_reverse_iterator crend() const
Definition: HallOfFame.hh:187
std::set< PairType, Compare > ContainerType
Definition: HallOfFame.hh:47
const_iterator upper_bound(double key) const
Definition: HallOfFame.hh:201
ContainerType::const_reverse_iterator const_reverse_iterator
Definition: HallOfFame.hh:49
GenericDatumFormatter< double, Result > score()
Main CASM namespace.
Definition: APICommand.hh:8
const double TOL
Definition: definitions.hh:30
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Results data structure for HallOfFame::insert.
Definition: HallOfFame.hh:53
HallOfFame::const_iterator const_iterator
Definition: HallOfFame.hh:54
InsertResult(std::pair< const_iterator, bool > _res, double _score, bool _excluded, const_iterator _excluded_pos)
Definition: HallOfFame.hh:64
InsertResult(const_iterator _pos, bool _success, double _score, bool _excluded, const_iterator _excluded_pos)
Definition: HallOfFame.hh:56