def test_eventually(): s = gr1.eventually_to_gr1('p', aux='c') assert isinstance(s, spec.GRSpec) assert 'aux' not in str(s) assert 'c' in s.sys_vars assert 'p' in s.sys_vars s.moore = False s.plus_one = False s.qinit = '\A \E' # []!p s0 = spec.GRSpec( sys_vars={'p'}, sys_safety={'!p'}, moore=False, plus_one=False, qinit='\A \E' ) assert not synth.is_realizable('omega', s | s0) # !p && []<>p && []<>!p s1 = spec.GRSpec( sys_vars={'p'}, sys_init={'!p'}, sys_prog={'!p', 'p'}, moore=False, plus_one=False, qinit='\A \E' ) assert synth.is_realizable('omega', s | s1)
def test_response(): s = gr1.response_to_gr1('p', 'q') assert isinstance(s, spec.GRSpec) assert 'p' in s.sys_vars assert 'q' in s.sys_vars # p && []!q s0 = spec.GRSpec(sys_vars={'p', 'q'}, sys_init={'p'}, sys_safety={'!q'}) assert not synth.is_realizable('gr1c', s | s0) # []!p && []!q s1 = spec.GRSpec(sys_vars={'p', 'q'}, sys_safety={'!p && !q'}) assert synth.is_realizable('gr1c', s | s1) # p && q s2 = spec.GRSpec( sys_vars={'p', 'q'}, sys_init={'p && q'}, ) assert synth.is_realizable('gr1c', s | s2) # alternating p, alternating q s3 = spec.GRSpec( sys_vars={'p', 'q'}, sys_safety={'p -> X !p', '!p -> X p', 'p -> X q', 'q -> X ! q'}) assert synth.is_realizable('gr1c', s | s3)
def test_stability(): s = gr1.stability_to_gr1('p', aux='a') assert isinstance(s, spec.GRSpec) assert 'aux' not in s.sys_vars assert 'a' in s.sys_vars assert 'p' in s.sys_vars s.moore = False s.plus_one = False s.qinit = r'\A \E' # p && X[]!p s0 = spec.GRSpec(sys_vars={'p'}, sys_init={'p'}, sys_safety={'p -> X !p', '!p -> X !p'}, moore=False, plus_one=False, qinit=r'\A \E') assert not synth.is_realizable(s | s0) # !p && X[]p s1 = spec.GRSpec(sys_vars={'p'}, sys_init={'!p'}, sys_safety={'!p -> X p', 'p -> X p'}, moore=False, plus_one=False, qinit=r'\A \E') assert synth.is_realizable(s | s1) # []<>p && []<>!p s2 = spec.GRSpec(sys_vars={'p'}, sys_prog={'p', '!p'}, moore=False, plus_one=False, qinit=r'\A \E') assert not synth.is_realizable(s | s2) # env b can prevent !p, but has tp <> become !b, # releasing sys to set p # # env: b && []<>!b # sys: !p && []((b && !p) -> X!p) s3 = spec.GRSpec(env_vars={'b'}, env_init={'b'}, env_prog={'!b'}, sys_vars={'p'}, sys_init={'!p'}, sys_safety={'(b && !p) -> X !p'}, moore=False, plus_one=False, qinit=r'\A \E') assert synth.is_realizable(s | s3) s3.env_prog = [] assert not synth.is_realizable(s | s3)
def setUp(self): self.f_triv = spec.GRSpec( sys_vars="y", moore=False, plus_one=False) self.trivial_unreachable = spec.GRSpec( sys_vars="y", sys_prog="False", moore=False, plus_one=False)
def test_response(): s = gr1.response_to_gr1('p', 'q') assert isinstance(s, spec.GRSpec) assert 'p' in s.sys_vars assert 'q' in s.sys_vars s.moore = False s.plus_one = False s.qinit = '\A \E' # p && []!q s0 = spec.GRSpec( sys_vars={'p', 'q'}, sys_init={'p'}, sys_safety={'!q'}, moore=False, plus_one=False, qinit='\A \E' ) assert not synth.is_realizable('omega', s | s0) # []!p && []!q s1 = spec.GRSpec( sys_vars={'p', 'q'}, sys_safety={'!p && !q'}, moore=False, plus_one=False, qinit='\A \E' ) assert synth.is_realizable('omega', s | s1) # p && q s2 = spec.GRSpec( sys_vars={'p', 'q'}, sys_init={'p && q'}, moore=False, plus_one=False, qinit='\A \E' ) assert synth.is_realizable('omega', s | s2) # alternating p, alternating q s3 = spec.GRSpec( sys_vars={'p', 'q'}, sys_safety={ 'p -> X !p', '!p -> X p', 'p -> X q', 'q -> X ! q'}, moore=False, plus_one=False, qinit='\A \E' ) assert synth.is_realizable('omega', s | s3)
def multiple_env_actions_check(solver='omega'): """Two env players, 3 states controlled by sys. sys wins marginally, due to assumption on next combination of actions by env players. """ # 1 <---> 2 # 1 ---> 3 env_actions = [ { 'name': 'env_alice', 'values': transys.MathSet({'left', 'right'}) }, { 'name': 'env_bob', 'values': transys.MathSet({'bleft', 'bright'}) } ] sys = transys.FTS(env_actions) sys.states.add_from({'s1', 's2', 's3'}) sys.states.initial.add_from({'s1'}) sys.add_edge('s1', 's2', env_alice='left', env_bob='bright') sys.add_edge('s2', 's1', env_alice='left', env_bob='bright') # at state 3 sys loses sys.add_edge('s1', 's3', env_alice='right', env_bob='bleft') logging.debug(sys) env_safe = {('(loc = "s1") -> X( (env_alice = "left") && ' '(env_bob = "bright") )')} sys_prog = {'loc = "s1"', 'loc = "s2"'} specs = spec.GRSpec( env_safety=env_safe, sys_prog=sys_prog, moore=False, plus_one=False, qinit='\A \E') r = synth.is_realizable(specs, sys=sys, solver=solver) assert r # slightly relax assumption specs = spec.GRSpec( sys_prog=sys_prog, moore=False, plus_one=False, qinit='\A \E') r = synth.is_realizable(specs, sys=sys, solver=solver) assert not r
def test_eventually(): s = gr1.eventually_to_gr1('p', aux='c') assert isinstance(s, spec.GRSpec) assert 'aux' not in str(s) assert 'c' in s.sys_vars assert 'p' in s.sys_vars # []!p s0 = spec.GRSpec(sys_vars={'p'}, sys_safety={'!p'}) assert not synth.is_realizable('gr1c', s | s0) # !p && []<>p && []<>!p s1 = spec.GRSpec(sys_vars={'p'}, sys_init={'!p'}, sys_prog={'!p', 'p'}) assert synth.is_realizable('gr1c', s | s1)
def globally(p='p', owner='env'): """ response implements the LTL formula : [](p) as a GR1specification && true && [](p) && []<> true :param p: :param owner: 'sys' -> guarantee or 'env'-> assume (Default) + placement of Aux :return: GR1spec """ env_vars, sys_vars, env_init, sys_init = set(), set(), set(), set() env_safe, sys_safe, env_prog, sys_prog = set(), set(), set(), set() if owner == 'env': env_vars = {} env_init = {} env_safe = {p} env_prog = set() elif owner == 'sys': sys_vars = {} sys_init = {} sys_safe = {p} sys_prog = set() print('Added globally:' + '[](' + p + ')') return spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog)
def test_multi_pg(): """Multiple progress groups for same mode""" ts = transys.AFTS() ts.owner = 'env' ts.sys_actions.add('mode0') ts.sys_actions.add('mode1') ts.atomic_propositions.add_from(['goal']) ts.states.add('s0') ts.states.add('s1', ap={'goal'}) ts.states.add('s2') ts.transitions.add('s0', 's0', sys_actions='mode0') ts.transitions.add('s0', 's1', sys_actions='mode0') ts.transitions.add('s2', 's1', sys_actions='mode0') ts.transitions.add('s2', 's2', sys_actions='mode0') ts.transitions.add('s1', 's2', sys_actions='mode1') ts.transitions.add('s1', 's0', sys_actions='mode1') ts.set_progress_map({'mode0': [set(['s0']), set(['s1'])]}) specs = spec.GRSpec(set(), set(), set(), set(), set(), set(), set(), 'goal') ctrl = synth.synthesize('gr1c', specs, env=ts, ignore_env_init=True) assert ctrl != None
def synth_ex(): sys = full_road_ex() variables = '' for i in range(0, len(sys.states())): #print sys.states()[i],i sys.atomic_propositions.add_from({'x' + str(i)}) sys.states[sys.states()[i]]['ap'].add('x' + str(i)) variables += ' && ((X (x' + str(i) + ')) <-> x' + str(i) + ')' variables = variables[4:] #print variables #sys.atomic_propositions.add_from({'home', 'lot'}) #sys.states.add('Xequal0spaceYequal0spacephiequal3', ap={'home'}) #sys.states.add('Xequal5spaceYequal5spacephiequal7', ap={'lot'}) sys.states.initial.add('Xequal0spaceYequal0spacephiequal3') env_vars = {'park', 'frontclear'} env_init = set() env_prog = {'frontclear'} env_safe = set() sys_vars = {'pReach'} sys_init = {'pReach'} sys_prog = {'x0', 'x10'} sys_safe = { '((X (pReach)) <-> ((pSpace) || (pReach && !park)))', '((!frontclear) -> (' + variables + '))' } sys_prog |= {'pReach'} specs = spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog) specs.moore = True specs.qinit = '\E \A' ctrl = synth.synthesize('omega', specs, sys=sys) assert ctrl is not None, 'unrealizable' #ctrl.save('discrete.pdf') return sys, ctrl
def test_env_act(): """Progress group with environment actions""" ts = transys.AFTS() ts.owner = 'env' ts.sys_actions.add('sys_m0') ts.sys_actions.add('sys_m1') ts.env_actions.add('env_m0') ts.env_actions.add('env_m1') ts.states.add('s0') ts.states.add('s1') ts.transitions.add('s0', 's1', sys_actions='sys_m0', env_actions='env_m0') ts.transitions.add('s0', 's1', sys_actions='sys_m0', env_actions='env_m1') ts.transitions.add('s0', 's1', sys_actions='sys_m1', env_actions='env_m0') ts.transitions.add('s0', 's1', sys_actions='sys_m1', env_actions='env_m1') ts.transitions.add('s1', 's1', sys_actions='sys_m0', env_actions='env_m0') ts.transitions.add('s1', 's1', sys_actions='sys_m1', env_actions='env_m0') ts.transitions.add('s1', 's1', sys_actions='sys_m1', env_actions='env_m1') ts.transitions.add('s1', 's0', sys_actions='sys_m0', env_actions='env_m1') specs = spec.GRSpec(set(), set(), set(), set(), set(), set(), set(), 'eloc = "s0"') # without PG ctrl = synth.synthesize('gr1c', specs, env=ts, ignore_env_init=True) assert ctrl == None # with PG ts.set_progress_map({('env_m0', 'sys_m0'): ('s1', )}) ctrl = synth.synthesize('gr1c', specs, env=ts, ignore_env_init=True) assert ctrl != None
def test_only_mode_control(): """Unrealizable due to non-determinism. Switched system with 2 modes: 'left', 'right'. Modes are controlled by the system. States are controlled by the environment. So only control over dynamics is through mode switching. Transitions are thus interpreted as non-deterministic. This can model uncertain outcomes in the real world, e.g., due to low quality actuators or bad low-level feedback controllers. """ # Create a finite transition system env_sws = transys.FTS() env_sws.owner = 'env' env_sws.sys_actions.add_from({'right', 'left'}) # str states n = 4 states = transys.prepend_with(range(n), 's') env_sws.atomic_propositions.add_from(['home', 'lot']) # label TS with APs ap_labels = [set(), set(), {'home'}, {'lot'}] for i, label in enumerate(ap_labels): state = 's' + str(i) env_sws.states.add(state, ap=label) # mode1 transitions transmat1 = np.array([[0, 1, 0, 1], [0, 1, 0, 0], [0, 1, 0, 1], [0, 0, 0, 1]]) env_sws.transitions.add_adj(sp.lil_matrix(transmat1), states, {'sys_actions': 'right'}) # mode2 transitions transmat2 = np.array([[1, 0, 0, 0], [1, 0, 1, 0], [0, 0, 1, 0], [1, 0, 1, 0]]) env_sws.transitions.add_adj(sp.lil_matrix(transmat2), states, {'sys_actions': 'left'}) env_vars = {'park'} env_init = {'eloc = "s0"', 'park'} env_prog = {'!park'} env_safe = set() sys_vars = {'X0reach'} sys_init = {'X0reach'} sys_prog = {'home'} sys_safe = {'next(X0reach) <-> lot || (X0reach && !park)'} sys_prog |= {'X0reach'} specs = spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog) r = synth.is_realizable('gr1c', specs, env=env_sws, ignore_env_init=True) assert not r
def test_until(): s = gr1.until_to_gr1('p', 'q', aux='c') assert isinstance(s, spec.GRSpec) assert 'aux' not in str(s) assert 'c' in s.sys_vars assert 'p' in s.sys_vars assert 'q' in s.sys_vars s.moore = False s.plus_one = False s.qinit = '\A \E' # []!q s0 = spec.GRSpec( sys_vars={'q'}, sys_safety={'!q'}, moore=False, plus_one=False, qinit='\A \E' ) assert not synth.is_realizable('omega', s | s0) # !q && <>q s1 = spec.GRSpec( sys_vars={'q'}, sys_init={'!q'}, sys_prog={'q'}, moore=False, plus_one=False, qinit='\A \E' ) assert synth.is_realizable('omega', s | s1) # !q && []!p && <>q s1 = spec.GRSpec( sys_vars={'q'}, sys_init={'!q'}, sys_safety={'!p'}, sys_prog={'q'}, moore=False, plus_one=False, qinit='\A \E' ) assert not synth.is_realizable('omega', s | s1)
def setUp(self): self.triv = spec.GRSpec(env_vars="x", sys_vars="y", env_init="x & y", env_prog="x", sys_init="y", sys_prog="y && x") self.triv_M = synth.synthesize('omega', self.triv) self.dcounter = spec.GRSpec( sys_vars={"y": (0, 5)}, env_init=['y = 0'], sys_prog=["y=0", "y=5"]) self.dcounter_M = synth.synthesize('omega', self.dcounter) self.enumf = spec.GRSpec( sys_vars={'y': ['a', 'b']}, env_init=['y="a"'], sys_safety=['y = "a" -> X(y = "b")', 'y = "b" -> X(y = "a")']) self.enumf_M = synth.synthesize('omega', self.enumf)
def test_singleton(): """AFTS with one mode and one state""" ts = transys.AFTS() ts.owner = 'env' ts.sys_actions.add('mode0') ts.states.add('s0') ts.transitions.add('s0', 's0', sys_actions='mode0') specs = spec.GRSpec(set(), set(), set(), set(), set(), set(), set(), set()) ctrl = synth.synthesize('gr1c', specs, env=ts, ignore_env_init=True) assert ctrl != None
def setup_method(self): self.triv = spec.GRSpec(env_vars="x", sys_vars="y", env_init="x & y", env_prog="x", sys_init="y", sys_prog="y && x") self.triv.qinit = r'\E \A' self.triv_M = synth.synthesize(self.triv, solver='omega') self.dcounter = spec.GRSpec(sys_vars={"y": (0, 5)}, env_init=['y = 0'], sys_prog=["y=0", "y=5"]) self.dcounter_M = synth.synthesize(self.dcounter, solver='omega') self.enumf = spec.GRSpec( sys_vars={'y': ['a', 'b']}, env_init=['y="a"'], sys_safety=['y = "a" -> X(y = "b")', 'y = "b" -> X(y = "a")']) self.enumf_M = synth.synthesize(self.enumf, solver='omega')
def create_controller(self): self.handle_init_system() self.handle_static_obstacles() self.handle_allowed_moves() self.follow_targets() #self.no_collision() specs = spec.GRSpec({} ,self.sys_vars,set() , self.sys_init , set() , self.sys_safe , set() , self.sys_prog) #print specs.pretty() specs.moore = True specs.qinit = '\E \A' ctrl = synth.synthesize('gr1c' , specs) assert ctrl is not None, 'Unrealizable' dumpsmach.write_python_case(self.controller_name+'_controller.py', ctrl, classname=self.controller_name+'Controller')
def test_with_pg(self): self.env_sws.set_progress_map({ 'mode0': ('s0', 's1', 's2', 's3'), 'mode1': ('s0', ) }) # eventually reach s4 sys_prog = {'exit'} specs = spec.GRSpec(set(), set(), set(), set(), set(), set(), set(), sys_prog) ctrl = synth.synthesize('gr1c', specs, env=self.env_sws, ignore_env_init=True) assert ctrl != None
def test_until(): s = gr1.until_to_gr1('p', 'q', aux='c') assert isinstance(s, spec.GRSpec) assert 'aux' not in str(s) assert 'c' in s.sys_vars assert 'p' in s.sys_vars assert 'q' in s.sys_vars # []!q s0 = spec.GRSpec(sys_vars={'q'}, sys_safety={'!q'}) assert not synth.is_realizable('gr1c', s | s0) # !q && <>q s1 = spec.GRSpec(sys_vars={'q'}, sys_init={'!q'}, sys_prog={'q'}) assert synth.is_realizable('gr1c', s | s1) # !q && []!p && <>q s1 = spec.GRSpec(sys_vars={'q'}, sys_init={'!q'}, sys_safety={'!p'}, sys_prog={'q'}) assert not synth.is_realizable('gr1c', s | s1)
def design_C(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog): logging.basicConfig(level=logging.WARNING) show = False # Constructing GR1spec from environment and systems specifications: specs = spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog) specs.moore = True specs.qinit = '\E \A' # Synthesize ctrl = synth.synthesize(specs) assert ctrl is not None, 'unrealizable' return ctrl
def test_env_act_all(): """System action progress group with environment actions""" ts = transys.AFTS() ts.owner = 'env' ts.sys_actions.add('sys_m0') ts.sys_actions.add('sys_m1') ts.env_actions.add('env_m0') ts.env_actions.add('env_m1') ts.states.add('s0') ts.states.add('s1') ts.states.add('s2') # all s0 -> s1 ts.transitions.add('s0', 's1', sys_actions='sys_m0', env_actions='env_m0') ts.transitions.add('s0', 's1', sys_actions='sys_m0', env_actions='env_m1') ts.transitions.add('s0', 's1', sys_actions='sys_m1', env_actions='env_m0') ts.transitions.add('s0', 's1', sys_actions='sys_m1', env_actions='env_m1') # all s1 -> s2 ts.transitions.add('s1', 's2', sys_actions='sys_m0', env_actions='env_m0') ts.transitions.add('s1', 's2', sys_actions='sys_m0', env_actions='env_m1') ts.transitions.add('s1', 's2', sys_actions='sys_m1', env_actions='env_m0') ts.transitions.add('s1', 's2', sys_actions='sys_m1', env_actions='env_m1') ts.transitions.add('s2', 's0', sys_actions='sys_m0', env_actions='env_m0') ts.transitions.add('s2', 's1', sys_actions='sys_m1', env_actions='env_m0') ts.transitions.add('s2', 's1', sys_actions='sys_m1', env_actions='env_m1') ts.transitions.add('s2', 's1', sys_actions='sys_m0', env_actions='env_m1') specs = spec.GRSpec(set(), set(), set(), set(), set(), set(), set(), 'eloc = "s0"') # without PG ctrl = synth.synthesize('gr1c', specs, env=ts, ignore_env_init=True) assert ctrl == None # with PG that depends on env (env can change and prevent reach) ts.set_progress_map({('env_m0', 'sys_m0'): ('s1', 's2')}) ctrl = synth.synthesize('gr1c', specs, env=ts, ignore_env_init=True) assert ctrl == None # with PG that does not depend on env ts.set_progress_map({'sys_m0': ('s1', 's2')}) ctrl = synth.synthesize('gr1c', specs, env=ts, ignore_env_init=True) assert ctrl != None
def parking_spec(): # barely realizable: assumption necessary env_prog = '! (eact = park)' sys_vars = {'X0reach'} sys_init = {'X0reach'} sys_prog = {'home'} # one additional requirement: if in lot, # then stay there until park signal is turned off sys_safe = {'(X (X0reach) <-> lot) || (X0reach && !(eact = "park") )', '((lot & (eact = "park") ) -> X(lot))'} sys_prog |= {'X0reach'} specs = spec.GRSpec(sys_vars=sys_vars, sys_init=sys_init, sys_safety=sys_safe, env_prog=env_prog, sys_prog=sys_prog) return specs
def test_translate_ast_to_gr1c(): x = '(loc = "s2") -> X((((env_alice = "left") && (env_bob = "bright"))))' s = spec.GRSpec(sys_vars={ 'loc': ['s0', 's2'], 'env_alice': ['left', 'right'], 'env_bob': ['bleft', 'bright'] }, sys_safety=[x]) s.str_to_int() sint = s._bool_int[x] print(repr(sint)) rint = s.ast(sint) print(repr(rint)) r = ts.translate_ast(rint, 'gr1c') print(repr(r)) print(r.flatten()) assert r.flatten() == ("( ( loc = 1 ) -> " "( ( env_alice' = 0 ) & ( env_bob' = 1 ) ) )")
def Xtimes(ap, x=1, newap=None, owner='env'): """ Create new variable which is true x time instances after ap is true. :param ap: string representing a simple boolean variable :param x: :param newap: string representing name of a boolean variable :param owner: 'sys' -> guarantee or 'env'-> assume (Default) + placement of newap :return: GRSpec """ env_vars, sys_vars, env_init, sys_init = set(), set(), set(), set() env_safe, sys_safe, env_prog, sys_prog = set(), set(), set(), set() if owner == 'env': old_var = ap for i in range(x - 1): # runs from 0..x-1 next_var = 'X' + str(i + 1) + ap env_vars |= {next_var} env_safe |= {next_var + ' <-> ' + '( X (' + old_var + '))'} old_var = next_var next_var = 'X' + str(x) + ap if newap: next_var = newap env_vars |= {next_var} env_safe |= {next_var + ' <-> ' + '( X (' + old_var + '))'} elif owner == 'sys': old_var = ap for i in range(x - 1): # runs from 0..x-1 next_var = 'X' + str(i + 1) + ap sys_vars |= {next_var} sys_safe |= {next_var + ' <-> ' + '( X (' + old_var + '))'} old_var = next_var next_var = 'X' + str(x) + ap sys_vars |= {next_var} sys_safe |= {next_var + ' <-> ' + '( X (' + old_var + '))'} print('Added variables ' + str(sys_vars | env_vars)) return spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog)
def test_nopg(): """PG for one mode but not the other""" ts = transys.AFTS() ts.owner = 'env' ts.sys_actions.add('mode0') ts.sys_actions.add('mode1') ts.states.add_from({'s0', 's1', 's2'}) ts.transitions.add('s0', 's1', sys_actions='mode0') ts.transitions.add('s1', 's0', sys_actions='mode0') ts.transitions.add('s1', 's2', sys_actions='mode0') ts.transitions.add('s2', 's2', sys_actions='mode0') ts.set_progress_map({'mode0': ('s0', 's1')}) specs = spec.GRSpec(set(), set(), set(), set(), set(), set(), set(), {'eloc = "s1"', 'eloc = "s2"'}) ctrl = synth.synthesize('gr1c', specs, env=ts, ignore_env_init=True) assert ctrl != None
def response(trig='trig', react='act', owner='env', aux='aux'): """ response implements the LTL formula : [](trig -> <> react) as a GR1specification && aux && [](X aux <-> (react || (aux && ! trig)) ) && []<> aux :param trig: Boolean formula :param react: Boolean formula :param owner: 'sys' -> guarantee or 'env'-> assume (Default) + placement of Aux :return: GR1spec """ env_vars, sys_vars, env_init, sys_init = set(), set(), set(), set() env_safe, sys_safe, env_prog, sys_prog = set(), set(), set(), set() if owner == 'env': env_vars = {aux} env_init = {aux} env_safe = { '(X ' + aux + ') <-> ( (' + react + ') ||' + '( ' + aux + ' && ! (' + trig + ') )' + ') ' } env_prog = {aux} elif owner == 'sys': sys_vars = {aux} sys_init = {aux} sys_safe = { '(X ' + aux + ') <-> ( (' + react + ') ||' + '( ' + aux + ' && !(' + trig + ') ) ' + ') ' } sys_prog = {aux} print('Added global Response :' + '[](' + trig + '-> <>' + react + ')') return spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog)
sys_prog |= {'temp_2'} sys_prog |= {'temp_3'} sys_safe |= {'X(temp_1) <-> (loc=0 || (temp_1 && !seat_0=1 && !seat_1=1))'} sys_safe |= {'X(temp_2) <-> (loc=1 || (temp_2 && !seat_0=2 && !seat_1=2))'} sys_safe |= {'X(temp_3) <-> (loc=2 || (temp_3 && !seat_0=3 && !seat_1=3))'} #sys_safe |= {'(!hold_00 || !hold_10) -> <>loc=3'} sys_vars["temp_4"] = 'boolean' sys_init |= {'temp_4'} sys_prog |= {'temp_4'} sys_safe |= {'X(temp_4) <-> (loc=3 || (temp_4 && hold_0=0 && hold_1=0))'} # @sys_specs_section_end@ # Initialize GRspec specs = spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog) ctrl = synth.synthesize('gr1c', specs) # dumpsmach.write_python_case("strategy_discrete.py", ctrl, classname="strategy") # write_matlab_case("strategy_discrete_3.m", ctrl, classname="strategy_discrete_3") # if not ctrl.save('taxi_planning_3person.png'): # print(ctrl) print(ctrl) # either select current state before simulation # ctrl.states.current = ['Sinit'] # ctrl.simulate(inputs_sequence='manual', iterations=50)
def spec_gen(num_req, num_pas): env_vars = set() env_init = set() env_safe = set() env_prog = set() sys_vars = set() sys_init = set() sys_safe = set() sys_prog = set() # Add variables for n in range(num_req): env_vars.add('req_' + str(n)) sys_vars.add('loc_' + str(n)) sys_vars.add('temp_' + str(n)) for l in range(num_pas): sys_vars.add('wait_' + str(l) + '_' + str(n)) sys_vars.add('seat_' + str(l) + '_' + str(n)) sys_vars.add('full') sys_vars.add('temp') sys_vars.add('des') env_vars.add('ready') # Add env specification env_prog.add('ready') all_req_zero = '' for n in range(num_req): if n == 0: all_req_zero += '!req_' + str(n) else: all_req_zero += ' && !req_' + str(n) env_safe.add('full -> X(' + all_req_zero + ')') if num_req > 1: for n in range(num_req): all_req_zero = '' for k in range(num_req): if n != k: if n == 0 and k == 1: all_req_zero += '!req_' + str(k) elif k == 0: all_req_zero += '!req_' + str(k) else: all_req_zero += ' && !req_' + str(k) env_safe.add('req_' + str(n) + ' -> ' + all_req_zero) # Add sys specification # @ For our special example, we add a bridge to connect locations and the destination. @ # TODO: Currently, if you give locations in the order of 3,2 or 2,1 or 3,1 it can pick up # both and then go to destination, but if reverse order, it will pick up and drop off # one at a time. I think if more intermediate locations like bridge are added, in # theory the taxi should pick up more passengers then go to des? Besides, haveing bridge # also ensures that a taxi will not take another 2 new requests when a passenger is # already in the taxi, since without bridge, the location then would have been des already. sys_vars.add('bridge') sys_safe |= {'bridge -> X(!ready -> bridge)'} sys_safe |= {'loc_0 -> X(ready -> loc_0 || loc_1 || loc_2 || bridge)'} sys_safe |= {'loc_1 -> X(ready -> loc_0 || loc_1 || loc_2 || bridge)'} sys_safe |= {'loc_2 -> X(ready -> loc_0 || loc_1 || loc_2 || bridge)'} sys_safe |= { 'bridge -> X(ready -> loc_0 || loc_1 || loc_2 || bridge || des)' } sys_safe |= {'des -> X(ready -> bridge || des)'} # @ End: For our special example, we add a bridge to connect locations and the destination. @ full_def = '' for l in range(num_pas): at_least_wait = '' for n in range(num_req): if n == 0: at_least_wait += 'wait_' + str(l) + '_' + str(n) else: at_least_wait += ' || wait_' + str(l) + '_' + str(n) at_least_seat = '' for n in range(num_req): if n == 0: at_least_seat += 'seat_' + str(l) + '_' + str(n) else: at_least_seat += ' || seat_' + str(l) + '_' + str(n) at_least_one = at_least_wait + ' || ' + at_least_seat if l == 0: full_def += '(' + at_least_one + ')' else: full_def += ' && (' + at_least_one + ')' sys_safe.add('full <-> (' + full_def + ')') if num_req > 1: for n in range(num_req): all_req_zero = '' for k in range(num_req): if n != k: if n == 0 and k == 1: all_req_zero += '!loc_' + str(k) elif k == 0: all_req_zero += '!loc_' + str(k) else: all_req_zero += ' && !loc_' + str(k) all_req_zero += '&& !des && !bridge' # For special cases sys_safe.add('loc_' + str(n) + ' -> ' + all_req_zero) all_req_zero = '' for n in range(num_req): if n == 0: all_req_zero += '!loc_' + str(n) else: all_req_zero += ' && !loc_' + str(n) sys_safe.add('des -> ' + all_req_zero + ' && !bridge') # For special cases sys_safe.add('bridge -> ' + all_req_zero + ' && !des') # For special cases at_least_req = '' for n in range(num_req): if n == 0: at_least_req += 'loc_' + str(n) else: at_least_req += ' || loc_' + str(n) at_least_req += ' || des || bridge' # For special cases sys_safe.add(at_least_req) if num_req > 1: for l in range(num_pas): for n in range(num_req): all_req_zero = '' for k in range(num_req): if n != k: if n == 0 and k == 1: all_req_zero += '!wait_' + str(l) + '_' + str(k) elif k == 0: all_req_zero += '!wait_' + str(l) + '_' + str(k) else: all_req_zero += ' && !wait_' + str(l) + '_' + str( k) sys_safe.add('wait_' + str(l) + '_' + str(n) + ' -> ' + all_req_zero) if num_req > 1: for l in range(num_pas): for n in range(num_req): all_req_zero = '' for k in range(num_req): if n != k: if n == 0 and k == 1: all_req_zero += '!seat_' + str(l) + '_' + str(k) elif k == 0: all_req_zero += '!seat_' + str(l) + '_' + str(k) else: all_req_zero += ' && !seat_' + str(l) + '_' + str( k) sys_safe.add('seat_' + str(l) + '_' + str(n) + ' -> ' + all_req_zero) for l in range(num_pas): all_wait_zero = '' for n in range(num_req): if n == 0: all_wait_zero += '!wait_' + str(l) + '_' + str(n) else: all_wait_zero += ' && !wait_' + str(l) + '_' + str(n) all_seat_zero = '' for n in range(num_req): if n == 0: all_seat_zero += '!seat_' + str(l) + '_' + str(n) else: all_seat_zero += ' && !seat_' + str(l) + '_' + str(n) at_least_one = '(' + all_wait_zero + ') || (' + all_seat_zero + ')' sys_safe.add(at_least_one) for n in range(num_req): sys_safe.add('loc_' + str(n) + ' -> X(!ready -> loc_' + str(n) + ')') sys_safe.add('des -> X(!ready -> des)') for l in range(num_pas): for n in range(num_req): sys_init.add('!seat_' + str(l) + '_' + str(n)) sys_init.add('des') for n in range(num_req): for l in range(num_pas): sys_vars.add('last_wait_' + str(l) + '_' + str(n)) sys_safe.add('X(last_wait_' + str(l) + '_' + str(n) + ') <-> wait_' + str(l) + '_' + str(n)) sys_init.add('!last_wait_' + str(l) + '_' + str(n)) for n in range(num_req): at_least_one = '' for l in range(num_pas): if l == 0: at_least_one += '(!last_wait_' + str(l) + '_' + str( n) + ' && ' + 'wait_' + str(l) + '_' + str(n) + ')' else: at_least_one += ' || (!last_wait_' + str(l) + '_' + str( n) + ' && ' + 'wait_' + str(l) + '_' + str(n) + ')' sys_safe.add('req_' + str(n) + ' -> (' + at_least_one + ')') if num_pas > 1: for n in range(num_req): for l in range(num_pas): all_no_change = '' for k in range(num_pas): if l != k: if l == 0 and k == 1: all_no_change += '(!last_wait_' + str( k) + '_' + str(n) + ' -> !wait_' + str( k) + '_' + str(n) + ')' elif k == 0: all_no_change += '(!last_wait_' + str( k) + '_' + str(n) + ' -> !wait_' + str( k) + '_' + str(n) + ')' else: all_no_change += ' && (!last_wait_' + str( k) + '_' + str(n) + ' -> !wait_' + str( k) + '_' + str(n) + ')' sys_safe.add('(!last_wait_' + str(l) + '_' + str(n) + ' && ' + 'wait_' + str(l) + '_' + str(n) + ') -> ' + all_no_change) for l in range(num_pas): for n in range(num_req): sys_safe.add('wait_' + str(l) + '_' + str(n) + ' -> X(!loc_' + str(n) + ' -> wait_' + str(l) + '_' + str(n) + ')') for l in range(num_pas): for n in range(num_req): sys_safe.add('wait_' + str(l) + '_' + str(n) + ' -> X(loc_' + str(n) + ' -> !wait_' + str(l) + '_' + str(n) + ' && ' + 'seat_' + str(l) + '_' + str(n) + ')') for l in range(num_pas): all_wait_zero = '' all_req_zero = '' for n in range(num_req): if n == 0: all_wait_zero += '!wait_' + str(l) + '_' + str(n) all_req_zero += '!req_' + str(n) else: all_wait_zero += ' && !wait_' + str(l) + '_' + str(n) all_req_zero += ' && !req_' + str(n) sys_safe.add('(' + all_wait_zero + ') -> X(' + all_req_zero + ' -> ' + all_wait_zero + ')') for l in range(num_pas): for n in range(num_req): sys_safe.add('seat_' + str(l) + '_' + str(n) + ' -> X(!des -> seat_' + str(l) + '_' + str(n) + ')') for l in range(num_pas): at_least_seat = '' all_seat_zero = '' for n in range(num_req): if n == 0: at_least_seat += 'seat_' + str(l) + '_' + str(n) all_seat_zero += '!seat_' + str(l) + '_' + str(n) else: at_least_seat += ' || seat_' + str(l) + '_' + str(n) all_seat_zero += ' && !seat_' + str(l) + '_' + str(n) sys_safe.add('(' + at_least_seat + ') -> X(des -> (' + all_seat_zero + '))') for l in range(num_pas): all_seat_zero = '' all_no_reach = '' for n in range(num_req): if n == 0: all_seat_zero += '!seat_' + str(l) + '_' + str(n) all_no_reach += '!(wait_' + str(l) + '_' + str( n) + ' && X(loc_' + str(n) + '))' else: all_seat_zero += ' && !seat_' + str(l) + '_' + str(n) all_no_reach += ' && !(wait_' + str(l) + '_' + str( n) + ' && X(loc_' + str(n) + '))' sys_safe.add('(' + all_seat_zero + ') -> ((' + all_no_reach + ') -> X(' + all_seat_zero + '))') # Two eventually constraints for n in range(num_req): all_wait_zero = '' for l in range(num_pas): if l == 0: all_wait_zero += '!wait_' + str(l) + '_' + str(n) else: all_wait_zero += ' && !wait_' + str(l) + '_' + str(n) sys_safe.add('X(temp_' + str(n) + ') <-> ((' + all_wait_zero + ' && temp_' + str(n) + ') || loc_' + str(n) + ')') sys_prog.add('temp_' + str(n)) sys_init.add('temp_' + str(n)) all_seat_zero = '' for l in range(num_pas): for n in range(num_req): if l == 0 and n == 0: all_seat_zero += '!seat_' + str(l) + '_' + str(n) else: all_seat_zero += ' && !seat_' + str(l) + '_' + str(n) sys_safe.add('X(temp) <-> ((' + all_seat_zero + ' && temp) || des)') sys_prog.add('temp') sys_init.add('temp') specs = spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog) return specs
iter1, gen_enabled_dir(enabled_dir_4, margin + env_size + sys_size, strict_dist=True)) sys_safe |= { '(obs_close = 1) && (env1 = ' + str(iter1) + ') -> (' + get_spec_list_neighbor('loc', next_state_all) + ')' } sys_safe |= { '(obs_close = 0) && (env1 = ' + str(iter1) + ') -> (' + get_spec_list_neighbor('loc', next_state) + ')' } #Don't allow sys and env to exchange position specs = spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog) #GR(1) specification created print specs.pretty() # # Controller synthesis # # The controller decides based on current variable values only, # without knowing yet the next values that environment variables take. # A controller with this information flow is known as Moore. specs.moore = False # Ask the synthesizer to find initial values for system variables # that, for each initial values that environment variables can # take and satisfy `env_init`, the initial state satisfies # `env_init /\ sys_init`. specs.qinit = '\E \A' # i.e., "there exist sys_vars: forall sys_vars" ctrl = synth.synthesize('gr1c', specs)
def specify_discretize_synthesize(): """Return PWA partition and controller, dump them to pickle files.""" # Problem parameters input_bound = 1.0 uncertainty = 0.01 # Continuous state space cont_state_space = box2poly([[0., 3.], [0., 2.]]) # Continuous dynamics A = np.array([[1.0, 0.], [0., 1.0]]) B = np.array([[0.1, 0.], [0., 0.1]]) E = np.array([[1., 0.], [0., 1.]]) # Available control, possible disturbances U = input_bound * np.array([[-1., 1.], [-1., 1.]]) W = uncertainty * np.array([[-1., 1.], [-1., 1.]]) # Convert to polyhedral representation U = box2poly(U) W = box2poly(W) # Construct the LTI system describing the dynamics sys_dyn = hybrid.LtiSysDyn(A, B, E, None, U, W, cont_state_space) # Define atomic propositions for relevant regions of state space cont_props = {} cont_props['home'] = box2poly([[0., 1.], [0., 1.]]) cont_props['lot'] = box2poly([[2., 3.], [1., 2.]]) # Compute proposition preserving partition of the continuous state space cont_partition = prop2part(cont_state_space, cont_props) pwa = discretize(cont_partition, sys_dyn, closed_loop=True, N=8, min_cell_volume=0.1, plotit=False) """Specifications""" # Environment variables and assumptions env_vars = {'park'} env_init = set() env_prog = '!park' env_safe = set() # System variables and requirements sys_vars = {'X0reach'} sys_init = {'X0reach'} sys_prog = {'home'} # []<>home sys_safe = {'(X(X0reach) <-> lot) || (X0reach && !park)'} sys_prog |= {'X0reach'} # Create the specification specs = spec.GRSpec(env_vars, sys_vars, env_init, sys_init, env_safe, sys_safe, env_prog, sys_prog) specs.qinit = '\A \E' specs.moore = False specs.plus_one = False """Synthesize""" ctrl = synth.synthesize(specs, sys=pwa.ts, ignore_sys_init=True, solver='gr1c') # store the result for future use if len(BUILDDIR) > 0 and not os.path.exists(BUILDDIR): os.mkdir(BUILDDIR) pickle.dump(ctrl, open(BUILDDIR + 'FSM.p', 'wb')) pickle.dump(pwa, open(BUILDDIR + 'AbstractPwa.p', 'wb')) return pwa, ctrl