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> |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame^] | 30 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 31 | |
| 32 | // generates intervals with uniform distribution |
| 33 | class RandomIntervalGenerator : public IntervalGenerator |
| 34 | { |
| 35 | public: |
| 36 | typedef enum |
| 37 | { |
| 38 | UP = 1, |
| 39 | DOWN = 2, |
| 40 | EVEN = 3 |
| 41 | } Direction; |
| 42 | |
| 43 | public: |
| 44 | // percent is random-range/interval; e.g. if interval is 10 and you wish the random-range to be 2 |
| 45 | // e.g. 9 ~ 11, percent = 0.2 |
| 46 | // direction shifts the random range; e.g. in the above example, UP would produce a range of |
| 47 | // 10 ~ 12, DOWN of 8 ~ 10, and EVEN of 9 ~ 11 |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 48 | RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN) |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame^] | 49 | // : m_rng(time(NULL)) |
| 50 | : m_rng (static_cast<int> (boost::posix_time::microsec_clock::local_time().time_of_day ().total_nanoseconds ())) |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 51 | , m_dist(0.0, fractional(percent)) |
| 52 | , m_random(m_rng, m_dist) |
| 53 | , m_direction(direction) |
| 54 | , m_percent(percent) |
| 55 | , m_interval(interval) |
| 56 | { } |
| 57 | |
| 58 | virtual ~RandomIntervalGenerator(){} |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame^] | 59 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 60 | virtual double |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 61 | nextInterval() _OVERRIDE |
| 62 | { |
| 63 | double percent = m_random(); |
| 64 | double interval = m_interval; |
| 65 | switch (m_direction) |
| 66 | { |
| 67 | case UP: interval = m_interval * (1.0 + percent); break; |
| 68 | case DOWN: interval = m_interval * (1.0 - percent); break; |
| 69 | case EVEN: interval = m_interval * (1.0 - m_percent/2.0 + percent); break; |
| 70 | default: break; |
| 71 | } |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 73 | return interval; |
| 74 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame^] | 75 | |
Alexander Afanasyev | 1b0e008 | 2013-01-17 16:48:26 -0800 | [diff] [blame] | 76 | private: |
| 77 | inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); } |
| 78 | |
| 79 | private: |
| 80 | typedef boost::mt19937 RNG_TYPE; |
| 81 | RNG_TYPE m_rng; |
| 82 | boost::uniform_real<> m_dist; |
| 83 | boost::variate_generator<RNG_TYPE &, boost::uniform_real<> > m_random; |
| 84 | Direction m_direction; |
| 85 | double m_percent; |
| 86 | double m_interval; |
| 87 | |
| 88 | }; |
| 89 | #endif // RANDOM_INTERVAL_GENERATOR_H |