def test_eat_star_bs1_1d(self): 'test 1d eat-star with example-1 derived from the ball_string system' settings = HylaaSettings(0.01, 2.0) array = np.array ha = LinearHybridAutomaton('Test Automaton') ha.variables = ["x"] mode = ha.new_mode('test_mode') center = array([0.]) # x = 0.5 * alpha (2x = alpha) # 0 <= alpha <= 1.0 -> (0 <= x <= 0.5) cur_star = Star(settings, center, array([[0.5]]), [ \ LinearConstraint(array([1.]), 1.0), \ LinearConstraint(array([-1.]), 0.0)], \ None, mode) # 2.0 <= alpha <= 3.0 -> (1.0 <= x <= 1.5) new_star = Star(settings, center, array([[0.5]]), [ \ LinearConstraint(array([1.]), 3.0), \ LinearConstraint(array([-1.]), -2.0)], \ None, mode) cur_star.eat_star(new_star) # should be 0.0 <= alpha <= 3.0 self.assertAlmostEqual(cur_star.constraint_list[0].value, 3.0) self.assertAlmostEqual(cur_star.constraint_list[1].value, 0.0)
def test_eat_star_bs2_1d(self): 'test 1d eat-star derived from example-2 of the ball_string system' array = np.array settings = HylaaSettings(0.01, 2.0) ha = LinearHybridAutomaton('Test Automaton') ha.variables = ["x"] mode = ha.new_mode('test_mode') center = array([0.]) basis_matrix = array([[1.0]], dtype=float) # -alpha <= 1.0 ----> -1.0 <= alpha # 2 * alpha <= -1 ----> alpha <= -0.5 cur_star = Star(settings, center, basis_matrix, [\ LinearConstraint(array([-1.]), 1.0), \ LinearConstraint(array([2.0]), -1.0),\ ], \ None, mode) # -0.7 <= alpha <= -0.6 new_star = Star(settings, center, basis_matrix, [\ LinearConstraint(array([-1.]), 0.7), \ LinearConstraint(array([1.]), -0.6), \ ], \ None, mode) cur_star.eat_star(new_star) # should be unchanged: -0.0 <= alpha and 2 * alpha <= 1.0 self.assertAlmostEqual(cur_star.constraint_list[0].value, 1.0) self.assertAlmostEqual(cur_star.constraint_list[1].value, -1.0)
def test_center_into_constraints_complex(self): '''test that center_into_constraints doesn't modify the stars''' mode = make_debug_mode() array = np.array star = Star(HylaaSettings(0.01, 2.0), array([-0.11060623, -0.62105371]), array([[-0.12748444, -6.33082449], [0.06330825, -0.3807174]]), [ LinearConstraint(array([1., 0.]), -0.95), LinearConstraint(array([-1., 0.]), 1.05), LinearConstraint(array([0., 1.]), 0.1), LinearConstraint(array([0., -1.]), 0.1), LinearConstraint(array([6.33082449, 0.3807174]), -0.6210537093866693), LinearConstraint(array([0.12748444, -0.06330825]), -0.11060623185202963), LinearConstraint(array([-0.12748444, 0.06330825]), 1.1106062318520296) ], None, mode, extra_init=(None, 20, 0)) star2 = star.clone() star2.center_into_constraints(star2.vector_to_star_basis(star2.center)) self.check_stars_equal(star, star2)
def test_boundaries_simple(self): 'check the boundaries for a harmonic oscillator after some time elapses' Star.init_plot_vecs(2, TestStar.plot_settings) center = [0, 0] # basis vectors after some time elapsed vec1 = [0.98510371, -0.09884004] vec2 = [0.09884004, 0.98510371] basis = [vec1, vec2] a_list = [[1, 0], [-1, 0], [0, 1], [0, -1]] b_list = [-4, 5, 1, 0] star = make_star(center, basis, a_list, b_list) Star.plot_vecs = [ np.array([0, -1], dtype=float), # max(y) np.array([-1, 0], dtype=float), # max(x) np.array([1, 0], dtype=float) ] # min(x) verts = star.verts() self.assertGreater(verts[0][1], 1.0) self.assertLess(verts[1][0], -3.0) self.assertGreater(verts[1][0], -4.0) self.assertGreater(verts[2][0], -5.0)
def __init__(self, ha, hylaa_settings): assert isinstance(hylaa_settings, HylaaSettings) assert isinstance(ha, LinearHybridAutomaton) #if not openblas.has_openblas(): # print "Performance warning: OpenBLAS not detected. Matrix operations may be slower than necessary." # print "Is numpy linked with OpenBLAS? (hylaa.operblas.has_openblas() returned False)" if hylaa_settings.simulation.threads is not None: openblas.set_num_threads(hylaa_settings.simulation.threads) self.hybrid_automaton = ha self.settings = hylaa_settings self.num_vars = len(ha.variables) if self.settings.plot.plot_mode != PlotSettings.PLOT_NONE: Star.init_plot_vecs(self.num_vars, self.settings.plot) self.plotman = PlotManager(self, self.settings.plot) # computation self.waiting_list = WaitingList() self.cur_state = None # a Star object self.cur_step_in_mode = None # how much dwell time in current continuous post self.max_steps_remaining = None # bound on num steps left in current mode ; assigned on pop self.cur_sim_bundle = None # set on pop self.reached_error = False self.result = None # a HylaaResult... assigned on run() if self.settings.plot.plot_mode == PlotSettings.PLOT_NONE: self.settings.simulation.use_presimulation = True
def test_eat_star_bs2_2d(self): 'test 2d eat-star derived from example-2 of the ball_string system' array = np.array mode = make_debug_mode() settings = HylaaSettings(0.01, 2.0) center = array([0., 0.]) basis_matrix = array([[1.0, 0], [0, 1.0]], dtype=float) cur_star = Star(settings, center, basis_matrix, [\ LinearConstraint(array([-1., 0.]), 1.0), \ LinearConstraint(array([0., 1.]), 2.1), \ LinearConstraint(array([0., -1.]), -2.0), \ LinearConstraint(array([2.0, 0.]), -1),\ ], \ None, mode) new_star = Star(settings, center, basis_matrix, [\ LinearConstraint(array([-1., 0.]), 0.7), \ LinearConstraint(array([1., 0.]), -0.6), \ LinearConstraint(array([0., 1.]), 2.3), \ LinearConstraint(array([0., -1.]), -2.2), \ ], \ None, mode) cur_star.eat_star(new_star) new_point = new_star.get_feasible_point() self.assertTrue(cur_star.contains_point(new_point))
def test_eat_tricky2(self): 'test 2d eat-star derived from a tricky example with the ball_string system' array = np.array mode = make_debug_mode() settings = HylaaSettings(0.01, 2.0) center = array([0., 0.]) basis_matrix = array([[-1., -1.], [-0.01, -0.4]]) cur_star = Star(settings, center, basis_matrix, [\ LinearConstraint(array([-1., 0.]), 0.5), \ LinearConstraint(array([0., -1.]), -1.), \ LinearConstraint(array([14., 0.1]), -5.)], \ None, mode) new_star = Star(settings, center, basis_matrix, [\ LinearConstraint(array([-1., 0.]), 0.5), \ LinearConstraint(array([0., -1.]), -1.), \ LinearConstraint(array([14.5, 0.15]), 0.),\ ], \ None, mode) cur_star.eat_star(new_star) new_point = new_star.get_feasible_point(standard_dir=[-1, 0]) # move towards inside a little new_point -= 1e-6 * np.array([-1., 0.], dtype=float) self.assertTrue(cur_star.contains_point(new_point))
def test_eat_star_bs_tricky(self): 'test 2d eat-star derived from a tricky example with the ball_string system' array = np.array mode = make_debug_mode() cur_star = Star(HylaaSettings(0.01, 2.0), array([0., 0.]), array([[-1.39364934, -6.33082449],\ [-0.01283523, -0.3807174]]), [LinearConstraint(array([1., 0.]), -0.65858417090053001), \ LinearConstraint(array([-1., 0.]), 0.7585841709005301), \ LinearConstraint(array([0., 1.]), 2.0388429332115034), \ LinearConstraint(array([0., -1.]), -1.8388429332115035), \ LinearConstraint(array([6.33082449, 0.3807174]), 1.9619999999999997), \ LinearConstraint(array([0.12748444, -0.06330825]), -0.19619999994184489), \ LinearConstraint(array([-0.12748444, 0.06330825]), 1.1961999999418449), \ LinearConstraint(array([1.39364934, 0.01283523]), -1.0000000000000002)], \ None, mode, extra_init=(array([[-1.39364934, -6.33082449],\ [-0.01283523, -0.3807174]]), 40, 0)) new_star = Star(HylaaSettings(0.01, 2.0), array([0., 0.]), array([[-1.45695758, -6.33082449],\ [-0.0166424, -0.3807174]]), [LinearConstraint(array([1., 0.]), -0.66180203160723172), \ LinearConstraint(array([-1., 0.]), 0.76180203160723181), \ LinearConstraint(array([0., 1.]), 2.3500231188180134), \ LinearConstraint(array([0., -1.]), -2.1500231188180132), \ LinearConstraint(array([6.33082449, 0.3807174]), 2.0600999999999994), \ LinearConstraint(array([0.12748444, -0.06330825]), -0.21631049992724288), \ LinearConstraint(array([-0.12748444, 0.06330825]), 1.2163104999272427), \ LinearConstraint(array([-1.39364934, -0.01283523]), 1.0004904999853983), \ LinearConstraint(array([1.45695758, 0.0166424]), -1.0000000000000002)], \ None, mode, extra_init=(array([[-1.45695758, -6.33082449],\ [-0.0166424, -0.3807174]]), 41, 0)) cur_star.eat_star(new_star) new_point = new_star.get_feasible_point(standard_dir=[5, -1]) self.assertTrue(cur_star.contains_point(new_point))
def test_eat_star_bs1_2d(self): 'test 2d eat-star derived from example-1 of the ball_string system' array = np.array mode = make_debug_mode() center = array([0., 0.]) basis_matrix = array([[1.0, 0], [0, 0.5]]) cur_star = Star(HylaaSettings(0.01, 2.0), center, basis_matrix, [ \ LinearConstraint(array([1, 0]), 1), \ LinearConstraint(array([-1., 0.]), 0), \ LinearConstraint(array([0., 1.]), 1.0), \ LinearConstraint(array([0., -1.]), 0.0)], \ None, mode) new_star = Star(HylaaSettings(0.01, 2.0), center, basis_matrix, [ \ LinearConstraint(array([1, 0]), 1), \ LinearConstraint(array([-1, 0]), 0), \ LinearConstraint(array([0., 1.]), 3.0), \ LinearConstraint(array([0., -1.]), -2.0)], \ None, mode) cur_star.eat_star(new_star) new_point = new_star.get_feasible_point() self.assertTrue(cur_star.contains_point(new_point))
def make_init_star(ha, hylaa_settings): '''returns a star''' bounds_list = [] dims = ha.modes.values()[0].a_matrix_csr.shape[0] for dim in xrange(dims): if dim == 270: # input 1 lb = 0 ub = 0.1 elif dim == 271: # input 2 lb = 0.8 ub = 1.0 elif dim == 272: # input 3 lb = 0.9 ub = 1.0 elif dim < 270: lb = -0.0001 ub = 0.0001 else: raise RuntimeError('Unknown dimension: {}'.format(dim)) bounds_list.append((lb, ub)) init_space, init_mat, init_mat_rhs, init_range_tuples = bounds_list_to_init(bounds_list) return Star(hylaa_settings, ha.modes['mode'], init_space, init_mat, init_mat_rhs, \ init_range_tuples=init_range_tuples)
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_clone(self): 'test that clone is working as expected' mode = make_debug_mode() array = np.array star = Star( HylaaSettings(0.1, 1.0), array([-0.11060623, -0.62105371]), array([[-0.12748444, -6.33082449], [0.06330825, -0.3807174]]), [ LinearConstraint(array([1., 0.]), -0.76877967521149881), LinearConstraint(array([-1., 0.]), 0.8687796752114989), LinearConstraint(array([0., 1.]), -1.2821810092782602), LinearConstraint(array([0., -1.]), 1.4821810092782604) ], None, mode, extra_init=(None, 20, 0)) self.check_stars_equal(star, star.clone())
def test_center_into_constraints(self): '''test that center_into_constraints doesn't modify the stars''' mode = make_debug_mode() array = np.array star = Star(HylaaSettings(0.1, 1.0), array([5.0, 5.0]), array([[0.707, 0.707], [-0.707, 0.707]]), [ LinearConstraint(array([1., 0.]), 1), LinearConstraint(array([-1., 0.]), 0.), LinearConstraint(array([0., 1.]), 1), LinearConstraint(array([0., -1.]), 0) ], None, mode, extra_init=(None, 20, 0)) star2 = star.clone() star2.center_into_constraints(star2.vector_to_star_basis(star2.center)) self.check_stars_equal(star, star2)
def make_star(center, basis, a_list, b_list): 'make a star without a parent' constraint_list = [] assert len(a_list) == len(b_list) for i in xrange(len(a_list)): vector = np.array(a_list[i], dtype=float) constraint_list.append(LinearConstraint(vector, b_list[i])) mode = make_debug_mode() parent = InitParent(mode) return Star(make_settings(), np.array(center, dtype=float), np.array(basis, dtype=float), constraint_list, parent, mode)
def make_init_star(ha, hylaa_settings): '''returns a star''' bounds_list = [] dims = ha.modes.values()[0].a_matrix_csr.shape[0] for dim in xrange(dims): if dim < 10: bounds_list.append((0.0002, 0.00025)) elif dim == 25: bounds_list.append((-0.0001, 0.0001)) elif dim == 48: # affine input effects term bounds_list.append((0.8, 1.0)) else: bounds_list.append((0.0, 0.0)) init_space, init_mat, init_mat_rhs, init_range_tuples = bounds_list_to_init( bounds_list) return Star(hylaa_settings, ha.modes['mode'], init_space, init_mat, init_mat_rhs, \ init_range_tuples=init_range_tuples)
def test_eat_star_bs3_2d(self): 'test 2d eat-star derived from example 3 with the ball_string system' array = np.array mode = make_debug_mode() settings = HylaaSettings(0.01, 2.0) center = array([0., 0.]) bm = array([[1.0, 1.0], [0., 1.0]]) cur_star = Star(settings, center, bm, [\ LinearConstraint(array([-1., 0.]), 1.), \ LinearConstraint(array([1., 0.]), 0.),\ LinearConstraint(array([0., -1.]), 0.), \ LinearConstraint(array([0., 1.]), 1.), \ ], \ None, mode) new_star = Star(settings, center, bm, [\ LinearConstraint(array([-1., 0.]), 1.), \ LinearConstraint(array([1., 0.]), 0.), \ LinearConstraint(array([0., -1]), -2.), \ LinearConstraint(array([0., 1.]), 3.), \ ], \ None, mode) cur_star.eat_star(new_star) # check that constraint #4 was expanded correctly self.assertAlmostEqual(cur_star.constraint_list[0].value, 1.0) self.assertAlmostEqual(cur_star.constraint_list[1].value, 0.0) self.assertAlmostEqual(cur_star.constraint_list[2].value, 0.0) self.assertAlmostEqual(cur_star.constraint_list[3].value, 3.0) new_point = new_star.get_feasible_point() self.assertTrue(cur_star.contains_point(new_point))
def setUp(self): 'setup function' Timers.reset() Star.init_plot_vecs(2, make_settings().plot)
def test_add_box_dir(self): 'test 2d eat-star with add box directions' array = np.array mode = make_debug_mode() cur_star = Star(HylaaSettings(0.01, 2.0), array([0., 0.]), array([[-1.39364934, -6.33082449],\ [-0.01283523, -0.3807174]]), [LinearConstraint(array([1., 0.]), -0.65858417090053001), \ LinearConstraint(array([-1., 0.]), 0.7585841709005301), \ LinearConstraint(array([0., 1.]), 2.0388429332115034), \ LinearConstraint(array([0., -1.]), -1.8388429332115035), \ LinearConstraint(array([6.33082449, 0.3807174]), 1.9619999999999997), \ LinearConstraint(array([0.12748444, -0.06330825]), -0.19619999994184489), \ LinearConstraint(array([-0.12748444, 0.06330825]), 1.1961999999418449), \ LinearConstraint(array([1.39364934, 0.01283523]), -1.0000000000000002)], \ None, mode, extra_init=(array([[-1.39364934, -6.33082449],\ [-0.01283523, -0.3807174]]), 40, 0)) new_star = Star(HylaaSettings(0.01, 2.0), array([0., 0.]), array([[-1.45695758, -6.33082449],\ [-0.0166424, -0.3807174]]), [LinearConstraint(array([1., 0.]), -0.66180203160723172), \ LinearConstraint(array([-1., 0.]), 0.76180203160723181), \ LinearConstraint(array([0., 1.]), 2.3500231188180134), \ LinearConstraint(array([0., -1.]), -2.1500231188180132), \ LinearConstraint(array([6.33082449, 0.3807174]), 2.0600999999999994), \ LinearConstraint(array([0.12748444, -0.06330825]), -0.21631049992724288), \ LinearConstraint(array([-0.12748444, 0.06330825]), 1.2163104999272427), \ LinearConstraint(array([-1.39364934, -0.01283523]), 1.0004904999853983), \ LinearConstraint(array([1.45695758, 0.0166424]), -1.0000000000000002)], \ None, mode, extra_init=(array([[-1.45695758, -6.33082449],\ [-0.0166424, -0.3807174]]), 41, 0)) # add box constraints for dim in xrange(cur_star.num_dims): vector = np.array( [1.0 if d == dim else 0.0 for d in xrange(cur_star.num_dims)], dtype=float) cur_star.add_std_constraint_direction(vector) cur_star.add_std_constraint_direction(-1 * vector) cur_star.eat_star(new_star) new_point = new_star.get_feasible_point(standard_dir=[5, -1]) self.assertTrue(cur_star.contains_point(new_point))