-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulator.py
More file actions
231 lines (182 loc) · 8.17 KB
/
Copy pathsimulator.py
File metadata and controls
231 lines (182 loc) · 8.17 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import numpy as np
import matplotlib.pyplot as plt
from time import perf_counter
class Simulator:
def __init__(self, model, x0, u0, timespan):
self.model = model
self.x0 = x0
self.u0 = u0
self.dt = self.model.dt
self.tspan = np.arange(0,timespan, self.dt)
self.data = np.empty([len(self.tspan),self.model.state_size() + self.model.input_size()])
self.input_bound = np.array([])
def add_intput_bound(self, bound):
assert len(bound) == len(self.u0)
self.input_bound = bound
def run(self):
x = self.x0
u = self.u0
y = self.x0 # full state
start_time = perf_counter()
for i in range(len(self.tspan)):
x = self.model.get_next_state(x,u,y)
u = self.model.get_control_input(x)
y = x # assume perfect sensors
self.data[i] = np.append(x, u)
print('Time for {} iterations of {} is {}'.format(len(self.tspan), self.model.get_name(), perf_counter() - start_time))
plt.rcParams.update({'font.size': 12})
plt.rcParams.update({
"text.usetex": True,
})
ns = self.model.state_size()
for i in range(ns):
plt.plot(self.tspan,self.data[:,i],linewidth=2,label=self.model.get_state_names()[i])
plt.xlabel('Time')
plt.ylabel('State')
plt.title(self.model.get_name())
plt.legend(loc='lower right')
plt.show()
nu = self.model.input_size()
fig, axs = plt.subplots(ns + nu)
fig.set_figheight(8)
fig.suptitle(self.model.get_name())
for i in range(ns):
axs[i].plot(self.tspan,self.data[:,i],linewidth=2)
axs[i].set_ylabel(self.model.get_state_names()[i])
for i in range(nu):
axs[i+ns].plot(self.tspan,self.data[:,i+ns],linewidth=2)
#axs[i+ns].set_ylabel(self.model.u[i].name())
if self.input_bound.any():
if self.input_bound[i].any():
upper = np.full((len(self.tspan),), self.input_bound[i][0])
lower = np.full((len(self.tspan),), self.input_bound[i][1])
axs[i+ns].plot(self.tspan,upper,linewidth=1)
axs[i+ns].plot(self.tspan,lower,linewidth=1)
plt.xlabel('Time')
plt.show()
class Comparison:
def __init__(self, model, model2, x0, u0, timespan):
self.model = model
self.model2 = model2
self.x0 = x0
self.u0 = u0
self.dt = self.model.dt
self.tspan = np.arange(0,timespan, self.dt)
self.data = np.empty([len(self.tspan),self.model.state_size() + 1])
self.data2 = np.empty([len(self.tspan),self.model.state_size() + 1])
self.input_bound = np.array([])
def add_intput_bound(self, bound):
assert len(bound) == len(self.u0)
self.input_bound = bound
def run(self):
x = self.x0
u = self.u0
y = self.x0 # full state
x2 = self.x0
u2 = self.u0
y2 = self.x0 # full state
start_time = perf_counter()
print(self.model2.goal_state)
for i in range(len(self.tspan)):
# x = self.model.get_next_state(x,u,y)
# u = self.model.get_control_input(x)
# y = x # assume perfect sensors
# self.data[i] = np.append(x, u[:1])
x = self.model2.A @ (x - self.model2.goal_state) + self.model2.B @ u2 + self.model2.goal_state
# u = self.model.get_control_input(x)
y = x # assume perfect sensors
self.data[i] = np.append(x, u2[:1])
x2 = self.model2.get_next_state(x2,u2,y2)
u2 = self.model2.get_control_input(x2)
y2 = x2 # assume perfect sensors
self.data2[i] = np.append(x2, u2)
print('Time for {} iterations of {} is {}'.format(len(self.tspan), self.model.get_name(), perf_counter() - start_time))
plt.rcParams.update({'font.size': 12})
plt.rcParams.update({
"text.usetex": True,
})
ns = self.model.state_size()
for i in range(ns):
plt.plot(self.tspan,self.data[:,i],linewidth=2,label=self.model.get_state_names()[i])
plt.plot(self.tspan,self.data2[:,i],linewidth=2,label=self.model.get_state_names()[i])
plt.xlabel('Time')
plt.ylabel('State')
plt.title(self.model.get_name())
plt.legend(loc='lower right')
plt.show()
nu = self.model2.input_size()
fig, axs = plt.subplots(ns + nu)
fig.set_figheight(8)
fig.suptitle(self.model.get_name())
for i in range(ns):
axs[i].plot(self.tspan,self.data[:,i],linewidth=2)
axs[i].plot(self.tspan,self.data2[:,i],linewidth=2)
axs[i].set_ylabel(self.model.get_state_names()[i])
for i in range(nu):
axs[i+ns].plot(self.tspan,self.data[:,i+ns],linewidth=2)
axs[i+ns].plot(self.tspan,self.data2[:,i+ns],linewidth=2)
#axs[i+ns].set_ylabel(self.model.u[i].name())
if self.input_bound.any():
if self.input_bound[i].any():
upper = np.full((len(self.tspan),), self.input_bound[i][0])
lower = np.full((len(self.tspan),), self.input_bound[i][1])
axs[i+ns].plot(self.tspan,upper,linewidth=1)
axs[i+ns].plot(self.tspan,lower,linewidth=1)
plt.xlabel('Time')
plt.show()
class NoisySimulator:
def __init__(self, model, x0, u0, timespan):
self.model = model
self.x0 = x0
self.u0 = u0
self.dt = model.dt
self.tspan = np.arange(0,timespan,self.dt)
self.true_data = np.empty([len(self.tspan),self.model.state_size() + self.model.input_size()])
self.noisy_data = np.empty([len(self.tspan),self.model.state_size() + self.model.input_size()])
self.kf_data = np.empty([len(self.tspan),self.model.state_size() + self.model.input_size()])
# set the default noise
self.noise = np.array([0.07, 0.0225])
def run(self):
num_measurements = self.model.C.shape[0]
x_noise = self.x0
x_kf = self.x0
x_true = self.x0
u_kf = np.concatenate((self.u0, self.model.C@self.x0), axis=0)
u_noise = self.u0
u_true = self.u0
noise = np.zeros(num_measurements)
sensors = np.zeros(num_measurements)
for i in range(len(self.tspan)):
# generate some noise
for j in range(num_measurements):
noise[j] = np.random.normal(0.0,np.sqrt(self.noise[j]))
x_true = self.model.get_next_state_nonlinear(x_true,u_true,self.dt)
u_true = self.model.get_control_input(x_true)
self.true_data[i] = np.append(x_true, u_true)
# perturb our state with some noise
x_noise = x_noise + noise@self.model.C
x_noise = self.model.get_next_state_linear(x_noise,u_noise,self.dt)
u_noise = self.model.get_control_input(x_noise)
self.noisy_data[i] = np.append(x_noise, u_noise)
x_kf = self.model.get_next_state_kf(x_kf, u_kf, self.dt)
u_kf[0] = self.model.get_control_input(x_kf)
u_kf[1:4] = noise + self.model.C@x_kf
self.kf_data[i] = np.append(x_kf, u_kf[0])
# plt.rcParams['figure.figsize'] = [8, 8]
plt.rcParams.update({'font.size': 12})
plt.rcParams.update({
"text.usetex": True,
})
m = np.ones((num_measurements,))
measurements = m@self.model.C
for i in range(self.model.state_size()):
if measurements[i] != 0.0:
plt.plot(self.tspan, self.noisy_data[:,i],linewidth=1,label=('true + noise'))
plt.plot(self.tspan,self.kf_data[:,i],linewidth=2,label='Kalman filter')
plt.plot(self.tspan,self.true_data[:,i],linewidth=1,label='true')
plt.xlabel('time')
plt.ylabel(self.model.state_names[i])
plt.legend()
plt.title(self.model.name)
# plt.savefig("documents/KFangular_velocity.pdf", format="pdf", bbox_inches="tight")
plt.show()