verse-lib 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- verse/__init__.py +8 -0
- verse/agents/__init__.py +2 -0
- verse/agents/base_agent.py +93 -0
- verse/agents/example_agent/__init__.py +3 -0
- verse/agents/example_agent/ball_agent.py +43 -0
- verse/agents/example_agent/ballg_agent.py +42 -0
- verse/agents/example_agent/car_agent.py +254 -0
- verse/agents/example_agent/line_agent.py +40 -0
- verse/agents/example_agent/origin_agent.py +183 -0
- verse/agents/example_agent/prarm.json +184 -0
- verse/agents/example_agent/quadrotor_agent.py +249 -0
- verse/agents/example_agent/robot_agent.py +37 -0
- verse/agents/example_agent/sign_agent.py +15 -0
- verse/analysis/__init__.py +6 -0
- verse/analysis/analysis_tree.py +571 -0
- verse/analysis/dryvr.py +320 -0
- verse/analysis/dryvr_disc.py +269 -0
- verse/analysis/incremental.py +402 -0
- verse/analysis/mixmonotone.py +291 -0
- verse/analysis/parsed_wrap/__init__.py +3 -0
- verse/analysis/parsed_wrap/_cache.py +95 -0
- verse/analysis/parsed_wrap/bounded_angular.py +434 -0
- verse/analysis/parsed_wrap/bounded_map.py +53 -0
- verse/analysis/parsed_wrap/bounded_piecewise.py +292 -0
- verse/analysis/parsed_wrap/parser_wrapper.py +454 -0
- verse/analysis/parsed_wrap/sensor_parser.py +582 -0
- verse/analysis/parsed_wrap/wrapper_consts.py +29 -0
- verse/analysis/simulator.py +893 -0
- verse/analysis/verifier.py +1899 -0
- verse/automaton/__init__.py +3 -0
- verse/automaton/guard.py +1348 -0
- verse/automaton/hybrid_automaton.py +34 -0
- verse/automaton/hybrid_io_automaton.py +30 -0
- verse/automaton/reset.py +110 -0
- verse/map/__init__.py +5 -0
- verse/map/example_map/__init__.py +2 -0
- verse/map/example_map/intersection.py +127 -0
- verse/map/example_map/map_tacas.py +262 -0
- verse/map/example_map/simple_map.py +31 -0
- verse/map/example_map/simple_map2.py +188 -0
- verse/map/example_map/simple_map_3d.py +278 -0
- verse/map/lane.py +60 -0
- verse/map/lane_3d.py +78 -0
- verse/map/lane_map.py +120 -0
- verse/map/lane_map_3d.py +293 -0
- verse/map/lane_segment.py +324 -0
- verse/map/lane_segment_3d.py +638 -0
- verse/map/opendrive_parser.py +483 -0
- verse/parser/__init__.py +2 -0
- verse/parser/astunparser.py +989 -0
- verse/parser/parser.py +967 -0
- verse/plotter/__init__.py +23 -0
- verse/plotter/plotter2D.py +2864 -0
- verse/plotter/plotter2D_old.py +223 -0
- verse/plotter/plotter3D.py +147 -0
- verse/plotter/plotter3D_new.py +735 -0
- verse/plotter/plotterStar.py +105 -0
- verse/scenario/__init__.py +2 -0
- verse/scenario/scenario.py +595 -0
- verse/sensor/__init__.py +2 -0
- verse/sensor/base_sensor.py +135 -0
- verse/sensor/base_sensor_stars.py +159 -0
- verse/sensor/example_sensor/__init__.py +0 -0
- verse/sensor/example_sensor/craft_sensor.py +62 -0
- verse/sensor/example_sensor/craft_star_sensor.py +81 -0
- verse/sensor/example_sensor/quadrotor_sensor.py +62 -0
- verse/sensor/example_sensor/single_sensor.py +179 -0
- verse/sensor/example_sensor/thermo_sensor.py +62 -0
- verse/stars/__init__.py +3 -0
- verse/stars/reach_at_star.py +73 -0
- verse/stars/starset.py +889 -0
- verse/stars/startests.py +383 -0
- verse/utils/__init__.py +2 -0
- verse/utils/fixed_points.py +125 -0
- verse/utils/star_diams.py +171 -0
- verse/utils/star_manhattan.py +232 -0
- verse/utils/utils.py +437 -0
- verse_lib-2.0.0.dist-info/METADATA +337 -0
- verse_lib-2.0.0.dist-info/RECORD +82 -0
- verse_lib-2.0.0.dist-info/WHEEL +5 -0
- verse_lib-2.0.0.dist-info/licenses/Licence.txt +16 -0
- verse_lib-2.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
from z3 import *
|
|
2
|
+
from typing import List, Tuple, Dict, Callable, Any
|
|
3
|
+
import ast
|
|
4
|
+
import inspect
|
|
5
|
+
import textwrap
|
|
6
|
+
|
|
7
|
+
def z3num_to_float(x):
|
|
8
|
+
"""Convert Z3 numeric expressions to Python float, handling rationals and epsilon terms."""
|
|
9
|
+
x = simplify(x)
|
|
10
|
+
if isinstance(x, (IntNumRef, RatNumRef, AlgebraicNumRef)):
|
|
11
|
+
s = x.as_string().rstrip('?')
|
|
12
|
+
if '/' in s:
|
|
13
|
+
num, den = s.split('/')
|
|
14
|
+
return float(num) / float(den)
|
|
15
|
+
return float(s)
|
|
16
|
+
s = str(x).replace("epsilon", "0")
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
return float(eval(s))
|
|
20
|
+
except:
|
|
21
|
+
raise ValueError(f"Cannot parse Z3 numeric expression: {x}")
|
|
22
|
+
|
|
23
|
+
def bounded_piecewise_z3(conditions_outputs, input_bounds):
|
|
24
|
+
"""
|
|
25
|
+
Evaluate a piecewise function over bounded inputs using Z3 optimization.
|
|
26
|
+
For each branch, finds the tightest input bounds satisfying that branch's condition.
|
|
27
|
+
|
|
28
|
+
Note
|
|
29
|
+
-----
|
|
30
|
+
Current implementation evaluates each elif condition independently without adding negations of earlier conditions.
|
|
31
|
+
This is sound for mutually exclusive branches like the one given in this file but overapproximates for overlapping elif conditions.
|
|
32
|
+
For Python semantics (first true branch wins), each elif should be conjoint with `and not [earlier conditions]`
|
|
33
|
+
This limitation is acceptable for disjoint conditions but should be fixed for general correctness.
|
|
34
|
+
"""
|
|
35
|
+
results = []
|
|
36
|
+
var_names = list(input_bounds.keys())
|
|
37
|
+
z3_vars = {name: Real(name) for name in var_names}
|
|
38
|
+
negated_conditions = []
|
|
39
|
+
|
|
40
|
+
for _, (condition, output_func) in enumerate(conditions_outputs):
|
|
41
|
+
if condition is None:
|
|
42
|
+
branch_condition = And(negated_conditions) if negated_conditions else BoolVal(True)
|
|
43
|
+
else:
|
|
44
|
+
# NOTE: For mutually exclusive conditions, this is correct.
|
|
45
|
+
# TODO: For overlapping elif conditions, conjoin with negated_conditions:
|
|
46
|
+
# branch_condition = And(condition, *negated_conditions) if negated_conditions else condition
|
|
47
|
+
branch_condition = condition
|
|
48
|
+
negated_conditions.append(Not(condition))
|
|
49
|
+
|
|
50
|
+
constrained_bounds = {}
|
|
51
|
+
for name in var_names:
|
|
52
|
+
opt_min = Optimize()
|
|
53
|
+
for bound_name, (min_val, max_val) in input_bounds.items():
|
|
54
|
+
opt_min.add(z3_vars[bound_name] >= min_val)
|
|
55
|
+
opt_min.add(z3_vars[bound_name] <= max_val)
|
|
56
|
+
opt_min.add(branch_condition)
|
|
57
|
+
obj = opt_min.minimize(z3_vars[name])
|
|
58
|
+
|
|
59
|
+
min_val = None
|
|
60
|
+
if opt_min.check() == sat:
|
|
61
|
+
inf = opt_min.lower(obj)
|
|
62
|
+
min_val = z3num_to_float(inf)
|
|
63
|
+
|
|
64
|
+
opt_max = Optimize()
|
|
65
|
+
for bound_name, (min_val_b, max_val_b) in input_bounds.items():
|
|
66
|
+
opt_max.add(z3_vars[bound_name] >= min_val_b)
|
|
67
|
+
opt_max.add(z3_vars[bound_name] <= max_val_b)
|
|
68
|
+
opt_max.add(branch_condition)
|
|
69
|
+
obj = opt_max.maximize(z3_vars[name])
|
|
70
|
+
|
|
71
|
+
max_val = None
|
|
72
|
+
if opt_max.check() == sat:
|
|
73
|
+
sup = opt_max.upper(obj)
|
|
74
|
+
max_val = z3num_to_float(sup)
|
|
75
|
+
|
|
76
|
+
if min_val is not None and max_val is not None:
|
|
77
|
+
constrained_bounds[name] = (min_val, max_val)
|
|
78
|
+
|
|
79
|
+
if len(constrained_bounds) == len(var_names):
|
|
80
|
+
results.append((constrained_bounds, output_func))
|
|
81
|
+
|
|
82
|
+
return results
|
|
83
|
+
|
|
84
|
+
def parse_piecewise_function(func: Callable) -> List[Tuple[Any, Any]]:
|
|
85
|
+
"""
|
|
86
|
+
Parse a Python piecewise function (if-elif-else) into conditions_outputs format.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
List of (condition_z3_expr, output_expr) Tuples
|
|
90
|
+
where output_expr is either a constant or an AST expression string
|
|
91
|
+
"""
|
|
92
|
+
source = inspect.getsource(func)
|
|
93
|
+
source = textwrap.dedent(source)
|
|
94
|
+
tree = ast.parse(source)
|
|
95
|
+
|
|
96
|
+
func_def = tree.body[0]
|
|
97
|
+
# NOTE: Verify that func is a funcion
|
|
98
|
+
if not isinstance(func_def, ast.FunctionDef):
|
|
99
|
+
raise ValueError("Input must be a function definition")
|
|
100
|
+
|
|
101
|
+
if_stmt = None
|
|
102
|
+
for node in func_def.body:
|
|
103
|
+
if isinstance(node, ast.If):
|
|
104
|
+
if_stmt = node
|
|
105
|
+
break
|
|
106
|
+
|
|
107
|
+
# NOTE: Verify that func has an if statement. This does not necessarily mean that it is piecewise, so consider adding further checks.
|
|
108
|
+
if if_stmt is None:
|
|
109
|
+
raise ValueError("Function must contain an if statement")
|
|
110
|
+
|
|
111
|
+
arg_names = [arg.arg for arg in func_def.args.args]
|
|
112
|
+
z3_vars = {name: Real(name) for name in arg_names}
|
|
113
|
+
|
|
114
|
+
conditions_outputs = []
|
|
115
|
+
|
|
116
|
+
# NOTE: Start loop at the first (should be only) `if`
|
|
117
|
+
current = if_stmt
|
|
118
|
+
while current is not None:
|
|
119
|
+
condition_z3 = ast_to_z3_condition(current.test, z3_vars)
|
|
120
|
+
output_expr = extract_return_value(current.body)
|
|
121
|
+
conditions_outputs.append((condition_z3, output_expr))
|
|
122
|
+
|
|
123
|
+
if len(current.orelse) == 1 and isinstance(current.orelse[0], ast.If):
|
|
124
|
+
current = current.orelse[0]
|
|
125
|
+
elif len(current.orelse) > 0:
|
|
126
|
+
output_expr = extract_return_value(current.orelse)
|
|
127
|
+
conditions_outputs.append((None, output_expr))
|
|
128
|
+
current = None
|
|
129
|
+
else:
|
|
130
|
+
# print(f'Warning, no else detected in piecewise function.')
|
|
131
|
+
current = None
|
|
132
|
+
|
|
133
|
+
return conditions_outputs
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def ast_to_z3_condition(node: ast.expr, z3_vars: Dict[str, Any]) -> Any:
|
|
137
|
+
"""
|
|
138
|
+
Convert an AST condition expression to a Z3 boolean expression.
|
|
139
|
+
"""
|
|
140
|
+
if isinstance(node, ast.Compare):
|
|
141
|
+
left = ast_to_z3_expr(node.left, z3_vars)
|
|
142
|
+
|
|
143
|
+
result = None
|
|
144
|
+
for op, comparator in zip(node.ops, node.comparators):
|
|
145
|
+
right = ast_to_z3_expr(comparator, z3_vars)
|
|
146
|
+
|
|
147
|
+
if isinstance(op, ast.Gt):
|
|
148
|
+
comparison = left > right
|
|
149
|
+
elif isinstance(op, ast.GtE):
|
|
150
|
+
comparison = left >= right
|
|
151
|
+
elif isinstance(op, ast.Lt):
|
|
152
|
+
comparison = left < right
|
|
153
|
+
elif isinstance(op, ast.LtE):
|
|
154
|
+
comparison = left <= right
|
|
155
|
+
elif isinstance(op, ast.Eq):
|
|
156
|
+
comparison = left == right
|
|
157
|
+
elif isinstance(op, ast.NotEq):
|
|
158
|
+
comparison = left != right
|
|
159
|
+
else:
|
|
160
|
+
raise ValueError(f"Unsupported comparison operator: {op}")
|
|
161
|
+
|
|
162
|
+
result = comparison if result is None else And(result, comparison)
|
|
163
|
+
|
|
164
|
+
return result
|
|
165
|
+
|
|
166
|
+
elif isinstance(node, ast.BoolOp):
|
|
167
|
+
operands = [ast_to_z3_condition(val, z3_vars) for val in node.values]
|
|
168
|
+
|
|
169
|
+
if isinstance(node.op, ast.And):
|
|
170
|
+
return And(operands)
|
|
171
|
+
elif isinstance(node.op, ast.Or):
|
|
172
|
+
return Or(operands)
|
|
173
|
+
else:
|
|
174
|
+
raise ValueError(f"Unsupported boolean operator: {node.op}")
|
|
175
|
+
|
|
176
|
+
elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not):
|
|
177
|
+
return Not(ast_to_z3_condition(node.operand, z3_vars))
|
|
178
|
+
|
|
179
|
+
else:
|
|
180
|
+
raise ValueError(f"Unsupported condition type: {type(node)}")
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def ast_to_z3_expr(node: ast.expr, z3_vars: Dict[str, Any]) -> Any:
|
|
184
|
+
"""
|
|
185
|
+
Convert an AST expression to a Z3 expression.
|
|
186
|
+
"""
|
|
187
|
+
if isinstance(node, ast.Name):
|
|
188
|
+
if node.id in z3_vars:
|
|
189
|
+
return z3_vars[node.id]
|
|
190
|
+
else:
|
|
191
|
+
raise ValueError(f"Unknown variable: {node.id}")
|
|
192
|
+
|
|
193
|
+
elif isinstance(node, ast.Constant):
|
|
194
|
+
return node.value
|
|
195
|
+
|
|
196
|
+
elif isinstance(node, ast.Num):
|
|
197
|
+
# NOTE: Python < 3.8 compatibility - ast.Num is deprecated in favor of ast.Constant
|
|
198
|
+
return node.n
|
|
199
|
+
|
|
200
|
+
elif isinstance(node, ast.UnaryOp):
|
|
201
|
+
operand = ast_to_z3_expr(node.operand, z3_vars)
|
|
202
|
+
|
|
203
|
+
if isinstance(node.op, ast.UAdd):
|
|
204
|
+
return +operand
|
|
205
|
+
elif isinstance(node.op, ast.USub):
|
|
206
|
+
return -operand
|
|
207
|
+
else:
|
|
208
|
+
raise ValueError(f"Unsupported unary operator: {node.op}")
|
|
209
|
+
|
|
210
|
+
elif isinstance(node, ast.BinOp):
|
|
211
|
+
left = ast_to_z3_expr(node.left, z3_vars)
|
|
212
|
+
right = ast_to_z3_expr(node.right, z3_vars)
|
|
213
|
+
|
|
214
|
+
if isinstance(node.op, ast.Add):
|
|
215
|
+
return left + right
|
|
216
|
+
elif isinstance(node.op, ast.Sub):
|
|
217
|
+
return left - right
|
|
218
|
+
elif isinstance(node.op, ast.Mult):
|
|
219
|
+
return left * right
|
|
220
|
+
elif isinstance(node.op, ast.Div):
|
|
221
|
+
return left / right
|
|
222
|
+
else:
|
|
223
|
+
raise ValueError(f"Unsupported binary operator: {node.op}")
|
|
224
|
+
|
|
225
|
+
else:
|
|
226
|
+
raise ValueError(f"Unsupported expression type: {type(node)}")
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def extract_return_value(body: List[ast.stmt]) -> Any:
|
|
230
|
+
"""
|
|
231
|
+
Extract the return value from a function body.
|
|
232
|
+
Returns either a constant value or unparsed AST expression string.
|
|
233
|
+
"""
|
|
234
|
+
for node in body:
|
|
235
|
+
if isinstance(node, ast.Return):
|
|
236
|
+
if isinstance(node.value, ast.Constant):
|
|
237
|
+
return node.value.value
|
|
238
|
+
elif isinstance(node.value, ast.Num):
|
|
239
|
+
# NOTE: Python < 3.8 compatibility
|
|
240
|
+
return node.value.n
|
|
241
|
+
else:
|
|
242
|
+
return ast.unparse(node.value)
|
|
243
|
+
|
|
244
|
+
raise ValueError("No return statement found")
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def create_output_func(output_expr: str, arg_names: List[str]) -> Callable:
|
|
248
|
+
"""
|
|
249
|
+
Create a lambda function from an output expression string.
|
|
250
|
+
Takes a dict of variable bounds and evaluates the expression.
|
|
251
|
+
"""
|
|
252
|
+
def output_func(var_dict):
|
|
253
|
+
expr = output_expr
|
|
254
|
+
for name in arg_names:
|
|
255
|
+
if name in var_dict:
|
|
256
|
+
expr = expr.replace(name, str(var_dict[name]))
|
|
257
|
+
|
|
258
|
+
return eval(expr)
|
|
259
|
+
|
|
260
|
+
return output_func
|
|
261
|
+
|
|
262
|
+
if __name__ == "__main__":
|
|
263
|
+
def vis_sensor_piecewise(psi, phi):
|
|
264
|
+
"""Example piecewise function for visibility sensor."""
|
|
265
|
+
if psi >= 0 and psi <= phi:
|
|
266
|
+
return -1
|
|
267
|
+
elif psi < 0 and psi >= -phi:
|
|
268
|
+
return 1
|
|
269
|
+
elif psi > phi:
|
|
270
|
+
return something(psi, 2)
|
|
271
|
+
else:
|
|
272
|
+
return 2
|
|
273
|
+
|
|
274
|
+
conditions_outputs = parse_piecewise_function(vis_sensor_piecewise)
|
|
275
|
+
|
|
276
|
+
input_bounds = {
|
|
277
|
+
'psi': [-1.1, 2],
|
|
278
|
+
'phi': [1, 1]
|
|
279
|
+
}
|
|
280
|
+
print(f'Condtions-outputs pairs: {conditions_outputs}')
|
|
281
|
+
|
|
282
|
+
z3_vars = {'psi': Real('psi'), 'phi': Real('phi')}
|
|
283
|
+
results = bounded_piecewise_z3(conditions_outputs, input_bounds)
|
|
284
|
+
|
|
285
|
+
for constrained_bounds, output_func in results:
|
|
286
|
+
print(f"Constrained bounds: {constrained_bounds}")
|
|
287
|
+
print(f"Output function: {output_func}")
|
|
288
|
+
|
|
289
|
+
# NOTE: The most complex part requiring some attention is the Z3 optimization loop in
|
|
290
|
+
# bounded_piecewise_z3(). The function creates separate Optimize() instances for min/max
|
|
291
|
+
# per variable per branch, which can be slow for complex conditions or many variables.
|
|
292
|
+
# Consider caching or refactoring if performance becomes an issue.
|