Alexandria  2.16
Please provide a description of the project.
SamplingPolicy.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * @file SamplingPolicy.h
21  * @author nikoapos
22  */
23 
24 #ifndef SOM_SAMPLINGPOLICY_H
25 #define SOM_SAMPLINGPOLICY_H
26 
27 #include <list>
28 #include <utility>
29 #include <random>
30 #include <iterator>
31 
32 namespace Euclid {
33 namespace SOM {
34 namespace SamplingPolicy {
35 
36 template <typename IterType>
37 class Interface {
38 
39 public:
40 
41  virtual IterType start(IterType begin, IterType end) const = 0;
42 
43  virtual IterType next(IterType iter) const = 0;
44 
45 };
46 
47 template <typename IterType>
48 class FullSet : public Interface<IterType> {
49 
50 public:
51 
52  IterType start(IterType begin, IterType) const override {
53  return begin;
54  }
55 
56  IterType next(IterType iter) const override {
57  return ++iter;
58  }
59 
60 };
61 
62 template <typename IterType>
63 class Bootstrap : public Interface<IterType> {
64 
65  public:
66 
67  IterType start(IterType begin, IterType end) const override {
68 
69  m_end = end;
70 
72  std::mt19937 gen(rd());
73  std::uniform_int_distribution<> dis(0, std::distance(begin, end) - 1);
74  auto random_index = dis(gen);
75 
76  auto result = begin;
77  std::advance(result, random_index);
78 
79  return result;
80  }
81 
82  IterType next(IterType) const override {
83  return m_end;
84  }
85 
86 private:
87 
88  mutable IterType m_end;
89 
90 };
91 
92 template <typename IterType>
94  return Bootstrap<IterType>{};
95 }
96 
97 template <typename IterType>
98 class Jackknife :public Interface<IterType> {
99 
100 public:
101 
102  explicit Jackknife(std::size_t sample_size) : m_sample_size(sample_size), m_current(sample_size) {
103  m_iter_list.reserve(sample_size);
104  }
105 
106  IterType start(IterType begin, IterType end) const override {
107 
108  m_end = end;
109 
110  // Clear the iterators list, for the case it has already something inside
111  m_iter_list.clear();
113 
114  // Put all the possible iterators in a temporary linked list
115  std::list<IterType> all_iter_list {};
116  for (auto it = begin; it != end; ++it) {
117  all_iter_list.push_back(it);
118  }
119 
120  // Create the random device to use
122  std::mt19937 gen(rd());
123 
124  // Pick up m_sample_size random iterators from the temporary list
125  int all_max_index = all_iter_list.size() - 1;
126  for (std::size_t i = 0; i < m_sample_size && all_max_index >= 0; ++i, --all_max_index) {
127  std::uniform_int_distribution<> dis(0, all_max_index);
128  auto it = all_iter_list.begin();
129  std::advance(it, dis(gen));
130  m_iter_list.push_back(*it);
131  all_iter_list.erase(it);
132  }
133 
134  // Set the current iterator at the beginning of the vector and return it
135  m_current = 0;
137  return m_iter_list[m_current];
138  }
139 
140  IterType next(IterType) const override {
141  ++m_current;
142  if (m_current >= m_iter_list_size) {
143  return m_end;
144  }
145  return m_iter_list[m_current];
146  }
147 
148 private:
149 
153  mutable IterType m_end;
155 
156 };
157 
158 template <typename IterType>
160  return Jackknife<IterType>{sample_size};
161 }
162 
163 }
164 }
165 }
166 
167 #endif /* SOM_SAMPLINGPOLICY_H */
168 
Euclid::SOM::SamplingPolicy::Bootstrap
Definition: SamplingPolicy.h:63
std::uniform_int_distribution
std::list
STL class.
Euclid::SOM::SamplingPolicy::FullSet
Definition: SamplingPolicy.h:48
std::vector::reserve
T reserve(T... args)
Euclid::SOM::SamplingPolicy::Jackknife::m_iter_list
std::vector< IterType > m_iter_list
Definition: SamplingPolicy.h:151
Euclid::SOM::SamplingPolicy::Jackknife::start
IterType start(IterType begin, IterType end) const override
Definition: SamplingPolicy.h:106
std::vector< IterType >
std::vector::size
T size(T... args)
Euclid::SOM::SamplingPolicy::Jackknife::Jackknife
Jackknife(std::size_t sample_size)
Definition: SamplingPolicy.h:102
Euclid::SOM::SamplingPolicy::Jackknife::next
IterType next(IterType) const override
Definition: SamplingPolicy.h:140
Euclid::SOM::SamplingPolicy::Jackknife
Definition: SamplingPolicy.h:98
std::distance
T distance(T... args)
Euclid::SOM::SamplingPolicy::Jackknife::m_iter_list_size
std::size_t m_iter_list_size
Definition: SamplingPolicy.h:152
Euclid::SOM::SamplingPolicy::jackknifeFactory
Jackknife< IterType > jackknifeFactory(IterType, std::size_t sample_size)
Definition: SamplingPolicy.h:159
std::vector::clear
T clear(T... args)
Euclid::SOM::SamplingPolicy::Jackknife::m_sample_size
std::size_t m_sample_size
Definition: SamplingPolicy.h:150
std::mt19937
std::list::push_back
T push_back(T... args)
Euclid::SOM::SamplingPolicy::Jackknife::m_current
std::size_t m_current
Definition: SamplingPolicy.h:154
std::random_device
Euclid::SOM::SamplingPolicy::Interface::start
virtual IterType start(IterType begin, IterType end) const =0
Euclid::SOM::SamplingPolicy::bootstrapFactory
Bootstrap< IterType > bootstrapFactory(IterType)
Definition: SamplingPolicy.h:93
Euclid::SOM::SamplingPolicy::Bootstrap::next
IterType next(IterType) const override
Definition: SamplingPolicy.h:82
Euclid::SOM::SamplingPolicy::Jackknife::m_end
IterType m_end
Definition: SamplingPolicy.h:153
Euclid::SOM::SamplingPolicy::Interface::next
virtual IterType next(IterType iter) const =0
Euclid::SOM::SamplingPolicy::Bootstrap::m_end
IterType m_end
Definition: SamplingPolicy.h:88
Euclid::SOM::SamplingPolicy::FullSet::next
IterType next(IterType iter) const override
Definition: SamplingPolicy.h:56
std::advance
T advance(T... args)
Euclid::SOM::SamplingPolicy::Interface
Definition: SamplingPolicy.h:37
std::size_t
Euclid::SOM::SamplingPolicy::Bootstrap::start
IterType start(IterType begin, IterType end) const override
Definition: SamplingPolicy.h:67
Euclid
Definition: InstOrRefHolder.h:29
Euclid::SOM::SamplingPolicy::FullSet::start
IterType start(IterType begin, IterType) const override
Definition: SamplingPolicy.h:52