def test_init_outside_invariant(): 'test when initial state is outside of the mode invariant' ha = HybridAutomaton() mode = ha.new_mode('mode') mode.set_dynamics([[0, 0, 1], [0, 0, 1], [0, 0, 0]]) # x' = 1, y' = 1, a' = 0 # x <= 2.5 mode.set_invariant([[1, 0, 0]], [2.5]) # initial set, x = [3, 4] init_lpi = lputil.from_box([(3, 4), (0, 1), (1, 1)], mode) init_list = [StateSet(init_lpi, mode)] # transition to error if x >= 10 error = ha.new_mode('error') trans = ha.new_transition(mode, error) trans.set_guard([[-1., 0, 0],], [-10]) # settings settings = HylaaSettings(1.0, 5.0) settings.stdout = HylaaSettings.STDOUT_VERBOSE try: Core(ha, settings).run(init_list) assert False, "running with initial state outside of invariant did not raise RuntimeError" except RuntimeError: pass
def define_ha(unsafe_box): '''make the hybrid automaton''' ha = HybridAutomaton() # dynamics: x' = y, y' = -x, t' == a a_mat = [[0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]] one = ha.new_mode('one') one.set_dynamics(a_mat) one.set_invariant([[0, 0, 1, 0]], [math.pi - 1e-6]) # t <= pi two = ha.new_mode('two') two.set_dynamics([[0, 0, 0, 0], [0, 0, 0, -1], [0, 0, 0, 1], [0, 0, 0, 0]]) two.set_invariant([[0, -1, 0, 0]], [3]) # y >= -3 t = ha.new_transition(one, two) t.set_guard_true() error = ha.new_mode('error') t = ha.new_transition(two, error) unsafe_rhs = [ -unsafe_box[0][0], unsafe_box[0][1], -unsafe_box[1][0], unsafe_box[1][1] ] t.set_guard([[-1, 0, 0, 0], [1, 0, 0, 0], [0, -1, 0, 0], [0, 1, 0, 0]], unsafe_rhs) return ha
def test_guard_strengthening(): 'simple 2-mode, 2-guard, 2d system with 1st guard A->B is x <= 2, 2nd guard A->B is y <= 2, and inv(B) is y <= 2' ha = HybridAutomaton() mode_a = ha.new_mode('A') mode_a.set_dynamics(np.identity(2)) mode_b = ha.new_mode('B') mode_b.set_dynamics(np.identity(2)) mode_b.set_invariant([[0, 1]], [2]) trans1 = ha.new_transition(mode_a, mode_b, 'first') trans1.set_guard([[1, 0]], [2]) trans2 = ha.new_transition(mode_a, mode_b, 'second') trans2.set_guard([[0, 1]], [2]) ha.do_guard_strengthening() # trans1 should now have 2 conditions assert (trans1.guard_csr.toarray() == np.array([[1, 0], [0, 1]], dtype=float)).all() assert (trans1.guard_rhs == np.array([2, 2], dtype=float)).all() # trans2 should still have 1 condition since invariant was redundant assert (trans2.guard_csr.toarray() == np.array([[0, 1]], dtype=float)).all()
def make_automaton(): 'make the hybrid automaton' ha = HybridAutomaton() # mode one: x' = y + u1, y' = -x + u2, c' = 1, a' = 0 m1 = ha.new_mode('m1') m1.set_dynamics([[0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]) b_mat = [[1, 0], [0, 1], [0, 0], [0, 0]] b_constraints = [[1, 0], [-1, 0], [0, 1], [0, -1]] b_rhs = [0.5, 0.5, 0.5, 0.5] m1.set_inputs(b_mat, b_constraints, b_rhs) # mode two: x' = -y, y' = x, a' = 0 m2 = ha.new_mode('m2') m2.set_dynamics([[0, -1], [1, 0]]) # m1 invariant: c <= pi/2 m1.set_invariant([[0, 0, 1, 0]], [math.pi / 2]) # guard: c >= pi/2 trans = ha.new_transition(m1, m2) trans.set_guard([[0, 0, -1, 0]], [-math.pi / 2]) # Assign the reset to the transition # y *= -1, also the reset is what is used to change the number of system variables (m1 has four vars, m2 has two) reset_csr = [[1, 0, 0, 0], [0, -1, 0, 0]] # no minkowski sum terms trans.set_reset(reset_csr) return ha
def define_ha(limit): '''make the hybrid automaton and return it''' ha = HybridAutomaton() mode = ha.new_mode('mode') dynamics = loadmat('build.mat') a_matrix = dynamics['A'] b_matrix = csc_matrix(dynamics['B']) mode.set_dynamics(csr_matrix(a_matrix)) # 0.8 <= u1 <= 1.0 u_mat = [[1.0], [-1.0]] u_rhs = [1.0, -0.8] mode.set_inputs(b_matrix, u_mat, u_rhs) error = ha.new_mode('error') y1 = dynamics['C'][0] mat = csr_matrix(y1, dtype=float) trans1 = ha.new_transition(mode, error) rhs = np.array([-limit], dtype=float) # safe trans1.set_guard(mat, rhs) # y3 >= limit return ha
def test_agg_with_reset(): 'test the aggregation of states with a reset' # m1 dynamics: x' == 1, y' == 0, x0: [-3, -2], y0: [0, 1], step: 1.0 # m1 invariant: x + y <= 0 # m1 -> m2 guard: x + y >= 0 and y <= 0.5, reset = [[0, -1, 0], [1, 0, 0]] (x' = -y, y' = x, remove a) # m2 dynamics: x' == 0, y' == 0 # time bound: 4 # expected result: last state is line (not box!) from (0, 0) to (-0.5, -0.5) ha = HybridAutomaton() # mode one: x' = 1, y' = 0, a' = 0 m1 = ha.new_mode('m1') m1.set_dynamics([[0, 0, 1], [0, 0, 0], [0, 0, 0]]) # mode two: x' = 0, y' = 1 m2 = ha.new_mode('m2') m2.set_dynamics([[0, 0], [0, 0]]) # invariant: x + y <= 0 m1.set_invariant([[1, 1, 0]], [0]) # guard: x + y == 0 & y <= 0.5 trans1 = ha.new_transition(m1, m2, 'trans1') trans1.set_guard([[-1, -1, 0], [1, 1, 0], [0, 1, 0]], [0, 0, 0.5]) #trans1.set_reset(np.identity(3)[:2]) trans1.set_reset(np.array([[0, -1, 0], [1, 0, 0]], dtype=float)) # initial set has x0 = [-3, -2], y = [0, 1], a = 1 init_lpi = lputil.from_box([(-3, -2), (0, 1), (1, 1)], m1) init_list = [StateSet(init_lpi, m1)] # settings, step size = 1.0 settings = HylaaSettings(1.0, 4.0) settings.stdout = HylaaSettings.STDOUT_NONE settings.plot.plot_mode = PlotSettings.PLOT_NONE # use agg_box settings.aggstrat.agg_type = Aggregated.AGG_BOX core = Core(ha, settings) result = core.run(init_list) lpi = result.last_cur_state.lpi # 2 basis matrix rows, 4 init constraints rows, 6 rows from guard conditions (2 from each) assert lpi.get_num_rows() == 2 + 4 + 6 verts = result.last_cur_state.verts(core.plotman) assert len(verts) == 3 assert np.allclose(verts[0], verts[-1]) assert pair_almost_in((0, 0), verts) assert pair_almost_in((-0.5, -0.5), verts)
def test_transition(): 'test a discrete transition' ha = HybridAutomaton() # mode one: x' = 1, t' = 1, a' = 0 m1 = ha.new_mode('m1') m1.set_dynamics([[0, 0, 1], [0, 0, 1], [0, 0, 0]]) # mode two: x' = -1, t' = 1, a' = 0 m2 = ha.new_mode('m2') m2.set_dynamics([[0, 0, -1], [0, 0, 1], [0, 0, 0]]) # invariant: t <= 2.5 m1.set_invariant([[0, 1, 0]], [2.5]) # guard: t >= 2.5 trans1 = ha.new_transition(m1, m2, 'trans1') trans1.set_guard([[0, -1, 0]], [-2.5]) # error t >= 4.5 error = ha.new_mode('error') trans2 = ha.new_transition(m2, error, "to_error") trans2.set_guard([[0, -1, 0]], [-4.5]) # initial set has x0 = [0, 1], t = [0, 0.2], a = 1 init_lpi = lputil.from_box([(0, 1), (0, 0.2), (1, 1)], m1) init_list = [StateSet(init_lpi, m1)] # settings, step size = 1.0 settings = HylaaSettings(1.0, 10.0) settings.stdout = HylaaSettings.STDOUT_VERBOSE settings.plot.plot_mode = PlotSettings.PLOT_NONE settings.plot.store_plot_result = True result = Core(ha, settings).run(init_list) ce = result.counterexample assert len(ce) == 2 assert ce[0].mode.name == 'm1' assert ce[0].outgoing_transition.name == 'trans1' assert ce[1].mode.name == 'm2' assert ce[1].outgoing_transition.name == 'to_error' assert ce[1].start[0] + 1e-9 >= 3.0 assert ce[1].end[0] - 1e-9 <= 2.0 polys = [obj[0] for obj in result.plot_data.mode_to_obj_list[0]['m1']] assert len(polys) == 4 polys = [obj[0] for obj in result.plot_data.mode_to_obj_list[0]['m2']] assert len(polys) == 3 assert result.last_cur_state.cur_steps_since_start[0] == 5
def test_agg_to_more_vars(): 'test the aggregation of states with a reset to a mode with new variables' ha = HybridAutomaton() # mode one: x' = 1, a' = 0 m1 = ha.new_mode('m1') m1.set_dynamics([[0, 1], [0, 0]]) # mode two: x' = 0, a' = 0, y' == 1 m2 = ha.new_mode('m2') m2.set_dynamics([[0, 0, 0], [0, 0, 0], [0, 1, 0]]) # invariant: x <= 3.0 m1.set_invariant([[1, 0]], [3.0]) # guard: True trans1 = ha.new_transition(m1, m2, 'trans1') trans1.set_guard_true() reset_mat = [[1, 0], [0, 1], [0, 0]] reset_minkowski = [[0], [0], [1]] reset_minkowski_constraints = [[1], [-1]] reset_minkowski_rhs = [3, -3] # y0 == 3 trans1.set_reset(reset_mat, reset_minkowski, reset_minkowski_constraints, reset_minkowski_rhs) # initial set has x0 = [0, 1], a = 1 init_lpi = lputil.from_box([(0, 1), (1, 1)], m1) init_list = [StateSet(init_lpi, m1)] # settings, step size = 1.0 settings = HylaaSettings(1.0, 4.0) settings.stdout = HylaaSettings.STDOUT_DEBUG settings.plot.plot_mode = PlotSettings.PLOT_NONE settings.plot.store_plot_result = True settings.plot.xdim_dir = 0 settings.plot.ydim_dir = {'m1': 1, 'm2': 2} result = Core(ha, settings).run(init_list) polys = [obj[0] for obj in result.plot_data.mode_to_obj_list[0]['m1']] # 4 steps because invariant is allowed to be false for the final step assert 4 <= len(polys) <= 5, "expected invariant to become false after 4/5 steps" assert_verts_is_box(polys[0], [[0, 1], [1, 1]]) assert_verts_is_box(polys[1], [[1, 2], [1, 1]]) assert_verts_is_box(polys[2], [[2, 3], [1, 1]]) assert_verts_is_box(polys[3], [[3, 4], [1, 1]]) polys = [obj[0] for obj in result.plot_data.mode_to_obj_list[0]['m2']] assert_verts_is_box(polys[0], [[1, 4], [3, 3]]) assert_verts_is_box(polys[1], [[1, 4], [4, 4]])
def make_automaton(): 'make the hybrid automaton' ha = HybridAutomaton() # mode one: x' = 2, y' = 1, a' = 0 m1 = ha.new_mode('m1') m1.set_dynamics([[0, 0, 2], [0, 0, 1], [0, 0, 0]]) # mode two: x' = 1, y' = 1, a' = 0 m2 = ha.new_mode('m2') m2.set_dynamics([[0, 0, 1], [0, 0, 1], [0, 0, 0]]) # invariant: x <= 9.9 m1.set_invariant([[1, 0, 0]], [9.9]) # guard: x >= 9.9 trans = ha.new_transition(m1, m2, 'transition_name') trans.set_guard([[-1, 0, 0]], [-9.9]) # Assign the reset to the transition: # # def set_reset(self, reset_csr=None, reset_minkowski_csr=None, reset_minkowski_constraints_csr=None, # reset_minkowski_constraints_rhs=None): # '''resets are of the form x' = Rx + My, Cy <= rhs, where y are fresh variables # the reset_minowski variables can be None if no new variables are needed. If unassigned, the identity # reset is assumed # # x' are the new variables # x are the old variables # reset_csr is R # reset_minkowski_csr is M # reset_minkowski_constraints_csr is C # reset_minkowski_constraints_rhs is rhs # ''' # we want the reset to set x' := [0, 1], y' := y - 10 reset_csr = [[0, 0, 0], [0, 1, 0], [0, 0, 1]] # two new minkowski variables, y0 = [0, 1], y1 = [-10, -10] minkowski_csr = [[1, 0], [0, 1], [0, 0]] constraints_csr = [[1, 0], [-1, 0], [0, 1], [0, -1]] constraints_rhs = [1, 0, -10, 10] trans.set_reset(reset_csr, minkowski_csr, constraints_csr, constraints_rhs) return ha
def test_zero_dynamics(): 'test a system with zero dynamic (only should process one frame)' ha = HybridAutomaton() # with time and affine variable mode = ha.new_mode('mode') mode.set_dynamics([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) # initial set init_lpi = lputil.from_box([(-5, -5), (0, 1), (0, 0), (1, 1)], mode) init_list = [StateSet(init_lpi, mode)] # settings settings = HylaaSettings(math.pi/4, 20*math.pi) settings.stdout = HylaaSettings.STDOUT_VERBOSE settings.plot.plot_mode = PlotSettings.PLOT_NONE core = Core(ha, settings) core.setup(init_list) core.do_step() # pop core.do_step() # propagate and remove assert core.aggdag.get_cur_state() is None, "cur state should be none, since mode dynamics were zero"
def define_ha(): '''make the hybrid automaton''' ha = HybridAutomaton(discrete=True) a_matrix = [[1, T_const, -T_const], [0, 1, 0], [0, 0, 1]] a_matrix_inv = np.linalg.inv(a_matrix) b_mat = [[0.5 * T_const * T_const, -0.5 * T_const * T_const], [T_const, 0], [0, T_const]] # b_mat = [[0.5*T_const*T_const, -0.5*T_const*T_const, 1], [T_const, 0, 1], [0, T_const, 1]] a_inv_b_mat = -1 * np.matmul(a_matrix_inv, b_mat) # print(a_inv_b_mat) b_constraints = [[1, 0], [-1, 0], [0, 1], [0, -1]] b_rhs = [0.26, 0.46, 0.26, 0.47] # b_constraints = [[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]] # b_rhs = [0.26, 0.46, 0.26, 0.47, 0.005, 0.005] mode = ha.new_mode('mode') mode.set_dynamics(a_matrix_inv) mode.set_inputs(a_inv_b_mat, b_constraints, b_rhs) # error = ha.new_mode('error') # # trans1 = ha.new_transition(mode, error) # trans1.set_guard([[-0, -1, -0], ], [-0.8, ]) return ha
def test_redundant_invariants(): 'test removing of redundant invariants' ha = HybridAutomaton() mode = ha.new_mode('mode') # dynamics: x' = 1, y' = 1, a' = 0 mode.set_dynamics([[0, 0, 1], [0, 0, 1], [0, 0, 0]]) # invariant: x <= 2.5 mode.set_invariant([[1, 0, 0]], [2.5]) # initial set has x0 = [0, 1] init_lpi = lputil.from_box([(0, 1), (0, 1), (1, 1)], mode) init_list = [StateSet(init_lpi, mode)] # settings, step size = 0.1 settings = HylaaSettings(0.1, 5.0) settings.stdout = HylaaSettings.STDOUT_NONE settings.plot.plot_mode = PlotSettings.PLOT_NONE result = Core(ha, settings).run(init_list) # check last cur_state to ensure redundant constraints were not added assert result.last_cur_state.lpi.get_num_rows() == 3 + 2*3 + 1 # 3 for basis matrix, 2*3 for initial constraints
def test_ha(): 'test for the harmonic oscillator example with line initial set (from ARCH 2018 paper)' ha = HybridAutomaton() # with time and affine variable mode = ha.new_mode('mode') mode.set_dynamics([[0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]) error = ha.new_mode('error') trans1 = ha.new_transition(mode, error) trans1.set_guard([[1., 0, 0, 0], [-1., 0, 0, 0]], [4.0, -4.0]) # initial set init_lpi = lputil.from_box([(-5, -5), (0, 1), (0, 0), (1, 1)], mode) init_list = [StateSet(init_lpi, mode)] # settings settings = HylaaSettings(math.pi/4, 2*math.pi) settings.stdout = HylaaSettings.STDOUT_VERBOSE settings.plot.store_plot_result = True settings.plot.plot_mode = PlotSettings.PLOT_NONE core = Core(ha, settings) result = core.run(init_list) assert result.has_concrete_error ce = result.counterexample[0] # [-5.0, 0.6568542494923828, 0.0, 1.0] -> [4.0, 3.0710678118654737, 2.356194490192345, 1.0] assert ce.mode == mode assert np.allclose(ce.start, np.array([-5, 0.65685, 0, 1], dtype=float)) assert np.allclose(ce.end, np.array([4, 3.07106, 2.35619, 1], dtype=float)) # check the reachable state (should always have x <= 3.5) obj_list = result.plot_data.mode_to_obj_list[0][mode.name] for obj in obj_list: verts = obj[0] for vert in verts: x, _ = vert assert x <= 4.9
def test_unaggregation(): 'test an unaggregated discrete transition' ha = HybridAutomaton() # mode one: x' = 1, t' = 1, a' = 0 m1 = ha.new_mode('m1') m1.set_dynamics([[0, 0, 1], [0, 0, 1], [0, 0, 0]]) # mode two: x' = -1, t' = 1, a' = 0 m2 = ha.new_mode('m2') m2.set_dynamics([[0, 0, -1], [0, 0, 1], [0, 0, 0]]) # invariant: t <= 2.5 m1.set_invariant([[0, 1, 0]], [2.5]) # guard: t >= 0.5 trans1 = ha.new_transition(m1, m2, 'trans1') trans1.set_guard([[0, -1, 0]], [-0.5]) # error x >= 4.5 error = ha.new_mode('error') trans2 = ha.new_transition(m2, error, "to_error") trans2.set_guard([[-1, 0, 0]], [-4.5]) # initial set has x0 = [0, 0.2], t = [0, 0.2], a = 1 init_lpi = lputil.from_box([(0, 0.2), (0, 0.2), (1, 1)], m1) init_list = [StateSet(init_lpi, m1)] # settings, step size = 1.0 settings = HylaaSettings(1.0, 10.0) settings.stdout = HylaaSettings.STDOUT_DEBUG settings.plot.store_plot_result = True settings.plot.plot_mode = PlotSettings.PLOT_NONE settings.aggstrat = aggstrat.Unaggregated() result = Core(ha, settings).run(init_list) # expected no exception # m2 should be reachable polys = [obj[0] for obj in result.plot_data.mode_to_obj_list[0]['m2']] assert len(polys) > 15
def test_agg_no_counterexample(): 'test that aggregation to error does not create a counterexample' # m1 dynamics: x' == 1, y' == 0, x0, y0: [0, 1], step: 1.0 # m1 invariant: x <= 3 # m1 -> m2 guard: True # m2 dynamics: x' == 0, y' == 1 # m2 -> error: y >= 3 ha = HybridAutomaton() # mode one: x' = 1, y' = 0, a' = 0 m1 = ha.new_mode('m1') m1.set_dynamics([[0, 0, 1], [0, 0, 0], [0, 0, 0]]) # mode two: x' = 0, y' = 1, a' = 0 m2 = ha.new_mode('m2') m2.set_dynamics([[0, 0, 0], [0, 0, 1], [0, 0, 0]]) # invariant: x <= 3.0 m1.set_invariant([[1, 0, 0]], [3.0]) # guard: True trans1 = ha.new_transition(m1, m2, 'trans1') trans1.set_guard_true() error = ha.new_mode('error') trans2 = ha.new_transition(m2, error, 'trans2') trans2.set_guard([[0, -1, 0]], [-3]) # y >= 3 # initial set has x0 = [0, 1], t = [0, 1], a = 1 init_lpi = lputil.from_box([(0, 1), (0, 1), (1, 1)], m1) init_list = [StateSet(init_lpi, m1)] # settings, step size = 1.0 settings = HylaaSettings(1.0, 10.0) settings.stdout = HylaaSettings.STDOUT_DEBUG settings.plot.plot_mode = PlotSettings.PLOT_NONE result = Core(ha, settings).run(init_list) assert not result.counterexample
def make_automaton(unsafe_box): 'make the hybrid automaton' ha = HybridAutomaton('Deaggregation Example') # x' = 2 m1 = ha.new_mode('mode0_right') m1.set_dynamics([[0, 0, 2], [0, 0, 0], [0, 0, 0]]) m1.set_invariant([[1, 0, 0]], [3.5]) # x <= 3.5 # y' == 2 m2 = ha.new_mode('mode1_up') m2.set_dynamics([[0, 0, 0], [0, 0, 2], [0, 0, 0]]) m2.set_invariant([0., 1., 0], [3.5]) # y <= 3.5 # x' == 2 m3 = ha.new_mode('mode2_right') m3.set_dynamics([[0, 0, 2], [0, 0, -0], [0, 0, 0]]) m3.set_invariant([1., 0, 0], [7]) # x <= 7 t = ha.new_transition(m1, m2) t.set_guard_true() t = ha.new_transition(m2, m3) t.set_guard_true() error = ha.new_mode('error') t = ha.new_transition(m3, error) unsafe_rhs = [ -unsafe_box[0][0], unsafe_box[0][1], -unsafe_box[1][0], unsafe_box[1][1] ] # x >= 1.1 x <= 1.9, y >= 2.7, y <= 4.3 t.set_guard([[-1, 0, 0], [1, 0, 0], [0, -1, 0], [0, 1, 0]], unsafe_rhs) t = ha.new_transition(m2, error) # x >= 1.1 x <= 1.9, y >= 2.7, y <= 4.3 t.set_guard([[-1, 0, 0], [1, 0, 0], [0, -1, 0], [0, 1, 0]], unsafe_rhs) return ha
def test_tt_with_invstr(): 'test time-triggered transitions combined with invariant strengthening' ha = HybridAutomaton() # mode one: x' = 1, a' = 0 m1 = ha.new_mode('m1') m1.set_dynamics([[0, 1], [0, 0]]) m1.set_invariant([[1, 0]], [2.0]) # invariant: x <= 2.0 # mode two: x' = 1, a' = 0 m2 = ha.new_mode('m2') m2.set_dynamics([[0, 1], [0, 0]]) m2.set_invariant([[1, 1]], [4.0]) # x + a <= 4.0 # guard: x >= 2.0 trans1 = ha.new_transition(m1, m2, 'trans1') trans1.set_guard([[-1, 0]], [-2.0]) # error x >= 4.0 error = ha.new_mode('error') trans2 = ha.new_transition(m2, error, "to_error") trans2.set_guard([[-1, 0]], [-4.0]) # initial set has x0 = [0, 1] init_lpi = lputil.from_box([(0, 1), (0, 1)], m1) init_list = [StateSet(init_lpi, m1)] # settings, step size = 0.1 settings = HylaaSettings(0.1, 5.0) settings.stdout = HylaaSettings.STDOUT_VERBOSE settings.plot.plot_mode = PlotSettings.PLOT_NONE # run setup() only and check the result core = Core(ha, settings) core.setup(init_list) assert trans1.time_triggered assert not trans2.time_triggered # not time-triggered because invariant of m2 is True
def define_ha(): '''make the hybrid automaton''' ha = HybridAutomaton() # dynamics: x' = y, y' = -x a_matrix = np.array([[0, 1], [-1, 0]], dtype=float) a_csr = csr_matrix(a_matrix, dtype=float) mode = ha.new_mode('mode') mode.set_dynamics(a_csr) return ha
def test_redundant_inv_transition(): 'test removing of redundant invariants with a transition' ha = HybridAutomaton() mode1 = ha.new_mode('mode1') # dynamics: x' = 1, y' = 1, a' = 0 mode1.set_dynamics([[0, 0, 1], [0, 0, 1], [0, 0, 0]]) # invariant: x <= 2.5 mode1.set_invariant([[1, 0, 0]], [2.5]) mode2 = ha.new_mode('mode2') mode2.set_dynamics([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) ha.new_transition(mode1, mode2).set_guard([[-1, 0, 0]], [-2.5]) # x >= 2.5 # initial set has x0 = [0, 1] init_lpi = lputil.from_box([(0, 1), (0, 1), (1, 1)], mode1) init_list = [StateSet(init_lpi, mode1)] # settings, step size = 0.1 settings = HylaaSettings(0.1, 5.0) settings.stdout = HylaaSettings.STDOUT_DEBUG settings.plot.plot_mode = PlotSettings.PLOT_NONE core = Core(ha, settings) core.setup(init_list) for _ in range(20): core.do_step() assert core.result.last_cur_state.lpi.get_num_rows() == 3 + 2*3 + 1 # 3 for basis matrix, 2*3 for init constraints assert len(core.aggdag.waiting_list) > 2 core.plotman.run_to_completion()
def make_automaton(): 'make the hybrid automaton' ha = HybridAutomaton() mode = ha.new_mode('mode') dynamics = loadmat('iss.mat') a_matrix = dynamics['A'] b_matrix = dynamics['B'] mode.set_dynamics(a_matrix) # input bounds # 0 <= u1 <= 0.1 # 0.8 <= u2 <= 1.0 # 0.9 <= u3 <= 1.0 bounds_mat = [[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]] bounds_rhs = [0.1, 0, 1.0, -0.8, 1.0, -0.9] mode.set_inputs(b_matrix, bounds_mat, bounds_rhs) error = ha.new_mode('error') # the third output defines the unsafe condition y3 = dynamics['C'][2] limit = 0.0005 #limit = 0.0007 # Error condition: y3 * x <= -limit OR y3 >= limit trans1 = ha.new_transition(mode, error) trans1.set_guard(y3, [-limit]) trans2 = ha.new_transition(mode, error) trans2.set_guard(-1 * y3, [-limit]) return ha
def make_automaton(): 'make the hybrid automaton' ha = HybridAutomaton('Hylaa Output (hylaa_check.py)') # mode one: x' = y + u1, y' = -x + + u1 + u2 # u1 in [-0.5, 0.5], u2 in [-1, 0] m1 = ha.new_mode('m1') m1.set_dynamics([[0, 1], [-1, 0]]) b_mat = [[1, 0], [1, 1]] b_constraints = [[1, 0], [-1, 0], [0, 1], [0, -1]] b_rhs = [0.5, 0.5, 0, 1] m1.set_inputs(b_mat, b_constraints, b_rhs) return ha
def define_ha(): '''make the hybrid automaton''' ha = HybridAutomaton(discrete=False) a_matrix = [[0, 1], [-1, 0]] b_mat = [[1], [0]] b_constraints = [[1], [-1]] b_rhs = [0.2, 0.2] mode = ha.new_mode('mode') mode.set_dynamics(a_matrix) mode.set_inputs(b_mat, b_constraints, b_rhs) return ha
def define_ha(): '''make the hybrid automaton''' ha = HybridAutomaton(discrete=True) # dynamics: x' = y, y' = -x a_matrix = np.array([[0.96065997, 0.1947354], [-0.1947354, 0.96065997]], dtype=float) a_csr = csr_matrix(a_matrix, dtype=float) b_mat = [[1, 0], [0, 1]] b_constraints = [[1, 0], [-1, 0], [0, 1], [0, -1]] b_rhs = [0.39340481, -0.39340481, -0.03933961, 0.03933961] mode = ha.new_mode('mode') mode.set_dynamics(a_csr) mode.set_inputs(b_mat, b_constraints, b_rhs, allow_constants=True) return ha
def test_over_time_range(): 'test plotting over time with aggergation (time range)' ha = HybridAutomaton() mode_a = ha.new_mode('A') mode_b = ha.new_mode('B') # dynamics: x' = a, a' = 0 mode_a.set_dynamics([[0, 1], [0, 0]]) mode_b.set_dynamics([[0, 1], [0, 0]]) # invariant: x <= 2.5 mode_a.set_invariant([[1, 0]], [2.5]) trans1 = ha.new_transition(mode_a, mode_b, 'first') trans1.set_guard_true() # initial set has x0 = [0, 0] init_lpi = lputil.from_box([(0, 0), (1, 1)], mode_a) init_list = [StateSet(init_lpi, mode_a)] # settings, step size = 1.0 settings = HylaaSettings(1.0, 4.0) settings.stdout = HylaaSettings.STDOUT_DEBUG settings.process_urgent_guards = True settings.plot.plot_mode = PlotSettings.PLOT_NONE settings.plot.store_plot_result = True settings.plot.xdim_dir = None settings.plot.ydim_dir = 0 result = Core(ha, settings).run(init_list) polys = [obj[0] for obj in result.plot_data.mode_to_obj_list[0][mode_b.name]] # expected with aggegregation: [0, 2.5] -> [1, 3.5] -> [2, 4.5] -> [3, 5.5] -> [4, 6.5] # 4 steps because invariant is allowed to be false for the final step assert len(polys) == 5, "expected invariant to become false after 5 steps" for i in range(5): assert_verts_is_box(polys[i], [[i, i + 3.0], [i, i + 3.0]])
def define_ha(): '''make the hybrid automaton''' ha = HybridAutomaton(discrete=True) a_matrix = [[1, T_const, -T_const], [0, 1, 0], [0, 0, 1]] a_matrix_inv = np.linalg.inv(a_matrix) b_mat = [[0.5 * T_const * T_const, -0.5 * T_const * T_const], [T_const, 0], [0, T_const]] # b_mat = [[0.5 * T_const * T_const, -0.5 * T_const * T_const, 1], [T_const, 0, 1], [0, T_const, 1]] a_inv_b_mat = -1 * np.matmul(a_matrix_inv, b_mat) b_constraints = [[1, 0], [-1, 0], [0, 1], [0, -1]] b_rhs = [0.41, 0.46, 0.41, 0.47] # b_constraints = [[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]] # b_rhs = [0.41, 0.46, 0.41, 0.47, 0.005, 0.005] mode = ha.new_mode('mode') mode.set_dynamics(a_matrix_inv) mode.set_inputs(a_inv_b_mat, b_constraints, b_rhs) return ha
def test_plot_over_time(): 'test doing a plot over time' ha = HybridAutomaton() mode = ha.new_mode('mode') mode.set_dynamics([[0, 1], [-1, 0]]) # initial set init_lpi = lputil.from_box([(-5, -4), (0, 1)], mode) init_list = [StateSet(init_lpi, mode)] # settings settings = HylaaSettings(math.pi/4, math.pi) settings.stdout = HylaaSettings.STDOUT_VERBOSE settings.plot.store_plot_result = True settings.plot.plot_mode = PlotSettings.PLOT_NONE settings.plot.ydim_dir = None # y dimension will be time result = Core(ha, settings).run(init_list) assert not result.has_aggregated_error and not result.has_concrete_error # check the reachable state # we would expect at the end that x = [4, 5], t = pi obj_list = result.plot_data.mode_to_obj_list[0][mode.name] for vert in obj_list[0][0]: x, y = vert assert abs(y) < 1e-6, "initial poly time is wrong" assert abs(-5 - x) < 1e-6 or abs(-4 - x) < 1e-6 for vert in obj_list[-1][0]: x, y = vert assert abs(math.pi - y) < 1e-6, "final poly time is wrong" assert abs(5 - x) < 1e-6 or abs(4 - x) < 1e-6
def test_init_unsat(): 'initial region unsat with multiple invariant conditions' ha = HybridAutomaton() mode = ha.new_mode('A') mode.set_dynamics(np.identity(2)) mode.set_invariant([[1, 0], [1, 0]], [2, 3]) # x <= 2 and x <= 3 # initial set lpi1 = lputil.from_box([(10, 11), (0, 1)], mode) lpi2 = lputil.from_box([(0, 1), (0, 1)], mode) init_list = [StateSet(lpi1, mode), StateSet(lpi2, mode)] # settings settings = HylaaSettings(1, 5) settings.stdout = HylaaSettings.STDOUT_VERBOSE settings.plot.plot_mode = PlotSettings.PLOT_NONE core = Core(ha, settings) core.run(init_list) # expect no exception during running
def test_multiple_init_states(): 'test with multiple initial states in the same mode (should NOT do aggregation)' ha = HybridAutomaton() # with time and affine variable mode = ha.new_mode('mode') mode.set_dynamics([[0, 0], [0, 0]]) # initial set init_lpi = lputil.from_box([(-5, -4), (0, 1)], mode) init_lpi2 = lputil.from_box([(-5, -5), (2, 3)], mode) init_list = [StateSet(init_lpi, mode), StateSet(init_lpi2, mode)] # settings settings = HylaaSettings(math.pi/4, math.pi) settings.stdout = HylaaSettings.STDOUT_NONE settings.plot.plot_mode = PlotSettings.PLOT_NONE core = Core(ha, settings) core.run(init_list)
def test_stateset_bad_init(): 'test constructing a stateset with a basis matrix that is not the identity (should raise error)' # this is from an issue reported by Mojtaba Zarei ha = HybridAutomaton() mode = ha.new_mode('mode') mode.set_dynamics([[0, 1], [-1, 0]]) # initial set init_lpi = lputil.from_box([(-5, -5), (0, 1)], mode) init_list = [StateSet(init_lpi, mode)] # settings settings = HylaaSettings(math.pi/4, math.pi) settings.stdout = HylaaSettings.STDOUT_NONE settings.plot.store_plot_result = True settings.plot.plot_mode = PlotSettings.PLOT_NONE core = Core(ha, settings) result = core.run(init_list) # use last result stateset = result.last_cur_state mode = stateset.mode lpi = stateset.lpi try: init_states = [StateSet(lpi, mode)] settings = HylaaSettings(0.1, 0.1) core = Core(ha, settings) result = core.run(init_states) assert False, "assertion should be raised if init basis matrix is not identity" except RuntimeError: pass
def test_invariants(): 'test invariant trimming' ha = HybridAutomaton() mode = ha.new_mode('mode') # dynamics: x' = 1, y' = 1, a' = 0 mode.set_dynamics([[0, 0, 1], [0, 0, 1], [0, 0, 0]]) # invariant: x <= 2.5 mode.set_invariant([[1, 0, 0]], [2.5]) # initial set has x0 = [0, 1] init_lpi = lputil.from_box([(0, 1), (0, 1), (1, 1)], mode) init_list = [StateSet(init_lpi, mode)] # settings, step size = 1.0 settings = HylaaSettings(1.0, 5.0) settings.stdout = HylaaSettings.STDOUT_VERBOSE settings.plot.store_plot_result = True result = Core(ha, settings).run(init_list) # check the reachable state polys = [obj[0] for obj in result.plot_data.mode_to_obj_list[0][mode.name]] # 4 steps because invariant is allowed to be false for the final step assert len(polys) == 4, "expected invariant to become false after 4 steps" assert_verts_is_box(polys[0], [[0, 1], [0, 1]]) assert_verts_is_box(polys[1], [[1, 2], [1, 2]]) assert_verts_is_box(polys[2], [[2, 3], [2, 3]]) assert_verts_is_box(polys[3], [[3, 3.5], [3, 4]])