-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_parser.py
311 lines (247 loc) · 10.4 KB
/
test_parser.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# Copyright (c) 2023, Alessandro Abate, Alec Edwards, Andrea Peruffo
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pylint: disable=not-callable
import unittest
import dreal
import z3
from cvc5 import pythonic as cvpy
import sympy
import numpy as np
from fossil.parser import (
parse_expression,
parse_domain,
parse_dynamical_system_to_numpy,
SymbolicParsingError,
DomainParsingError,
)
from fossil import domains
def compare_without_whitespace(str1, str2):
return str1.replace(" ", "").replace("\t", "").replace("\n", "") == str2.replace(
" ", ""
).replace("\t", "").replace("\n", "")
class TestParserZ3(unittest.TestCase):
def test_z3_integer(self):
s = "-5"
result = parse_expression(s)
self.assertEqual(result, -5)
def test_z3_decimal(self):
s = "1.5"
result = parse_expression(s)
self.assertEqual(result, 1.5)
def test_z3_variable(self):
s = "x3"
result = parse_expression(s)
self.assertIsInstance(result, z3.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s))
def test_z3_negative_variable(self):
s = "-x3"
result = parse_expression(s)
self.assertIsInstance(result, z3.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s))
def test_z3_arithmetic(self):
s = "x0 + x1 * 2 - 3 / x2"
result = parse_expression(s)
self.assertIsInstance(result, z3.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s))
def test_z3_complex(self):
s = "1.5*x1+x2+ x3*x2*x1**2"
s_compare = "3/2*x1+x2+x3*x2*x1**2"
result = parse_expression(s, output="z3")
self.assertIsInstance(result, z3.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s_compare))
def test_z3_control(self):
s = "1.5*x1+2*x2+ x3*x2*x1**2 + u0"
s_compare = "3/2*x1+2*x2+ x3*x2*x1**2 + u0"
result = parse_expression(s, output="z3")
self.assertIsInstance(result, z3.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s_compare))
# def test_z3_function(self):
# s = "If(x0, x1, x2)"
# result = parse_expression(s)
# self.assertIsInstance(result, z3.BoolRef)
def test_invalid_expression(self):
s = "invalid_expr"
with self.assertRaises(SymbolicParsingError):
parse_expression(s)
def test_invalid_output_format(self):
s = "x0"
with self.assertRaises(ValueError):
parse_expression(s, output="invalid_output")
class TestParserCVC(unittest.TestCase):
def test_cvpy_integer(self):
s = "-5"
result = parse_expression(s, output="cvc5")
self.assertEqual(result, -5)
def test_cvpy_decimal(self):
s = "1.5"
result = parse_expression(s, output="cvc5")
self.assertEqual(result, 1.5)
def test_cvpy_variable(self):
s = "x3"
result = parse_expression(s, output="cvc5")
self.assertIsInstance(result, cvpy.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s))
def test_cvpy_negative_variable(self):
s = "-x3"
result = parse_expression(s, output="cvc5")
self.assertIsInstance(result, cvpy.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s))
def test_cvpy_arithmetic(self):
s = "x0 + x1 * 2 - 3 / x2"
result = parse_expression(s, output="cvc5")
self.assertIsInstance(result, cvpy.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s))
def test_cvpy_complex(self):
s = "1.5*x1+x2+ x3*x2*x1**2"
s_compare = "3/2*x1+x2+x3*x2*x1**2"
result = parse_expression(s, output="cvc5")
self.assertIsInstance(result, cvpy.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s_compare))
def test_cvpy_control(self):
s = "1.5*x1+2*x2+ x3*x2*x1**2 + u0"
s_compare = "3/2*x1+2*x2+ x3*x2*x1**2 + u0"
result = parse_expression(s, output="cvc5")
self.assertIsInstance(result, cvpy.ArithRef)
self.assertTrue(compare_without_whitespace(str(result), s_compare))
# def test_cvpy_function(self):
# s = "If(x0, x1, x2)"
# result = parse_expression(s, output="cvc5")
# self.assertIsInstance(result, cvpy.BoolRef)
def test_invalid_expression(self):
s = "invalid_expr"
with self.assertRaises(SymbolicParsingError):
parse_expression(s, output="cvc5")
def test_invalid_output_format(self):
s = "x0"
with self.assertRaises(ValueError):
parse_expression(s, output="invalid_output")
# Sympy and Dreal rearrange expressions internally, so we can't compare the strings
class TestParserDreal(unittest.TestCase):
def test_dreal_integer(self):
s = "5"
result = parse_expression(s, output="dreal")
self.assertEqual(result, 5)
def test_dreal_decimal(self):
s = "1.5"
result = parse_expression(s, output="dreal")
self.assertEqual(result, 1.5)
def test_dreal_variable(self):
s = "x3"
result = parse_expression(s, output="dreal")
self.assertIsInstance(result, dreal.Expression)
def test_dreal_negative_variable(self):
s = "-x3"
result = parse_expression(s, output="dreal")
self.assertIsInstance(result, dreal.Expression)
def test_dreal_arithmetic(self):
s = "x0 + x1 * 2 - 3 / x2"
result = parse_expression(s, output="dreal")
self.assertIsInstance(result, dreal.Expression)
def test_dreal_complex(self):
s = "1.5*x1+x2+ sin(x3*x2*x1)"
result = parse_expression(s, output="dreal")
self.assertIsInstance(result, dreal.Expression)
def test_dreal_control(self):
s = "1.5*x1+x2+ sin(x3*x2*x1) + u0"
result = parse_expression(s, output="dreal")
self.assertIsInstance(result, dreal.Expression)
def test_dreal_function(self):
s = "sin(x1)"
result = parse_expression(s, output="dreal")
self.assertIsInstance(result, dreal.Expression)
class TestParserSympy(unittest.TestCase):
def test_sympy_integer(self):
s = "-5"
result = parse_expression(s, output="sympy")
self.assertEqual(result, -5)
def test_sympy_decimal(self):
s = "1.5"
result = parse_expression(s, output="sympy")
self.assertEqual(result, 1.5)
def test_sympy_variable(self):
s = "x3"
result = parse_expression(s, output="sympy")
self.assertIsInstance(result, sympy.Symbol)
self.assertTrue(compare_without_whitespace(str(result), s))
def test_sympy_negative_variable(self):
s = "-x3"
result = parse_expression(s, output="sympy")
self.assertIsInstance(
result, sympy.Mul
) # Since -x3 is a multiplication of -1 and x3
self.assertTrue(compare_without_whitespace(str(result), s))
def test_sympy_arithmetic(self):
s = "x0 + 2 * x1 - 3 / x2"
result = parse_expression(s, output="sympy")
self.assertIsInstance(
result, sympy.Add
) # Since this is an arithmetic expression
def test_sympy_complex(self):
s = "1.5*x1+x2+ x3*x2*x1**2"
s_compare = "3/2*x1+x2+x3*x2*x1**2"
result = parse_expression(s, output="sympy")
self.assertIsInstance(result, sympy.Add)
def test_sympy_function(self):
s = "sin(x1)"
result = parse_expression(s, output="sympy")
self.assertIsInstance(result, sympy.sin)
self.assertTrue(compare_without_whitespace(str(result), s))
class TestDomainsParser(unittest.TestCase):
def testSphere(self):
s = "Sphere([3.0, 0], 3)"
result = parse_domain(s)
self.assertIsInstance(result, domains.Sphere)
self.assertEqual(result.radius, 3)
self.assertEqual(result.centre, [3.0, 0])
def testRectangle(self):
s = "Rectangle([0, 1.0], [2.0, 3.0])"
result = parse_domain(s)
self.assertIsInstance(result, domains.Rectangle)
self.assertEqual(result.lower_bounds, [0, 1.0])
self.assertEqual(result.upper_bounds, [2.0, 3.0])
def testTorus(self):
s = "Torus([0, 1.0], 2.0, 1.0)"
result = parse_domain(s)
self.assertIsInstance(result, domains.Torus)
self.assertEqual(result.centre, [0, 1.0])
self.assertEqual(result.inner_radius, 1.0)
self.assertEqual(result.outer_radius, 2.0)
def testInvalidDomain(self):
s = "InvalidDomain([0, 1.0], 1.0, 2.0)"
with self.assertRaises(DomainParsingError):
parse_domain(s)
class TestParseDynamicalSystemToNumpy(unittest.TestCase):
def test_no_controls(self):
dynamical_system = ["x0 + 2*x1", "x1 - 3*x0"]
funcs = parse_dynamical_system_to_numpy(dynamical_system)
self.assertEqual(len(funcs), 2)
# Evaluate the parsed functions at x0=1, x1=2
result1 = funcs[0](np.array([1, 2]))
result2 = funcs[1](np.array([1, 2]))
self.assertAlmostEqual(result1, 5)
self.assertAlmostEqual(result2, -1)
def test_with_controls(self):
# This test is currently broken because the parser interprets the final expression as
# "x1 - 3*x0 - u1". I'm not sure why atm.
dynamical_system = ["x0 + 2*x1 + u0", "x1 - (3*x0) + u1"]
funcs = parse_dynamical_system_to_numpy(dynamical_system)
self.assertEqual(len(funcs), 2)
# Evaluate the parsed functions at x0=1, x1=2 and u0=1, u1=-1
result1 = funcs[0](np.array([1, 2]), np.array([1, -1]))
result2 = funcs[1](np.array([1, 2]), np.array([1, -1]))
self.assertAlmostEqual(result1, 6)
self.assertAlmostEqual(result2, -2)
def test_mixed_symbols(self):
dynamical_system = ["x1**2 + u0", "x0*x1 + u1"]
funcs = parse_dynamical_system_to_numpy(dynamical_system)
self.assertEqual(len(funcs), 2)
# Evaluate the parsed functions at x0=2, x1=3 and u0=1, u1=-1
result1 = funcs[0](np.array([2, 3]), np.array([1, -1]))
result2 = funcs[1](np.array([2, 3]), np.array([1, -1]))
self.assertAlmostEqual(result1, 10) # 3^2 + 1
self.assertAlmostEqual(result2, 5) # 2*3 - 1
if __name__ == "__main__":
unittest.main()