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
verse/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
__version__ = "2.0.0"
|
|
2
|
+
|
|
3
|
+
from verse import agents, sensor, scenario, map, parser, automaton, analysis
|
|
4
|
+
from verse.agents import BaseAgent
|
|
5
|
+
from verse.sensor import BaseSensor
|
|
6
|
+
from verse.map import LaneSegment, LaneMap, Lane
|
|
7
|
+
from verse.scenario import Scenario, ScenarioConfig
|
|
8
|
+
from verse.utils import fixed_points
|
verse/agents/__init__.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from verse.parser.parser import ControllerIR
|
|
2
|
+
import numpy as np
|
|
3
|
+
from scipy.integrate import ode, odeint
|
|
4
|
+
import copy
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BaseAgent:
|
|
8
|
+
"""
|
|
9
|
+
Agent Base class
|
|
10
|
+
|
|
11
|
+
Methods
|
|
12
|
+
-------
|
|
13
|
+
TC_simulate
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
id,
|
|
19
|
+
code=None,
|
|
20
|
+
file_name=None,
|
|
21
|
+
initial_state = None,
|
|
22
|
+
initial_mode = None
|
|
23
|
+
):
|
|
24
|
+
"""
|
|
25
|
+
Constructor of agent base class.
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
id : str
|
|
30
|
+
id of the agent.
|
|
31
|
+
code: str
|
|
32
|
+
actual code of python controller
|
|
33
|
+
file_name: str
|
|
34
|
+
file name to the python controller
|
|
35
|
+
"""
|
|
36
|
+
self.decision_logic: ControllerIR = ControllerIR.parse(code, file_name)
|
|
37
|
+
self.id = id
|
|
38
|
+
self.init_cont = initial_state
|
|
39
|
+
self.init_disc = initial_mode
|
|
40
|
+
self.static_parameters = None
|
|
41
|
+
self.uncertain_parameters = None
|
|
42
|
+
|
|
43
|
+
def set_initial(self, initial_state, initial_mode, static_param=None, uncertain_param=None):
|
|
44
|
+
'''Initialize the states
|
|
45
|
+
'''
|
|
46
|
+
self.set_initial_state(initial_state)
|
|
47
|
+
self.set_initial_mode(initial_mode)
|
|
48
|
+
self.set_static_parameter(static_param)
|
|
49
|
+
self.set_uncertain_parameter(uncertain_param)
|
|
50
|
+
|
|
51
|
+
def set_initial_state(self, initial_state):
|
|
52
|
+
self.init_cont = copy.deepcopy(initial_state)
|
|
53
|
+
|
|
54
|
+
def set_initial_mode(self, initial_mode):
|
|
55
|
+
self.init_disc = copy.deepcopy(initial_mode)
|
|
56
|
+
|
|
57
|
+
def set_static_parameter(self, static_param):
|
|
58
|
+
self.static_parameters = copy.deepcopy(static_param)
|
|
59
|
+
|
|
60
|
+
def set_uncertain_parameter(self, uncertain_param):
|
|
61
|
+
self.uncertain_parameters = copy.deepcopy(uncertain_param)
|
|
62
|
+
|
|
63
|
+
@staticmethod
|
|
64
|
+
def dynamics(t, state, u):
|
|
65
|
+
raise NotImplementedError()
|
|
66
|
+
|
|
67
|
+
def TC_simulate(self, mode, initialSet, time_horizon, time_step, map=None):
|
|
68
|
+
# TODO: P1. Should TC_simulate really be part of the agent definition or
|
|
69
|
+
# should it be something more generic?
|
|
70
|
+
# TODO: P2. Looks like this should be a global parameter;
|
|
71
|
+
# some config file should be setting this.
|
|
72
|
+
"""
|
|
73
|
+
Abstract simulation function
|
|
74
|
+
|
|
75
|
+
Parameters
|
|
76
|
+
----------
|
|
77
|
+
mode: str
|
|
78
|
+
The current mode to simulate
|
|
79
|
+
initialSet: List[float]
|
|
80
|
+
The initial condition to perform the simulation
|
|
81
|
+
time_horizon: float
|
|
82
|
+
The time horizon for simulation
|
|
83
|
+
time_step: float
|
|
84
|
+
time_step for performing simulation
|
|
85
|
+
map: LaneMap, optional
|
|
86
|
+
Provided if the map is used
|
|
87
|
+
"""
|
|
88
|
+
y0 = initialSet
|
|
89
|
+
t = np.round(np.arange(0.0, time_horizon+time_step/2, time_step), 8)
|
|
90
|
+
trace = odeint(func = self.dynamics, y0 = y0, t = t, tfirst=True)
|
|
91
|
+
t = t.reshape((-1,1))
|
|
92
|
+
trace = np.hstack((t, trace))
|
|
93
|
+
return trace
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Example agent.
|
|
2
|
+
from typing import Tuple, List
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from scipy.integrate import ode
|
|
6
|
+
|
|
7
|
+
from verse import BaseAgent
|
|
8
|
+
from verse import LaneMap
|
|
9
|
+
from verse.plotter.plotter2D import *
|
|
10
|
+
import plotly.graph_objects as go
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BallAgent(BaseAgent):
|
|
14
|
+
"""Dynamics of a frictionless billiard ball
|
|
15
|
+
on a 2D-plane"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, id, code=None, file_name=None):
|
|
18
|
+
"""Contructor for the agent
|
|
19
|
+
EXACTLY one of the following should be given
|
|
20
|
+
file_name: name of the decision logic (DL)
|
|
21
|
+
code: pyhton string defning the decision logic (DL)
|
|
22
|
+
"""
|
|
23
|
+
# Calling the constructor of tha base class
|
|
24
|
+
super().__init__(id, code, file_name)
|
|
25
|
+
|
|
26
|
+
def dynamics(self, t, state):
|
|
27
|
+
"""Defines the RHS of the ODE used to simulate trajectories"""
|
|
28
|
+
x, y, vx, vy = state
|
|
29
|
+
x_dot = vx
|
|
30
|
+
y_dot = vy
|
|
31
|
+
vx_dot = 0
|
|
32
|
+
vy_dot = 0
|
|
33
|
+
return [x_dot, y_dot, vx_dot, vy_dot]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
if __name__ == "__main__":
|
|
37
|
+
aball = BallAgent(
|
|
38
|
+
"red_ball", file_name="/Users/mitras/Dpp/GraphGeneration/demo/ball_bounces.py"
|
|
39
|
+
)
|
|
40
|
+
trace = aball.TC_simulate({"none"}, [5, 10, 2, 2], 10, 0.05)
|
|
41
|
+
fig = simulation_tree(trace, map=None, fig=go.Figure(), x_dim = 1, y_dim = 2, print_dim_list=None, map_type='lines', scale_type='trace', label_mode='None', sample_rate=1)
|
|
42
|
+
fig.show()
|
|
43
|
+
print(trace)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Example agent.
|
|
2
|
+
from typing import Tuple, List
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from scipy.integrate import ode
|
|
6
|
+
|
|
7
|
+
from verse import BaseAgent
|
|
8
|
+
from verse import LaneMap
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Ball_g_agent(BaseAgent):
|
|
12
|
+
"""Dynamics of point object falling under gravity"""
|
|
13
|
+
g_acc: float
|
|
14
|
+
|
|
15
|
+
def __init__(self, id, code=None, file_name=None, g_acc_val=9.8):
|
|
16
|
+
"""Contructor
|
|
17
|
+
id: A string which will be treated as the name of the agent
|
|
18
|
+
EXACTLY one of the following should be given
|
|
19
|
+
file_name: name of the file containing the decision logic (DL)
|
|
20
|
+
code: pyhton string defining the decision logic (DL)
|
|
21
|
+
Then
|
|
22
|
+
g_acc_val: value of acceleration due to gravity
|
|
23
|
+
"""
|
|
24
|
+
super().__init__(id, code, file_name)
|
|
25
|
+
self.g_acc = g_acc_val
|
|
26
|
+
|
|
27
|
+
@staticmethod
|
|
28
|
+
def dynamics(self, t, state):
|
|
29
|
+
"""Defines the RHS of the ODE used to simulate trajectories"""
|
|
30
|
+
x, y, vx, vy = state
|
|
31
|
+
x_dot = vx
|
|
32
|
+
y_dot = vy
|
|
33
|
+
vx_dot = -self.g_acc # Does not work.
|
|
34
|
+
vy_dot = 0
|
|
35
|
+
return [x_dot, y_dot, vx_dot, vy_dot]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
if __name__ == "__main__":
|
|
39
|
+
aball = Ball_g_agent(
|
|
40
|
+
"red_ball", file_name="/Users/mitras/Dpp/Verse-library/demo/ball/ball_bounces.py")
|
|
41
|
+
trace = aball.TC_simulate({"none"}, [5, 10, 2, 2], 10, 0.05)
|
|
42
|
+
print(trace)
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# Example agent.
|
|
2
|
+
from typing import Tuple, List
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from scipy.integrate import ode
|
|
6
|
+
|
|
7
|
+
from verse import BaseAgent
|
|
8
|
+
from verse import LaneMap
|
|
9
|
+
from verse.utils.utils import wrap_to_pi
|
|
10
|
+
from verse.analysis.analysis_tree import TraceType
|
|
11
|
+
from verse.parser import ControllerIR
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class NPCAgent(BaseAgent):
|
|
15
|
+
def __init__(self, id, initial_state=None, initial_mode=None):
|
|
16
|
+
self.id = id
|
|
17
|
+
self.decision_logic = ControllerIR.empty()
|
|
18
|
+
self.set_initial_state(initial_state)
|
|
19
|
+
self.set_initial_mode(initial_mode)
|
|
20
|
+
self.set_static_parameter(None)
|
|
21
|
+
self.set_uncertain_parameter(None)
|
|
22
|
+
|
|
23
|
+
@staticmethod
|
|
24
|
+
def dynamics(t, state, u):
|
|
25
|
+
theta, v = state[2:4]
|
|
26
|
+
delta, a = u
|
|
27
|
+
x_dot = v * np.cos(theta + delta)
|
|
28
|
+
y_dot = v * np.sin(theta + delta)
|
|
29
|
+
theta_dot = v / 1.75 * np.sin(delta)
|
|
30
|
+
v_dot = a
|
|
31
|
+
dots = [x_dot, y_dot, theta_dot, v_dot]
|
|
32
|
+
if len(state) == 4:
|
|
33
|
+
return dots
|
|
34
|
+
return dots + [1]
|
|
35
|
+
|
|
36
|
+
def action_handler(self, mode, state, lane_map: LaneMap) -> Tuple[float, float]:
|
|
37
|
+
"""Computes steering and acceleration based on current lane, target lane and
|
|
38
|
+
current state using a Stanley controller-like rule"""
|
|
39
|
+
x, y, theta, v = state[:4]
|
|
40
|
+
vehicle_mode = mode[0]
|
|
41
|
+
vehicle_lane = mode[1]
|
|
42
|
+
vehicle_pos = np.array([x, y])
|
|
43
|
+
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos)
|
|
44
|
+
psi = lane_map.get_lane_heading(vehicle_lane, vehicle_pos) - theta
|
|
45
|
+
steering = psi + np.arctan2(0.45 * d, v)
|
|
46
|
+
steering = np.clip(steering, -0.61, 0.61)
|
|
47
|
+
a = 0
|
|
48
|
+
return steering, a
|
|
49
|
+
|
|
50
|
+
def TC_simulate(
|
|
51
|
+
self, mode: Tuple[str], init, time_bound, time_step, lane_map: LaneMap = None
|
|
52
|
+
) -> TraceType:
|
|
53
|
+
time_bound = float(time_bound)
|
|
54
|
+
num_points = int(np.ceil(time_bound / time_step))
|
|
55
|
+
trace = np.zeros((num_points + 1, 1 + len(init)))
|
|
56
|
+
trace[1:, 0] = [round(i * time_step, 10) for i in range(num_points)]
|
|
57
|
+
trace[0, 1:] = init
|
|
58
|
+
for i in range(num_points):
|
|
59
|
+
steering, a = self.action_handler(mode, init, lane_map)
|
|
60
|
+
r = ode(self.dynamics)
|
|
61
|
+
r.set_initial_value(init).set_f_params([steering, a])
|
|
62
|
+
res: np.ndarray = r.integrate(r.t + time_step)
|
|
63
|
+
init = res.flatten()
|
|
64
|
+
if init[3] < 0:
|
|
65
|
+
init[3] = 0
|
|
66
|
+
trace[i + 1, 0] = time_step * (i + 1)
|
|
67
|
+
trace[i + 1, 1:] = init
|
|
68
|
+
return trace
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class CarAgent(BaseAgent):
|
|
72
|
+
def __init__(
|
|
73
|
+
self,
|
|
74
|
+
id,
|
|
75
|
+
code=None,
|
|
76
|
+
file_name=None,
|
|
77
|
+
initial_state=None,
|
|
78
|
+
initial_mode=None,
|
|
79
|
+
speed: float = 2,
|
|
80
|
+
accel: float = 1,
|
|
81
|
+
):
|
|
82
|
+
super().__init__(
|
|
83
|
+
id, code, file_name, initial_state=initial_state, initial_mode=initial_mode
|
|
84
|
+
)
|
|
85
|
+
self.speed = speed
|
|
86
|
+
self.accel = accel
|
|
87
|
+
|
|
88
|
+
@staticmethod
|
|
89
|
+
def dynamics(t, state, u):
|
|
90
|
+
x, y, theta, v = state
|
|
91
|
+
delta, a = u
|
|
92
|
+
x_dot = v * np.cos(theta + delta)
|
|
93
|
+
y_dot = v * np.sin(theta + delta)
|
|
94
|
+
theta_dot = v / 1.75 * np.sin(delta)
|
|
95
|
+
v_dot = a
|
|
96
|
+
return [x_dot, y_dot, theta_dot, v_dot]
|
|
97
|
+
|
|
98
|
+
def action_handler(self, mode: List[str], state, lane_map: LaneMap) -> Tuple[float, float]:
|
|
99
|
+
x, y, theta, v = state
|
|
100
|
+
vehicle_mode, vehicle_lane = mode
|
|
101
|
+
vehicle_pos = np.array([x, y])
|
|
102
|
+
a = 0
|
|
103
|
+
lane_width = lane_map.get_lane_width(vehicle_lane)
|
|
104
|
+
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos)
|
|
105
|
+
if vehicle_mode == "Normal" or vehicle_mode == "Stop":
|
|
106
|
+
pass
|
|
107
|
+
elif vehicle_mode == "SwitchLeft":
|
|
108
|
+
d += lane_width
|
|
109
|
+
elif vehicle_mode == "SwitchRight":
|
|
110
|
+
d -= lane_width
|
|
111
|
+
elif vehicle_mode == "Brake":
|
|
112
|
+
a = max(-self.accel, -v)
|
|
113
|
+
elif vehicle_mode == "Accel":
|
|
114
|
+
a = min(self.accel, self.speed - v)
|
|
115
|
+
else:
|
|
116
|
+
raise ValueError(f"Invalid mode: {vehicle_mode}")
|
|
117
|
+
|
|
118
|
+
heading = lane_map.get_lane_heading(vehicle_lane, vehicle_pos)
|
|
119
|
+
psi = wrap_to_pi(heading - theta)
|
|
120
|
+
steering = psi + np.arctan2(0.45 * d, v)
|
|
121
|
+
steering = np.clip(steering, -0.61, 0.61)
|
|
122
|
+
return steering, a
|
|
123
|
+
|
|
124
|
+
def TC_simulate(
|
|
125
|
+
self, mode: List[str], init, time_bound, time_step, lane_map: LaneMap = None
|
|
126
|
+
) -> TraceType:
|
|
127
|
+
time_bound = float(time_bound)
|
|
128
|
+
num_points = int(np.ceil(time_bound / time_step))
|
|
129
|
+
trace = np.zeros((num_points + 1, 1 + len(init)))
|
|
130
|
+
trace[1:, 0] = [round(i * time_step, 10) for i in range(num_points)]
|
|
131
|
+
trace[0, 1:] = init
|
|
132
|
+
for i in range(num_points):
|
|
133
|
+
steering, a = self.action_handler(mode, init, lane_map)
|
|
134
|
+
r = ode(self.dynamics)
|
|
135
|
+
r.set_initial_value(init).set_f_params([steering, a])
|
|
136
|
+
res: np.ndarray = r.integrate(r.t + time_step)
|
|
137
|
+
init = res.flatten()
|
|
138
|
+
if init[3] < 0:
|
|
139
|
+
init[3] = 0
|
|
140
|
+
trace[i + 1, 0] = time_step * (i + 1)
|
|
141
|
+
trace[i + 1, 1:] = init
|
|
142
|
+
return trace
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class WeirdCarAgent(CarAgent):
|
|
146
|
+
def __init__(self, id, code=None, file_name=None):
|
|
147
|
+
super().__init__(id, code, file_name)
|
|
148
|
+
self.gain = 0.2
|
|
149
|
+
self.thres = 0.8
|
|
150
|
+
|
|
151
|
+
def action_handler(self, mode: List[str], state, lane_map: LaneMap) -> Tuple[float, float]:
|
|
152
|
+
vehicle_mode = mode[0]
|
|
153
|
+
steering, a = self._action_handler(mode, state, lane_map)
|
|
154
|
+
# print("agnt", vehicle_mode, state, steering, a)
|
|
155
|
+
return steering, a
|
|
156
|
+
|
|
157
|
+
def _action_handler(self, mode: List[str], state, lane_map: LaneMap) -> Tuple[float, float]:
|
|
158
|
+
vehicle_mode = mode[0]
|
|
159
|
+
theta = state[2]
|
|
160
|
+
if abs(theta) > self.thres:
|
|
161
|
+
return 0, 0
|
|
162
|
+
if vehicle_mode == "SwitchLeft" and theta >= 0:
|
|
163
|
+
return self.gain, 0
|
|
164
|
+
elif vehicle_mode == "SwitchRight" and theta <= 0:
|
|
165
|
+
return -self.gain, 0
|
|
166
|
+
else:
|
|
167
|
+
return 0, 0
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class CarAgentDebounced(CarAgent):
|
|
171
|
+
def __init__(
|
|
172
|
+
self,
|
|
173
|
+
id,
|
|
174
|
+
code=None,
|
|
175
|
+
file_name=None,
|
|
176
|
+
initial_state=None,
|
|
177
|
+
initial_mode=None,
|
|
178
|
+
speed: float = 2,
|
|
179
|
+
accel: float = 1,
|
|
180
|
+
):
|
|
181
|
+
super().__init__(
|
|
182
|
+
id,
|
|
183
|
+
code,
|
|
184
|
+
file_name,
|
|
185
|
+
initial_state=initial_state,
|
|
186
|
+
initial_mode=initial_mode,
|
|
187
|
+
speed=speed,
|
|
188
|
+
accel=accel,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
@staticmethod
|
|
192
|
+
def dynamics(t, state, u):
|
|
193
|
+
return super(CarAgentDebounced, CarAgentDebounced).dynamics(t, state[:4], u) + [1]
|
|
194
|
+
|
|
195
|
+
def action_handler(self, mode: List[str], state, lane_map: LaneMap) -> Tuple[float, float]:
|
|
196
|
+
return super(CarAgentDebounced, self).action_handler(
|
|
197
|
+
mode, state[:4], lane_map
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class CarAgentSwitch2(CarAgent):
|
|
202
|
+
def __init__(
|
|
203
|
+
self,
|
|
204
|
+
id,
|
|
205
|
+
code=None,
|
|
206
|
+
file_name=None,
|
|
207
|
+
initial_state=None,
|
|
208
|
+
initial_mode=None,
|
|
209
|
+
speed: float = 2,
|
|
210
|
+
accel: float = 1,
|
|
211
|
+
):
|
|
212
|
+
super().__init__(
|
|
213
|
+
id,
|
|
214
|
+
code,
|
|
215
|
+
file_name,
|
|
216
|
+
initial_state=initial_state,
|
|
217
|
+
initial_mode=initial_mode,
|
|
218
|
+
speed=speed,
|
|
219
|
+
accel=accel,
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
@staticmethod
|
|
223
|
+
def dynamics(t, state, u):
|
|
224
|
+
return super(CarAgentSwitch2, CarAgentSwitch2).dynamics(t, state[:4], u) + [1]
|
|
225
|
+
|
|
226
|
+
def action_handler(self, mode: List[str], state, lane_map: LaneMap) -> Tuple[float, float]:
|
|
227
|
+
x, y, theta, v, _ = state
|
|
228
|
+
vehicle_mode, vehicle_lane = mode
|
|
229
|
+
vehicle_pos = np.array([x, y])
|
|
230
|
+
a = 0
|
|
231
|
+
lane_width = lane_map.get_lane_width(vehicle_lane)
|
|
232
|
+
d = -lane_map.get_lateral_distance(vehicle_lane, vehicle_pos)
|
|
233
|
+
if vehicle_mode == "Normal" or vehicle_mode == "Stop":
|
|
234
|
+
pass
|
|
235
|
+
elif vehicle_mode == "SwitchLeft":
|
|
236
|
+
d += lane_width
|
|
237
|
+
elif vehicle_mode == "SwitchRight":
|
|
238
|
+
d -= lane_width
|
|
239
|
+
elif vehicle_mode == "SwitchLeft2":
|
|
240
|
+
d += lane_width * 2
|
|
241
|
+
elif vehicle_mode == "SwitchRight2":
|
|
242
|
+
d -= lane_width * 2
|
|
243
|
+
elif vehicle_mode == "Brake":
|
|
244
|
+
a = max(-self.accel, -v)
|
|
245
|
+
elif vehicle_mode == "Accel":
|
|
246
|
+
a = min(self.accel, self.speed - v)
|
|
247
|
+
else:
|
|
248
|
+
raise ValueError(f"Invalid mode: {vehicle_mode}")
|
|
249
|
+
|
|
250
|
+
heading = lane_map.get_lane_heading(vehicle_lane, vehicle_pos)
|
|
251
|
+
psi = wrap_to_pi(heading - theta)
|
|
252
|
+
steering = psi + np.arctan2(0.45 * d, v)
|
|
253
|
+
steering = np.clip(steering, -0.61, 0.61)
|
|
254
|
+
return steering, a
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Example agent.
|
|
2
|
+
from typing import Tuple, List
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from scipy.integrate import ode
|
|
6
|
+
|
|
7
|
+
from verse import BaseAgent
|
|
8
|
+
from verse import LaneMap
|
|
9
|
+
from verse.plotter.plotter2D import *
|
|
10
|
+
import plotly.graph_objects as go
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class LineAgent(BaseAgent):
|
|
14
|
+
"""Dynamics of a frictionless point on a line"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, id, code=None, file_name=None):
|
|
17
|
+
"""Contructor for the agent
|
|
18
|
+
EXACTLY one of the following should be given
|
|
19
|
+
file_name: name of the decision logic (DL)
|
|
20
|
+
code: pyhton string defning the decision logic (DL)
|
|
21
|
+
"""
|
|
22
|
+
# Calling the constructor of tha base class
|
|
23
|
+
super().__init__(id, code, file_name)
|
|
24
|
+
|
|
25
|
+
def dynamics(self, t, state):
|
|
26
|
+
"""Defines the RHS of the ODE used to simulate trajectories"""
|
|
27
|
+
x, vx = state
|
|
28
|
+
x_dot = vx
|
|
29
|
+
vx_dot = 0
|
|
30
|
+
return [x_dot, vx_dot]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
aball = BallAgent(
|
|
35
|
+
"red_ball", file_name="/Users/mitras/Dpp/GraphGeneration/demo/ball_bounces.py"
|
|
36
|
+
)
|
|
37
|
+
trace = aball.TC_simulate({"none"}, [5, 0, 2], 10, 0.05)
|
|
38
|
+
fig = simulation_tree(trace, map=None, fig=go.Figure(), x_dim = 1, y_dim = 2, print_dim_list=None, map_type='lines', scale_type='trace', label_mode='None', sample_rate=1)
|
|
39
|
+
fig.show()
|
|
40
|
+
print(trace)
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# Example agent.
|
|
2
|
+
from typing import Tuple, List
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from scipy.integrate import ode
|
|
6
|
+
|
|
7
|
+
from verse import BaseAgent
|
|
8
|
+
from verse import LaneMap
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class vanderpol_agent(BaseAgent):
|
|
12
|
+
def __init__(self, id, code=None, file_name=None):
|
|
13
|
+
# Calling the constructor of tha base class
|
|
14
|
+
super().__init__(id, code, file_name)
|
|
15
|
+
|
|
16
|
+
@staticmethod
|
|
17
|
+
def dynamics(t, state):
|
|
18
|
+
x, y = state
|
|
19
|
+
x = float(x)
|
|
20
|
+
y = float(y)
|
|
21
|
+
x_dot = y
|
|
22
|
+
y_dot = (1 - x**2) * y - x
|
|
23
|
+
return [x_dot, y_dot]
|
|
24
|
+
|
|
25
|
+
def TC_simulate(
|
|
26
|
+
self, mode: List[str], initialCondition, time_bound, time_step, lane_map: LaneMap = None
|
|
27
|
+
) -> np.ndarray:
|
|
28
|
+
time_bound = float(time_bound)
|
|
29
|
+
number_points = int(np.ceil(time_bound / time_step))
|
|
30
|
+
t = [round(i * time_step, 10) for i in range(0, number_points)]
|
|
31
|
+
# note: digit of time
|
|
32
|
+
init = initialCondition
|
|
33
|
+
trace = [[0] + init]
|
|
34
|
+
for i in range(len(t)):
|
|
35
|
+
r = ode(self.dynamics)
|
|
36
|
+
r.set_initial_value(init)
|
|
37
|
+
res: np.ndarray = r.integrate(r.t + time_step)
|
|
38
|
+
init = res.flatten().tolist()
|
|
39
|
+
trace.append([t[i] + time_step] + init)
|
|
40
|
+
return np.array(trace)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class thermo_agent(BaseAgent):
|
|
44
|
+
def __init__(self, id, code=None, file_name=None):
|
|
45
|
+
# Calling the constructor of tha base class
|
|
46
|
+
super().__init__(id, code, file_name)
|
|
47
|
+
|
|
48
|
+
@staticmethod
|
|
49
|
+
def dynamics(t, state, rate):
|
|
50
|
+
temp, total_time, cycle_time = state
|
|
51
|
+
temp = float(temp)
|
|
52
|
+
total_time = float(total_time)
|
|
53
|
+
cycle_time = float(cycle_time)
|
|
54
|
+
temp_dot = temp * rate
|
|
55
|
+
total_time_dot = 1
|
|
56
|
+
cycle_time_dot = 1
|
|
57
|
+
return [temp_dot, total_time_dot, cycle_time_dot]
|
|
58
|
+
|
|
59
|
+
def action_handler(self, mode):
|
|
60
|
+
if mode == "ON":
|
|
61
|
+
rate = 0.1
|
|
62
|
+
elif mode == "OFF":
|
|
63
|
+
rate = -0.1
|
|
64
|
+
else:
|
|
65
|
+
print(mode)
|
|
66
|
+
raise ValueError(f"Invalid mode: {mode}")
|
|
67
|
+
return rate
|
|
68
|
+
|
|
69
|
+
def TC_simulate(
|
|
70
|
+
self, mode: List[str], initialCondition, time_bound, time_step, lane_map: LaneMap = None
|
|
71
|
+
) -> np.ndarray:
|
|
72
|
+
time_bound = float(time_bound)
|
|
73
|
+
number_points = int(np.ceil(time_bound / time_step))
|
|
74
|
+
t = [round(i * time_step, 10) for i in range(0, number_points)]
|
|
75
|
+
|
|
76
|
+
init = initialCondition
|
|
77
|
+
trace = [[0] + init]
|
|
78
|
+
for i in range(len(t)):
|
|
79
|
+
rate = self.action_handler(mode[0])
|
|
80
|
+
r = ode(self.dynamics)
|
|
81
|
+
r.set_initial_value(init).set_f_params(rate)
|
|
82
|
+
res: np.ndarray = r.integrate(r.t + time_step)
|
|
83
|
+
init = res.flatten().tolist()
|
|
84
|
+
trace.append([t[i] + time_step] + init)
|
|
85
|
+
return np.array(trace)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class craft_agent(BaseAgent):
|
|
89
|
+
def __init__(self, id, code=None, file_name=None):
|
|
90
|
+
# Calling the constructor of tha base class
|
|
91
|
+
super().__init__(id, code, file_name)
|
|
92
|
+
|
|
93
|
+
@staticmethod
|
|
94
|
+
def ProxA_dynamics(t, state):
|
|
95
|
+
xp, yp, xd, yd, total_time, cycle_time = state
|
|
96
|
+
xp_dot = xd
|
|
97
|
+
yp_dot = yd
|
|
98
|
+
xd_dot = (
|
|
99
|
+
-2.89995083970656 * xd
|
|
100
|
+
- 0.0576765518445905 * xp
|
|
101
|
+
+ 0.00877200894463775 * yd
|
|
102
|
+
+ 0.000200959896519766 * yp
|
|
103
|
+
- (1.43496e18 * xp + 6.050365344e25) * pow(pow(yp, 2) + pow(xp + 42164000, 2), -1.5)
|
|
104
|
+
+ 807.153595726846
|
|
105
|
+
)
|
|
106
|
+
yd_dot = (
|
|
107
|
+
-0.00875351105536225 * xd
|
|
108
|
+
- 0.000174031357370456 * xp
|
|
109
|
+
- 2.90300269286856 * yd
|
|
110
|
+
- 1.43496e18 * yp * pow(pow(yp, 2) + pow(xp + 42164000, 2), -1.5)
|
|
111
|
+
- 0.0664932019993982 * yp
|
|
112
|
+
)
|
|
113
|
+
total_time_dot = 1
|
|
114
|
+
cycle_time_dot = 1
|
|
115
|
+
return [xp_dot, yp_dot, xd_dot, yd_dot, total_time_dot, cycle_time_dot]
|
|
116
|
+
|
|
117
|
+
@staticmethod
|
|
118
|
+
def ProxB_dynamics(t, state):
|
|
119
|
+
xp, yp, xd, yd, total_time, cycle_time = state
|
|
120
|
+
xp_dot = xd
|
|
121
|
+
yp_dot = yd
|
|
122
|
+
xd_dot = (
|
|
123
|
+
-19.2299795908647 * xd
|
|
124
|
+
- 0.576076729033652 * xp
|
|
125
|
+
+ 0.00876275931760007 * yd
|
|
126
|
+
+ 0.000262486079431672 * yp
|
|
127
|
+
- (1.43496e18 * xp + 6.050365344e25) * pow(pow(yp, 2) + pow(xp + 42164000, 2), -1.5)
|
|
128
|
+
+ 807.153595726846
|
|
129
|
+
)
|
|
130
|
+
yd_dot = (
|
|
131
|
+
-0.00876276068239993 * xd
|
|
132
|
+
- 0.000262486080737868 * xp
|
|
133
|
+
- 19.2299765959399 * yd
|
|
134
|
+
- 1.43496e18 * yp * pow(pow(yp, 2) + pow(xp + 42164000, 2), -1.5)
|
|
135
|
+
- 0.575980743701182 * yp
|
|
136
|
+
)
|
|
137
|
+
total_time_dot = 1
|
|
138
|
+
cycle_time_dot = 1
|
|
139
|
+
return [xp_dot, yp_dot, xd_dot, yd_dot, total_time_dot, cycle_time_dot]
|
|
140
|
+
|
|
141
|
+
@staticmethod
|
|
142
|
+
def Passive_dynamics(t, state):
|
|
143
|
+
xp, yp, xd, yd, total_time, cycle_time = state
|
|
144
|
+
xp_dot = xd
|
|
145
|
+
yp_dot = yd
|
|
146
|
+
xd_dot = 0.0000575894721132000 * xp + 0.00876276 * yd
|
|
147
|
+
yd_dot = -0.00876276 * xd
|
|
148
|
+
total_time_dot = 1
|
|
149
|
+
cycle_time_dot = 1
|
|
150
|
+
return [xp_dot, yp_dot, xd_dot, yd_dot, total_time_dot, cycle_time_dot]
|
|
151
|
+
|
|
152
|
+
def action_handler(self, mode):
|
|
153
|
+
if mode == "ProxA":
|
|
154
|
+
return ode(self.ProxA_dynamics)
|
|
155
|
+
elif mode == "ProxB":
|
|
156
|
+
return ode(self.ProxB_dynamics)
|
|
157
|
+
elif mode == "Passive":
|
|
158
|
+
return ode(self.Passive_dynamics)
|
|
159
|
+
else:
|
|
160
|
+
raise ValueError
|
|
161
|
+
|
|
162
|
+
def TC_simulate(
|
|
163
|
+
self, mode: List[str], initialCondition, time_bound, time_step, lane_map: LaneMap = None
|
|
164
|
+
) -> np.ndarray:
|
|
165
|
+
time_bound = float(time_bound)
|
|
166
|
+
number_points = int(np.ceil(time_bound / time_step))
|
|
167
|
+
t = [round(i * time_step, 10) for i in range(0, number_points)]
|
|
168
|
+
|
|
169
|
+
init = initialCondition
|
|
170
|
+
trace = [[0] + init]
|
|
171
|
+
for i in range(len(t)):
|
|
172
|
+
r = self.action_handler(mode[0])
|
|
173
|
+
r.set_initial_value(init)
|
|
174
|
+
res: np.ndarray = r.integrate(r.t + time_step)
|
|
175
|
+
init = res.flatten().tolist()
|
|
176
|
+
trace.append([t[i] + time_step] + init)
|
|
177
|
+
return np.array(trace)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
if __name__ == "__main__":
|
|
181
|
+
aball = vanderpol_agent("agent1")
|
|
182
|
+
trace = aball.TC_Simulate(["none"], [1.25, 2.25], 7, 0.05)
|
|
183
|
+
print(trace)
|