Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef RANDOM_INTERVAL_GENERATOR_H |
| 23 | #define RANDOM_INTERVAL_GENERATOR_H |
| 24 | |
| 25 | #include "interval-generator.h" |
| 26 | |
| 27 | #include <boost/random/mersenne_twister.hpp> |
| 28 | #include <boost/random/uniform_real.hpp> |
| 29 | #include <boost/random/variate_generator.hpp> |
| 30 | |
| 31 | // generates intervals with uniform distribution |
| 32 | class RandomIntervalGenerator : public IntervalGenerator |
| 33 | { |
| 34 | public: |
| 35 | typedef enum |
| 36 | { |
| 37 | UP = 1, |
| 38 | DOWN = 2, |
| 39 | EVEN = 3 |
| 40 | } Direction; |
| 41 | |
| 42 | public: |
| 43 | // percent is random-range/interval; e.g. if interval is 10 and you wish the random-range to be 2 |
| 44 | // e.g. 9 ~ 11, percent = 0.2 |
| 45 | // direction shifts the random range; e.g. in the above example, UP would produce a range of |
| 46 | // 10 ~ 12, DOWN of 8 ~ 10, and EVEN of 9 ~ 11 |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 47 | RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN) |
| 48 | : m_rng(time(NULL)) |
| 49 | , m_dist(0.0, fractional(percent)) |
| 50 | , m_random(m_rng, m_dist) |
| 51 | , m_direction(direction) |
| 52 | , m_percent(percent) |
| 53 | , m_interval(interval) |
| 54 | { } |
| 55 | |
| 56 | virtual ~RandomIntervalGenerator(){} |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 57 | |
| 58 | virtual double |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 59 | nextInterval() _OVERRIDE |
| 60 | { |
| 61 | double percent = m_random(); |
| 62 | double interval = m_interval; |
| 63 | switch (m_direction) |
| 64 | { |
| 65 | case UP: interval = m_interval * (1.0 + percent); break; |
| 66 | case DOWN: interval = m_interval * (1.0 - percent); break; |
| 67 | case EVEN: interval = m_interval * (1.0 - m_percent/2.0 + percent); break; |
| 68 | default: break; |
| 69 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 71 | return interval; |
| 72 | } |
| 73 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 74 | private: |
| 75 | inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); } |
| 76 | |
| 77 | private: |
| 78 | typedef boost::mt19937 RNG_TYPE; |
| 79 | RNG_TYPE m_rng; |
| 80 | boost::uniform_real<> m_dist; |
| 81 | boost::variate_generator<RNG_TYPE &, boost::uniform_real<> > m_random; |
| 82 | Direction m_direction; |
| 83 | double m_percent; |
| 84 | double m_interval; |
| 85 | |
| 86 | }; |
| 87 | #endif // RANDOM_INTERVAL_GENERATOR_H |