blob: bd4621426091a6d5b8935e694efdc7afd0e589ba [file] [log] [blame]
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -08001/* -*- 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 Afanasyevfc720362013-01-24 21:49:48 -080030#include <boost/date_time/posix_time/posix_time_types.hpp>
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080031
32// generates intervals with uniform distribution
33class RandomIntervalGenerator : public IntervalGenerator
34{
35public:
36 typedef enum
37 {
38 UP = 1,
39 DOWN = 2,
40 EVEN = 3
41 } Direction;
42
43public:
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 Afanasyevabe952a2013-01-17 17:06:32 -080048 RandomIntervalGenerator(double interval, double percent, Direction direction = EVEN)
Alexander Afanasyevfc720362013-01-24 21:49:48 -080049 // : m_rng(time(NULL))
50 : m_rng (static_cast<int> (boost::posix_time::microsec_clock::local_time().time_of_day ().total_nanoseconds ()))
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080051 , 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 Afanasyevfc720362013-01-24 21:49:48 -080059
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080060 virtual double
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080061 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 Afanasyev1b0e0082013-01-17 16:48:26 -080072
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080073 return interval;
74 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080075
Alexander Afanasyev1b0e0082013-01-17 16:48:26 -080076private:
77 inline double fractional(double x) { double dummy; return abs(modf(x, &dummy)); }
78
79private:
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