def test_update_partial_solution(self): pi = ['E'] new_bpsg = mdp_graph.update_partial_solution( pi, V_i, '1', S, bpsg, mdp_graph.init_graph(graph)) self.assertDictEqual( new_bpsg, { "1": { "Adj": [{ "name": "1", "A": { "E": 0.5 } }, { "name": "2", "A": { "E": 0.5 } }] }, "2": { "Adj": [] }, })
def test_unexpanded_states_2(self): mdp_g = mdp_graph.init_graph(graph) # Expand states '1' and '2': explicit, mdp_g = mdp_graph.expand_state('1', mdp_g, bpsg) bpsg_, mdp_g = mdp_graph.expand_state('2', mdp_g, explicit) unexpanded = mdp_graph.get_unexpanded_states(mdp_g, bpsg_2) self.assertListEqual(unexpanded, [])
def test_expand_state_goal(self): state = '3' mdp_g = mdp_graph.init_graph(graph) with pytest.raises( ValueError, match="State %d can't be expanded because it is a goal state" % int(state)): _, mdp_g = mdp_graph.expand_state(state, mdp_g, {})
def test_init_graph(self): mdp_g = mdp_graph.init_graph(graph) success = True for k in mdp_g: success &= not mdp_g[k]['expanded'] state = deepcopy(mdp_g[k]) state.pop('expanded') success &= state == graph[k] assert success, "Initializes the graph correctly"
def test_expand_state(self): init_state = '1' explicit_graph = mdp_graph.add_state_graph(init_state, {}) mdp_g = mdp_graph.init_graph(graph) init_state_neighbours = map(lambda _s: _s["name"], mdp_g[init_state]['Adj']) new_explicit_graph, mdp_g = mdp_graph.expand_state( init_state, mdp_g, explicit_graph) assert mdp_g[init_state]['expanded'] for s in init_state_neighbours: assert s in new_explicit_graph
def test_lao_with_convergence_test(self): V = np.array([2.0, 1, 0, 3, 2, 1]) pi = np.array([None] * len(S)) V_, pi_ = lao.lao('1', V, V_i, pi, S, A, mg.init_graph(graph)) self.assertListEqual(pi_.tolist(), ['E', 'E', None, 'E', None, None])
import numpy as np import mdp_graph as mg from lao import lao, ilao from utils import read_json, output, parse_args def try_int(key): try: return int(key) except: return key args = parse_args() mdp = mg.init_graph(read_json(args.file_input)) A = mg.get_actions(mdp) S = sorted(mdp.keys(), key=try_int) pi = np.array([None] * len(S)) V_i = {S[i]: i for i in range(len(S))} heuristic = np.fromiter((map(lambda s: mdp[s]['heuristic'], S)), float) V = None if args.algorithm == 'lao': V, pi = lao(args.initial_state, heuristic, V_i, pi, S,
def test_unexpanded_states_1(self): mdp_g = mdp_graph.init_graph(graph) unexpanded = mdp_graph.get_unexpanded_states(mdp_g, bpsg) self.assertListEqual(unexpanded, ['1'])
"name": "2", "A": { "E": 0.5, } }] }, "3": { "Adj": [] } } A = ['N', 'S', 'E'] S = list(graph.keys()) V_i = {S[i]: i for i in range(len(S))} graph_env_1 = mdp_graph.init_graph(read_json('./env1.json')) class TestMDPGraph(unittest.TestCase): def test_init_graph(self): mdp_g = mdp_graph.init_graph(graph) success = True for k in mdp_g: success &= not mdp_g[k]['expanded'] state = deepcopy(mdp_g[k]) state.pop('expanded') success &= state == graph[k] assert success, "Initializes the graph correctly" def test_get_actions(self):