-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathprimitives_test.py
224 lines (199 loc) · 8.59 KB
/
primitives_test.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
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
import unittest
import numpy as np
from pydrake.autodiffutils import AutoDiffXd
from pydrake.symbolic import Expression
from pydrake.systems.analysis import Simulator
from pydrake.systems.framework import (
AbstractValue,
BasicVector,
DiagramBuilder,
VectorBase,
)
from pydrake.systems.test.test_util import (
MyVector2,
)
from pydrake.systems.primitives import (
Adder, Adder_,
AffineSystem, AffineSystem_,
ConstantVectorSource, ConstantVectorSource_,
ControllabilityMatrix,
FirstOrderTaylorApproximation,
Integrator, Integrator_,
IsControllable,
IsObservable,
Linearize,
LinearSystem, LinearSystem_,
Multiplexer, Multiplexer_,
ObservabilityMatrix,
PassThrough, PassThrough_,
Saturation, Saturation_,
SignalLogger, SignalLogger_,
WrapToSystem, WrapToSystem_,
)
def compare_value(test, a, b):
# Compares a vector or abstract value.
if isinstance(a, VectorBase):
test.assertTrue(np.allclose(a.get_value(), b.get_value()))
else:
test.assertEquals(type(a.get_value()), type(b.get_value()))
test.assertEquals(a.get_value(), b.get_value())
class TestGeneral(unittest.TestCase):
def _check_instantiations(self, template, supports_symbolic=True):
default_cls = template[None]
self.assertTrue(template[float] is default_cls)
self.assertTrue(template[AutoDiffXd] is not default_cls)
if supports_symbolic:
self.assertTrue(template[Expression] is not default_cls)
def test_instantiations(self):
# TODO(eric.cousineau): Refine tests once NumPy functionality is
# resolved for dtype=object, or dtype=custom is used.
self._check_instantiations(Adder_)
self._check_instantiations(AffineSystem_)
self._check_instantiations(ConstantVectorSource_)
self._check_instantiations(Integrator_)
self._check_instantiations(LinearSystem_)
self._check_instantiations(Multiplexer_)
self._check_instantiations(PassThrough_)
self._check_instantiations(Saturation_)
self._check_instantiations(SignalLogger_)
self._check_instantiations(WrapToSystem_)
def test_signal_logger(self):
# Log the output of a simple diagram containing a constant
# source and an integrator.
builder = DiagramBuilder()
kValue = 2.4
source = builder.AddSystem(ConstantVectorSource([kValue]))
kSize = 1
integrator = builder.AddSystem(Integrator(kSize))
logger = builder.AddSystem(SignalLogger(kSize))
builder.Connect(source.get_output_port(0),
integrator.get_input_port(0))
builder.Connect(integrator.get_output_port(0),
logger.get_input_port(0))
diagram = builder.Build()
simulator = Simulator(diagram)
simulator.StepTo(1)
t = logger.sample_times()
x = logger.data()
self.assertTrue(t.shape[0] > 2)
self.assertTrue(t.shape[0] == x.shape[1])
self.assertAlmostEqual(x[0, -1], t[-1]*kValue, places=2)
logger.reset()
def test_linear_affine_system(self):
# Just make sure linear system is spelled correctly.
A = np.identity(2)
B = np.array([[0], [1]])
f0 = np.array([[0], [0]])
C = np.array([[0, 1]])
D = [0]
y0 = [0]
system = LinearSystem(A, B, C, D)
context = system.CreateDefaultContext()
self.assertEqual(system.get_input_port(0).size(), 1)
self.assertEqual(context
.get_mutable_continuous_state_vector().size(), 2)
self.assertEqual(system.get_output_port(0).size(), 1)
self.assertTrue((system.A() == A).all())
self.assertTrue((system.B() == B).all())
self.assertTrue((system.f0() == f0).all())
self.assertTrue((system.C() == C).all())
self.assertEqual(system.D(), D)
self.assertEqual(system.y0(), y0)
self.assertEqual(system.time_period(), 0.)
Co = ControllabilityMatrix(system)
self.assertEqual(Co.shape, (2, 2))
self.assertFalse(IsControllable(system))
self.assertFalse(IsControllable(system, 1e-6))
Ob = ObservabilityMatrix(system)
self.assertEqual(Ob.shape, (2, 2))
self.assertFalse(IsObservable(system))
system = AffineSystem(A, B, f0, C, D, y0, .1)
context = system.CreateDefaultContext()
self.assertEqual(system.get_input_port(0).size(), 1)
self.assertEqual(context.get_discrete_state_vector().size(), 2)
self.assertEqual(system.get_output_port(0).size(), 1)
self.assertTrue((system.A() == A).all())
self.assertTrue((system.B() == B).all())
self.assertTrue((system.f0() == f0).all())
self.assertTrue((system.C() == C).all())
self.assertEqual(system.D(), D)
self.assertEqual(system.y0(), y0)
self.assertEqual(system.time_period(), .1)
context.FixInputPort(0, BasicVector([0]))
linearized = Linearize(system, context)
self.assertTrue((linearized.A() == A).all())
taylor = FirstOrderTaylorApproximation(system, context)
self.assertTrue((taylor.y0() == y0).all())
def test_vector_pass_through(self):
model_value = BasicVector([1., 2, 3])
system = PassThrough(model_value.size())
context = system.CreateDefaultContext()
context.FixInputPort(0, model_value)
output = system.AllocateOutput(context)
input_eval = system.EvalVectorInput(context, 0)
compare_value(self, input_eval, model_value)
system.CalcOutput(context, output)
output_value = output.get_vector_data(0)
compare_value(self, output_value, model_value)
def test_abstract_pass_through(self):
model_value = AbstractValue.Make("Hello world")
system = PassThrough(model_value)
context = system.CreateDefaultContext()
context.FixInputPort(0, model_value)
output = system.AllocateOutput(context)
input_eval = system.EvalAbstractInput(context, 0)
compare_value(self, input_eval, model_value)
system.CalcOutput(context, output)
output_value = output.get_data(0)
compare_value(self, output_value, model_value)
def test_saturation(self):
system = Saturation((0., -1., 3.), (1., 2., 4.))
context = system.CreateDefaultContext()
output = system.AllocateOutput(context)
def mytest(input, expected):
context.FixInputPort(0, BasicVector(input))
system.CalcOutput(context, output)
self.assertTrue(np.allclose(output.get_vector_data(
0).CopyToVector(), expected))
mytest((-5., 5., 4.), (0., 2., 4.))
mytest((.4, 0., 3.5), (.4, 0., 3.5))
def test_wrap_to_system(self):
system = WrapToSystem(2)
system.set_interval(1, 1., 2.)
context = system.CreateDefaultContext()
output = system.AllocateOutput(context)
def mytest(input, expected):
context.FixInputPort(0, BasicVector(input))
system.CalcOutput(context, output)
self.assertTrue(np.allclose(output.get_vector_data(
0).CopyToVector(), expected))
mytest((-1.5, 0.5), (-1.5, 1.5))
mytest((.2, .3), (.2, 1.3))
def test_multiplexer(self):
my_vector = MyVector2(data=[1., 2.])
test_cases = [
dict(has_vector=False, mux=Multiplexer(num_scalar_inputs=4),
data=[[5.], [3.], [4.], [2.]]),
dict(has_vector=False, mux=Multiplexer(input_sizes=[2, 3]),
data=[[8., 4.], [3., 6., 9.]]),
dict(has_vector=True, mux=Multiplexer(model_vector=my_vector),
data=[[42.], [3.]]),
]
for case in test_cases:
mux = case['mux']
port_size = sum([len(vec) for vec in case['data']])
self.assertEqual(mux.get_output_port(0).size(), port_size)
context = mux.CreateDefaultContext()
output = mux.AllocateOutput(context)
num_ports = len(case['data'])
self.assertEqual(context.get_num_input_ports(), num_ports)
for j, vec in enumerate(case['data']):
context.FixInputPort(j, BasicVector(vec))
mux.CalcOutput(context, output)
self.assertTrue(
np.allclose(output.get_vector_data(0).get_value(),
[elem for vec in case['data'] for elem in vec]))
if case['has_vector']:
# Check the type matches MyVector2.
value = output.get_vector_data(0)
self.assertTrue(isinstance(value, MyVector2))