def test_dist_precompute(cluster4): states = hd.USet(2, "q") alphabet = hd.USet(2, "a") delta = hd.Mappings(states * alphabet, states) r1 = delta.cnfs().run() r2 = delta.cnfs().run(cluster4.ctx) assert len(r1) == len(r2)
def test_cnfs(): x = hd.Range(10) assert list(x.cnfs()) == range(10) ax = hd.USet(4, "a") bx = hd.USet(2, "b") result = (ax * (ax + bx)).cnfs().filter(lambda x: x[1].parent == bx).run() assert result == [(ax.get(0), bx.get(0))]
def test_cnfs_to_values(): ax = hd.USet(3, "a") a0, a1, a2 = ax c = hd.CnfValues((a0, )) v = c.to_values() assert isinstance(v, hd.Values) assert list(c) == list(v)
def test_cnfs_invalid_values(): ax = hd.USet(3, "a") a0, a1, a2 = ax with pytest.raises(Exception): hd.CnfValues((a0, a1)) with pytest.raises(Exception): hd.CnfValues([(a2, a2)])
def main(): n_states = 6 # Number of states n_symbols = 2 # Number of symbols in alphabet states = hd.USet(n_states, "q") # set of states q0, q1, ..., q_{n_states} alphabet = hd.USet(n_symbols, "a") # set of symbols a0, ..., a_{a_symbols} # Mappings (states * alphabet) -> states delta = hd.Mappings(states * alphabet, states) # Let us precompute some values that will be repeatedly used init_state = frozenset(states) max_steps = (n_states**3 - n_states) / 6 def check_automaton(delta): # This function takes automaton as a transition function and # returns the minimal length of synchronizing word or 0 if there # is no such word def step(state, depth): # A step in bread-first search; gives a set of states # and return a set reachable by one step for a in alphabet: yield frozenset(delta[(s, a)] for s in state) delta = delta.to_dict() return search.bfs( init_state, # Initial state step, # Step lambda state, depth: depth if len(state) == 1 else None, # Run until we reach a single state max_depth=max_steps, # Limit depth of search not_found_value=0) # Return 0 when we exceed depth limit # Create & run pipeline pipeline = delta.cnfs().map(check_automaton).max(size=1) result = pipeline.run() print("The maximal length of a minimal reset word for an " "automaton with {} states and {} symbols is {}.".format( n_states, n_symbols, result[0]))
def test_sequence_flags(): a = hd.USet(4, "a") f = a.filter(lambda x: True) m = a.map(lambda x: True) p1 = hd.Sequences(a, 2) p2 = hd.Sequences(f, 2) p3 = hd.Sequences(m, 2) assert not p1.filtered assert p2.filtered assert not p3.filtered assert p1.step_jumps assert p2.step_jumps assert p3.step_jumps assert p1.strict assert not p2.strict assert not p3.strict
def test_product_flags(): a = hd.USet(4, "a") f = a.filter(lambda x: True) m = a.map(lambda x: True) p1 = a * a p2 = f * a p3 = m * a assert not p1.filtered assert p2.filtered assert not p3.filtered assert p1.step_jumps assert p2.step_jumps assert p3.step_jumps assert p1.strict assert not p2.strict assert not p3.strict
def test_cnfs_strict(): ax = hd.USet(3, "a") a0, a1, a2 = ax assert hd.CnfValues((a0, 123, "a")).strict
def test_cnfs_to_cnf_values(): ax = hd.USet(3, "a") a0, a1, a2 = ax c = hd.CnfValues((a0,)) assert c == c.to_cnf_values()
def test_cnfs_values(): ax = hd.USet(3, "a") a0, a1, a2 = ax assert list(hd.CnfValues((a0,)).create_cn_iter()) == [a0] assert list(hd.CnfValues((a0,))) == [a0, a1, a2]