def test_connection(): T0 = np.array([[0, 1], [1, 0]]) T1 = np.array([[1, 0], [0, 1]]) mdp1 = MDP([T0, T1], input_fcn=lambda m: m, output_fcn=lambda n: n, input_name='u', output_name='z') T0 = np.array([[1, 0], [1, 0]]) T1 = np.array([[0, 1], [0, 1]]) mdp2 = MDP([T0, T1], input_fcn=lambda m: m, output_fcn=lambda n: n, input_name='z', output_name='y') pmdp = mdp1.product(mdp2, lambda n1: set([n1])) np.testing.assert_almost_equal( pmdp.T(0).todense(), np.array([[0, 0, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0], [1, 0, 0, 0]])) np.testing.assert_almost_equal( pmdp.T(1).todense(), np.array([[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1]])) vals1, _ = pmdp.solve_reach(accept=lambda s: s[0] == 0 and s[1] == 1) np.testing.assert_almost_equal(vals1[0], [0, 1, 0, 0]) vals2, _ = pmdp.solve_reach(accept=lambda s: s[0] == 0 and s[1] == 0) np.testing.assert_almost_equal(vals2[0], [1, 1, 1, 1])
def test_ltl_synth(): T1 = np.array([[0.25, 0.25, 0.25, 0.25], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) T2 = np.array([[0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0], [0.9, 0, 0, 0.1]]) def connection(n): # map Y1 -> 2^(2^AP) if n == 1: return set((('s1', ), )) # { {s1} } elif n == 3: return set((('s2', ), )) # { {s2} } else: return set(((), ), ) # { { } } system = MDP([T1, T2]) formula = '( ( F s1 ) & ( F s2 ) )' dfsa, init, final, _ = formula_to_mdp(formula) prod = system.product(dfsa, connection) V, _ = prod.solve_reach(accept=lambda s: s[1] in final) np.testing.assert_almost_equal(V[0][:, 0], [0.5, 0, 0, 0.5], decimal=4)
def test_connection(): T0 = np.array([[0.5, 0.5], [0, 1.]]) T1 = np.array([[0.2, 0.8], [1, 0]]) T2 = np.array([[0.2, 0.8], [1, 0]]) mdp1 = MDP([T0, T1, T2]) mdp2 = MDP([T0, T1, T2]) mdp3 = MDP([T0, T1, T2]) prod = mdp1.product(mdp2, connection=lambda n: set([1])) prod = prod.product(mdp3, connection=lambda n: set([2])) pT = np.kron(np.kron(T0, T1), T2) np.testing.assert_almost_equal(prod.T(0).todense(), pT)
def test_mdp_dfsa(): def output(n1): if n1 == 2: return 1 else: return 0 T0 = np.array([[0.5, 0.25, 0.25], [0, 1, 0], [0, 0, 1]]) mdp = MDP([T0], output_fcn=output) T1 = np.array([[1, 0], [0, 1]]) T2 = np.array([[0, 1], [0, 1]]) fsa = MDP([T1, T2]) connection = lambda n1: set([n1]) prod = mdp.product(fsa, connection) V, _ = prod.solve_reach(accept=lambda y: y[1] == 1) np.testing.assert_almost_equal(V[0], [[0.5, 1], [0, 1], [1, 1]], decimal=4)
def test_mdp_dfsa_nondet(): def connection(n1): if n1 == 2: return set([1]) elif n1 == 1: return set([1, 0]) else: return set([0]) T0 = np.array([[0.5, 0.25, 0.25], [0, 1, 0], [0, 0, 1]]) mdp = MDP([T0]) T1 = np.array([[1, 0], [0, 1]]) T2 = np.array([[0, 1], [0, 1]]) fsa = MDP([T1, T2]) prod = mdp.product(fsa, connection) V, _ = prod.solve_reach(accept=lambda y: y[1] == 1) np.testing.assert_almost_equal(V[0], [[0.5, 1], [0, 1], [1, 1]], decimal=4)