CluE  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uniformsampling.h
Go to the documentation of this file.
1 #ifndef UNIFORMSAMPLING_H
2 #define UNIFORMSAMPLING_H
3 
4 #include "../base/inputsetter.h"
5 #include "../base/algorithm.h"
6 #include "../base/solutionprovider.h"
7 #include "../datastructure/discreteproxysolution.h"
8 #include "../misc/randomness.h"
9 #include "../exception/invalidruntimeconfigurationexception.h"
10 
11 #include <set>
12 #include <ctime>
13 
14 namespace CluE
15 {
16 
21 template<typename T> class UniformSampling : public Algorithm, public InputSetter<T>
22 {
23 public:
24  UniformSampling(std::vector<T*> const* data = NULL, unsigned int numberOfSamples = 0);
25 
26  virtual ~UniformSampling()
27  {
28  }
29 
39 
40  virtual void setInput(std::vector<T*> const*);
41  void setNumberOfSamples(unsigned int);
42 
48 
49 private:
50  std::vector<T*> const* input;
51  unsigned int number_of_samples;
52 };
53 
54 template<typename T> UniformSampling<T>::UniformSampling(std::vector<T*> const* data,
55  unsigned int n) : input(data), number_of_samples(n)
56 {
57 }
58 
60 {
61  if(this->input==NULL)
62  throw InvalidRuntimeConfigurationException(0, "Input is NULL.");
63 
64  time_t start, end;
65  start = time(0);
66 
68 
69  unsigned int N = this->input->size();
70  if(N==0)
71  throw InvalidRuntimeConfigurationException(2, "Empty input set.");
72  unsigned int samplenum = this->number_of_samples;
73  if(samplenum==0)
74  throw InvalidRuntimeConfigurationException(3, "Desired number of samples is 0.");
75  if(samplenum>N)
76  throw InvalidRuntimeConfigurationException(4, "Desired number of samples is larger than size of input.");
77 
78  std::vector<T*> unchosen = *this->input; // copy the input vector
79  solution->proxysets.push_back(std::vector<T*>());
80 
81  for(unsigned int i=0; i<samplenum; i++)
82  {
84  std::uniform_int_distribution<int> dis(0, N-i-1);
85  int index = dis(rg);
86 
87  solution->proxysets[0].push_back(unchosen[index]); // choose at random from remaining elements
88  unchosen.erase(unchosen.begin()+index); // remove chosen sample from dataset
89  }
90 
91  end = time(0);
92  solution->seconds=end-start;
93  std::clog << "CluE::UniformSampling<T>::compute() - finished" << std::endl;
94  return solution;
95 }
96 
97 template<typename T> void UniformSampling<T>::setInput(std::vector<T*> const* data)
98 {
99  this->input = data;
100 }
101 
102 template<typename T> void UniformSampling<T>::setNumberOfSamples(unsigned int n)
103 {
104  this->number_of_samples = n;
105 }
106 
108 {
109  return dynamic_cast<UniformSampling<T>*>(s);
110 }
111 
112 }
113 
114 #endif
Encapsulates an STL random generator.
std::vector< T * > const * input
unsigned int number_of_samples
virtual DiscreteProxySolution< T > * compute()
Computes the sample set.
virtual void setInput(std::vector< T * > const *)
Data structure for discrete proxies.
void setNumberOfSamples(unsigned int)
static UniformSampling< T > * toUniformSampling(Algorithm *s)
does a dynamic cast of the given Algorithm to UniformSampling
std::vector< std::vector< T * > > proxysets
Uniform sampling.
UniformSampling(std::vector< T * > const *data=NULL, unsigned int numberOfSamples=0)
static RandomGenerator getRandomGenerator()
Definition: randomness.h:23
Abstract base class for algorithms.
Definition: algorithm.h:17
Indicates that a computation entered an invalid configuration state.
Interface to propagate the ability to set input data.
Definition: inputsetter.h:13