diff options
author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-02-07 11:21:23 -0500 |
---|---|---|
committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-02-07 11:21:23 -0500 |
commit | 39020cf5ae3030bc15035925a0c72eb44eea67b7 (patch) | |
tree | fb82b339c1e5c14334f8d9839f8d836b120fbb08 /gen/rv.py | |
parent | d524da9bd072ad1be4ac0d633e3e783094ddc2d7 (diff) |
Added gen_exps.py script.
Diffstat (limited to 'gen/rv.py')
-rw-r--r-- | gen/rv.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/gen/rv.py b/gen/rv.py new file mode 100644 index 0000000..e6f4d0f --- /dev/null +++ b/gen/rv.py | |||
@@ -0,0 +1,86 @@ | |||
1 | from __future__ import division | ||
2 | import random | ||
3 | |||
4 | def uniform_int(minval, maxval): | ||
5 | "Create a function that draws ints uniformly from {minval, ..., maxval}" | ||
6 | def _draw(): | ||
7 | return random.randint(minval, maxval) | ||
8 | return _draw | ||
9 | |||
10 | def uniform(minval, maxval): | ||
11 | "Create a function that draws floats uniformly from [minval, maxval]" | ||
12 | def _draw(): | ||
13 | return random.uniform(minval, maxval) | ||
14 | return _draw | ||
15 | |||
16 | def bernoulli(p): | ||
17 | "Create a function that flips a weight coin with probability p" | ||
18 | def _draw(): | ||
19 | return random.random() < p | ||
20 | return _draw | ||
21 | |||
22 | def uniform_choice(choices): | ||
23 | "Create a function that draws uniformly elements from choices" | ||
24 | selector = uniform_int(0, len(choices) - 1) | ||
25 | def _draw(): | ||
26 | return choices[selector()] | ||
27 | return _draw | ||
28 | |||
29 | def truncate(minval, maxval): | ||
30 | def _limit(fun): | ||
31 | def _f(*args, **kargs): | ||
32 | val = fun(*args, **kargs) | ||
33 | return min(maxval, max(minval, val)) | ||
34 | return _f | ||
35 | return _limit | ||
36 | |||
37 | def redraw(minval, maxval): | ||
38 | def _redraw(dist): | ||
39 | def _f(*args, **kargs): | ||
40 | in_range = False | ||
41 | while not in_range: | ||
42 | val = dist(*args, **kargs) | ||
43 | in_range = minval <= val <= maxval | ||
44 | return val | ||
45 | return _f | ||
46 | return _redraw | ||
47 | |||
48 | def exponential(minval, maxval, mean, limiter=redraw): | ||
49 | """Create a function that draws floats from an exponential | ||
50 | distribution with expected value 'mean'. If a drawn value is less | ||
51 | than minval or greater than maxval, then either another value is | ||
52 | drawn (if limiter=redraw) or the drawn value is set to minval or | ||
53 | maxval (if limiter=truncate).""" | ||
54 | def _draw(): | ||
55 | return random.expovariate(1.0 / mean) | ||
56 | return limiter(minval, maxval)(_draw) | ||
57 | |||
58 | def multimodal(weighted_distributions): | ||
59 | """Create a function that draws values from several distributions | ||
60 | with probability according to the given weights in a list of | ||
61 | (distribution, weight) pairs.""" | ||
62 | total_weight = sum([w for (d, w) in weighted_distributions]) | ||
63 | selector = uniform(0, total_weight) | ||
64 | def _draw(): | ||
65 | x = selector() | ||
66 | wsum = 0 | ||
67 | for (d, w) in weighted_distributions: | ||
68 | wsum += w | ||
69 | if wsum >= x: | ||
70 | return d() | ||
71 | assert False # should never drop off | ||
72 | return _draw | ||
73 | |||
74 | def uniform_slack(min_slack_ratio, max_slack_ratio): | ||
75 | """Choose deadlines uniformly such that the slack | ||
76 | is within [cost + min_slack_ratio * (period - cost), | ||
77 | cost + max_slack_ratio * (period - cost)]. | ||
78 | |||
79 | Setting max_slack_ratio = 1 implies constrained deadlines. | ||
80 | """ | ||
81 | def choose_deadline(cost, period): | ||
82 | slack = period - cost | ||
83 | earliest = slack * min_slack_ratio | ||
84 | latest = slack * max_slack_ratio | ||
85 | return cost + random.uniform(earliest, latest) | ||
86 | return choose_deadline | ||