示例#1
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
示例#2
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
示例#3
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
示例#4
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
示例#5
0
def define_ha(limit):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()

    mode = ha.new_mode('mode')
    dynamics = loadmat('iss.mat')
    a_matrix = dynamics['A']

    # a is a csc_matrix
    col_ptr = [n for n in a_matrix.indptr]
    rows = [n for n in a_matrix.indices]
    data = [n for n in a_matrix.data]

    b_matrix = dynamics['B']
    num_inputs = b_matrix.shape[1]

    for u in xrange(num_inputs):
        rows += [n for n in b_matrix[:, u].indices]
        data += [n for n in b_matrix[:, u].data]
        col_ptr.append(len(data))

    combined_mat = csc_matrix((data, rows, col_ptr), \
        shape=(a_matrix.shape[0] + num_inputs, a_matrix.shape[1] + num_inputs))

    mode.set_dynamics(csr_matrix(combined_mat))

    error = ha.new_mode('error')

    y3 = dynamics['C'][2]
    col_ptr = [n for n in y3.indptr] + num_inputs * [y3.data.shape[0]]
    y3 = csc_matrix((y3.data, y3.indices, col_ptr), shape=(1, y3.shape[1] + num_inputs))
    output_space = csr_matrix(y3)

    #print "y3.data = {}, y3.indices = {}, y3.col_ptr = {}".format(y3.data, y3.indices, y3.col_ptr)

    mode.set_output_space(output_space)

    trans1 = ha.new_transition(mode, error)
    mat = csr_matrix(([1], [0], [0, 1]), dtype=float, shape=(1, 1))
    rhs = np.array([-limit], dtype=float) # safe
    trans1.set_guard(mat, rhs) # y3 <= -limit

    trans2 = ha.new_transition(mode, error)
    mat = csr_matrix(([-1], [0], [0, 1]), dtype=float, shape=(1, 1))
    rhs = np.array([-limit], dtype=float) # safe
    trans2.set_guard(mat, rhs) # y3 >= limit

    return ha
示例#6
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
示例#7
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
示例#8
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
示例#9
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
示例#10
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
示例#11
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
示例#12
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
示例#13
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
示例#14
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
示例#15
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.9951, 0.0098], [-0.9786, 0.9559]],
                                  dtype=float)
    extension.c_vector = np.array([-0.0005, -0.0960], dtype=float)
    extension.inv_list.append(LinearConstraint([1.0, 0.0], 0))  # x <= 0

    freefall = ha.new_mode('freefall')
    freefall.a_matrix = np.array([[1.0, 0.01], [0.0, 1.0]], dtype=float)
    freefall.c_vector = np.array([-0.0005, -0.0981], 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([-1.0, 0.0], 0.5))
        usafe_set_constraint_list.append(LinearConstraint([0.0, -1.0], -5.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)

    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
示例#16
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
示例#17
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
示例#18
0
def define_ha(limit):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()

    mode = ha.new_mode('mode')
    dynamics = loadmat('iss.mat')
    a_matrix = dynamics['A']
    b_matrix = dynamics['B']

    mode.set_dynamics(csr_matrix(a_matrix))

    # 0 <= u1 <= 0.1
    # 0.8 <= u2 <= 1.0
    # 0.9 <= u3 <= 1.0
    bounds_list = [(0, 0.1), (0.8, 1.0), (0.9, 1.0)]
    _, u_mat, u_rhs, u_range_tuples = bounds_list_to_init(bounds_list)

    mode.set_inputs(b_matrix, u_mat, u_rhs, u_range_tuples)

    error = ha.new_mode('error')

    y3 = dynamics['C'][2]
    output_space = csr_matrix(y3)

    mode.set_output_space(output_space)

    trans1 = ha.new_transition(mode, error)
    mat = csr_matrix(([1], [0], [0, 1]), dtype=float, shape=(1, 1))
    rhs = np.array([-limit], dtype=float)  # safe
    trans1.set_guard(mat, rhs)  # y3 <= -limit

    trans2 = ha.new_transition(mode, error)
    mat = csr_matrix(([-1], [0], [0, 1]), dtype=float, shape=(1, 1))
    rhs = np.array([-limit], dtype=float)  # safe
    trans2.set_guard(mat, rhs)  # y3 >= limit

    return ha
示例#19
0
def define_ha():
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton('Trimmed Harmonic Oscillator w/ successor')
    ha.variables = ["x", "y"]

    loc1 = ha.new_mode('green')
    loc1.a_matrix = nparray([[0, 1], [-1, 0]])
    loc1.c_vector = nparray([0, 0])

    inv1 = LinearConstraint([0., -1.], 0.0)  # y >= 0
    loc1.inv_list = [inv1]

    loc2 = ha.new_mode('cyan')
    loc2.a_matrix = nparray([[0, 0], [0, 0]])
    loc2.c_vector = nparray([0, -2])

    inv2 = LinearConstraint([0., -1.], 2.5)  # y >= -2.5
    loc2.inv_list = [inv2]

    loc3 = ha.new_mode('orange')
    loc3.a_matrix = nparray([[0, 0], [0, 0]])
    loc3.c_vector = nparray([0, -2])
    inv3 = LinearConstraint([0., -1.], 4.0)  # y >= -4
    loc3.inv_list = [inv3]

    guard = LinearConstraint([0., -1.], 0.0)  # y >= 0
    trans = ha.new_transition(loc1, loc2)
    trans.condition_list = [guard]

    guard1 = LinearConstraint([0., 1.], -0)  # y <= -0
    guard2 = LinearConstraint([1., 0], 0.5)  # x <= 0.5
    guard3 = LinearConstraint([-1., 0], 0.5)  # x >= -0.5
    trans = ha.new_transition(loc2, loc3)
    trans.condition_list = [guard1, guard2, guard3]

    return ha
示例#20
0
def define_ha(spec, limit, uncertain_inputs):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()

    mode = ha.new_mode('mode')
    dynamics = loadmat('build.mat')
    a_matrix = dynamics['A']
    n = a_matrix.shape[0]
    b_matrix = csc_matrix(dynamics['B'])

    if uncertain_inputs:
        mode.set_dynamics(csr_matrix(a_matrix))

        # 0 <= u1 <= 0.1
        bounds_list = [(0.8, 1.0)]
        _, u_mat, u_rhs, u_range_tuples = bounds_list_to_init(bounds_list)

        mode.set_inputs(b_matrix, u_mat, u_rhs, u_range_tuples)
    else:
        # add the input as a state variable
        big_a_mat = np.zeros((n + 1, n + 1))
        big_a_mat[0:n, 0:n] = a_matrix.toarray()
        big_a_mat[0:n, n:n + 1] = b_matrix.toarray()
        a_matrix = big_a_mat

        mode.set_dynamics(csr_matrix(big_a_mat))

    error = ha.new_mode('error')

    y1 = dynamics['C'][0]

    if not uncertain_inputs:
        new_y1 = np.zeros((1, n + 1))
        new_y1[0, 0:n] = y1
        y1 = new_y1

    output_space = csr_matrix(y1)

    mode.set_output_space(output_space)

    trans1 = ha.new_transition(mode, error)
    mat = csr_matrix(([-1], [0], [0, 1]), dtype=float, shape=(1, 1))
    rhs = np.array([-limit], dtype=float)  # safe
    trans1.set_guard(mat, rhs)  # y3 >= limit

    return ha
示例#21
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"]

    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)

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

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

    k_matrix = np.array([[24.2999, 2.2873, -19.7882, 0.0153],
                         [0.0771, 46.7949, -0.0618, 4.2253]],
                        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
示例#22
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["e1", "e1p", "a1", "e2", "e2p", "a2", "e3", "e3p", "a3"]

    loc1 = ha.new_mode('loc1')

    loc1.a_matrix = np.array([[0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, -1, 0, 0, 0, 0, 0, 0],
        [1.7152555329, 3.9705119979, -4.3600526739, -0.9999330812, -1.5731541104, 0.2669165553, -0.2215507198,
         -0.4303855023, 0.0669078193],
        [0, 0, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, -1, 0, 0, 0],
        [0.7153224517, 2.3973578876, 0.2669165553, 1.4937048131, 3.5401264957, -4.2931448546, -1.0880831031,
         -1.7613009555, 0.2991352608],
        [0, 0, 0, 0, 0, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, -1],
        [0.493771732, 1.9669723853, 0.0669078193, 0.6271724298, 2.2092110425, 0.2991352608, 1.4593953061, 3.4633677762,
         -4.2704788265]], dtype=float)
    loc1.c_vector = np.array([0, 0, 0, 0, 0, 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([0, 1, 0, 0, 0, 0, 0, 0, 0], -0.37))

    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
示例#23
0
def define_ha():
    '''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

    return ha
def define_ha(settings, usafe_r):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    # k = -0.0025  # Unsafe
    # k = -1.5  # Safe
    ha = LinearHybridAutomaton()
    ha.variables = ["s", "v", "vf", "a", "t"]

    loc1 = ha.new_mode('loc1')

    # loc1.a_matrix = np.array([[0, -1, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [1, -4, 3, -3, 0], [0, 0, 0, 0, 0]])
    # loc1.a_matrix = np.array([[0, -1, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [1, -3, 2, -3, 0], [0, 0, 0, 0, 0]])
    # loc1.a_matrix = np.array([[0, -1, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [1, -3, 2, -2, 0], [0, 0, 0, 0, 0]])
    loc1.a_matrix = np.array([[0, -1, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [1, -4, 3, -1.2, 0], [0, 0, 0, 0, 0]])
    # loc1.a_matrix = np.array([[0, -1, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [2, -5, 3, -4, 0], [0, 0, 0, 0, 0]])
    # Not stable
    loc1.c_vector = np.array([0, 0, 0, -10, 1], 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([-1, 0, 0, 0, 0], -0))
        # usafe_set_constraint_list.append(LinearConstraint([1, 0, 0, 0, 0], 2))
        usafe_set_constraint_list.append(LinearConstraint([0, 1, 0, 0, 0], 15))
        # usafe_set_constraint_list.append(LinearConstraint([0, -1, 0, 0, 0], -20))
    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
def define_ha(settings, usafe_r):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

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

    loc1 = ha.new_mode('loc1')

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

    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.50))
        # usafe_set_constraint_list.append(LinearConstraint([-1, 0, 0], -0.25))
        usafe_set_constraint_list.append(LinearConstraint([1, 0, 0, 0], 0.8))
        usafe_set_constraint_list.append(LinearConstraint([-1, 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
示例#26
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

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

    p2 = ha.new_mode('P2')
    p2.a_matrix = np.array([[0, 0, 1, 0, 0], [0, 0, 0, 1, 0],
                            [
                                -0.057599765881773, 0.0002009598965197660,
                                -2.89995083970656, 0.00877200894463775, 0
                            ],
                            [
                                -0.000174031357370456, -0.0665123984901026,
                                0.00875351105536225, -2.90300269286856, 0
                            ], [0, 0, 0, 0, 0]],
                           dtype=float)
    p2.c_vector = np.array([0, 0, 0, 0, 1], dtype=float)
    p2.inv_list.append(LinearConstraint([0.0, 0.0, 0.0, 0.0, 1.0],
                                        125))  # t <= 125
    p2.inv_list.append(LinearConstraint([1.0, 0.0, 0.0, 0.0, 0.0],
                                        -100))  # x <= -100

    p3 = ha.new_mode('P3')
    p3.a_matrix = np.array([[0, 0, 1, 0, 0], [0, 0, 0, 0, 1],
                            [
                                -0.575999943070835, 0.000262486079431672,
                                -19.2299795908647, 0.00876275931760007, 0
                            ],
                            [
                                -0.000262486080737868, -0.575999940191886,
                                0.00876276068239993, -19.2299765959399, 0
                            ], [0, 0, 0, 0, 0]],
                           dtype=float)
    p3.c_vector = np.array([0, 0, 0, 0, 1], dtype=float)
    p3.inv_list.append(LinearConstraint([0.0, 0.0, 0.0, 0.0, 1.0],
                                        125))  # t <= 125
    p3.inv_list.append(LinearConstraint([-1.0, 0.0, 0.0, 0.0, 0.0],
                                        100))  # x >= -100
    p3.inv_list.append(LinearConstraint([1.0, 0.0, 0.0, 0.0, 0.0],
                                        100))  # x <= 100
    p3.inv_list.append(LinearConstraint([0.0, -1.0, 0.0, 0.0, 0.0],
                                        100))  # y >= -100
    p3.inv_list.append(LinearConstraint([0.0, 1.0, 0.0, 0.0, 0.0],
                                        100))  # y <= 100
    p3.inv_list.append(LinearConstraint([-1.0, -1.0, 0.0, 0.0, 0.0],
                                        141.1))  # x+y >= -141.1
    p3.inv_list.append(LinearConstraint([1.0, 1.0, 0.0, 0.0, 0.0],
                                        141.1))  # x+y <= 141.1
    p3.inv_list.append(LinearConstraint([-1.0, 1.0, 0.0, 0.0, 0.0],
                                        141.1))  # y-x <= 141.1
    p3.inv_list.append(LinearConstraint([1.0, -1.0, 0.0, 0.0, 0.0],
                                        141.1))  # y-x >= -141.1

    passive = ha.new_mode('passive')
    passive.a_matrix = np.array([[0, 0, 1, 0, 0], [0, 0, 0, 1, 0],
                                 [0.0000575894721132000, 0, 0, 0.00876276, 0],
                                 [0, 0, -0.00876276, 0, 0], [0, 0, 0, 0, 0]],
                                dtype=float)
    passive.c_vector = np.array([0, 0, 0, 0, 1], dtype=float)
    passive.inv_list.append(LinearConstraint([0.0, 0.0, 0.0, 0.0, -1.0],
                                             -120))  # t >= 120

    trans1 = ha.new_transition(p2, p3)
    trans1.condition_list.append(
        LinearConstraint([-1.0, 0.0, 0.0, 0.0, 0.0], 100))  # x >= -100
    trans2 = ha.new_transition(p2, passive)
    trans2.condition_list.append(
        LinearConstraint([0.0, 0.0, 0.0, 0.0, -1.0], -120))  # t >= 120
    trans3 = ha.new_transition(p3, passive)
    trans3.condition_list.append(
        LinearConstraint([0.0, 0.0, 0.0, 0.0, -1.0], -120))  # t >= 120

    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], -100))  # x < -100
        usafe_set_constraint_list.append(
            LinearConstraint([1.0, 0.0, 0.0, 0.0, 0.0], -800))  # x < -800
        usafe_set_constraint_list.append(
            LinearConstraint([-1.0, 0.0, 0.0, 0.0, 0.0], 850))  # x > -850
    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)
    trans4 = ha.new_transition(p2, error)
    trans5 = ha.new_transition(p3, error)
    trans6 = ha.new_transition(passive, error)
    for pred in usafe_set_constraint_list:
        trans4.condition_list.append(pred.clone())
        trans5.condition_list.append(pred.clone())
        trans6.condition_list.append(pred.clone())

    return ha, usafe_set_constraint_list
def define_ha():
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31", "x32", "x33", "x34", "x35", "x36", "x37", "x38", "x39", "x40", "x41", "x42", "x43", "x44", "x45", "x46", "x47", "x48", "t", "u1"]


    Model = ha.new_mode('Model')
    a_matrix = np.array([ \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], \
        [-606.16, 34.67, -5.0432, -734.04, 124.55, -302.91, -98.21, -2.204, -10.668, -41.864, 8.1784, 51.36, -10.956, -2.3086, -11.998, -42.003, 4.9418, -1.5216, -10.35, 8.6969, 14.96, 37.885, -24.546, -19.3, -1.1333, 0.036523, -0.0053128, -0.77328, 0.13121, -0.3191, -0.10346, -0.0023218, -0.011238, -0.044102, 0.0086155, 0.054106, -0.011541, -0.002432, -0.012639, -0.044248, 0.0052059, -0.0016029, -0.010904, 0.0091618, 0.01576, 0.03991, -0.025858, -0.020331, 0, 0.013697], \
        [33.51, -664.52, 79.773, 207.45, 829.03, 94.055, -4.3133, -136.48, 16.276, 28.243, -76.936, -0.55452, 30.931, 51.904, 11.87, 9.6948, -15.613, 15.14, 7.6133, -5.7122, 3.7841, -21.172, -29.014, 10.245, 0.035301, -1.1948, 0.084037, 0.21854, 0.87334, 0.099082, -0.0045438, -0.14378, 0.017146, 0.029753, -0.081048, -0.00058416, 0.032585, 0.054679, 0.012505, 0.010213, -0.016447, 0.01595, 0.0080202, -0.0060176, 0.0039864, -0.022303, -0.030565, 0.010792, 0, 0], \
        [-3.4577, 76.572, -799.41, -347.78, -142.59, 867.7, -165.08, 36.992, 91.256, -66.266, 26.434, 39.519, 67.373, -32.177, 17.164, 133.45, 34.378, 6.5652, 27.869, -51.473, 41.979, -44.928, 34.417, 30.824, -0.0036425, 0.080665, -1.3369, -0.36637, -0.15021, 0.91408, -0.17391, 0.038969, 0.096134, -0.069808, 0.027847, 0.041632, 0.070975, -0.033897, 0.018082, 0.14058, 0.036215, 0.0069161, 0.029358, -0.054224, 0.044223, -0.04733, 0.036257, 0.032471, 0, 0], \
        [-734.86, 206.8, -348.32, -1910.4, -135.41, -218.05, -1130, 263.61, -178.6, -285.02, 65.125, 125.09, -78.164, -2.0288, 11.414, -54.481, -17.625, -14.273, -13.334, 11.514, 88.076, 56.304, -12.53, 19.497, -0.77414, 0.21785, -0.36694, -2.5072, -0.14264, -0.2297, -1.1904, 0.2777, -0.18815, -0.30026, 0.068606, 0.13177, -0.082342, -0.0021372, 0.012024, -0.057393, -0.018567, -0.015036, -0.014047, 0.012129, 0.092784, 0.059314, -0.013199, 0.020539, 0, 0], \
        [126.41, 830.41, -142.11, -134.84, -1968.7, 54.561, 141.8, 1183.9, 118.23, -32.897, 295.19, -13.727, -6.9809, -150.49, -16.631, 76.503, 25.453, 26.848, 8.696, -20.18, -16.511, 8.533, 112.09, 9.1024, 0.13317, 0.8748, -0.14971, -0.14205, -2.5687, 0.057478, 0.14938, 1.2472, 0.12455, -0.034656, 0.31097, -0.014461, -0.0073541, -0.15853, -0.01752, 0.080592, 0.026813, 0.028283, 0.0091609, -0.021259, -0.017394, 0.0089891, 0.11808, 0.009589, 0, 0], \
        [-308.62, 89.14, 845.85, -220.15, 49.494, -2326.8, 132.76, 67.169, -1400.6, 24.168, 24.278, -85.266, -294.4, 127.92, -24.456, -379.06, -100.16, -46.537, -67.411, 169.3, -100.19, 200.07, -126.91, -44.045, -0.32511, 0.093904, 0.89106, -0.23192, 0.052139, -2.9459, 0.13986, 0.07076, -1.4755, 0.02546, 0.025576, -0.089824, -0.31014, 0.13476, -0.025763, -0.39932, -0.10551, -0.049024, -0.071015, 0.17835, -0.10555, 0.21077, -0.13369, -0.046399, 0, 0], \
        [-97.135, -3.2644, -166.03, -1130.1, 140.17, 149.9, -2042.2, 77.153, 122.7, -1223.6, -66.505, 249.18, -16.632, 2.6274, 56.399, -46.871, -118.4, -62.011, -2.1658, -30.269, 130.25, 5.2191, -24.931, 6.3303, -0.10233, -0.0034389, -0.17491, -1.1905, 0.14767, 0.15792, -2.6461, 0.081277, 0.12926, -1.289, -0.07006, 0.2625, -0.01752, 0.0027678, 0.059414, -0.049377, -0.12472, -0.065326, -0.0022816, -0.031887, 0.13721, 0.005498, -0.026264, 0.0066687, 0, 0], \
        [-2.5435, -138.29, 34.596, 264.02, 1185, 58.931, 77.078, -2124.4, -54.178, 240.2, -1279.6, 14.033, 37.313, 238.99, 1.7648, -96.468, 33.324, -103.99, -129.69, 35.961, 34.916, -10.09, -99.84, -13.713, -0.0026795, -0.14568, 0.036445, 0.27814, 1.2484, 0.062081, 0.081198, -2.7327, -0.057074, 0.25304, -1.348, 0.014783, 0.039308, 0.25177, 0.0018592, -0.10162, 0.035106, -0.10955, -0.13662, 0.037884, 0.036783, -0.010629, -0.10518, -0.014446, 0, 0], \
        [-11.068, -4.3846, 54.576, -167.43, 119.06, -1413.1, 116.3, -50.978, -2668.8, 58.12, 61.221, -299.51, -1455.4, 600.59, -14.962, -609.81, -179.98, -140.89, -67.425, 261.44, -215.8, 374.46, -267.83, -167.39, -0.01166, -0.004619, 0.057493, -0.17638, 0.12543, -1.4886, 0.12251, -0.053703, -3.3062, 0.061226, 0.064493, -0.31552, -1.5332, 0.63269, -0.015762, -0.6424, -0.1896, -0.14842, -0.071029, 0.27541, -0.22734, 0.39448, -0.28215, -0.17634, 0, 0], \
        [-42.219, 29.028, -59.833, -284.78, -31.96, 16.189, -1220.6, 238.17, 45.965, -2290.9, 219.74, 1329.3, -104.55, -231.45, 196.73, -91.293, -418.1, -203.17, 77.147, -64.407, 127.6, -66.422, 17.647, 14.772, -0.044476, 0.03058, -0.063031, -0.3, -0.033668, 0.017054, -1.2858, 0.2509, 0.048422, -2.9081, 0.23149, 1.4003, -0.11014, -0.24382, 0.20725, -0.096173, -0.44045, -0.21403, 0.08127, -0.06785, 0.13442, -0.069972, 0.01859, 0.015562, 0, 0], \
        [5.3147, -77.32, 33.098, 62.873, 295.95, 11.239, -66.808, -1279.2, 57.198, 221.07, -2387.6, 120.01, 572.58, 1229.1, 124.63, -56.089, 180.3, -312.48, -445.95, -24.274, 86.343, -2.8317, -45.574, -33.346, 0.0055988, -0.081453, 0.034867, 0.066234, 0.31177, 0.011839, -0.070379, -1.3476, 0.060255, 0.23288, -3.0099, 0.12643, 0.60318, 1.2948, 0.13129, -0.059087, 0.18994, -0.32918, -0.46979, -0.025571, 0.090958, -0.002983, -0.04801, -0.035129, 0, 0], \
        [48.409, -3.7335, 32.385, 123.4, -10.509, -100.46, 251, 15.115, -306.96, 1327.9, 119.59, -2059.6, -316.77, 309.66, -870.95, -176.05, 612.8, 311.81, -23.027, 201.61, -171.24, 169.57, -91.873, -44.001, 0.050997, -0.0039331, 0.034116, 0.13, -0.011071, -0.10583, 0.26442, 0.015922, -0.32337, 1.3989, 0.12598, -2.6645, -0.3337, 0.32621, -0.9175, -0.18546, 0.64555, 0.32848, -0.024258, 0.21239, -0.18039, 0.17864, -0.096784, -0.046353, 0, 0], \
        [-18.711, 16.772, 31.337, -86.799, -1.8261, -326.22, -26.875, 48.423, -1444.1, -111.75, 580.84, -316.97, -2618.4, 213.69, -227.06, -1560.3, -475.12, 287.08, 62.345, 349.71, -307.69, 551.86, -413.54, -278.79, -0.019712, 0.017669, 0.033012, -0.091439, -0.0019237, -0.34366, -0.028311, 0.051011, -1.5212, -0.11773, 0.61189, -0.33391, -3.2531, 0.22511, -0.23919, -1.6437, -0.50052, 0.30242, 0.065677, 0.36841, -0.32414, 0.58136, -0.43564, -0.29369, 0, 0], \
        [2.1605, 59.152, -19.397, 2.4357, -153.32, 143.74, 5.1672, 235.26, 606.81, -229.22, 1225.8, 308.88, 220.47, -2060.7, -41.024, 847.46, -549.36, 704.76, 575.93, -182.76, 62.77, -246.62, 238.45, 159.35, 0.002276, 0.062314, -0.020434, 0.0025659, -0.16151, 0.15142, 0.0054434, 0.24784, 0.63924, -0.24147, 1.2913, 0.32539, 0.23225, -2.6656, -0.043217, 0.89276, -0.57872, 0.74243, 0.60672, -0.19253, 0.066125, -0.2598, 0.25119, 0.16786, 0, 0], \
        [-11.108, 11.65, 6.8996, 10.745, -14.165, -24.224, 58.6, 1.0473, -23.411, 194.71, 124.74, -874.49, -232.17, -36.538, -982.44, -164.62, 658.46, 612.29, 81.858, 340.63, -202.91, 81.196, -56.024, -15.279, -0.011702, 0.012273, 0.0072684, 0.01132, -0.014922, -0.025519, 0.061732, 0.0011033, -0.024662, 0.20512, 0.1314, -0.92123, -0.24458, -0.038491, -1.5297, -0.17342, 0.69366, 0.64502, 0.086233, 0.35883, -0.21375, 0.085536, -0.059019, -0.016096, 0, 0], \
        [-45.16, 0.34677, 87.266, -58.989, 81.86, -393.99, -43.617, -87.105, -585.65, -95.775, -55.621, -175.4, -1550.2, 849.01, -150.24, -2831, -714.73, -306.41, -276.74, 1235.6, -213.26, 900.37, -708.02, -408.51, -0.047574, 0.00036531, 0.09193, -0.062142, 0.086236, -0.41505, -0.045948, -0.091761, -0.61696, -0.10089, -0.058594, -0.18478, -1.633, 0.89439, -0.15827, -3.477, -0.75293, -0.32279, -0.29153, 1.3016, -0.22465, 0.94849, -0.74586, -0.43035, 0, 0], \
        [3.5059, -19.002, 24.585, -17.341, 25.7, -100.44, -121.83, 37.286, -153.78, -417.65, 181.1, 615.65, -480.91, -550.2, 663.43, -703.07, -2922.4, -629.58, 982.78, -72.585, 517.72, 573.36, -34.103, -92.294, 0.0036933, -0.020017, 0.025899, -0.018268, 0.027073, -0.1058, -0.12834, 0.039279, -0.162, -0.43998, 0.19078, 0.64856, -0.50662, -0.57961, 0.69889, -0.74065, -3.5734, -0.66323, 1.0353, -0.076465, 0.54539, 0.60401, -0.035926, -0.097227, 0, 0], \
        [-6.541, 11.689, 14.446, -16.841, 27.253, -50.506, -63.95, -102.32, -140.13, -204.16, -309.41, 316.39, 287.25, 700.16, 615.11, -292.97, -628.33, -1965.7, -1181.8, -160.14, 504.08, 10.684, -328.24, -67.729, -0.0068906, 0.012313, 0.015219, -0.017741, 0.028709, -0.053205, -0.067368, -0.10779, -0.14762, -0.21507, -0.32595, 0.33331, 0.3026, 0.73758, 0.64799, -0.30863, -0.66191, -2.5655, -1.245, -0.16869, 0.53103, 0.011255, -0.34578, -0.071349, 0, 0], \
        [-11.411, 5.9383, 26.834, -15.359, 6.1346, -51.136, -1.6072, -127.63, -64.662, 76.069, -444.22, -20.062, 77.799, 570.73, 84.558, -263.82, 983.07, -1184.4, -2653.4, 240.05, 17.029, -433.29, -721.56, -85.171, -0.012021, 0.0062557, 0.028268, -0.01618, 0.0064625, -0.05387, -0.0016931, -0.13445, -0.068118, 0.080135, -0.46796, -0.021135, 0.081958, 0.60124, 0.089078, -0.27792, 1.0356, -1.2477, -3.2899, 0.25288, 0.017939, -0.45645, -0.76013, -0.089724, 0, 0], \
        [10.661, 0.75629, -31.37, 18.757, -29.414, 193.91, -34.395, 32.908, 271.89, -54.429, -33.158, 199.94, 354.88, -181.87, 333.88, 1248.3, -59.084, -157.94, 239.26, -1528.9, 772.83, -858.92, 734.27, 372.63, 0.011231, 0.00079672, -0.033047, 0.01976, -0.030986, 0.20427, -0.036234, 0.034667, 0.28643, -0.057338, -0.03493, 0.21063, 0.37385, -0.19159, 0.35173, 1.315, -0.062242, -0.16638, 0.25204, -2.1053, 0.81414, -0.90483, 0.77352, 0.39255, 0, 0], \
        [21.145, -0.67362, 25.339, 82.664, -18.143, -108.58, 131.04, 40.675, -205.5, 125.37, 92.038, -169.72, -294.36, 57.772, -200.48, -211.37, 512.5, 500.79, 15.006, 785.7, -3685.6, 248.17, -125.37, -428.07, 0.022275, -0.00070963, 0.026694, 0.087083, -0.019113, -0.11438, 0.13805, 0.04285, -0.21648, 0.13207, 0.096957, -0.17879, -0.31009, 0.06086, -0.2112, -0.22266, 0.5399, 0.52756, 0.015808, 0.8277, -4.3773, 0.26143, -0.13208, -0.45095, 0, 0], \
        [30.438, -10.312, -14.559, 71.32, 9.4074, 217.12, 3.4082, -22.61, 366.85, -62.437, -17.337, 170.53, 551.59, -246.57, 70.631, 918.77, 591.41, 17.745, -424.27, -874.95, 255.27, -4294.4, 510.73, 1033.1, 0.032064, -0.010863, -0.015337, 0.075133, 0.0099102, 0.22872, 0.0035904, -0.023818, 0.38646, -0.065775, -0.018263, 0.17964, 0.58107, -0.25975, 0.074406, 0.96788, 0.62302, 0.018694, -0.44695, -0.92172, 0.26891, -5.0186, 0.53803, 1.0883, 0, 0], \
        [-18.027, -37.47, 11.835, -25.981, 110.26, -133.09, -21.113, -89.428, -265.96, 11.486, -31.545, -88.775, -389.13, 228.95, -47.085, -703.45, -45.639, -333.53, -724.09, 748.64, -119.88, 488.53, -3962.5, -875.81, -0.01899, -0.039473, 0.012468, -0.027369, 0.11615, -0.1402, -0.022242, -0.094208, -0.28018, 0.0121, -0.033231, -0.093521, -0.40993, 0.24119, -0.049602, -0.74105, -0.048079, -0.35136, -0.7628, 0.78865, -0.12629, 0.51464, -4.669, -0.92262, 0, 0], \
        [12.708, 4.8669, -6.4356, 9.4963, 3.5537, -74.352, 13.537, -16.221, -158.43, 2.6078, -31.934, -48.095, -252.14, 158.72, -25.236, -416.39, -103.09, -75.108, -90.118, 375.82, -425.25, 1006.8, -878.11, -4455.6, 0.013388, 0.0051271, -0.0067796, 0.010004, 0.0037436, -0.078327, 0.01426, -0.017088, -0.1669, 0.0027471, -0.033641, -0.050665, -0.26562, 0.16721, -0.026585, -0.43865, -0.1086, -0.079123, -0.094935, 0.39591, -0.44798, 1.0606, -0.92505, -5.1884, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], dtype=float)
    Model.set_dynamics(a_matrix, c_vector)

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

    trans = ha.new_transition(Model, _error)
    trans.condition_list.append(LinearConstraint([-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0], -0.0051)) # x25 >= 0.0051
    
    #trans.condition_list.append(LinearConstraint([-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -1, -0], -20)) # t >= 20.0

    return ha
示例#28
0
def define_ha(settings, args):
    # x' = Ax + Bu + c
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()

    a_matrices = args[0]
    t_jumps = list(args[1])
    usafe_r = args[2]

    n_locations = len(a_matrices)
    n_variables = len(a_matrices[0][0])
    print(n_locations, n_variables)

    ha.variables = []
    for idx in range(n_variables-1):
        x_var_name = "x"+str(idx)
        ha.variables.append(x_var_name)
    ha.variables.append("t")
    # ha.variables = ["x1", "x2", "x3", "x4", "t"]

    locations = []
    for idx in range(n_locations):
        loc_name = 'loc' + str(idx)
        loc = ha.new_mode(loc_name)
        loc.a_matrix = a_matrices[idx]
        c_vector = [0.0] * n_variables
        c_vector[n_variables-1] = 1
        loc.c_vector = c_vector
        if idx == 0:
            loc_inv_vec = [0.0] * n_variables
            loc_inv_vec[n_variables - 1] = 1.0
            loc.inv_list.append(LinearConstraint(loc_inv_vec, step_size * t_jumps[idx]))
        elif idx == n_locations - 1:
            loc_inv_vec = [0.0] * n_variables
            loc_inv_vec[n_variables - 1] = -1.0
            loc.inv_list.append(LinearConstraint(loc_inv_vec, -step_size * t_jumps[idx - 1]))
        else:
            loc_inv_vec = [0.0] * n_variables
            loc_inv_vec[n_variables - 1] = -1.0
            loc.inv_list.append(LinearConstraint(loc_inv_vec, -step_size * t_jumps[idx - 1]))
            loc_inv_vec = [0.0] * n_variables
            loc_inv_vec[n_variables - 1] = 1.0
            loc.inv_list.append(LinearConstraint(loc_inv_vec, step_size * t_jumps[idx]))
        locations.append(loc)

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

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

    usafe_set_constraint_list = []
    if usafe_r is None:
        trans_vec = np.zeros(n_variables, dtype=float)
        trans_vec[0] = 1.0
        # usafe_set_constraint_list.append(LinearConstraint(trans_vec, -5.0))
        usafe_set_constraint_list.append(LinearConstraint([1.0, 0.0, 0.0, 0.0, 0.0], -5))
        # usafe_set_constraint_list.append(LinearConstraint([0.0, -1.0, 0.0, 0.0, 0], -0.5))
        # usafe_set_constraint_list.append(LinearConstraint([0.0, 1.0, 0.0, 0.0, 0], 1.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 idx in range(n_locations):
        trans_u = ha.new_transition(locations[idx], error)
        for constraint in usafe_set_constraint_list:
            trans_u.condition_list.append(constraint)

    return ha, usafe_set_constraint_list
示例#29
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 = ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "t"]

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

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

    k_matrix = np.array(
        [[-7.36421144, -8.67472342, -1.46914025, -1.55897265, -1.02212334, -1.08623604,
  -0.72144969, -0.76789506, -0.51416355, -0.54811612, -0.36710532, -0.39192759,
  -0.25914664, -0.27703871, -0.1765116,  -0.18890941, -0.10991462, -0.11773395,
  -0.05276657, -0.05654998, 0],
 [-5.89507119, -7.11575077,  6.3420881,   7.58848739, -2.19058994, -2.32686771,
  -1.53628689, -1.63435216, -1.08855502, -1.15982265, -0.77331019, -0.82515483,
  -0.54361692, -0.580837,   -0.36906126, -0.39477265, -0.22927817, -0.24545939,
  -0.10991462, -0.11773395, 0],
 [-4.87294785, -6.02951473,  5.1736215,   6.34785571,  5.82792455,  7.04037127,
  -2.55769526, -2.7187953,  -1.79543353, -1.91139086, -1.26506662, -1.34873206,
  -0.88322481, -0.94288877, -0.59638349, -0.63738698, -0.36906126, -0.39477265,
  -0.1765116,  -0.18890941, 0],
 [-4.15149816, -5.26161967,  4.3587843,   5.48139862,  4.80651618,  5.95592812,
   5.56877791,  6.76333256, -2.73420686, -2.90770471, -1.90534815, -2.02912481,
  -1.31783318, -1.40528204, -0.88322481, -0.94288877, -0.54361692, -0.580837,
  -0.25914664, -0.27703871, 0],
 [-3.63733461, -4.71350356,  3.78439284,  4.86969208,  4.09963766,  5.20435991,
   4.63000458,  5.76701871,  5.45886329,  6.64559862, -2.78697343, -2.96425469,
  -1.90534815, -2.02912481, -1.26506662, -1.34873206, -0.77331019, -0.82515483,
  -0.36710532, -0.39192759, 0],
 [-3.27022929, -4.32157596,  3.37818797,  4.43646485,  3.60788124,  4.68078267,
   3.98972304,  5.08662596,  4.57723801,  5.71046873,  5.45886329,  6.64559862,
  -2.73420686, -2.90770471, -1.79543353, -1.91139086, -1.08855502, -1.15982265,
  -0.51416355, -0.54811612, 0],
 [-3.01108265, -4.04453726,  3.09371769,  4.13266655,  3.26827335,  4.3187309,
   3.55511467,  4.6242327,   3.98972304,  5.08662596,  4.63000458,  5.76701871,
   5.56877791,  6.76333256, -2.55769526, -2.7187953,  -1.53628689, -1.63435216,
  -0.72144969, -0.76789506, 0],
 [-2.83457105, -3.85562785,  2.90116803,  3.92680331,  3.04095112,  4.07611658,
   3.26827335,  4.3187309,   3.60788124,  4.68078267,  4.09963766,  5.20435991,
   4.80651618,  5.95592812,  5.82792455,  7.04037127, -2.19058994, -2.32686771,
  -1.02212334, -1.08623604, 0],
 [-2.72465643, -3.7378939,   2.78180448,  3.79907787,  2.90116803,  3.92680331,
   3.09371769,  4.13266655,  3.37818797,  4.43646485,  3.78439284,  4.86969208,
   4.3587843,   5.48139862,  5.1736215,   6.34785571,  6.3420881,   7.58848739,
  -1.46914025, -1.55897265, 0],
 [-2.67188986, -3.68134393,  2.72465643,  3.7378939,   2.83457105,  3.85562785,
   3.01108265,  4.04453726,  3.27022929,  4.32157596,  3.63733461,  4.71350356,
   4.15149816,  5.26161967,  4.87294785,  6.02951473,  5.89507119,  7.11575077,
   7.36421144,  8.67472342, 0]], dtype=float)

    locations = []
    n_locations = len(step_inputs)-4
    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[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, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 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, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 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

    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, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.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
示例#30
0
def define_ha(settings, usafe_r=None):
    '''make the hybrid automaton and return it'''

    ha = LinearHybridAutomaton()
    ha.variables = ["x1", "x2", "x3", "x4", "x5", "x6"]
    #
    loc1 = ha.new_mode('loc1')
    a_matrix = np.array([[0, 1, 0, 0, 0, 0], [0, 0, -0.0106, 0.0106, -0.0106, 0.0106], [0, 0, -10, 0, 0, 0],
                         [0, 0, 0, -10, 0, 0], [0, 0, 0, 0, -10, 0], [0, 0, 0, 0, 0, -10]], dtype=float)

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

    print(a_matrix,  b_matrix)
    R_mult_factor = 0.01

    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, 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([-1.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.4))

        # exp 2
        usafe_set_constraint_list.append(LinearConstraint([0.0, 0.0, -1.0, 0.0, 0.0, 0.0], -0.41))
        # To find the error (reverse - number of counterexamples reduced in equ run)
        # usafe_set_constraint_list.append(LinearConstraint([0.0, 0.0, -1.0, 0.0, 0.0, 0.0], -0.47))

        # exp 3 To find the error (reverse - number of counterexamples increased in equ run)
        # Gets fixed with following values
        #         bigM = 10000.0
        #         lb = -1000.0
        #         ub = 1000.0
        # or epsilon2 = 0.00001 (Earlier it was 0.0005)
        # usafe_set_constraint_list.append(LinearConstraint([-1.0, 0.0, 0.0, 0.0, 0.0, 0.0], -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)

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

    return ha, usafe_set_constraint_list