forked from mlberkeley/genetic-algs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_generator.py
152 lines (118 loc) · 4.33 KB
/
data_generator.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
148
149
150
151
152
import numpy as np
import pickle
""" Module to generate fake data to test the program. """
def make_pendulum(m, l, angle_0, t=10, sample_period=1e-2, fname="pendulum.pkl"):
""" Simulate a pendulum with mass `m` and length `l` starting at
angle `angle_0` relative to the vertical.
Args:
m: mass (in kg)
l: length (in m)
angle_0: initial angle (in radians)
t: time to run the simulation (in seconds) (default=10)
sample_period: time period of each sample (in seconds) (default=1e-2)
fname: filename to save .pkl file to (default="pendulum.pkl")
Outputs:
pickled dictionary file with the following mappings:
"x": x position of the pendulum (right is positive)
"y": y position of the pendulum (up is positive)
"sample_period": time per sample (in seconds)
"""
# Solution to the system is theta = Acos(wt)
g = 9.81
times = np.arange(0, t, sample_period)
angular_freq = np.sqrt(g / l)
xs = []
ys = []
for time in times:
angle = angle_0 * np.cos(angular_freq * time)
x = l * np.sin(angle)
y = l * np.cos(angle)
xs.append(x)
ys.append(y)
xs = np.array(xs)
ys = np.array(ys)
# Save data
data = {}
data["arrays"] = {}
data["arrays"]["t"] = times
data["arrays"]["x"] = xs
data["arrays"]["y"] = ys
# Save sample period
data["sample_period"] = sample_period
pickle.dump(data, open(fname, "wb"))
def make_const(x, t=10, sample_period=1e-2, fname="const.pkl"):
""" Simulate a constant.
Args:
x: constant
t: time to run the simulation (in seconds) (default=10)
fname: filename to save .pkl file to (default="const.pkl")
Outputs:
pickled dictionary file with the following mappings:
"x": constant value
"sample_period": time per sample (in seconds)
"""
# Solution to the system is theta = Acos(wt)
times = np.arange(0, t, sample_period)
# TODO this is kinda dumb
xs = np.array([x for _ in times])
# Save data
data = {}
data["arrays"] = {}
data["arrays"]["t"] = times
data["arrays"]["x"] = xs
# Save sample period
data["sample_period"] = sample_period
pickle.dump(data, open(fname, "wb"))
def make_linear(m, b, t=10, sample_period=1e-2, fname="linear.pkl"):
""" Simulate a linear relationship with slope and y-intercept.
Args:
m: slope
b: y-intercept
t: time to run the simulation (in seconds) (default=10)
fname: filename to save .pkl file to (default="const.pkl")
Outputs:
pickled dictionary file with the following mappings:
"x": constant value
"sample_period": time per sample (in seconds)
"""
# Solution to the system is theta = Acos(wt)
times = np.arange(0, t, sample_period)
xs = np.array([m * time + b for time in times])
# Save data
data = {}
data["arrays"] = {}
data["arrays"]["t"] = times
data["arrays"]["x"] = xs
# Save sample period
data["sample_period"] = sample_period
pickle.dump(data, open(fname, "wb"))
def make_quadratic(a, b, c, t=10, sample_period=1e-2, fname="quadratic.pkl"):
""" Simulate a quadratic relationship:
at^2 + bt + c
Args:
a: degree-2 term
b: degree-1 term
c: degree-0 term
t: time to run the simulation (in seconds) (default=10)
fname: filename to save .pkl file to (default="const.pkl")
Outputs:
pickled dictionary file with the following mappings:
"x": constant value
"sample_period": time per sample (in seconds)
"""
# Solution to the system is theta = Acos(wt)
times = np.arange(0, t, sample_period)
xs = np.array([a * time ** 2 + b * time + c for time in times])
# Save data
data = {}
data["arrays"] = {}
data["arrays"]["t"] = times
data["arrays"]["x"] = xs
# Save sample period
data["sample_period"] = sample_period
pickle.dump(data, open(fname, "wb"))
if __name__ == "__main__":
make_pendulum(1, 10, 1, t=0.1)
make_const(3, t=0.1)
make_linear(5, 0, t=10, sample_period=1e-1)
make_quadratic(4, 5, 2, t=10, sample_period=1e-1)