Ejemplo n.º 1
0
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"
Ejemplo n.º 2
0
def make_init(ha):
    '''returns list of initial states'''

    bounds_list = []

    dims = list(ha.modes.values())[0].a_csr.shape[0]

    for dim in range(dims):
        if dim < 10:
            lb = 0.0002
            ub = 0.00025
        elif dim == 25:
            lb = -0.0001
            ub = 0.0001
        else:
            lb = ub = 0

        bounds_list.append((lb, ub))

    mode = ha.modes['mode']
    init_lpi = lputil.from_box(bounds_list, mode)

    init_list = [StateSet(init_lpi, mode)]

    return init_list
Ejemplo n.º 3
0
    def make_op_transition(self, t, t_lpi, state, parent_node):
        'make an OpTransition object, can return null if lp solving fails'

        step_in_mode = state.cur_step_in_mode
        steps_since_start = state.cur_steps_since_start
        is_concrete = state.is_concrete

        successor_has_inputs = t.to_mode.b_csr is not None

        op = OpTransition(step_in_mode, parent_node, None, t, None)

        succeeded = self.settings.aggstrat.pretransition(t, t_lpi, op)

        if not succeeded:
            self.core.print_verbose(
                f"Warning: aggstrat.pretransition returned None (LP solving failed)"
            )
            op = None
        else:
            lputil.add_reset_variables(t_lpi, t.to_mode.mode_id, t.transition_index, \
                reset_csr=t.reset_csr, minkowski_csr=t.reset_minkowski_csr, \
                minkowski_constraints_csr=t.reset_minkowski_constraints_csr, \
                minkowski_constraints_rhs=t.reset_minkowski_constraints_rhs, successor_has_inputs=successor_has_inputs)

            if not t_lpi.is_feasible():
                raise RuntimeError(
                    "cur_state became infeasible after reset was applied")

            op_list = [op]
            state = StateSet(t_lpi, t.to_mode, steps_since_start, op_list,
                             is_concrete)
            op.poststate = state

        return op
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
def make_init(ha):
    'make the initial states'

    mode = ha.modes['mode0_right']
    init_lpi = lputil.from_box([(0, 1), (0, 1.0), (1.0, 1.0)], mode)

    init_list = [StateSet(init_lpi, mode)]

    return init_list
Ejemplo n.º 8
0
def make_init(ha):
    '''returns list of initial states'''

    mode = ha.modes['mode']
    init_lpi = lputil.from_box([[0.4, 5.0], [-0.2, 0.5], [-0.2, 0.5]], mode)

    init_list = [StateSet(init_lpi, mode)]

    return init_list
Ejemplo n.º 9
0
def make_init(ha):
    'make the initial states'

    p2 = ha.modes['Far']
    init_lpi = lputil.from_box([(-925.0, -875.0), (-425.0, -375.0), (0.0, 0.0),
                                (0.0, 0.0), (0.0, 0.0), (1.0, 1.0)], p2)
    init_list = [StateSet(init_lpi, p2)]

    return init_list
Ejemplo n.º 10
0
def make_init(ha):
    '''returns list of initial states'''

    mode = ha.modes['mode']
    # init states: x in [-5, -4], y in [0, 1]
    init_lpi = lputil.from_box([[-6, -5], [0, 1]], mode)

    init_list = [StateSet(init_lpi, mode)]

    return init_list
Ejemplo n.º 11
0
def make_init(ha):
    'make the initial states'

    # initial set has x0 = [-5, -4], y = [0, 1], c = 0, a = 1
    mode = ha.modes['m1']
    init_lpi = lputil.from_box([(-5, -4), (0, 1), (0, 0), (1, 1)], mode)

    init_list = [StateSet(init_lpi, mode)]

    return init_list
Ejemplo n.º 12
0
def make_init(ha):
    '''returns list of initial states'''

    mode = ha.modes['mode']
    init_lpi = lputil.from_box([[0.53, 10], [0.2, 0.6], [0.2, 0.6]], mode)
    # print(init_lpi.get_full_constraints().toarray(), init_lpi.get_rhs(), init_lpi.get_types())

    init_list = [StateSet(init_lpi, mode)]

    return init_list
Ejemplo n.º 13
0
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]])
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
def make_init(ha):
    'make the initial states'

    # initial set has every variable as [-0.0001, 0.0001]
    mode = ha.modes['mode']

    dims = mode.a_csr.shape[0]
    init_box = dims * [[-0.0001, 0.0001]]
    init_lpi = lputil.from_box(init_box, mode)

    init_list = [StateSet(init_lpi, mode)]

    return init_list
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
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)
Ejemplo n.º 18
0
def make_init(ha):
    'make the initial states'

    p2 = ha.modes['Far']
    box = [[-925.0, -875.0], [-425.0, -375.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [1.0, 1.0]]

    # move init y up 300
    #box[0][0] += 400
    #box[0][1] += 400
    
    init_lpi = lputil.from_box(box, p2)
    init_list = [StateSet(init_lpi, p2)]

    return init_list
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
def define_init_states(ha):
    '''returns a list of StateSet objects'''
    # Variable ordering: [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, t, affine]
    rv = []

    mode = ha.modes['negAngleInit']

    #X_0 = {center + alpha * generator, alpha in [-1, 1]}
    center = [-0.0432, -11, 0, 30, 0, 30, 360, -0.0013, 30, -0.0013, 30, 0, 1]
    generator = [0.0056, 4.67, 0, 10, 0, 10, 120, 0.0006, 10, 0.0006, 10, 0, 0]

    lpi = lputil.from_zonotope(center, [generator], mode)

    rv.append(StateSet(lpi, mode))

    return rv
Ejemplo n.º 21
0
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
Ejemplo n.º 22
0
def make_init(ha, box):
    'make the initial states'

    mode = ha.modes['move_free']
    # px==-0.0165 & py==0.003 & vx==0 & vy==0 & I==0 & affine==1.0
    init_lpi = lputil.from_box(box, mode)

    #init_lpi = lputil.from_box([(-0.02, -0.02), (-0.005, -0.003), (0, 0), (0, 0), (0, 0), (1.0, 1.0)], mode)
    #start = [-0.02, -0.004213714568273684, 0.0, 0.0, 0.0, 1.0]
    #tol = 1e-7
    #init_lpi = lputil.from_box([(x - tol, x + tol) if i < 2 else (x, x) for i, x in enumerate(start)], mode)

    init_list = [StateSet(init_lpi, mode)]

    # why does 0.003-0.005 reach an error with i=30 for roots first but not leaves first?

    return init_list
Ejemplo n.º 23
0
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
Ejemplo n.º 24
0
def aggregate_chull(agg_list, op_list, print_func):
    '''
    perform template-based aggregation on the passed-in list of states

    Currently, this can either use box template directions or arnoldi (+box) template directions
    '''

    min_step = min([state.cur_steps_since_start[0] for state in agg_list])
    max_step = max([state.cur_steps_since_start[1] for state in agg_list])
    step_interval = [min_step, max_step]

    print_func("Convex hull aggregation time step interval: {}".format(step_interval))

    postmode = agg_list[0].mode
    lpi_list = [state.lpi for state in agg_list]

    new_lpi = lputil.aggregate_chull(lpi_list, postmode)

    return StateSet(new_lpi, agg_list[0].mode, step_interval, op_list, is_concrete=False)
Ejemplo n.º 25
0
def make_init(ha):
    'make the initial states'

    p2 = ha.modes['P2']

    box = [(-925.0, -875.0), (-425.0, -375.0), (0.0, 0.0), (0.0, 0.0),
           (0.0, 0.0), (1.0, 1.0)]

    init_lpi = lputil.from_box(box, p2)
    init_list = [StateSet(init_lpi, p2)]

    #corners = [[(box[0][0], box[0][0]), (box[1][0], box[1][0]), (0, 0), (0, 0), (0, 0), (1, 1)], \
    #           [(box[0][1], box[0][1]), (box[1][0], box[1][0]), (0, 0), (0, 0), (0, 0), (1, 1)], \
    #           [(box[0][1], box[0][1]), (box[1][1], box[1][1]), (0, 0), (0, 0), (0, 0), (1, 1)], \
    #           [(box[0][0], box[0][0]), (box[1][0], box[1][0]), (0, 0), (0, 0), (0, 0), (1, 1)]]

    #init_list = [StateSet(lputil.from_box(c, p2), p2) for c in corners]

    return init_list
Ejemplo n.º 26
0
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
Ejemplo n.º 27
0
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]])
Ejemplo n.º 28
0
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
Ejemplo n.º 29
0
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
Ejemplo n.º 30
0
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()