Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

GreedySampler.h

Go to the documentation of this file.
00001 #ifndef INCLUDED_GreedySampler_h_
00002 #define INCLUDED_GreedySampler_h_
00003 
00004 #include <vector>
00005 #include <stdexcept>
00006 #include <iosfwd>
00007 
00008 //! Iteratively samples over a one dimensional numeric region by subdividing the largest unsampled region
00009 /*! This could be implemented 'closed form' to compute the samples iteratively
00010  *  without explicitly storing the regions, but one of the goals of this class
00011  *  is to 'seed' the initial samples based on domain-specific heuristics, and
00012  *  then use the sampler to pick any additional samples to explore the remaining
00013  *  space efficiently. */
00014 class GreedySampler {
00015 public:
00016   //! Construct with the range to be sampled, and mark if the range is circular
00017   GreedySampler(float min, float max, bool circ) : full(min,max-min), reqMin(min), ranges(), circular(circ) {
00018     if(!circular)
00019       ranges.push_back(full);
00020   }
00021   
00022   //! Subdivides the largest region, returns the midpoint
00023   float sample();
00024   
00025   //! returns the span of the largest unsampled region
00026   float resolution();
00027   
00028   //! Informs the sampler of an externally taken sample
00029   /*! If using a circular region, @a x will be internally normalized for processing,
00030    *  but it is still more efficient to pass a 'close' value */
00031   void remove(float x);
00032   
00033   //! remove sampled points, start fresh sampling
00034   void reset();
00035   
00036   //! remove sampled points, start fresh sampling
00037   void reset(float min, float max, bool circ);
00038   
00039 protected:
00040   //! assumes a circular range, puts x in the range which is being sampled (#full) not the requested (#reqMin)
00041   float normalize(float x);
00042   
00043   //! output for debugging, displays regions in the heap
00044   friend std::ostream& operator<<(std::ostream& os, const GreedySampler& gs);
00045   
00046   struct range {
00047     range(float x, float r) : min(x), span(r) {}
00048     float min, span;
00049     bool operator<(const range& r) const { return span < r.span; }
00050   };
00051   range full; //!< the range being sampled (may differ from #reqMin for circular regions with initial samples removed)
00052   float reqMin; //!< requested output range (may differ from #full.min for circular regions with initial samples removed)
00053   std::vector<range> ranges; //!< stores the unsampled regions
00054   bool circular; //!< if true, affects initialization and first sample, also causes remove to normalize its argument
00055 };
00056 
00057 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:41 2016 by Doxygen 1.6.3