def test_eat_other(self): 'test star.eat_star() on a non-equal star: [1,2] eating [2, 3] results in [1, 3]' star = init_hr_to_star(make_settings(), HyperRectangle([(1, 2)]), TestStar.loc) star2 = init_hr_to_star(make_settings(), HyperRectangle([(2, 3)]), TestStar.loc) expected = init_hr_to_star(make_settings(), HyperRectangle([(1, 3)]), TestStar.loc) star.eat_star(star2) self.check_stars_equal(star, expected)
def test_eat_other_2d(self): 'test star.eat_star() on a 2-d non-equal star' star = init_hr_to_star(make_settings(), HyperRectangle([(-5, -2), (10, 20)]), TestStar.loc) star2 = init_hr_to_star(make_settings(), HyperRectangle([(20, 34), (-3, -2)]), TestStar.loc) expected = init_hr_to_star(make_settings(), HyperRectangle([(-5, 34), (-3, 20)]), TestStar.loc) star.eat_star(star2) self.check_stars_equal(star, expected)
def test_eat_rotated_unit(self): 'test star.eat_star() on a rotated unit-square eating the unit square' size = math.sqrt(2.0) / 2.0 basis = [[size, size], [size, -size]] a_mat = [[1., 0.], [-1., 0.], [0., 1.], [0., -1.]] b_vec = [1, 1, 1, 1] center = [0, 0] star = make_star(center, basis, a_mat, b_vec) star2 = init_hr_to_star(make_settings(), HyperRectangle([(-1, 1), (-1, 1)]), TestStar.loc) size = 1 basis = [[size, size], [size, -size]] a_mat = [[1., 0.], [-1., 0.], [0., 1.], [0., -1.]] b_vec = [1, 1, 1, 1] center = [0, 0] expected = make_star(center, basis, a_mat, b_vec) star.eat_star(star2) self.check_stars_equal(star, expected)
def test_eat_self(self): 'test star.eat_star() on an equal star (should not change constraints)' hr = HyperRectangle([(1, 2), (2, 2), (-6, -3)]) star = init_hr_to_star(make_settings(), hr, TestStar.loc) star2 = init_hr_to_star(make_settings(), hr, TestStar.loc) star.eat_star(star2) tol = 1e-6 # constraints should be the same as before a_mat = make_star_a_mat(star) a_mat2 = make_star_a_mat(star2) b_vec = make_star_b_vec(star) b_vec2 = make_star_b_vec(star2) self.assertEqual(len(b_vec), len(b_vec2)) for row_index in xrange(len(b_vec)): assert_allclose(a_mat[row_index], a_mat2[row_index], rtol=tol, atol=tol) self.assertAlmostEqual(b_vec[row_index], b_vec2[row_index]) self.check_stars_equal(star, star2)
def define_init_states(ha): '''returns a list of (mode, HyperRectangle)''' # Variable ordering: [x, y] rv = [] r = HyperRectangle([(-5.9, -4.9), (0, 1)]) rv.append((ha.modes['loc1'], r)) return rv
def define_init_states(ha): '''returns a list of (mode, HyperRectangle)''' # Variable ordering: [x, v] rv = [] rv.append( (ha.modes['extension'], HyperRectangle([(-1.05, -0.95), (-0.1, 0.1)]))) return rv
def test_square_eat_parallelogram(self): 'test square star eating parallelogram star' star = init_hr_to_star(make_settings(), HyperRectangle([(0, 1), (0, 1)]), TestStar.loc) basis = [[1, 0], [1, 1]] a_mat = [[1., 0.], [-1, 0.], [0., 1.], [0., -1.]] b_vec = [1, 0, 1, 0] center = [0, 0] par = make_star(center, basis, a_mat, b_vec) expected = init_hr_to_star(make_settings(), HyperRectangle([(0, 2), (0, 1)]), TestStar.loc) star.eat_star(par) self.check_stars_equal(star, expected)
def define_init_states(ha): '''returns a list of (mode, HyperRectangle)''' # Variable ordering: [x, y, vx, vy, t] rv = [] r = HyperRectangle([(-925.0, -875.0), (-425.0, -375.0), \ (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (1.0, 1.0)]) rv.append((ha.modes['P2'], r)) return rv
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)
def test_boundaries_optimized(self): 'test finding the boundaries in an optimized fashion' Star.init_plot_vecs(2, TestStar.plot_settings) hr = HyperRectangle([ (0, 1), (0, 1), ]) star = init_hr_to_star(make_settings(), hr, TestStar.loc) start_op = LpInstance.total_optimizations() verts = star.verts() num_op = LpInstance.total_optimizations() - start_op self.assertEqual(len(verts), 5) self.assertLess(num_op, 100)
def test_offset_dif_basis(self): 'test a case an offset square eating another one, with non-standard basis' basis = [[0, 1], [1, 0]] a_mat = [[0, 1], [-1, 0], [1, 0], [0, -1]] b_vec = [4, 1, 1, -2] center = [0, 0] a2 = make_star(center, basis, a_mat, b_vec) basis = [[0, 1], [1, 0]] a_mat = [[0, 1], [-1, 0], [1, 0], [0, -1]] b_vec = [1, 1, 1, 1] center = [0, 0] b = make_star(center, basis, a_mat, b_vec) a2.eat_star(b) expected = init_hr_to_star(make_settings(), HyperRectangle([(-1, 4), (-1, 1)]), TestStar.loc) self.check_stars_equal(a2, expected)
def test_rectangular(self): '''test integration of x' = 1, y' = 2''' ha = LinearHybridAutomaton('Harmonic Oscillator') ha.variables = ["x", "y"] # x' = x loc1 = ha.new_mode('loc') loc1.a_matrix = np.array([[0, 0], [0, 0]]) loc1.c_vector = np.array([1, 2]) # x(0) = 1, y(0) = 2 init_list = [(ha.modes['loc'], HyperRectangle([(0.99, 1.01), (1.99, 2.01)]))] plot_settings = PlotSettings() plot_settings.plot_mode = PlotSettings.PLOT_NONE settings = HylaaSettings(step=0.1, max_time=1.1, plot_settings=plot_settings) settings.print_output = False engine = HylaaEngine(ha, settings) engine.load_waiting_list(init_list) # x(t) = 1 + t; y(t) = 2 + 2*t # pop from waiting_list (doesn't advance state) engine.do_step() for i in xrange(10): engine.do_step() t = 0.1 * (i + 1) star = engine.cur_state point = [1 + t, 2 + 2 * t] self.assertTrue(star.contains_point(point))
def test_exp(self): '''test integration of x' = x''' ha = LinearHybridAutomaton('Harmonic Oscillator') ha.variables = ["x"] # x' = x a_matrix = np.array([[1]], dtype=float) c_vector = np.array([0], dtype=float) loc1 = ha.new_mode('loc') loc1.set_dynamics(a_matrix, c_vector) # x(0) = 1 init_list = [(ha.modes['loc'], HyperRectangle([(0.99, 1.01)]))] plot_settings = PlotSettings() plot_settings.plot_mode = PlotSettings.PLOT_NONE settings = HylaaSettings(step=0.1, max_time=1.1, plot_settings=plot_settings) settings.print_output = False engine = HylaaEngine(ha, settings) engine.load_waiting_list(init_list) # pop from waiting_list (doesn't advance state) engine.do_step() # x(t) should be e^t for i in xrange(10): engine.do_step() t = 0.1 * (i + 1) star = engine.cur_state self.assertTrue(star.contains_point([math.exp(t)]))
def test_hr_to_star(self): 'test hr converstion to a star' hr = HyperRectangle([(1, 2), (2, 2), (-6, -3)]) star = init_hr_to_star(make_settings(), hr, TestStar.loc) self.assertTrue(isinstance(star, Star)) expected_basis = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) assert_allclose(star.basis_matrix, expected_basis, rtol=1e-10, atol=1e-10) expected_a_mat = np.array([[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]]) a_mat = make_star_a_mat(star) assert_allclose(a_mat, expected_a_mat, rtol=1e-10, atol=1e-10) expected_b = np.array([2, -1, 2, -2, -3, 6]) b_vec = make_star_b_vec(star) assert_allclose(b_vec, expected_b, rtol=1e-10, atol=1e-10)
k_matrices[loc] = k_matrix a_bk_matrix = a_matrix_ext - np.matmul(b_matrix_ext, k_matrix) a_bk_matrices[loc] = a_bk_matrix new_pv_object = run_hylaa(settings, init_r, a_bk_matrices, t_jumps, usafe_r) u_interval = new_pv_object.compute_usafe_interval(loc_idx) if u_interval[0] == u_interval[1] == -1: current_loc_safe = True loc_idx = loc_idx + 1 last_pv_object = new_pv_object print(Q_mult_factors, Q_weigths) return t_jumps, k_matrices if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(0.3, 0.7), (1.3, 1.7), (0, 0), (0, 0), (0, 0)]) usafe_r = None 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([[1, 0], [0, 0], [1, 0], [0, 1]], dtype=float) (t_jumps, k_matrices) = find_safe_controller(a_matrix, b_matrix, init_r, usafe_r) print(t_jumps, k_matrices)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=float) n_locations = len(step_inputs) for idx in range(n_locations): a_matrices.append(a_matrix) c_vector = np.matmul(b_matrix, step_inputs[idx]) c_vector[dim-1] = step_size # print(c_vector) c_vectors.append(c_vector) max_steps.append(1) ref_state = np.array([0.0, 20.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]) ref_simulation = np.array(compute_simulation(ref_state, a_matrices, c_vectors, max_steps, 1, disc_dyn=True)) print(ref_simulation) sim_t = np.array(ref_simulation).T plt.plot(sim_t[0], sim_t[1], 'b', linestyle='--') plt.show() init_r = HyperRectangle([(-0.06708257865432346, 0.06708257865432346), (19.932917421345678, 20.067082578654322), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0.9329174213456766, 1.0670825786543234), (-0.06708257865432346, 0.06708257865432346), (0, 0)]) pv_object = run_hylaa(settings, init_r, None, ref_simulation, step_inputs)
'Runs hylaa with the given settings, returning the HylaaResult object.' # assert len(args) > 0 ha, usafe_set_constraint_list = define_ha(settings, args) init = define_init_states(ha, init_r) engine = HylaaEngine(ha, settings) reach_tree = engine.run(init) return PVObject(len(ha.variables), usafe_set_constraint_list, reach_tree) if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(2.0, 3.0), (-3.5, -2.5), (0, 0), (0, 0)]) pv_object = run_hylaa(settings, init_r, None) depth_direction = np.identity(len(init_r.dims)) control_f = open("./control_vals.txt", "a") for idx in range(len(init_r.dims)): deepest_ce_1 = pv_object.compute_deepest_ce(depth_direction[idx]) # print(deepest_ce_1.ce_depth) deepest_ce_2 = pv_object.compute_deepest_ce(-depth_direction[idx]) print("depth difference: {}".format( abs(deepest_ce_1.ce_depth - deepest_ce_2.ce_depth))) # print(deepest_ce_2.ce_depth) control_f.write("depth difference: " + str(abs(deepest_ce_1.ce_depth - deepest_ce_2.ce_depth)) + "\n") control_f.write("******************\n")
ha, usafe_set_constraint_list = define_ha(settings, usafe_r) init = define_init_states(ha, init_r) engine = HylaaEngine(ha, settings) reach_tree = engine.run(init) return PVObject(len(ha.variables), usafe_set_constraint_list, reach_tree) if __name__ == '__main__': settings = define_settings() # exp 1 and 2 init_r = HyperRectangle([(-0.1, 0.1), (-0.1, 0.1), (-0.1, 0.1), (-0.1, 0.1), (-0.1, 0.1), (-0.1, 0.1)]) # exp 3 # init_r = HyperRectangle([(-0.1, 0.1), (-0.1, 0.1), (-0.05, 0.05), (-0.1, 0.1), (-0.1, 0.1), (-0.1, 0.1)]) pv_object = run_hylaa(settings, init_r, None) # longest_ce = pv_object.compute_longest_ce() # mid-order = +2 # random: [15, 12, 17, 5, 18, 16, 7, 13, 14, 1, 11, 6, 0, 2, 9, 10, 3, 8, 4] bdd_ce_object = BDD4CE(pv_object, equ_run=False, smt_mip='mip') bdd_graphs = bdd_ce_object.create_bdd_w_level_merge(level_merge=0, order='mid-order') valid_exps, invalid_exps = bdd_graphs[0].generate_expressions() print(len(valid_exps), len(invalid_exps))
with open("simulation", 'w') as f: f.write('milp_simulation = [') for point in milp_ce_simulation: f.write('{},{};\n'.format(str(point[0]), str(point[1]))) f.write(']') f.write('\n**************************************\n') f.write('z3_simulation = [') for point in z3_ce_simulation: f.write('{},{};\n'.format(point[0], str(point[1]))) f.write(']') if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(-6, -5), (0, 1)]) # usafe_r = HyperRectangle([(-1.5, 1.5), (4, 6)]) # Small usafe_r = HyperRectangle([(-2, 2), (1, 5)]) # milp # usafe_r = HyperRectangle([(-3, 2), (1, 5)]) # Medium # usafe_r = HyperRectangle([(-4, 2), (-1, 6)]) # Large pv_object = run_hylaa(settings, init_r, usafe_r) # pv_object.compute_longest_ce() # depth_direction = np.identity(len(init_r.dims)) # pv_object.compute_deepest_ce(depth_direction[1]) robust_pt = pv_object.compute_robust_ce() # pv_object.compute_counter_examples_using_z3(4) # pv_object.compute_z3_counterexample()
def run_hylaa(settings, init_r, usafe_r): 'Runs hylaa with the given settings, returning the HylaaResult object.' ha, usafe_set_constraint_list = define_ha(settings, usafe_r) init = define_init_states(ha, init_r) engine = HylaaEngine(ha, settings) reach_tree = engine.run(init) return PVObject(len(ha.variables), usafe_set_constraint_list, reach_tree) if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(0.5, 1.5), (0.5, 1.5)]) pv_object = run_hylaa(settings, init_r, None) depth_direction = np.identity(len(init_r.dims)) control_f = open("./control_vals.txt", "a") for idx in range(len(init_r.dims)): deepest_ce_1 = pv_object.compute_deepest_ce(depth_direction[idx]) # print(deepest_ce_1.ce_depth) deepest_ce_2 = pv_object.compute_deepest_ce(-depth_direction[idx]) print("depth difference: {}".format( abs(deepest_ce_1.ce_depth - deepest_ce_2.ce_depth))) # print(deepest_ce_2.ce_depth) control_f.write("depth difference: " + str(abs(deepest_ce_1.ce_depth - deepest_ce_2.ce_depth)) + "\n") control_f.write("******************\n")
plot_settings=plot_settings) s.stop_when_error_reachable = False return s def run_hylaa(settings, init_r, usafe_r): 'Runs hylaa with the given settings, returning the HylaaResult object.' ha, usafe_set_constraint_list = define_ha(settings, usafe_r) init = define_init_states(ha, init_r) engine = HylaaEngine(ha, settings) reach_tree = engine.run(init) return PVObject(len(ha.variables), usafe_set_constraint_list, reach_tree) if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(-6, -5), (0, 1)]) pv_object = run_hylaa(settings, init_r, None) # pv_object.compute_longest_ce() bdd_ce_object = BDD4CE(pv_object, equ_run=True, smt_mip='mip') bdd_graphs = bdd_ce_object.create_bdd_w_level_merge(level_merge=0, order='default') valid_exps, invalid_exps = bdd_graphs[0].generate_expressions() print(len(valid_exps), len(invalid_exps)) Timers.print_stats()
deepest_ce_simulation = compute_simulation(deepest_ce, a_matrix, c_vector, 0.2, 100) with open("simulation", 'w') as f: f.write('longest_simulation = [') t = 0.0 for point in longest_ce_simulation: f.write('{},{};\n'.format(str(point[0]), str(point[1]))) t = t + 0.2 f.write(']') f.write('\n**************************************\n') f.write('deepest_simulation = [') t = 0.0 for point in deepest_ce_simulation: f.write('{},{};\n'.format(point[0], str(point[1]))) t = t + 0.2 f.write(']') if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(0.1, 0.5), (63, 77), (65, 65), (0, 0), (0, 0)]) # init_r = HyperRectangle([(100, 120), (55, 65), (60, 60), (0, 0)]) pv_object = run_hylaa(settings, init_r, None) direction = np.zeros(len(init_r.dims)) longest_ce = pv_object.compute_longest_ce() depth_direction = np.identity(len(init_r.dims)) deepest_ce = pv_object.compute_deepest_ce(depth_direction[1]) Timers.print_stats()
# simulation = compute_simulation(init_point, a_matrix, c_vector, post_verif_object.step, post_verif_object.num_steps) # ce_vector = counterExample.compute_ce_vector(simulation, post_verif_object.usafe_set_constraint_list, direction) # longest_subseq = counterExample.compute_longest_sequence(ce_vector, post_verif_object.init_star, direction) # ce_vectors.append(ce_vector) # longest_ces.append(longest_subseq) # post_verif_object.compute_longest_ce(direction) # depth_direction = np.identity(len(init_r.dims)) # post_verif_object.compute_deepest_ce(depth_direction[4]) # Timers.print_stats() if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), \ (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1), (0.9, 1.1) \ ]) # usafe_r = HyperRectangle( # [(-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), # (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), # (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), # (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2), (-0.5, -0.2)]) #usafe_r = HyperRectangle( # [(-0.5, 0.91), (-0.5, -0.2), (-0.5, 0.99), (-0.5, 0.9), (-0.5, 0.94), (-0.5, 0.93), (-0.5, 0.92), (-0.5, 0.96), # (-0.5, 0.92), (-0.5, 0.9), (-0.5, -0.2), (-0.5, 0.9), (-0.5, 0.94), (-0.5, 0.9), (-0.5, 0.99), (-0.5, 0.9), # (-0.5, -0.2), (-0.5, 0.93), (-0.5, 0.9), (-0.5, 0.9), (-0.5, 0.99), (-0.5, 0.98), (-0.5, 0.91), (-0.5, 0.91), # (-0.5, 1.0), (-0.5, 0.92), (-0.5, 0.93), (-0.5, 0.92), (-0.5, 0.98), (-0.5, -0.2)]) #usafe_r = HyperRectangle(
with open("simulation", 'w') as f: f.write('milp_simulation = [') for point in milp_ce_simulation: f.write('{},{};\n'.format(str(point[0]), str(point[1]))) f.write(']') f.write('\n**************************************\n') f.write('z3_simulation = [') for point in z3_ce_simulation: f.write('{},{};\n'.format(point[0], str(point[1]))) f.write(']') if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(-6, -5), (0, 1)]) # usafe_r = HyperRectangle([(-1.5, 1.5), (4, 6)]) # Small # usafe_r = HyperRectangle([(-2, 4), (1, 5)]) # milp - Check why SMT gives wrong answer!! usafe_r = HyperRectangle([(-3, 2), (1, 5)]) # Medium # usafe_r = HyperRectangle([(-4, 2), (-1, 6)]) # Large pv_object = run_hylaa(settings, init_r, usafe_r) # pv_object.compute_longest_ce() # depth_direction = np.identity(len(init_r.dims)) # pv_object.compute_deepest_ce(depth_direction[1]) robust_pt = pv_object.compute_robust_ce() # pv_object.compute_counter_examples_using_z3(4) # pv_object.compute_z3_counterexample()
settings = HylaaSettings(step=0.1, max_time=10, plot_settings=plot_settings) settings.stop_when_error_reachable = False return settings def run_hylaa(settings, init_r, usafe_r): '''run hylaa with the given settings, returning the HylaaResult object.''' ha, usafe_set_constraint_list = define_ha(settings, usafe_r) init = define_init_states(ha, init_r) engine = HylaaEngine(ha, settings) reach_tree = engine.run(init) return PVObject(len(ha.variables), usafe_set_constraint_list, reach_tree) if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(-925, -875), (-425, -375), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0)]) pv_object = run_hylaa(settings, init_r, None) pv_object.compute_longest_ce() # longest_ce = pv_object.compute_longest_ce() # depth_direction = np.identity(len(init_r.dims)) # deepest_ce = new_pv_object.compute_deepest_ce(depth_direction[0])
plot_settings.ydim = 1 settings = HylaaSettings(step=0.02, max_time=2.0, plot_settings=plot_settings) settings.stop_when_error_reachable = False return settings def run_hylaa(settings, init_r, usafe_r): 'Runs hylaa with the given settings, returning the HylaaResult object.' ha, usafe_set_constraint_list = define_ha(settings, usafe_r) init = define_init_states(ha, init_r) engine = HylaaEngine(ha, settings) reach_tree = engine.run(init) return PVObject(len(ha.variables), usafe_set_constraint_list, reach_tree) if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(540, 541), (10, 20), (10, 20)]) # Direction(s) in which we intend to minimize co-efficients (alpha's) direction = np.ones(len(init_r.dims)) # direction = np.identity(len(init_r.dims)) pv_object = run_hylaa(settings, init_r, None) longest_ce = pv_object.compute_longest_ce()
b_matrices = [] c_vectors = [] max_steps = [] step_inputs = [] for idx in range(n_locations): step_input = ast.literal_eval(lines[l_idx]) step_inputs.append(step_input) l_idx += 1 a_matrices.append(a_matrix) b_matrices.append(b_matrix) c_vector = np.matmul(b_matrix, step_input) c_vector[dim - 1] = step_size # print(c_vector) c_vectors.append(c_vector) max_steps.append(1) init_rect = ast.literal_eval(lines[l_idx]) last_pair = (0.0, 0.0) init_rect.append(last_pair) print(init_rect) l_idx += 1 ref_simulation = np.array(compute_simulation(ref_state, a_matrices, c_vectors, max_steps, 1, disc_dyn=True)) print(ref_simulation) # sim_t = np.array(ref_simulation).T # plt.plot(sim_t[0], sim_t[1], 'b', linestyle='--') # plt.show() init_r = HyperRectangle(init_rect) pv_object = run_hylaa(settings, init_r, None, ref_simulation, step_inputs, k_matrix, a_matrices, b_matrices)
def get_input(A, B, Q, R): (X1, L, G) = care(A, B, Q, R) G = np.array(G) k_matrix = [] for idx in range(len(G)): k_token = G[idx].copy().tolist() k_token.append(0.0) k_matrix.append(k_token) k_matrix = np.array(k_matrix, dtype=float) return k_matrix if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(-1.5, -0.5), (-1.5, -0.5), (-1.5, -0.5), (-1.5, -0.5), (0, 0)]) usafe_r = None a_matrix = np.array( [[0.02366, -0.31922, 0.0012041, -4.0292e-17], [0.25, 0, 0, 0], [0, 0.0019531, 0, 0], [0, 0, 0.0019531, 0]], dtype=float) b_matrix = np.array([[256], [0], [0], [0]], dtype=float) a_matrix_ext, b_matrix_ext = extend_a_b(a_matrix, b_matrix) Q1 = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=float) u_dim = len(b_matrix[0])
def run_hylaa(settings, init_r, usafe_r): 'Runs hylaa with the given settings, returning the HylaaResult object.' ha, usafe_set_constraint_list = define_ha(settings, usafe_r) init = define_init_states(ha, init_r) engine = HylaaEngine(ha, settings) reach_tree = engine.run(init) return PVObject(len(ha.variables), usafe_set_constraint_list, reach_tree) if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(-2.0, -1.0), (-2.0, -1.0)]) pv_object = run_hylaa(settings, init_r, None) depth_direction = np.identity(len(init_r.dims)) control_f = open("./control_vals.txt", "a") for idx in range(len(init_r.dims)): deepest_ce_1 = pv_object.compute_deepest_ce(depth_direction[idx]) # print(deepest_ce_1.ce_depth) deepest_ce_2 = pv_object.compute_deepest_ce(-depth_direction[idx]) print("depth difference: {}".format( abs(deepest_ce_1.ce_depth - deepest_ce_2.ce_depth))) # print(deepest_ce_2.ce_depth) control_f.write("depth difference: " + str(abs(deepest_ce_1.ce_depth - deepest_ce_2.ce_depth)) + "\n") control_f.write("******************\n")
s = HylaaSettings(step=0.2, max_time=20.0, plot_settings=plot_settings) s.stop_when_error_reachable = False return s def run_hylaa(settings, init_r, usafe_r): ha, usafe_set_constraint_list = define_ha(settings, usafe_r) init = define_init_states(ha, init_r) engine = HylaaEngine(ha, settings) reach_tree = engine.run(init) return PVObject(len(ha.variables), usafe_set_constraint_list, reach_tree) if __name__ == '__main__': settings = define_settings() init_r = HyperRectangle([(-0.0020389, -0.0011302), (-0.0004378, 0.0007629), (-0.0017922, -0.0003620), (-0.0019804, -0.0005436), (-0.0008931, 0.0008771), (-0.0001891, 0.0014606), (-0.0006451, 0.0012905), (-0.0002263, 0.0020114), (-0.0011494, 0.0012987), (-0.0016059, 0.0017489), (0.0465752, 0.1101447), (0.5000000, 1.0000000), (0, 0), (20.0, 20.0)]) pv_object = run_hylaa(settings, init_r, None) # pv_object.compute_z3_counterexamples() # pv_object.compute_milp_counterexamples()