Exemplo n.º 1
0
def define_ha(settings, args):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    usafe_r = args[0]

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3", "x4"]
    dim = len(ha.variables)

    loc1 = ha.new_mode('loc1')
    loc1.c_vector = np.array([0, 0, 0, 0], dtype=float)
    print(loc1.c_vector.shape)

    a_matrix = np.array(
        [[1, 0, 0.1, 0], [0, 1, 0, 0.1], [0, 0, 0.8870, 0.0089], [0, 0, 0.0089, 0.8870]], dtype=float)
    b_matrix = np.array([[0, 0], [0, 0], [1, 0], [0, 1]], dtype=float)

    # Q = 1 * np.eye(dim)
    Q = np.array([[1, 0, 0, 0],
                 [0, 1, 0, 0],
                 [0, 0, 1, 0],
                 [0, 0, 0, 100]], dtype=float)

    u_dim = len(b_matrix[0])
    R = np.eye(u_dim)
    (X, L, G) = care(a_matrix, b_matrix, Q, R)
    control_f = open("./control_vals.txt", "a")
    control_f.write("Q: " + str(Q) + "\n")
    control_f.write("P: " + str(X) + "\n")
    control_f.write("K: " + str(G) + "\n")
    control_f.write("PBK: " + str(np.matmul(X, np.matmul(b_matrix, G))) + "\n")
    control_f.write("PA: " + str(np.matmul(X, a_matrix)) + "\n")
    control_f.write("A'P: " + str(np.matmul(a_matrix.T, X)) + "\n")
    control_f.close()
    k_matrix = np.array(G)

    loc1.a_matrix = a_matrix - np.matmul(b_matrix, k_matrix)

    error = ha.new_mode('_error')
    error.is_error = True

    trans = ha.new_transition(loc1, error)

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0, 0.0, 0.0], 2.0))
        usafe_set_constraint_list.append(LinearConstraint([1.0, 0.0, 0.0, 0.0], -1.0))
        usafe_set_constraint_list.append(LinearConstraint([0.0, -1.0, 0.0, 0.0], 3))
        usafe_set_constraint_list.append(LinearConstraint([0.0, 1.0, 0.0, 0.0], -2))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 2
0
def define_ha(settings, usafe_r):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x", "y"]

    loc1 = ha.new_mode('loc1')

    loc1.a_matrix = np.array([[-0.1, 1], [-1, -0.1]])
    # loc1.a_matrix = np.array([[0, 1], [-1, 0]])
    loc1.c_vector = np.array([2, 0], dtype=float)
    # loc1.set_dynamics(a_matrix, c_vector)
    error = ha.new_mode('_error')
    error.is_error = True

    trans = ha.new_transition(loc1, error)

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0], 2))
        usafe_set_constraint_list.append(LinearConstraint([1.0, 0.0], 2))
        usafe_set_constraint_list.append(LinearConstraint([0.0, -1.0], -1))
        usafe_set_constraint_list.append(LinearConstraint([0.0, 1.0], 6))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 3
0
def init_hr_to_star(settings, hr, mode):
    'convert a HyperRectangle to a Star'

    assert isinstance(mode, LinearAutomatonMode)

    num_dims = len(hr.dims)

    constraint_list = []

    for i in range(num_dims):
        (low, high) = hr.dims[i]

        vector = np.array([1 if d == i else 0 for d in range(num_dims)],
                          dtype=float)
        value = high
        constraint_list.append(LinearConstraint(vector, value))

        vector = np.array([-1 if d == i else 0 for d in range(num_dims)],
                          dtype=float)
        value = -low
        constraint_list.append(LinearConstraint(vector, value))

    parent = InitParent(mode)
    basis_matrix = np.identity(num_dims, dtype=float)

    center = np.array([0.0] * num_dims, dtype=float)
    return Star(settings, center, basis_matrix, constraint_list, parent, mode)
Exemplo n.º 4
0
    def compute_ce_vector(self,
                          simulation,
                          usafe_set_constraint_list,
                          direction=None,
                          sim_start_time=0,
                          current_mode_idx=0):
        usafe_points = []
        ce_vector = []
        for time in self.error_time_steps[current_mode_idx]:
            point = simulation[int(time) - sim_start_time]
            usafe_lpi = LpInstance(self.num_dims, self.num_dims)
            identity_matrix = np.identity(self.num_dims)
            usafe_lpi.update_basis_matrix(np.identity(self.num_dims))
            for dim in range(identity_matrix.ndim):
                lc = LinearConstraint(identity_matrix[dim], point[dim])
                usafe_lpi.add_basis_constraint(lc.vector, lc.value)
                lc = LinearConstraint(-1 * identity_matrix[dim], -point[dim])
                usafe_lpi.add_basis_constraint(lc.vector, lc.value)
            for constraints in usafe_set_constraint_list:
                usafe_lpi.add_basis_constraint(constraints.vector,
                                               constraints.value)

            direction = np.zeros(self.num_dims)
            usafe_point = np.zeros(self.num_dims)
            is_feasible = usafe_lpi.minimize(direction,
                                             usafe_point,
                                             error_if_infeasible=False)
            usafe_points.append(usafe_point)
            if is_feasible:
                ce_vector.append(1)
            else:
                ce_vector.append(0)
        return ce_vector
Exemplo n.º 5
0
def define_ha(settings, args):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    usafe_r = args[0]
    x_ref = args[1]
    step_inputs = args[2]
    k_matrix = args[3]
    a_matrices = args[4]
    b_matrices = args[5]
    dim = len(b_matrices[0])

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3", "x4", "t"]

    locations = []
    n_locations = len(step_inputs)
    for idx in range(n_locations):
        loc_name = 'loc' + str(idx)
        loc = ha.new_mode(loc_name)
        a_matrix = a_matrices[idx]
        b_matrix = b_matrices[idx]
        b_k_matrix = np.matmul(b_matrix, k_matrix)
        loc.a_matrix = a_matrix + b_k_matrix
        c_vector = -np.matmul(b_k_matrix, x_ref[idx])
        c_vector = c_vector + np.matmul(b_matrix, step_inputs[idx])
        c_vector[dim - 1] = step_size
        # print(c_vector)
        loc.c_vector = c_vector
        loc.inv_list.append(
            LinearConstraint([0.0, 0.0, 0.0, 0.0, 1.0],
                             step_size * idx))  # t <= 0.1
        locations.append(loc)

    for idx in range(n_locations - 1):
        trans = ha.new_transition(locations[idx], locations[idx + 1])
        trans.condition_list.append(
            LinearConstraint([-0.0, -0.0, 0.0, 0.0, -1.0],
                             -step_size * idx))  # t >= 0.1

    error = ha.new_mode('_error')
    error.is_error = True

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(
            LinearConstraint([1.0, 0.0, 0.0, 0.0, 0.0], -3.5))
        # usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0, 0.0, 0.0, 0.0], -3.5))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for idx in range(n_locations - 1):
        trans = ha.new_transition(locations[idx], error)
        for constraint in usafe_set_constraint_list:
            trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 6
0
    def test_eat_star_bs1_1d(self):
        'test 1d eat-star with example-1 derived from the ball_string system'

        settings = HylaaSettings(0.01, 2.0)
        array = np.array
        ha = LinearHybridAutomaton('Test Automaton')
        ha.variables = ["x"]
        mode = ha.new_mode('test_mode')

        center = array([0.])

        # x = 0.5 * alpha    (2x = alpha)

        # 0 <= alpha <= 1.0   -> (0 <= x <= 0.5)
        cur_star = Star(settings, center, array([[0.5]]), [ \
           LinearConstraint(array([1.]), 1.0), \
           LinearConstraint(array([-1.]), 0.0)], \
           None, mode)

        # 2.0 <= alpha <= 3.0    -> (1.0 <= x <= 1.5)
        new_star = Star(settings, center, array([[0.5]]), [ \
           LinearConstraint(array([1.]), 3.0), \
           LinearConstraint(array([-1.]), -2.0)], \
           None, mode)

        cur_star.eat_star(new_star)

        # should be 0.0 <= alpha <= 3.0
        self.assertAlmostEqual(cur_star.constraint_list[0].value, 3.0)
        self.assertAlmostEqual(cur_star.constraint_list[1].value, 0.0)
Exemplo n.º 7
0
    def test_eat_star_bs2_1d(self):
        'test 1d eat-star derived from example-2 of the ball_string system'

        array = np.array
        settings = HylaaSettings(0.01, 2.0)
        ha = LinearHybridAutomaton('Test Automaton')
        ha.variables = ["x"]
        mode = ha.new_mode('test_mode')
        center = array([0.])
        basis_matrix = array([[1.0]], dtype=float)

        # -alpha <= 1.0  ----> -1.0 <= alpha
        # 2 * alpha <= -1   ----> alpha <= -0.5
        cur_star = Star(settings, center, basis_matrix, [\
           LinearConstraint(array([-1.]), 1.0), \
           LinearConstraint(array([2.0]), -1.0),\
           ], \
           None, mode)

        # -0.7 <= alpha <= -0.6
        new_star = Star(settings, center, basis_matrix, [\
           LinearConstraint(array([-1.]), 0.7), \
           LinearConstraint(array([1.]), -0.6), \
           ], \
           None, mode)

        cur_star.eat_star(new_star)

        # should be unchanged: -0.0 <= alpha and 2 * alpha <= 1.0
        self.assertAlmostEqual(cur_star.constraint_list[0].value, 1.0)
        self.assertAlmostEqual(cur_star.constraint_list[1].value, -1.0)
Exemplo n.º 8
0
    def trim_to_invariant(self):
        '''
        trim the star to the mode's invariant.

        returns (is_still_feasible, inv_vio_star_list)
        '''

        still_feasible = True
        inv_vio_star_list = []

        if len(self.mode.inv_list) > 0:
            assert self.mode.num_inputs == 0, "mode invariants + dynamics with time-varying inputs not yet supported"

            # check each invariant condition to see if it is violated
            lpi = self.get_lpi()

            for lin_con in self.mode.inv_list:
                objective = np.array([-ele for ele in lin_con.vector], dtype=float)
                result = np.zeros(2 * self.num_dims)

                lpi.minimize(objective, result, error_if_infeasible=True)

                offset = result[0:self.num_dims]
                point = self.center + offset

                val = np.dot(point, lin_con.vector)

                if val > lin_con.value:
                    # add the constraint to the star's constraints
                    # first, convert the condition to the star's basis

                    # basis vectors (non-transpose) * standard_condition
                    basis_condition = np.dot(self.basis_matrix, lin_con.vector)
                    center_value = np.dot(self.center, lin_con.vector)
                    remaining_value = lin_con.value - center_value

                    basis_lc = LinearConstraint(basis_condition, remaining_value)

                    if self.settings.plot.plot_mode != PlotSettings.PLOT_NONE:
                        # use the inverse of the invariant constraint for plotting

                        inv_lc = LinearConstraint([-1 * ele for ele in basis_lc.vector], -basis_lc.value)

                        inv_vio_star = self.clone()
                        inv_vio_star.add_basis_constraint(inv_lc)

                        # re-check for feasibility after adding the constraint
                        if inv_vio_star.is_feasible():
                            inv_vio_star_list.append(inv_vio_star)

                    # add the constraint AFTER making the plot violation star
                    self.add_basis_constraint(basis_lc)

                # we added a new constraint to the star, check if it's still feasible
                if not self.is_feasible():
                    still_feasible = False
                    break # don't check the remaining invariant linear conditions

        return (still_feasible, inv_vio_star_list)
Exemplo n.º 9
0
def define_ha(settings, args):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    usafe_r = args[0]
    if len(args) > 2:
        x_ref = args[1]
        step_inputs = args[2]

    ha = LinearHybridAutomaton()
    ha.variables = ["x", "y", "t"]

    a_matrix = np.array([[2, -1, 0], [1, 0, 0], [0, 0, 1]])
    b_matrix = np.array([[2], [0], [0]], dtype=float)
    k_matrix = np.array([[-0.85559968, 0.46109066, 0]], dtype=float)

    locations = []
    n_locations = len(step_inputs)
    for idx in range(n_locations):
        loc_name = 'loc' + str(idx)
        loc = ha.new_mode(loc_name)
        b_k_matrix = np.matmul(b_matrix, k_matrix)
        loc.a_matrix = a_matrix + b_k_matrix
        c_vector = -np.matmul(b_k_matrix, x_ref[idx])
        c_vector = c_vector + np.matmul(b_matrix, step_inputs[idx])
        c_vector[len(ha.variables) - 1] = step_size
        # print(c_vector)
        loc.c_vector = c_vector
        # loc.c_vector = np.array([step_inputs[idx], step_inputs[idx], 1], dtype=float)
        loc.inv_list.append(LinearConstraint([0.0, 0.0, 1.0],
                                             step_size * idx))  # t <= 0.1
        locations.append(loc)

    for idx in range(n_locations - 1):
        trans = ha.new_transition(locations[idx], locations[idx + 1])
        trans.condition_list.append(
            LinearConstraint([-0.0, -0.0, -1.0], -step_size * idx))  # t >= 0.1

    error = ha.new_mode('_error')
    error.is_error = True

    trans = ha.new_transition(locations[0], error)

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0, 0.0], 1))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 10
0
def define_ha(settings, usafe_r):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x", "y"]

    loc1 = ha.new_mode('loc1')

    a_matrix = np.array([[0.98965, 1.4747e-08], [7.4506e-09, 0]], dtype=float)

    loc1.c_vector = np.array([0, 0], dtype=float)

    b_matrix = np.array([[16], [0]], dtype=float)

    Q = np.array([[1, 0], [0, 1]], dtype=float)
    u_dim = len(b_matrix[0])

    R = np.eye(u_dim)
    (X, L, G) = care(a_matrix, b_matrix, Q, R)
    control_f = open("./control_vals.txt", "a")
    control_f.write("Q: "+str(Q)+"\n")
    control_f.write("P: "+str(X)+"\n")
    control_f.write("K: "+str(G)+"\n")
    control_f.write("PBK: "+str(np.matmul(X, np.matmul(b_matrix, G)))+"\n")
    control_f.write("PA: "+str(np.matmul(X, a_matrix))+"\n")
    control_f.write("A'P: "+str(np.matmul(a_matrix.T, X))+"\n")
    control_f.close()
    k_matrix = np.array(G)

    a_bk_matrix = a_matrix - np.matmul(b_matrix, k_matrix)
    loc1.a_matrix = a_bk_matrix

    error = ha.new_mode('_error')
    error.is_error = True

    trans = ha.new_transition(loc1, error)

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0], -0.2))
        usafe_set_constraint_list.append(LinearConstraint([0.0, 1.0], 0.0))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 11
0
def define_ha(settings, usafe_r):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x", "y", "t"]

    step_inputs = [[-0.6835318568657612], [-0.4274739688077575],
                   [0.4047028719575525], [-1.7181706660550653],
                   [0.6195838154872904], [-0.981255069072019],
                   [1.0521099187388827], [1.1240072822724865], [2.0],
                   [1.0260517738498387]]
    a_matrix = np.array([[0, 2, 0], [1, 0, 0], [0, 0, 1]])
    b_matrix = np.array([[1], [1], [0]], dtype=float)

    locations = []
    n_locations = len(step_inputs)
    for idx in range(n_locations):
        loc_name = 'loc' + str(idx)
        loc = ha.new_mode(loc_name)
        loc.a_matrix = a_matrix
        c_vector = np.matmul(b_matrix, step_inputs[idx])
        c_vector[len(ha.variables) - 1] = step_size
        print(c_vector)
        loc.c_vector = c_vector
        # loc.c_vector = np.array([step_inputs[idx], step_inputs[idx], 1], dtype=float)
        loc.inv_list.append(LinearConstraint([0.0, 0.0, 1.0], step_size * idx))
        locations.append(loc)

    for idx in range(n_locations - 1):
        trans = ha.new_transition(locations[idx], locations[idx + 1])
        trans.condition_list.append(
            LinearConstraint([-0.0, -0.0, -1.0], -idx * step_size))

    error = ha.new_mode('_error')
    error.is_error = True

    trans = ha.new_transition(locations[0], error)

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0, 0.0], 1))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 12
0
def define_init_states(ha):
    '''returns a list of (mode, list(LinearConstraint])'''
    # Variable ordering: [x, y]
    rv = []
    
    constraints = []
    constraints.append(LinearConstraint([-1, 0], 5)) # -5.0 <= x
    constraints.append(LinearConstraint([1, 0], -5)) # x <= -5.0
    constraints.append(LinearConstraint([0, -1], 0)) # 0.0 <= y
    constraints.append(LinearConstraint([0, 1], 0)) # y <= 0.0
    rv.append((ha.modes['loc1'], constraints))
    
    return rv
Exemplo n.º 13
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["temp", "t"]

    power = 7.0
    high = 22.0
    low = 18.0
    c = 0.4
    Tenv = 10.0

    on = ha.new_mode('on')
    on.a_matrix = np.array([[-c, 0.0], [0.0, 0.0]], dtype=float)
    on.c_vector = np.array([Tenv * c + power, 1.0], dtype=float)
    on.inv_list.append(LinearConstraint([1.0, 0.0], high))  # temp <= high

    off = ha.new_mode('off')
    off.a_matrix = np.array([[-c, 0.0], [0.0, 0.0]], dtype=float)
    off.c_vector = np.array([Tenv * c, 1.0], dtype=float)
    off.inv_list.append(LinearConstraint([-1.0, 0.0], -low))  # temp >= low

    trans1_2 = ha.new_transition(on, off)
    trans1_2.condition_list.append(LinearConstraint([-1.0, 0.0],
                                                    -high))  # temp > high

    trans2_1 = ha.new_transition(off, on)
    trans2_1.condition_list.append(LinearConstraint([1.0, 0.0],
                                                    low))  # temp < low

    error = ha.new_mode('_error')
    error.is_error = True

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0],
                                                          -21))  # temp >= high
        # usafe_set_constraint_list.append(LinearConstraint([1.0, 0.0], low))  # temp <= low
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])
        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    trans1_error = ha.new_transition(on, error)
    trans2_error = ha.new_transition(off, error)
    for constraint in usafe_set_constraint_list:
        trans1_error.condition_list.append(constraint)
        trans2_error.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 14
0
    def test_eat_star_bs1_2d(self):
        'test 2d eat-star derived from example-1 of the ball_string system'

        array = np.array
        mode = make_debug_mode()
        center = array([0., 0.])

        basis_matrix = array([[1.0, 0], [0, 0.5]])

        cur_star = Star(HylaaSettings(0.01, 2.0), center, basis_matrix, [ \
           LinearConstraint(array([1, 0]), 1), \
           LinearConstraint(array([-1., 0.]), 0), \
           LinearConstraint(array([0., 1.]), 1.0), \
           LinearConstraint(array([0., -1.]), 0.0)], \
           None, mode)

        new_star = Star(HylaaSettings(0.01, 2.0), center, basis_matrix, [ \
           LinearConstraint(array([1, 0]), 1), \
           LinearConstraint(array([-1, 0]), 0), \
           LinearConstraint(array([0., 1.]), 3.0), \
           LinearConstraint(array([0., -1.]), -2.0)], \
           None, mode)

        cur_star.eat_star(new_star)

        new_point = new_star.get_feasible_point()
        self.assertTrue(cur_star.contains_point(new_point))
Exemplo n.º 15
0
    def test_eat_star_bs2_2d(self):
        'test 2d eat-star derived from example-2 of the ball_string system'

        array = np.array
        mode = make_debug_mode()
        settings = HylaaSettings(0.01, 2.0)
        center = array([0., 0.])

        basis_matrix = array([[1.0, 0], [0, 1.0]], dtype=float)

        cur_star = Star(settings, center, basis_matrix, [\
           LinearConstraint(array([-1., 0.]), 1.0), \
           LinearConstraint(array([0., 1.]), 2.1), \
           LinearConstraint(array([0., -1.]), -2.0), \
           LinearConstraint(array([2.0, 0.]), -1),\
           ], \
           None, mode)

        new_star = Star(settings, center, basis_matrix, [\
           LinearConstraint(array([-1., 0.]), 0.7), \
           LinearConstraint(array([1., 0.]), -0.6), \
           LinearConstraint(array([0., 1.]), 2.3), \
           LinearConstraint(array([0., -1.]), -2.2), \
           ], \
           None, mode)

        cur_star.eat_star(new_star)

        new_point = new_star.get_feasible_point()
        self.assertTrue(cur_star.contains_point(new_point))
Exemplo n.º 16
0
    def add_std_constraint_direction(self, standard_direction):
        '''
        add a constraint direction, given in the standard basis to the star
        '''

        assert isinstance(standard_direction, np.ndarray)
        assert standard_direction.shape == (self.num_dims, )

        lpi = self.get_lpi()
        basis_direction = np.dot(self.basis_matrix, standard_direction)

        result = np.zeros((2 * self.num_dims))

        # multiplying by -1 turns it into a maximization
        lpi.minimize(-1 * standard_direction, result, error_if_infeasible=True)

        opt_pt = result[:self.num_dims]
        basis_pt = self.vector_to_star_basis(opt_pt)

        opt_val = np.dot(basis_pt, basis_direction)

        # offset the multiple to account for the stars' centers
        opt_val -= np.dot(basis_direction, self.center)

        lc = LinearConstraint(basis_direction, opt_val)

        self.add_basis_constraint(lc)
Exemplo n.º 17
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3", "x4"]
    #
    loc1 = ha.new_mode('loc1')
    a_matrix = np.array([[1, 0, 0.1, 0], [0, 1, 0, 0.1],
                         [0, 0, 0.8870, 0.0089], [0, 0, 0.0089, 0.8870]],
                        dtype=float)

    # exp 1
    b_matrix = np.array([[1, 0], [0, 0], [1, 0], [0, 1]], dtype=float)

    print(a_matrix, b_matrix)
    R_mult_factor = 0.1

    Q_matrix = np.eye(len(a_matrix[0]), dtype=float)

    u_dim = len(b_matrix[0])
    R_matrix = R_mult_factor * np.eye(u_dim)

    print(a_matrix, b_matrix, Q_matrix, R_matrix)
    k_matrix = get_input(a_matrix, b_matrix, Q_matrix, R_matrix)

    print(k_matrix)
    a_bk_matrix = a_matrix - np.matmul(b_matrix, k_matrix)

    loc1.a_matrix = a_bk_matrix
    loc1.c_vector = np.array([0.0, 0.0, 0.0, 0.0], dtype=float)
    # print(a_bk_matrix)

    error = ha.new_mode('_error')
    error.is_error = True

    usafe_set_constraint_list = []
    if usafe_r is None:

        # exp 1
        # significant diff (10 sec) across equivalent/non-equ runs for p_intersect without reverse
        # usafe_set_constraint_list.append(LinearConstraint([1.0, 0.0, 0.0, 0.0], -4.8))

        # exp 2
        # significant diff (13-15 sec) across equivalent/non-equ runs for p_intersect without reverse
        # usafe_set_constraint_list.append(LinearConstraint([0.0, 0.0, 1.0, 0.0], -5.0))

        # exp 3
        usafe_set_constraint_list.append(
            LinearConstraint([1.0, 0.0, 0.0, 0.0], -5.2))

    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])
        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    trans = ha.new_transition(loc1, error)
    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 18
0
def define_ha(settings, usafe_r):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton(name="Oscillating Particle")
    ha.variables = ["x", "y", "z"]

    loc1 = ha.new_mode('loc1')

    loc1.a_matrix = np.array([[0.722468865032875, -0.523371053120237, 0], [0.785056579680355, 0.696300312376864, 0], [0, 0, 0.930530895811206]])
    loc1.c_vector = np.array([0, 0.1, 0.03], dtype=float)
    error = ha.new_mode('_error')
    error.is_error = True

    trans = ha.new_transition(loc1, error)

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([0, -1, 0], -0.5))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 19
0
def define_ha(settings, usafe_r):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton(name="Damped Oscillator")
    ha.variables = ["x", "y"]

    loc1 = ha.new_mode('loc1')

    loc1.a_matrix = np.array([[0.960659959352277, 0.194735414472060],
                              [-0.194735414472060, 0.960659959352277]])
    loc1.c_vector = np.array([0.196702394590923, -0.019669801188631],
                             dtype=float)
    error = ha.new_mode('_error')
    error.is_error = True

    trans = ha.new_transition(loc1, error)

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([0.0, -1.0], -4))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 20
0
    def convert_usafe_basis_pred_in_basis_center(usafe_basis_pred,
                                                 basis_center):
        offset = np.dot(usafe_basis_pred.vector, basis_center)
        new_val = usafe_basis_pred.value - offset
        new_lc = LinearConstraint(usafe_basis_pred.vector, new_val)

        return new_lc
Exemplo n.º 21
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3"]
    #
    loc1 = ha.new_mode('loc1')

    loc1.a_matrix = np.array([[-0.1, -1, 0], [1, -0.1, 0], [0, 0, -0.15]])
    loc1.c_vector = np.array([0, 0, 0], dtype=float)
    # print(a_bk_matrix)

    error = ha.new_mode('_error')
    error.is_error = True

    usafe_set_constraint_list = []
    if usafe_r is None:

        # exp 1
        usafe_set_constraint_list.append(LinearConstraint([1, 0, 0], -0.46))

    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])
        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    trans = ha.new_transition(loc1, error)
    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 22
0
def define_ha():
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "t"]

    # input variable order: [u1, u2]

    Model = ha.new_mode('Model')
    a_matrix = np.array([[0, 1, 0, 0, 0, 0, 0, 0, 0], [0, -1.0865, 8487.2, 0, 0, 0, 0, 0, 0], [-2592.1, -21.119, -698.91, -141399, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, -1.0865, 8487.2, 0, 0], [0, 0, 0, 0, -2592.1, -21.119, -698.91, -141399, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=float)
    c_vector = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=float)
    Model.set_dynamics(a_matrix, c_vector)
    
    # u1 >= 0.16
    # u1 <= 0.3
    # u2 >= 0.2
    # u2 <= 0.4
    u_constraints_a = np.array([[-1, -0], [1, 0], [-0, -1], [0, 1]], dtype=float)
    u_constraints_b = np.array([-0.16, 0.3, -0.2, 0.4], dtype=float)
    b_matrix = np.array([[0, 0], [0, 0], [0, 0], [-1, 0], [0, 0], [0, 0], [0, 0], [0, -1], [0, 0]], dtype=float)
    Model.set_inputs(u_constraints_a, u_constraints_b, b_matrix)

    _error = ha.new_mode('_error')
    _error.is_error = True

    trans = ha.new_transition(Model, _error)
    trans.condition_list.append(LinearConstraint([0, 0, 0, 0, -1, 0, 0, 0, 0], -0.001501)) # 0.45 <= x5

    return ha
Exemplo n.º 23
0
    def test_center_into_constraints_complex(self):
        '''test that center_into_constraints doesn't modify the stars'''

        mode = make_debug_mode()
        array = np.array

        star = Star(HylaaSettings(0.01, 2.0),
                    array([-0.11060623, -0.62105371]),
                    array([[-0.12748444, -6.33082449],
                           [0.06330825, -0.3807174]]),
                    [
                        LinearConstraint(array([1., 0.]), -0.95),
                        LinearConstraint(array([-1., 0.]), 1.05),
                        LinearConstraint(array([0., 1.]), 0.1),
                        LinearConstraint(array([0., -1.]), 0.1),
                        LinearConstraint(array([6.33082449, 0.3807174]),
                                         -0.6210537093866693),
                        LinearConstraint(array([0.12748444, -0.06330825]),
                                         -0.11060623185202963),
                        LinearConstraint(array([-0.12748444, 0.06330825]),
                                         1.1106062318520296)
                    ],
                    None,
                    mode,
                    extra_init=(None, 20, 0))

        star2 = star.clone()
        star2.center_into_constraints(star2.vector_to_star_basis(star2.center))

        self.check_stars_equal(star, star2)
Exemplo n.º 24
0
    def test_init_constraints(self):
        'test hylaa initialization using constraints'

        hr = HyperRectangle([(-1, 4), (-1, 1)])
        star = init_hr_to_star(make_settings(), hr, TestStar.loc)

        # now try a list of LinearConstraints
        constraints = [
            LinearConstraint([-1, 0], 1),
            LinearConstraint([1, 0], 4),
            LinearConstraint([0, -1], 1),
            LinearConstraint([0, 1], 1)
        ]

        star2 = init_constraints_to_star(make_settings(), constraints,
                                         TestStar.loc)

        self.check_stars_equal(star, star2)
Exemplo n.º 25
0
    def compute_point_as_star_basis(self, point, error_star):
        basis_predicates = []
        identity_matrix = np.identity(self.num_dims, dtype=float)
        for dim in range(identity_matrix.ndim):
            lc = LinearConstraint(identity_matrix[dim], point[dim])
            basis_predicates.append(LinearConstraint(lc.vector, lc.value))
            lc = LinearConstraint(-1 * identity_matrix[dim], -point[dim])
            basis_predicates.append(LinearConstraint(lc.vector, lc.value))

        basis_predicates_in_star_basis = []
        for lc in basis_predicates:
            lc_vector = lc.vector

            new_lc_vector = np.dot(error_star.basis_matrix, lc_vector)
            new_lc_value = lc.value - np.dot(error_star.center, lc_vector)
            basis_predicates_in_star_basis.append(LinearConstraint(new_lc_vector, new_lc_value))

        return basis_predicates_in_star_basis
Exemplo n.º 26
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2"]
    #
    loc1 = ha.new_mode('loc1')

    # exp 1 and 2
    a_matrix = np.array([[0.983498664120250, 0.101548195541291],
                         [-0.013528375561473, 0.935610369333783]],
                        dtype=float)

    # exp 1
    b_matrix = np.array([[0.0], [0.0]], dtype=float)

    # # exp2
    # b_matrix = np.array([[1], [1]], dtype=float)

    print(a_matrix, b_matrix)
    R_mult_factor = 0.2

    Q_matrix = np.eye(len(a_matrix[0]), dtype=float)

    u_dim = len(b_matrix[0])
    R_matrix = R_mult_factor * np.eye(u_dim)

    print(a_matrix, b_matrix, Q_matrix, R_matrix)
    k_matrix = get_input(a_matrix, b_matrix, Q_matrix, R_matrix)

    print(k_matrix)
    # a_bk_matrix = a_matrix_ext - np.matmul(b_matrix_ext, k_matrix)
    a_bk_matrix = a_matrix - np.matmul(b_matrix, k_matrix)

    loc1.a_matrix = a_bk_matrix
    loc1.c_vector = np.array([0.0, 0.0], dtype=float)

    error = ha.new_mode('_error')
    error.is_error = True

    usafe_set_constraint_list = []
    if usafe_r is None:
        # exp 1
        usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0], -2.0))

        # usafe_set_constraint_list.append(LinearConstraint([0.0, 1.0], -0.85))

    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])
        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    trans = ha.new_transition(loc1, error)
    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 27
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3", "x4"]
    #
    loc1 = ha.new_mode('loc1')
    a_matrix = np.array(
        [[1, 0, 0, 0], [0, 2, 0.5, 0], [0, 0, 1, 0], [0, 0, 0, 0.5]],
        dtype=float)

    # exp 1
    b_matrix = np.array(
        [[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 3, 0], [0, 2, 0, 1]], dtype=float)

    print(a_matrix, b_matrix)
    R_mult_factor = 0.1

    Q_matrix = np.eye(len(a_matrix[0]), dtype=float)

    u_dim = len(b_matrix[0])
    R_matrix = R_mult_factor * np.eye(u_dim)

    print(a_matrix, b_matrix, Q_matrix, R_matrix)
    k_matrix = get_input(a_matrix, b_matrix, Q_matrix, R_matrix)

    print(k_matrix)
    a_bk_matrix = a_matrix - np.matmul(b_matrix, k_matrix)

    loc1.a_matrix = a_bk_matrix
    loc1.c_vector = np.array([0.0, 0.0, 0.0, 0.0], dtype=float)
    # print(a_bk_matrix)

    error = ha.new_mode('_error')
    error.is_error = True

    usafe_set_constraint_list = []
    if usafe_r is None:

        # exp 1
        # usafe_set_constraint_list.append(LinearConstraint([0.0, 0.0, 0.0, 1.0], -2.9))

        # exp 2
        usafe_set_constraint_list.append(
            LinearConstraint([0.0, 0.0, 0.0, 1.0], -3.42))

    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])
        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    trans = ha.new_transition(loc1, error)
    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 28
0
    def compute_usafe_std_pred_in_star_basis(self, error_star):
        usafe_std_predicates = []

        for usafe_set_lc in self.usafe_set_constraint_list:
            ## To correctly compute the dot product
            lc_t = usafe_set_lc.vector

            new_lc_value = usafe_set_lc.value - np.dot(error_star.center, lc_t)
            usafe_std_predicates.append(LinearConstraint(usafe_set_lc.vector, new_lc_value))

        return usafe_std_predicates
Exemplo n.º 29
0
def define_ha(settings, usafe_r):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8"]

    loc1 = ha.new_mode('loc1')

    a_matrix = np.array([[1, 0.1, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 0.1, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0.1, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0.1],
        [0, 0, 0, 0, 0, 0, 0, 1]], dtype=float)

    loc1.c_vector = np.array([0, 0, 0, 0, 0, 0, 0, 0], dtype=float)

    b_matrix = np.array([[0, 0, 0, 0],
        [0.1, 0, 0, 0],
        [0, 0, 0, 0],
        [0.1, -0.1, 0, 0],
        [0, 0, 0, 0],
        [0, 0.1, -0.1, 0],
        [0, 0, 0, 0],
        [0, 0, 0.1, -0.1]], dtype=float)

    k_matrix = np.array([[401.0025, 40.0500, 1.0000, 0.0499, 0.0025, 0.0001, 0.0000, 0.0000],
                        [400.0025, 40.0001, -401.0000, -40.0499, 1.0000, 0.0499, 0.0025, 0.0001],
                         [400.0000, 40.0000, -400.0025, -40.0001, -401.0000, -40.0499, 1.0000, 0.0499],
                         [400.0000, 40.0000, -400.0000, -40.0000, -400.0025, -40.0001, -401.0025, -40.0500]], dtype=float)

    loc1.a_matrix = a_matrix - np.matmul(b_matrix, k_matrix)

    error = ha.new_mode('_error')
    error.is_error = True

    trans = ha.new_transition(loc1, error)

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0, 0.0, 0.0], 2))
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])

        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    for constraint in usafe_set_constraint_list:
        trans.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
Exemplo n.º 30
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x", "v"]

    extension = ha.new_mode('extension')
    extension.a_matrix = np.array([[0.0, 1.0], [-100.0, -4.0]], dtype=float)
    extension.c_vector = np.array([0.0, -9.81], dtype=float)
    extension.inv_list.append(LinearConstraint([1.0, 0.0], 0))  # x <= 0

    freefall = ha.new_mode('freefall')
    freefall.a_matrix = np.array([[0.0, 1.0], [0.0, 0.0]], dtype=float)
    freefall.c_vector = np.array([0.0, -9.81], dtype=float)
    freefall.inv_list.append(LinearConstraint([-1.0, 0.0], 0.0))  # 0 <= x
    freefall.inv_list.append(LinearConstraint([1.0, 0.0], 1.0))  # x <= 1

    trans = ha.new_transition(extension, freefall)
    trans.condition_list.append(LinearConstraint([-0.0, -1.0], -0.0))  # v >= 0

    error = ha.new_mode('_error')
    error.is_error = True

    usafe_set_constraint_list = []
    if usafe_r is None:
        usafe_set_constraint_list.append(LinearConstraint([0.0, -1.0], -6.5))
        # usafe_set_constraint_list.append(LinearConstraint([3.0, -1.0], -7))  # for line with pts (-1, 4) and (0, 7)
    else:
        usafe_star = init_hr_to_star(settings, usafe_r, ha.modes['_error'])
        for constraint in usafe_star.constraint_list:
            usafe_set_constraint_list.append(constraint)

    trans1 = ha.new_transition(extension, error)
    for constraint in usafe_set_constraint_list:
        trans1.condition_list.append(constraint)

    trans2 = ha.new_transition(freefall, error)
    for constraint in usafe_set_constraint_list:
        trans2.condition_list.append(constraint)

    return ha, usafe_set_constraint_list