def abstract_composite(composite: CompositeInterface, samples=10000): """ Abstract the continuous dynamics with randomly generated boxes """ pspace = composite['x'] anglespace = composite['theta'] bits = 7 precision = { 'x': bits, 'y': bits, 'theta': bits, 'xnext': bits, 'ynext': bits, 'thetanext': bits } abs_starttime = time.time() np.random.seed(1337) for _ in range(samples): iobox = generate_random_io(pspace, anglespace) # Refine abstraction with granularity specified in the precision variable composite = composite.io_refined(iobox, nbits=precision) print("Abstraction Time: ", time.time() - abs_starttime) composite.check() return composite
def test_reachavoid(): composite = CompositeInterface((pcomp, vcomp)) dcpre = DecompCPre(composite, (('p', 'pnext'), ('v', 'vnext')), ('a')) target = pspace.conc2pred(mgr, 'p', [-3, 3], 6, innerapprox=True) target &= vspace.conc2pred(mgr, 'v', [0, 2], 6, innerapprox=True) targetint = Interface(mgr, { 'p': pspace, 'v': vspace }, {}, guar=mgr.true, assum=target) safe = pspace.conc2pred(mgr, 'p', [-8, 8], 6, innerapprox=True) safe &= ~pspace.conc2pred(mgr, 'p', [-.4, .4], 6, innerapprox=True) safe &= vspace.conc2pred(mgr, 'v', [-4, 4], 6, innerapprox=True) safeint = Interface(mgr, { 'p': pspace, 'v': vspace }, {}, guar=mgr.true, assum=safe) game = ReachAvoidGame(dcpre, safeint, targetint) basin, _, _ = game.run() # scatter2D(mgr, ('p', pspace), ('v', vspace), # basin.pred, # fname = "reachavoid_doubleint.png" # )
def __init__(self, mod: CompositeInterface, states, control, elim_order: Sequence = None) -> None: # Check if all modules aren't just a parallel composition if len(mod.sorted_mods()) > 2: raise NotImplementedError( "Only implemented for parallel composed modules.") ControlPre.__init__(self, mod, states, control) self.elimorder = elim_order
def test_safe_control(): composite = CompositeInterface((pcomp, vcomp)) dcpre = DecompCPre(composite, (('p', 'pnext'), ('v', 'vnext')), ('a')) safe = pspace.conc2pred(mgr, 'p', [-8, 8], 6, innerapprox=True) safesink = Interface(mgr, { 'p': pspace, 'v': vspace }, {}, guar=mgr.true, assum=safe) # Solve game and plot 2D invariant region game = SafetyGame(dcpre, safesink) dinv, _, controller = game.run() system = pcomp * vcomp cpre = ControlPre(system, (('p', 'pnext'), ('v', 'vnext')), ('a')) game = SafetyGame(cpre, safesink) inv, _, _ = game.run(verbose=True) assert dinv == inv # assert dinv.count_nb(p_precision + v_precision) == approx(5988) # Simulate for initial states state_box = fn.first(controller.winning_states()) assert state_box is not None state = {k: .5 * (v[0] + v[1]) for k, v in state_box.items()} for step in range(30): u = fn.first(controller.allows(state)) assert u is not None picked_u = { 'a': u['a'][0] } # Pick lower bound of first allowed control voxel state.update(picked_u) nextstate = dynamics(**state) state = {'p': nextstate[0], 'v': nextstate[1]}
def test_reach_control(): composite = CompositeInterface((pcomp, vcomp)) dcpre = DecompCPre(composite, (('p', 'pnext'), ('v', 'vnext')), ('a')) target = pspace.conc2pred(mgr, 'p', [-2, 2], 6, innerapprox=True) targetint = Interface(mgr, { 'p': pspace, 'v': vspace }, {}, guar=mgr.true, assum=target) # Solve game and plot 2D invariant region game = ReachGame(dcpre, targetint) dbasin, _, _ = game.run() system = pcomp * vcomp cpre = ControlPre(system, (('p', 'pnext'), ('v', 'vnext')), ('a')) game = ReachGame(cpre, targetint) basin, _, _ = game.run() assert dbasin == basin
# Sample a valid safe control input from the controller.allows(state) iterator u = fn.first(controller.allows(state)) if u is None: print("No more valid control inputs. Terminating simulation.") break state.update(u) # Iterate dynamics nextstate = dynamics(**state) state = {'x': nextstate[0], 'y': nextstate[1], 'theta': nextstate[2]} if __name__ is "__main__": mgr, dubins_x, dubins_y, dubins_theta = setup() composite = CompositeInterface([dubins_x, dubins_y, dubins_theta]) composite = abstract_composite(composite, samples=15000) # Heuristic used to reduce the size or "compress" the abstraction representation. # Higher significant bits first mgr.reorder(order_heuristic(mgr)) mgr.configure(reordering=False) targetmod = make_target(mgr, composite) basin, controller = run_reach(targetmod, composite) plots(mgr, basin, composite) simulate(controller)
pspace = DynamicCover(-10, 10) vspace = DynamicCover(-16, 16) aspace = DynamicCover(0, 20) # Smaller component modules pcomp = Interface(mgr, {'p': pspace, 'v': vspace}, {'pnext': pspace}) vcomp = Interface(mgr, {'v': vspace, 'a': aspace}, {'vnext': vspace}) bounds = {'p': [-10, 10], 'v': [-16, 16]} # Monolithic system system = pcomp * vcomp # Composite system composite = CompositeInterface((pcomp, vcomp)) # Declare grid precision p_precision = 7 v_precision = 7 precision = { 'p': p_precision, 'v': v_precision, 'a': 7, 'pnext': p_precision, 'vnext': v_precision } bittotal = sum(precision.values()) outorder = {0: 'pnext', 1: 'vnext'} possible_transitions = (pcomp * vcomp).count_io_space(bittotal)
except NameError: mgr, subsys = setup(init=init) abs_iters = 0 # Save abstractions for f in subsys: mgr._dump_dddmp(subsys[f].pred, "{0}/subsys/{1}.dddmp".format(directory, f)) epoch = 0 def count(pred): return mgr.count(pred, statebits) load_prior_controller = False if load_prior_controller: f = CompositeInterface(tuple(subsys[i] for i in states)) target = f['x'].conc2pred(mgr, 'x', (-.1, .1), 7, innerapprox=True) target &= f['y'].conc2pred(mgr, 'y', (1.2, 1.28), 7, innerapprox=False) target &= f['theta'].conc2pred(mgr, 'theta', (-.15, .15), 5, innerapprox=True) target &= f['vy'].conc2pred(mgr, 'vy', (-.8, .1), 6, innerapprox=True) elimorder = [i for i in nextstates] cpre = DecompCPre(f, (('x', 'xnext'), ('y', 'ynext'), ('vx', 'vxnext'), ('vy', 'vynext'), ('theta', 'thetanext'), ('omega', 'omeganext')), ('t', 's'), elim_order=elimorder) from redax.controllers import MemorylessController c = mgr.load("landerreach.dddmp") controller = MemorylessController(cpre, c)