-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathrandom_.py
147 lines (114 loc) · 4.17 KB
/
random_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from typing import Optional
import numpy as np
from numpy.random import RandomState
from axelrod.action import Action
C, D = Action.C, Action.D
class RandomGenerator(object):
"""Container around a random number generator.
Enables reproducibility of player behavior, matches,
and tournaments."""
def __init__(self, seed: Optional[int] = None):
# _random is the internal object that generators random values
self._random = RandomState()
self.original_seed = seed
self.seed(seed)
def seed(self, seed_: Optional[int] = None):
"""Sets a seed"""
self._random.seed(seed_)
def random(self, *args, **kwargs):
return self._random.rand(*args, **kwargs)
def randint(self, *args, **kwargs):
return self._random.randint(*args, **kwargs)
def random_seed_int(self) -> int:
return self.randint(low=0, high=2**32 - 1, dtype="uint64")
def choice(self, *args, **kwargs):
return self._random.choice(*args, **kwargs)
def uniform(self, *args, **kwargs):
return self._random.uniform(*args, **kwargs)
def random_choice(self, p: float = 0.5) -> Action:
"""
Return C with probability `p`, else return D
No random sample is carried out if p is 0 or 1.
Parameters
----------
p : float
The probability of picking C
Returns
-------
axelrod.Action
"""
if p == 0:
return D
if p == 1:
return C
r = self.random()
if r < p:
return C
return D
def random_flip(self, action: Action, threshold: float) -> Action:
"""
Return flipped action with probability `threshold`
No random sample is carried out if threshold is 0 or 1.
Parameters
----------
action:
The action to flip or not
threshold : float
The probability of flipping action
Returns
-------
axelrod.Action
"""
if self.random_choice(threshold) == C:
return action.flip()
return action
def randrange(self, a: int, b: int) -> int:
"""Returns a random integer uniformly between a and b: [a, b)."""
c = b - a
r = c * self.random()
return a + int(r)
def random_vector(self, size):
"""Create a random vector of values in [0, 1] that sums to 1."""
vector = self.random(size)
return np.array(vector) / np.sum(vector)
class Pdf(object):
"""A class for a probability distribution"""
def __init__(self, counter, seed=None):
"""Take as an instance of collections.counter"""
self.sample_space, self.counts = zip(*counter.items())
self.size = len(self.sample_space)
self.total = sum(self.counts)
self.probability = list([v / self.total for v in self.counts])
self._random = RandomGenerator(seed=seed)
def sample(self):
"""Sample from the pdf"""
index = self._random.choice(a=range(self.size), p=self.probability)
# Numpy cannot sample from a list of n dimensional objects for n > 1,
# need to sample an index.
return self.sample_space[index]
class BulkRandomGenerator(object):
"""Bulk generator of random integers for tournament seeding and
reproducibility. Bulk generation of random values is more efficient.
Use this class like a generator."""
def __init__(self, seed=None, batch_size: int = 1000):
self._random_generator = RandomState()
self._random_generator.seed(seed)
self._ints = None
self._batch_size = batch_size
self._index = 0
self._fill_ints()
def _fill_ints(self):
# Generate more random values. Store as a list since generators
# cannot be pickled.
self._ints = self._random_generator.randint(
low=0, high=2**32 - 1, size=self._batch_size, dtype="uint64"
)
self._index = 0
def __next__(self):
try:
x = self._ints[self._index]
except IndexError:
self._fill_ints()
x = self._ints[self._index]
self._index += 1
return x