def figureTwoTest(): alphabet = {0, 1, 2} states = { 0, 1, 2, 3, 4, 5, 6, 7 } adversarial = lambda state, step: state == 0 hardStates = { 3, 4, 5, 6, 7 } softStates = { 3, 4 } delta = defaultdict(dict) delta[0] = { 0: 1, 1: 2, 2: 2 } delta[1] = { 0: 3, 1: 6, 2: 4 } delta[2] = { 0: 4, 1: 7, 2: 5 } hard = ExplicitDFA(alphabet, states, hardStates, 0, delta) soft = ExplicitDFA(alphabet, states, softStates, 0, delta) impro = generate(alphabet, 2, hard, soft, 1.0 / 3.0, adversarial)[0] counts = defaultdict(lambda: 0) for i in range(30000): word = [] it = impro() word += it.send(None) word += it.send(1) #print(word) counts[tuple(word)] += 1 print(counts)
def spec(self, advPos, disallowRepeats=False): numLocs = len(self.targets) failure = 0 success = 1 baseStates = set( itertools.product(itertools.product(range(self.k), repeat=2), itertools.product(range(self.k), repeat=2), range(2), itertools.product((False, True), repeat=numLocs))) states = baseStates | {failure, success} accepting = {success} initial = ((0, 0), advPos, 0, tuple(False for i in range(numLocs))) delta = defaultdict(dict) for mx, my in itertools.product(range(self.k), repeat=2): for ax, ay in itertools.product(range(self.k), repeat=2): for p in range(2): for v in itertools.product((False, True), repeat=numLocs): t = [(mx, my), (ax, ay), p, v] state = tuple(t) loc = state[p] transitions = delta[state] for symbol in self.alphabet: dx, dy = symbol newLoc = (clamp(loc[0] + dx), clamp(loc[1] + dy)) o = 1 - p if p == 1 and newLoc in advNoFly: # adv made illegal move transitions[symbol] = success elif t[o] == newLoc: # collision transitions[symbol] = failure else: t[p] = newLoc t[2] = o fail = False if p == 0: for index, target in enumerate( self.targets): if disallowRepeats and newLoc == target and v[ index]: fail = True break if loc == target: nv = list(v) nv[index] = True t[3] = tuple(nv) break transitions[ symbol] = failure if fail else tuple(t) if all(v): accepting.add(state) tf = delta[failure] ts = delta[success] for symbol in alphabet: tf[symbol] = failure ts[symbol] = success return ExplicitDFA(self.alphabet, states, accepting, initial, delta)
def dilated(dfa): alphabet = dfa.alphabet states = set(itertools.product(range(2), dfa.states)) accepting = set(itertools.product(range(2), dfa.acceptingStates)) initial = (0, dfa.initialState) delta = dict() for state in dfa.states: transitions = dfa.delta[state] delta[(0, state)] = { symbol: (1, transitions[symbol]) for symbol in transitions } delta[(1, state)] = { symbol: (0, state) for symbol in alphabet } return ExplicitDFA(alphabet, states, accepting, initial, delta)
def avoidanceTest(k=5): clamp = lambda x: max(0, min(k-1, x)) alphabet = { (1,0), (-1,0), (0,1), (0,-1) } failure = 0 accepting = set( itertools.product( itertools.product(range(k), repeat=2), itertools.product(range(k), repeat=2), range(2)) ) states = accepting | { failure } initial = ((0, 0), (k-1, k-1), 0) delta = defaultdict(dict) for mx in range(k): for my in range(k): for ax in range(k): for ay in range(k): for p in range(2): t = [(mx, my), (ax, ay), p] state = tuple(t) loc = state[p] transitions = delta[state] for symbol in alphabet: dx, dy = symbol newLoc = (clamp(loc[0] + dx), clamp(loc[1] + dy)) o = 1 - p if t[o] == newLoc: # collision transitions[symbol] = failure else: t[p] = newLoc t[2] = o transitions[symbol] = tuple(t) transitions = delta[failure] for symbol in alphabet: transitions[symbol] = failure hard = ExplicitDFA(alphabet, states, accepting, initial, delta) states = set(itertools.product((False, True), repeat=4)) accepting = { (True, True, True, True) } initial = (False, False, False, False) delta = dict() oa = list(alphabet) for state in states: transitions = dict() for (index, symbol) in enumerate(oa): ns = list(state) ns[index] = True transitions[symbol] = tuple(ns) delta[state] = transitions soft = dilated(ExplicitDFA(alphabet, states, accepting, initial, delta)) #soft = DFA.fullDFA(alphabet) impro = generate(alphabet, 10, hard, soft, 1.0 / 20.0)[0] def printGrid(mpos, apos): for y in range(k-1, -1, -1): for x in range(k): pos = [x, y] if pos == mpos: print('R', end='') elif pos == apos: print('A', end='') else: print('.', end='') print() direction = { 'a': (-1,0), 'd': (1,0), 'w': (0,1), 's': (0,-1) } mpos = [0, 0] apos = [k-1, k-1] it = impro() step = None while True: try: step = it.send(step) assert len(step) == 1 step = step[0] mpos[0] = clamp(mpos[0] + step[0]) mpos[1] = clamp(mpos[1] + step[1]) printGrid(mpos, apos) step = direction[input()] apos[0] = clamp(apos[0] + step[0]) apos[1] = clamp(apos[1] + step[1]) #printGrid(mpos, apos) except StopIteration: s = (tuple(mpos), tuple(apos), 0) it = impro(si=s, sa=(s, soft.initialState)) step = None