Fleet  0.0.9
Inference in the LOT
StructureToSamples.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 #include "Singleton.h"
6 #include "Polynomial.h"
7 #include "ReservoirSample.h"
8 
25 class StructuresToSamples : public Singleton<StructuresToSamples> {
26 public:
27  using key_type = std::pair<std::string,double>;
29 
30  std::map<key_type,value_type> M;
31 
32  const size_t trim_at = 5000; // when we get this big in overall_sample structures
33  const size_t trim_to = 1000; // trim to this size
34 
35  const double rounding_factor = 2.0; // round to the nearest this for determining groups
36 
38  return std::make_pair(h.structure_string(false),
39  round(h.posterior/rounding_factor)*rounding_factor );
40  }
41 
42  // Some light wrappers for M
43  decltype(M.begin()) begin() { return M.begin(); }
44  decltype(M.end()) end() { return M.end(); }
45  size_t size() const { return M.size(); }
46  value_type& operator[](key_type& t) { return M[t]; }
47 
48 
49  void add(MyHypothesis& h) {
50 
51  // convert to a key for the map
52  auto k = key(h);
53 
54  // create it if it doesn't exist
55  if(not M.count(k)) {
56  M.emplace(k,nsamples);
57  }
58 
59  // and add it
60  M[k] << h;
61 
62  // and trim if we must
63  if(M.size() >= trim_at) {
64  trim(trim_to);
65  }
66  }
67  void operator<<(MyHypothesis& h) { add(h);}
68 
70  return max_of(rs.values(), get_posterior<MyHypothesis>).second;
71  }
72 
73  void trim(const size_t N) {
74 
75  if(M.size() < N or N <= 0)
76  return;
77 
78  std::vector<double> structureScores;
79  for(auto& [ss, R] : M) {
80  structureScores.push_back( max_posterior(R) );
81  }
82 
83  // sorts low to high to find the nstructs best's score
84  std::sort(structureScores.begin(), structureScores.end(), std::greater<double>());
85  double cutoff = structureScores[N-1];
86 
87  // now go through and trim
88  auto it = M.begin();
89  while(it != M.end()) {
90  double b = max_posterior(it->second);
91  if(b < cutoff) { // if we erase
92  it = M.erase(it); // wow, erases returns a new iterator which is valid
93  }
94  else {
95  ++it;
96  }
97  }
98  }
99 
StructuresToSamples overall_samples
Definition: Singleton.h:6
value_type & operator[](key_type &t)
Definition: StructureToSamples.h:46
void trim(const size_t N)
Definition: StructureToSamples.h:73
void add(MyHypothesis &h)
Definition: StructureToSamples.h:49
std::pair< std::string, double > key_type
Definition: StructureToSamples.h:27
size_t size() const
Definition: StructureToSamples.h:45
decltype(M.end()) end()
Definition: StructureToSamples.h:44
std::map< key_type, value_type > M
Definition: StructureToSamples.h:30
Definition: StructureToSamples.h:25
double posterior
Definition: Bayesable.h:44
we don&#39;t need inputs/outputs for out MyHypothesis
Definition: MyHypothesis.h:6
const std::vector< T > & values() const
Definition: ReservoirSample.h:63
size_t nsamples
Definition: Main.cpp:23
const size_t trim_at
Definition: StructureToSamples.h:32
const double rounding_factor
Definition: StructureToSamples.h:35
std::pair< t *, double > max_of(const T &s, const std::function< double(const t &)> &f)
Definition: Random.h:410
key_type key(MyHypothesis &h)
Definition: StructureToSamples.h:37
A simple resevoir sampling algorithm. One great disappointment is that this doesn&#39;t implement the ver...
void operator<<(MyHypothesis &h)
Definition: StructureToSamples.h:67
const size_t trim_to
Definition: StructureToSamples.h:33
Definition: ReservoirSample.h:16
double max_posterior(const ReservoirSample< MyHypothesis > &rs)
Definition: StructureToSamples.h:69
T round(T v, int n)
Definition: Numerics.h:62
decltype(M.begin()) begin()
Definition: StructureToSamples.h:43