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,95 @@
|
|
|
1
|
+
"""On-disk cache for sensor-program bounds.
|
|
2
|
+
|
|
3
|
+
Verification calls a sensor many times on the same input boxes, so computed bounds are cached across runs.
|
|
4
|
+
A cache entry is keyed by everything the result depends on: the source code of the functions involved, the
|
|
5
|
+
input bounds, every option that changes the computation, and the Verse version. Editing a sensor function
|
|
6
|
+
therefore never returns bounds computed for its old body.
|
|
7
|
+
|
|
8
|
+
The cache lives in ``$VERSE_CACHE_DIR`` if set, otherwise in the user cache directory
|
|
9
|
+
(``~/Library/Caches/verse`` on macOS, ``$XDG_CACHE_HOME/verse`` or ``~/.cache/verse`` elsewhere).
|
|
10
|
+
"""
|
|
11
|
+
import hashlib
|
|
12
|
+
import inspect
|
|
13
|
+
import os
|
|
14
|
+
import pickle
|
|
15
|
+
import sys
|
|
16
|
+
import tempfile
|
|
17
|
+
import textwrap
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Any, Callable, Optional
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def cache_root() -> Path:
|
|
23
|
+
env = os.environ.get("VERSE_CACHE_DIR")
|
|
24
|
+
if env:
|
|
25
|
+
return Path(env).expanduser()
|
|
26
|
+
if sys.platform == "darwin":
|
|
27
|
+
return Path.home() / "Library" / "Caches" / "verse"
|
|
28
|
+
return Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache")) / "verse"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def cache_dir(kind: str) -> Path:
|
|
32
|
+
d = cache_root() / "parsed_wrap" / kind
|
|
33
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
34
|
+
return d
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def function_fingerprint(func: Optional[Callable]) -> str:
|
|
38
|
+
"""Name and source of a function (bytecode and constants when the source is unavailable)."""
|
|
39
|
+
if func is None:
|
|
40
|
+
return "None"
|
|
41
|
+
if isinstance(func, str):
|
|
42
|
+
return func
|
|
43
|
+
try:
|
|
44
|
+
src = textwrap.dedent(inspect.getsource(func))
|
|
45
|
+
except (OSError, TypeError):
|
|
46
|
+
code = getattr(func, "__code__", None)
|
|
47
|
+
src = repr((code.co_code, code.co_consts, code.co_names)) if code is not None else repr(func)
|
|
48
|
+
return f"{getattr(func, '__qualname__', repr(func))}\n{src}"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def object_fingerprint(obj: Any) -> str:
|
|
52
|
+
"""Stable fingerprint of a map or mode; falls back to the type name when the object cannot be pickled."""
|
|
53
|
+
if obj is None:
|
|
54
|
+
return "None"
|
|
55
|
+
try:
|
|
56
|
+
return hashlib.sha256(pickle.dumps(obj, protocol=4)).hexdigest()
|
|
57
|
+
except Exception:
|
|
58
|
+
return f"{type(obj).__module__}.{type(obj).__qualname__}:{obj!r}"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def make_key(*parts: Any) -> str:
|
|
62
|
+
from verse import __version__
|
|
63
|
+
|
|
64
|
+
h = hashlib.sha256()
|
|
65
|
+
for part in (__version__,) + parts:
|
|
66
|
+
h.update(repr(part).encode())
|
|
67
|
+
h.update(b"\x00")
|
|
68
|
+
return h.hexdigest()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def load(kind: str, key: str):
|
|
72
|
+
path = cache_dir(kind) / f"{key}.pkl"
|
|
73
|
+
if not path.exists():
|
|
74
|
+
return None
|
|
75
|
+
try:
|
|
76
|
+
with open(path, "rb") as f:
|
|
77
|
+
return pickle.load(f)
|
|
78
|
+
except Exception:
|
|
79
|
+
return None # unreadable or partially written entry: recompute
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def store(kind: str, key: str, value) -> None:
|
|
83
|
+
d = cache_dir(kind)
|
|
84
|
+
# write to a temporary file and rename, so parallel workers never read a partial entry
|
|
85
|
+
fd, tmp = tempfile.mkstemp(dir=d, suffix=".tmp")
|
|
86
|
+
with os.fdopen(fd, "wb") as f:
|
|
87
|
+
pickle.dump(value, f)
|
|
88
|
+
os.replace(tmp, d / f"{key}.pkl")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def clear(kind: str) -> None:
|
|
92
|
+
d = cache_root() / "parsed_wrap" / kind
|
|
93
|
+
if d.exists():
|
|
94
|
+
for f in d.glob("*.pkl"):
|
|
95
|
+
f.unlink()
|
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from scipy.optimize import differential_evolution, minimize
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
|
|
5
|
+
def wrap_angle(angle):
|
|
6
|
+
"""Wrap angle to [-pi, pi]."""
|
|
7
|
+
if angle % (2 * np.pi) == np.pi:
|
|
8
|
+
return np.pi
|
|
9
|
+
return (angle + np.pi) % (2 * np.pi) - np.pi
|
|
10
|
+
|
|
11
|
+
def angle_in_wrapped_interval(angle, ang_min, ang_max):
|
|
12
|
+
"""
|
|
13
|
+
Returns True if 'angle' ∈ [ang_min, ang_max] with wrap-around.
|
|
14
|
+
All angles should be in [-π, π).
|
|
15
|
+
"""
|
|
16
|
+
angle = wrap_angle(angle)
|
|
17
|
+
ang_min = wrap_angle(ang_min)
|
|
18
|
+
ang_max = wrap_angle(ang_max)
|
|
19
|
+
|
|
20
|
+
if ang_min <= ang_max:
|
|
21
|
+
return ang_min <= angle <= ang_max
|
|
22
|
+
else:
|
|
23
|
+
return angle >= ang_min or angle <= ang_max
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def projection_bounds_general(theta, psi, eps_theta, axis):
|
|
27
|
+
"""
|
|
28
|
+
Compute all candidate projections P for the given axis.
|
|
29
|
+
axis ∈ {"x", "y", "z"}.
|
|
30
|
+
"""
|
|
31
|
+
psi_min = wrap_angle(psi - eps_theta)
|
|
32
|
+
psi_max = wrap_angle(psi + eps_theta)
|
|
33
|
+
psi_candidates = [psi_min, psi_max]
|
|
34
|
+
|
|
35
|
+
if axis in ("x", "y"):
|
|
36
|
+
if angle_in_wrapped_interval(0.0, psi_min, psi_max):
|
|
37
|
+
psi_candidates.append(0)
|
|
38
|
+
if angle_in_wrapped_interval(np.pi, psi_min, psi_max):
|
|
39
|
+
psi_candidates.append(np.pi)
|
|
40
|
+
elif axis == "z":
|
|
41
|
+
if angle_in_wrapped_interval(np.pi/2, psi_min, psi_max):
|
|
42
|
+
psi_candidates.append(np.pi/2)
|
|
43
|
+
if angle_in_wrapped_interval(-np.pi/2, psi_min, psi_max):
|
|
44
|
+
psi_candidates.append(-np.pi/2)
|
|
45
|
+
|
|
46
|
+
theta_min = wrap_angle(theta - eps_theta)
|
|
47
|
+
theta_max = wrap_angle(theta + eps_theta)
|
|
48
|
+
theta_candidates = [theta_min, theta_max]
|
|
49
|
+
|
|
50
|
+
if axis in ("x", "y"):
|
|
51
|
+
if angle_in_wrapped_interval(np.pi/2, theta_min, theta_max):
|
|
52
|
+
theta_candidates.append(np.pi/2)
|
|
53
|
+
if angle_in_wrapped_interval(-np.pi/2, theta_min, theta_max):
|
|
54
|
+
theta_candidates.append(-np.pi/2)
|
|
55
|
+
if axis == "x":
|
|
56
|
+
if angle_in_wrapped_interval(0, theta_min, theta_max):
|
|
57
|
+
theta_candidates.append(0)
|
|
58
|
+
if angle_in_wrapped_interval(np.pi, theta_min, theta_max):
|
|
59
|
+
theta_candidates.append(np.pi)
|
|
60
|
+
else:
|
|
61
|
+
theta_candidates = [0.0]
|
|
62
|
+
|
|
63
|
+
P_values = []
|
|
64
|
+
for psi_val in psi_candidates:
|
|
65
|
+
cos_psi = np.cos(psi_val)
|
|
66
|
+
sin_psi = np.sin(psi_val)
|
|
67
|
+
for theta_val in theta_candidates:
|
|
68
|
+
cos_theta = np.cos(theta_val)
|
|
69
|
+
sin_theta = np.sin(theta_val)
|
|
70
|
+
if axis == "x":
|
|
71
|
+
P = cos_psi * cos_theta
|
|
72
|
+
elif axis == "y":
|
|
73
|
+
P = cos_psi * sin_theta
|
|
74
|
+
elif axis == "z":
|
|
75
|
+
P = sin_psi
|
|
76
|
+
else:
|
|
77
|
+
raise ValueError("Axis must be 'x', 'y', or 'z'")
|
|
78
|
+
P_values.append(P)
|
|
79
|
+
|
|
80
|
+
return np.array(P_values)
|
|
81
|
+
|
|
82
|
+
def point_error_general(xyz, eps_r, eps_theta, axis):
|
|
83
|
+
x, y, z = xyz
|
|
84
|
+
rho = np.sqrt(x**2 + y**2 + z**2)
|
|
85
|
+
theta = np.arctan2(y, x)
|
|
86
|
+
psi = np.arctan2(z, np.sqrt(x**2 + y**2))
|
|
87
|
+
|
|
88
|
+
cos_psi = np.cos(psi)
|
|
89
|
+
sin_psi = np.sin(psi)
|
|
90
|
+
cos_theta = np.cos(theta)
|
|
91
|
+
sin_theta = np.sin(theta)
|
|
92
|
+
|
|
93
|
+
if axis == "x":
|
|
94
|
+
P_true = cos_psi * cos_theta
|
|
95
|
+
elif axis == "y":
|
|
96
|
+
P_true = cos_psi * sin_theta
|
|
97
|
+
elif axis == "z":
|
|
98
|
+
P_true = sin_psi
|
|
99
|
+
|
|
100
|
+
P_vals = projection_bounds_general(theta, psi, eps_theta, axis)
|
|
101
|
+
|
|
102
|
+
sensed_max = max(
|
|
103
|
+
(rho + eps_r)*P if P >= 0 else (rho - eps_r)*P
|
|
104
|
+
for P in P_vals
|
|
105
|
+
)
|
|
106
|
+
sensed_min = min(
|
|
107
|
+
(rho - eps_r)*P if P >= 0 else (rho + eps_r)*P
|
|
108
|
+
for P in P_vals
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
e_max = sensed_max - rho * P_true
|
|
112
|
+
e_min = sensed_min - rho * P_true
|
|
113
|
+
|
|
114
|
+
return e_max, e_min
|
|
115
|
+
|
|
116
|
+
# --- Outer box optimizer ---
|
|
117
|
+
|
|
118
|
+
def error_max_obj(xyz, eps_r, eps_theta, axis):
|
|
119
|
+
"""Objective for maximizing error (negated for minimization)."""
|
|
120
|
+
e_max, _ = point_error_general(xyz, eps_r, eps_theta, axis)
|
|
121
|
+
return -e_max
|
|
122
|
+
|
|
123
|
+
def error_min_obj(xyz, eps_r, eps_theta, axis):
|
|
124
|
+
"""Objective for minimizing error."""
|
|
125
|
+
_, e_min = point_error_general(xyz, eps_r, eps_theta, axis)
|
|
126
|
+
return e_min
|
|
127
|
+
|
|
128
|
+
def box_extreme_error(bounds, eps_r, eps_theta, axis) -> Tuple[float, float]:
|
|
129
|
+
"""
|
|
130
|
+
Compute extreme error bounds using differential evolution optimization.
|
|
131
|
+
|
|
132
|
+
Returns: e_max, e_min
|
|
133
|
+
Args:
|
|
134
|
+
bounds: [(x_min, x_max), (y_min, y_max), (z_min, z_max)]
|
|
135
|
+
axis: "x", "y", or "z"
|
|
136
|
+
"""
|
|
137
|
+
res_max = differential_evolution(
|
|
138
|
+
error_max_obj, bounds=bounds, args=(eps_r, eps_theta, axis),
|
|
139
|
+
updating="deferred", polish=True
|
|
140
|
+
)
|
|
141
|
+
e_k_max = -res_max.fun
|
|
142
|
+
|
|
143
|
+
res_min = differential_evolution(
|
|
144
|
+
error_min_obj, bounds=bounds, args=(eps_r, eps_theta, axis),
|
|
145
|
+
updating="deferred", polish=True
|
|
146
|
+
)
|
|
147
|
+
e_k_min = res_min.fun
|
|
148
|
+
|
|
149
|
+
return e_k_max, e_k_min
|
|
150
|
+
|
|
151
|
+
def angular_bounds_rectangle(x_bounds, y_bounds):
|
|
152
|
+
"""
|
|
153
|
+
Compute angular bounds for vectors pointing from rectangle to origin.
|
|
154
|
+
"""
|
|
155
|
+
x_min, x_max = x_bounds
|
|
156
|
+
y_min, y_max = y_bounds
|
|
157
|
+
x_min, x_max = -x_max, -x_min
|
|
158
|
+
y_min, y_max = -y_max, -y_min
|
|
159
|
+
|
|
160
|
+
corners = [
|
|
161
|
+
(x_min, y_min),
|
|
162
|
+
(x_min, y_max),
|
|
163
|
+
(x_max, y_min),
|
|
164
|
+
(x_max, y_max)
|
|
165
|
+
]
|
|
166
|
+
angles = np.array([np.arctan2(y, x) for x, y in corners])
|
|
167
|
+
|
|
168
|
+
if x_min < 0 < x_max and y_min < 0 < y_max:
|
|
169
|
+
return -np.pi, np.pi
|
|
170
|
+
|
|
171
|
+
if not (x_max < 0 and y_min < 0 < y_max):
|
|
172
|
+
theta_min = angles.min()
|
|
173
|
+
theta_max = angles.max()
|
|
174
|
+
return theta_min, theta_max
|
|
175
|
+
|
|
176
|
+
angles_mod = np.mod(angles, 2*np.pi)
|
|
177
|
+
theta_min = angles_mod.min()
|
|
178
|
+
theta_max = angles_mod.max()
|
|
179
|
+
theta_min = (theta_min + np.pi) % (2*np.pi) - np.pi
|
|
180
|
+
theta_max = (theta_max + np.pi) % (2*np.pi) - np.pi
|
|
181
|
+
return theta_min, theta_max
|
|
182
|
+
|
|
183
|
+
def rect_corners(xmin, xmax, ymin, ymax):
|
|
184
|
+
"""Return the 4 corners of an axis-aligned rectangle."""
|
|
185
|
+
return np.array([
|
|
186
|
+
[xmin, ymin],
|
|
187
|
+
[xmin, ymax],
|
|
188
|
+
[xmax, ymin],
|
|
189
|
+
[xmax, ymax]
|
|
190
|
+
])
|
|
191
|
+
|
|
192
|
+
def angular_span_between_rects(rect1, rect2):
|
|
193
|
+
"""
|
|
194
|
+
Returns angular span of vectors pointing from rect1 to rect2.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
rect1, rect2: arraylikes of form [xmin, xmax, ymin, ymax]
|
|
198
|
+
|
|
199
|
+
Returns:
|
|
200
|
+
(theta_min, theta_max) where theta_max may be < theta_min if wrapping occurs
|
|
201
|
+
"""
|
|
202
|
+
x1min, x1max, y1min, y1max = rect1
|
|
203
|
+
x2min, x2max, y2min, y2max = rect2
|
|
204
|
+
|
|
205
|
+
if (x1min < x2min and x2min < x1max and y1min < y2min and y2min < y1max) or (x2min < x1min and x1min < x2max and y2min < y1min and y1min < y2max):
|
|
206
|
+
return -np.pi, np.pi
|
|
207
|
+
|
|
208
|
+
c1 = rect_corners(x1min, x1max, y1min, y1max)
|
|
209
|
+
c2 = rect_corners(x2min, x2max, y2min, y2max)
|
|
210
|
+
|
|
211
|
+
vecs = (c2[:, None, :] - c1[None, :, :]).reshape(-1, 2)
|
|
212
|
+
angles = np.arctan2(vecs[:, 1], vecs[:, 0])
|
|
213
|
+
|
|
214
|
+
amin, amax = angles.min(), angles.max()
|
|
215
|
+
|
|
216
|
+
if amax - amin <= np.pi:
|
|
217
|
+
return amin, amax
|
|
218
|
+
else:
|
|
219
|
+
angles_mod = np.mod(angles, 2*np.pi)
|
|
220
|
+
amin, amax = angles_mod.min(), angles_mod.max()
|
|
221
|
+
if amin > np.pi:
|
|
222
|
+
amin -= 2*np.pi
|
|
223
|
+
if amax > np.pi:
|
|
224
|
+
amax -= 2*np.pi
|
|
225
|
+
return amin, amax
|
|
226
|
+
|
|
227
|
+
def angular_span_rect(rect, split: bool = False):
|
|
228
|
+
"""
|
|
229
|
+
Essentially atan2 for an entire rectangle.
|
|
230
|
+
|
|
231
|
+
Args:
|
|
232
|
+
rect: arraylike of form [xmin, xmax, ymin, ymax]
|
|
233
|
+
split: whether to split wrapped intervals
|
|
234
|
+
"""
|
|
235
|
+
theta_min, theta_max = angular_span_between_rects([0, 0, 0, 0], rect)
|
|
236
|
+
if not split or theta_min < theta_max:
|
|
237
|
+
return [(theta_min, theta_max)]
|
|
238
|
+
else:
|
|
239
|
+
return [(theta_min, np.pi), (-np.pi, theta_max)]
|
|
240
|
+
|
|
241
|
+
def angular_span_rect_parser(y_bounds, x_bounds):
|
|
242
|
+
"""
|
|
243
|
+
Essentially atan2 for an entire rectangle.
|
|
244
|
+
|
|
245
|
+
Args:
|
|
246
|
+
y_bounds, x_bounds: tuples of (min, max)
|
|
247
|
+
"""
|
|
248
|
+
rect = list(x_bounds) + list(y_bounds)
|
|
249
|
+
theta_min, theta_max = angular_span_between_rects([0, 0, 0, 0], rect)
|
|
250
|
+
if theta_min <= theta_max:
|
|
251
|
+
return (theta_min, theta_max)
|
|
252
|
+
else:
|
|
253
|
+
return [(theta_min, np.pi), (-np.pi, theta_max)]
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def angular_bounds_diff_correct(theta, theta_ref) -> Tuple[float]:
|
|
257
|
+
"""
|
|
258
|
+
Returns the bounds for angular difference theta-theta_ref with the range [-pi, pi].
|
|
259
|
+
|
|
260
|
+
Args:
|
|
261
|
+
theta: angular bound with theta_min >= -pi, theta_max <= pi
|
|
262
|
+
theta_ref: angular reference bound with theta_min >= -pi, theta_max <= pi
|
|
263
|
+
"""
|
|
264
|
+
theta_min, theta_max = theta
|
|
265
|
+
theta_ref_min, theta_ref_max = theta_ref
|
|
266
|
+
if (theta_min == -np.pi and theta_max == np.pi) or (theta_ref_min == -np.pi and theta_ref_max == np.pi):
|
|
267
|
+
return -np.pi, np.pi
|
|
268
|
+
|
|
269
|
+
diff_min_raw = theta_min - theta_ref_max
|
|
270
|
+
diff_max_raw = theta_max - theta_ref_min
|
|
271
|
+
|
|
272
|
+
n = 0
|
|
273
|
+
diff_min = diff_min_raw
|
|
274
|
+
while diff_min < 0:
|
|
275
|
+
diff_min += 2*np.pi
|
|
276
|
+
n += 1
|
|
277
|
+
|
|
278
|
+
diff_max = diff_max_raw + 2*np.pi*n
|
|
279
|
+
|
|
280
|
+
if diff_min <= np.pi <= diff_max:
|
|
281
|
+
return (diff_min, np.pi), (-np.pi, diff_max - 2*np.pi)
|
|
282
|
+
else:
|
|
283
|
+
diff_min_wrapped = wrap_angle(diff_min)
|
|
284
|
+
diff_max_wrapped = wrap_angle(diff_max)
|
|
285
|
+
|
|
286
|
+
if diff_min_wrapped > diff_max_wrapped:
|
|
287
|
+
return (diff_min_wrapped, np.pi), (-np.pi, diff_max_wrapped)
|
|
288
|
+
else:
|
|
289
|
+
return diff_min_wrapped, diff_max_wrapped
|
|
290
|
+
|
|
291
|
+
def combine_angular_bounds(bounds_list):
|
|
292
|
+
"""
|
|
293
|
+
Combine a list of angular bounds without adding conservativeness.
|
|
294
|
+
Each bound is either a single interval (min, max) or a tuple of two intervals
|
|
295
|
+
representing wrapped intervals like [(a, π), (-π, b)].
|
|
296
|
+
|
|
297
|
+
Args:
|
|
298
|
+
bounds_list: List of bounds, each being either:
|
|
299
|
+
- (min, max): single interval
|
|
300
|
+
- ((min1, max1), (min2, max2)): wrapped interval (two parts)
|
|
301
|
+
|
|
302
|
+
Returns:
|
|
303
|
+
Combined bounds in the same format, merged where possible
|
|
304
|
+
|
|
305
|
+
Note:
|
|
306
|
+
Current implementation is suboptimal w.r.t. both sorting, why sort both negative and positive, and list merging.
|
|
307
|
+
"""
|
|
308
|
+
|
|
309
|
+
if not bounds_list:
|
|
310
|
+
return []
|
|
311
|
+
|
|
312
|
+
normalized = []
|
|
313
|
+
for bound in bounds_list:
|
|
314
|
+
if isinstance(bound[0], tuple):
|
|
315
|
+
normalized.extend(list(bound))
|
|
316
|
+
else:
|
|
317
|
+
min_val, max_val = bound
|
|
318
|
+
if min_val > max_val:
|
|
319
|
+
normalized.append((min_val, np.pi))
|
|
320
|
+
normalized.append((-np.pi, max_val))
|
|
321
|
+
else:
|
|
322
|
+
normalized.append((min_val, max_val))
|
|
323
|
+
|
|
324
|
+
if not normalized:
|
|
325
|
+
return []
|
|
326
|
+
|
|
327
|
+
def sort_key(interval):
|
|
328
|
+
start, end = interval
|
|
329
|
+
if start >= 0:
|
|
330
|
+
return (0, start)
|
|
331
|
+
else:
|
|
332
|
+
return (1, start)
|
|
333
|
+
|
|
334
|
+
normalized.sort(key=sort_key)
|
|
335
|
+
|
|
336
|
+
merged = [normalized[0]]
|
|
337
|
+
|
|
338
|
+
for current in normalized[1:]:
|
|
339
|
+
last = merged[-1]
|
|
340
|
+
last_start, last_end = last
|
|
341
|
+
curr_start, curr_end = current
|
|
342
|
+
|
|
343
|
+
if _intervals_overlap_or_adjacent(last, current):
|
|
344
|
+
new_start = min(last_start, curr_start)
|
|
345
|
+
new_end = max(last_end, curr_end)
|
|
346
|
+
merged[-1] = (new_start, new_end)
|
|
347
|
+
else:
|
|
348
|
+
merged.append(current)
|
|
349
|
+
|
|
350
|
+
if len(merged) > 1:
|
|
351
|
+
merged = _intervals_overlap_or_adjacent_wrapped(merged)
|
|
352
|
+
|
|
353
|
+
return _format_bounds(merged)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _intervals_overlap_or_adjacent(interval1, interval2):
|
|
357
|
+
"""
|
|
358
|
+
Check if two intervals in [-π, π] overlap or are adjacent.
|
|
359
|
+
Assumes both are non-wrapping intervals and interval1.start < interval2.start.
|
|
360
|
+
"""
|
|
361
|
+
s1, e1 = interval1
|
|
362
|
+
s2, e2 = interval2
|
|
363
|
+
|
|
364
|
+
if s1 <= e1 and s2 <= e2:
|
|
365
|
+
return s1 <= e2 and s2 <= e1
|
|
366
|
+
|
|
367
|
+
return False
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def _intervals_overlap_or_adjacent_wrapped(merged_intervals):
|
|
371
|
+
"""
|
|
372
|
+
After initial merging, check if the last negative interval wraps into
|
|
373
|
+
positive territory and merge accordingly.
|
|
374
|
+
"""
|
|
375
|
+
if len(merged_intervals) < 2:
|
|
376
|
+
return merged_intervals
|
|
377
|
+
|
|
378
|
+
last_positive_idx = -1
|
|
379
|
+
for i in range(len(merged_intervals)):
|
|
380
|
+
if merged_intervals[i][0] >= 0:
|
|
381
|
+
last_positive_idx = i
|
|
382
|
+
|
|
383
|
+
if last_positive_idx == -1 or last_positive_idx == len(merged_intervals) - 1:
|
|
384
|
+
return merged_intervals
|
|
385
|
+
|
|
386
|
+
last_neg_start, last_neg_end = merged_intervals[-1]
|
|
387
|
+
|
|
388
|
+
if last_neg_end <= 0:
|
|
389
|
+
return merged_intervals
|
|
390
|
+
|
|
391
|
+
largest_ei_idx = -1
|
|
392
|
+
for i in range(last_positive_idx + 1):
|
|
393
|
+
if last_neg_end > merged_intervals[i][1]:
|
|
394
|
+
largest_ei_idx = i
|
|
395
|
+
|
|
396
|
+
if largest_ei_idx == -1:
|
|
397
|
+
return merged_intervals
|
|
398
|
+
|
|
399
|
+
merged_intervals = merged_intervals[:last_positive_idx - largest_ei_idx] + [(last_neg_start, merged_intervals[largest_ei_idx][1])]
|
|
400
|
+
|
|
401
|
+
return merged_intervals
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def _format_bounds(merged):
|
|
405
|
+
"""
|
|
406
|
+
Convert merged intervals back to the original format.
|
|
407
|
+
Returns either single tuples or wrapped tuples.
|
|
408
|
+
"""
|
|
409
|
+
if not merged:
|
|
410
|
+
return []
|
|
411
|
+
|
|
412
|
+
positive_parts = [iv for iv in merged if iv[0] >= 0]
|
|
413
|
+
negative_parts = [iv for iv in merged if iv[1] <= 0]
|
|
414
|
+
|
|
415
|
+
result = []
|
|
416
|
+
|
|
417
|
+
if positive_parts and negative_parts:
|
|
418
|
+
pos = positive_parts[0]
|
|
419
|
+
neg = negative_parts[0]
|
|
420
|
+
return (pos, neg)
|
|
421
|
+
elif positive_parts:
|
|
422
|
+
return [iv for iv in merged if iv[0] >= 0]
|
|
423
|
+
elif negative_parts:
|
|
424
|
+
return [iv for iv in merged if iv[1] <= 0]
|
|
425
|
+
else:
|
|
426
|
+
return merged
|
|
427
|
+
|
|
428
|
+
if __name__ == "__main__":
|
|
429
|
+
theta = [0.1, np.pi]
|
|
430
|
+
theta_ref = [0, np.pi/2]
|
|
431
|
+
print(f'Diff bounds: {angular_bounds_diff_correct(theta, theta_ref)}')
|
|
432
|
+
|
|
433
|
+
# NOTE: The wrap-around logic in _intervals_overlap_or_adjacent_wrapped handles edge cases
|
|
434
|
+
# where intervals cross the -π/π boundary. Test thoroughly when modifying merge behavior.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from scipy.optimize import differential_evolution
|
|
2
|
+
|
|
3
|
+
# NOTE: this should work fairly well in practice given that the bounds are not too large, but unclear if these are actually guaranteed to be overapproximations or good approximations
|
|
4
|
+
def get_heading_bounds_optimized(lane_map, lane_idx, x_bounds, y_bounds):
|
|
5
|
+
"""
|
|
6
|
+
More precise bound computation using optimization
|
|
7
|
+
"""
|
|
8
|
+
def neg_heading(pos):
|
|
9
|
+
return -lane_map.get_lane_heading(lane_idx, pos)
|
|
10
|
+
|
|
11
|
+
def pos_heading(pos):
|
|
12
|
+
return lane_map.get_lane_heading(lane_idx, pos)
|
|
13
|
+
|
|
14
|
+
bounds = [(x_bounds[0], x_bounds[1]), (y_bounds[0], y_bounds[1])]
|
|
15
|
+
|
|
16
|
+
res_min = differential_evolution(pos_heading, bounds)
|
|
17
|
+
res_max = differential_evolution(neg_heading, bounds)
|
|
18
|
+
|
|
19
|
+
return [res_min.fun, -res_max.fun]
|
|
20
|
+
|
|
21
|
+
def get_lateral_distance_bounds_optimized(lane_map, lane_idx, x_bounds, y_bounds):
|
|
22
|
+
"""
|
|
23
|
+
More precise bound computation using optimization
|
|
24
|
+
"""
|
|
25
|
+
def neg_distance(pos):
|
|
26
|
+
return -lane_map.get_lateral_distance(lane_idx, pos)
|
|
27
|
+
|
|
28
|
+
def pos_distance(pos):
|
|
29
|
+
return lane_map.get_lateral_distance(lane_idx, pos)
|
|
30
|
+
|
|
31
|
+
bounds = [(x_bounds[0], x_bounds[1]), (y_bounds[0], y_bounds[1])]
|
|
32
|
+
|
|
33
|
+
res_min = differential_evolution(pos_distance, bounds)
|
|
34
|
+
res_max = differential_evolution(neg_distance, bounds)
|
|
35
|
+
|
|
36
|
+
return [res_min.fun, -res_max.fun]
|
|
37
|
+
|
|
38
|
+
def get_long_pos_bounds_optimized(lane_map, lane_idx, x_bounds, y_bounds):
|
|
39
|
+
"""
|
|
40
|
+
More precise bound computation using optimization
|
|
41
|
+
"""
|
|
42
|
+
def neg_pos(pos):
|
|
43
|
+
return -lane_map.get_longitudinal_position(lane_idx, pos)
|
|
44
|
+
|
|
45
|
+
def pos_pos(pos):
|
|
46
|
+
return lane_map.get_longitudinal_position(lane_idx, pos)
|
|
47
|
+
|
|
48
|
+
bounds = [(x_bounds[0], x_bounds[1]), (y_bounds[0], y_bounds[1])]
|
|
49
|
+
|
|
50
|
+
res_min = differential_evolution(pos_pos, bounds)
|
|
51
|
+
res_max = differential_evolution(neg_pos, bounds)
|
|
52
|
+
|
|
53
|
+
return [res_min.fun, -res_max.fun]
|