def test_non_fluent_param_init(self): objs = {'x1': 1, 'x2': 2, 'x3': 3, 'x4': 4} rddl = f''' domain my_test {{ types {{ obj : object; }}; pvariables {{ C(obj) : {{ non-fluent, int, default = -1}}; p : {{ state-fluent, int, default = 1 }}; a : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p' = p + 1; }}; reward = 0; }} non-fluents my_test_nf {{ domain = my_test; non-fluents {{ {'; '.join(f'C({o})={v}' for o, v in objs.items())}; }}; objects {{ obj : {{{', '.join(objs.keys())}}}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ a; }}; }} ''' conv = Converter() conv.convert_str(rddl) for o, v in objs.items(): self.assertIn(Converter.get_feature_name(('C', o)), conv.constants) self.assertEqual( conv.constants[Converter.get_feature_name(('C', o))], v)
def test_non_fluent(self): agents = {'John': 1.22, 'Paul': 3.75, 'George': -1.14, 'Ringo': 4.73} rddl = f''' domain my_test {{ types {{ agent : object; }}; pvariables {{ C(agent) : {{ non-fluent, real, default = 1}}; a : {{ action-fluent, bool, default = false }}; }}; cpfs {{ }}; reward = 0; }} non-fluents my_test_empty {{ domain = my_test; non-fluents {{ {'; '.join(f'C({a}) = {v}' for a, v in agents.items())}; }}; objects {{ agent : {{ {", ".join(agents.keys())} }}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ a; }}; }} ''' conv = Converter() conv.convert_str(rddl) for a, v in agents.items(): self.assertIn(Converter.get_feature_name(('C', a)), conv.constants) self.assertEqual( conv.constants[Converter.get_feature_name(('C', a))], v)
def test_fluent_param_def(self): objs = {'x1': True, 'x2': False, 'x3': False, 'x4': True} rddl = f''' domain my_test {{ types {{ obj : object; }}; pvariables {{ p(obj) : {{ state-fluent, bool, default = true }}; a : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p'(?o) = false; }}; reward = 0; }} non-fluents my_test_empty {{ domain = my_test; objects {{ obj : {{{', '.join(objs.keys())}}}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ a; }}; }} ''' conv = Converter() conv.convert_str(rddl) for o, v in objs.items(): p = conv.world.getState(WORLD, Converter.get_feature_name(('p', o)), unique=True) self.assertEqual(p, True) conv.world.step() for o, v in objs.items(): p = conv.world.getState(WORLD, Converter.get_feature_name(('p', o)), unique=True) self.assertEqual(p, False)
def test_fluent_multi_param_dyn_const(self): objs = { ('x1', 'x1'): True, ('x1', 'x2'): False, ('x2', 'x1'): False, ('x2', 'x2'): True } consts = { ('x1', 'x1'): 1, ('x1', 'x2'): 2, ('x2', 'x1'): 3, ('x2', 'x2'): 4 } rddl = f''' domain my_test {{ types {{ obj : object; }}; pvariables {{ C(obj, obj) : {{ non-fluent, int, default = -1}}; p(obj, obj) : {{ state-fluent, bool, default = true }}; a : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p'(?o1, ?o2) = (C(?o1, ?o2) >= 1) ^ p(?o1, ?o2); }}; reward = 0; }} non-fluents my_test_empty {{ domain = my_test; non-fluents {{ {'; '.join(f'C({",".join(o)}) = {v}' for o, v in consts.items())}; }}; objects {{ obj : {{{', '.join({x for x, _ in objs.keys()})}}}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ {'; '.join(f'p({",".join(o)}) = {str(v).lower()}' for o, v in objs.items())}; }}; }} ''' conv = Converter() conv.convert_str(rddl) for o, v in objs.items(): p = conv.world.getState(WORLD, Converter.get_feature_name(('p', *o)), unique=True) self.assertEqual(p, v) conv.world.step() for o, v in objs.items(): p = conv.world.getState(WORLD, Converter.get_feature_name(('p', *o)), unique=True) self.assertEqual(p, v and (consts[o] >= 1))
def test_actions_param_legal(self): agents = {'John': 1.22, 'Paul': 3.75, 'George': -1.14, 'Ringo': 4.73} objs = {'x1': True, 'x2': False, 'x3': False, 'x4': True} rddl = f''' domain my_test {{ types {{ agent : object; obj: object; }}; pvariables {{ p(obj, agent) : {{ state-fluent, real, default = 0 }}; q(obj) : {{ state-fluent, bool, default = True }}; a(obj, agent) : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p'(?o, ?a) = p(?o, ?a); }}; reward = 0; state-action-constraints {{ forall_{{?o: obj, ?a: agent}}[ a(?o, ?a) => q(?o) ]; }}; }} non-fluents my_test_nf {{ domain = my_test; objects {{ agent : {{{', '.join(agents.keys())}}}; obj : {{{', '.join(objs.keys())}}}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ {'; '.join(f'p({o},{a})={v if l else -1}' for a, v in agents.items() for o, l in objs.items())}; {'; '.join(f'q({o})={str(v).lower()}' for o, v in objs.items())}; }}; horizon = 0; }} ''' conv = Converter(const_as_assert=True) conv.convert_str(rddl) conv.world.step() for ag_name, val in agents.items(): legal_acts = conv.world.agents[ag_name].getLegalActions() for obj_name, legal in objs.items(): p = conv.world.getState(ag_name, Converter.get_feature_name( ('p', obj_name)), unique=True) self.assertEqual(p, val if legal else -1) a = conv.actions[ag_name][Converter.get_feature_name( ('a', obj_name, ag_name))] if legal: self.assertIn(a, legal_acts) else: self.assertNotIn(a, legal_acts)
def test_actions_param_conditions(self): agents = {'John': 1.22, 'Paul': 3.75, 'George': -1.14, 'Ringo': 4.73} rddl = f''' domain my_test {{ types {{ agent : object; }}; pvariables {{ p(agent) : {{ state-fluent, real, default = 0 }}; a1(agent) : {{ action-fluent, bool, default = false }}; a2(agent) : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p'(?a) = if ( a1(?a) ) then p(?a) + 1 else if ( a2(?a) ) then p(?a) - 1 else 0; }}; reward = - sum_{{?a : agent}} p(?a); }} non-fluents my_test_empty {{ domain = my_test; objects {{ agent : {{ {", ".join(agents.keys())} }}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ {'; '.join(f'p({a}) = {v}' for a, v in agents.items())}; }}; horizon = 0; }} ''' conv = Converter(const_as_assert=True) conv.convert_str(rddl) conv.world.step() for ag_name in conv.world.agents.keys(): a1 = conv.actions[ag_name][Converter.get_feature_name( ('a1', ag_name))] a2 = conv.actions[ag_name][Converter.get_feature_name( ('a2', ag_name))] p = stateKey(ag_name, 'p') self.assertIn(a1, conv.world.dynamics) self.assertIn(a2, conv.world.dynamics) self.assertIn(True, conv.world.dynamics) self.assertIn(p, conv.world.dynamics[a1]) self.assertIn(p, conv.world.dynamics[a2]) self.assertIn(p, conv.world.dynamics[True]) self.assertIn(a1, conv.world.dynamics[p]) self.assertIn(a2, conv.world.dynamics[p]) self.assertIn(True, conv.world.dynamics[p])
def test_fluent_multi_param_init2(self): objs = { ('x1', 'x1'): True, ('x1', 'x2'): False, ('x2', 'x1'): False, ('x2', 'x2'): True } rddl = f''' domain my_test {{ types {{ x_obj : object; y_obj : object; }}; pvariables {{ p(x_obj, y_obj) : {{ state-fluent, bool, default = true }}; a : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p'(?x, ?y) = false; }}; reward = 0; }} non-fluents my_test_empty {{ domain = my_test; objects {{ x_obj : {{{', '.join({x for x, _ in objs.keys()})}}}; y_obj : {{{', '.join({y for _, y in objs.keys()})}}}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ {'; '.join(f'p({",".join(o)}) = {str(v).lower()}' for o, v in objs.items())}; }}; }} ''' conv = Converter() conv.convert_str(rddl) for o, v in objs.items(): p = conv.world.getState(WORLD, Converter.get_feature_name(('p', *o)), unique=True) self.assertEqual(p, v) conv.world.step() for o, v in objs.items(): p = conv.world.getState(WORLD, Converter.get_feature_name(('p', *o)), unique=True) self.assertEqual(p, False)
def test_actions_param_legal_logic(self): objs = {'x1': True, 'x2': False, 'x3': False, 'x4': True} rddl = f''' domain my_test {{ types {{ obj : object; }}; pvariables {{ p(obj) : {{ state-fluent, bool, default = false }}; a(obj) : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p(?o)' = p(?o); }}; reward = 0; state-action-constraints {{ forall_{{?o : obj}}[ a(?o) => exists_{{?o2 : obj}} [ p(?o) ^ p(?o2) ] ]; }}; }} non-fluents my_test_nf {{ domain = my_test; objects {{ obj : {{{', '.join(objs.keys())}}}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ {'; '.join(f'p({o})={str(v).lower()}' for o, v in objs.items())}; }}; }} ''' conv = Converter(const_as_assert=True) conv.convert_str(rddl) conv.world.step() ag_name = next(iter(conv.world.agents.keys())) legal_acts = conv.world.agents[ag_name].getLegalActions() for name, val in objs.items(): p = conv.world.getState(WORLD, Converter.get_feature_name(('p', name)), unique=True) self.assertEqual(p, val) a = conv.actions[ag_name][Converter.get_feature_name(('a', name))] if val: self.assertIn(a, legal_acts) else: self.assertNotIn(a, legal_acts)
def test_actions_conditions_multi(self): rddl = ''' domain my_test { types { agent : object; }; pvariables { p : { state-fluent, int, default = 0 }; a1(agent) : { action-fluent, bool, default = false }; a2(agent) : { action-fluent, bool, default = false }; }; cpfs { p' = if (exists_{?a : agent} [a1(?a)] ) then p + 1 else if ( exists_{?a : agent} [a2(?a)] ) then p - 1 else 0; }; reward = -p; } non-fluents my_test_empty { domain = my_test; objects { agent: { Paul, John, George, Ringo }; }; } instance my_test_inst { domain = my_test; init-state { p = 0; }; horizon = 2; } ''' conv = Converter(const_as_assert=True) conv.convert_str(rddl) conv.world.step() for ag_name in conv.world.agents.keys(): a1 = conv.actions[ag_name][Converter.get_feature_name( ('a1', ag_name))] a2 = conv.actions[ag_name][Converter.get_feature_name( ('a2', ag_name))] p = stateKey(WORLD, 'p') self.assertIn(a1, conv.world.dynamics) self.assertIn(a2, conv.world.dynamics) self.assertIn(True, conv.world.dynamics) self.assertIn(p, conv.world.dynamics[a1]) self.assertIn(p, conv.world.dynamics[a2]) self.assertIn(p, conv.world.dynamics[True]) self.assertIn(a1, conv.world.dynamics[p]) self.assertIn(a2, conv.world.dynamics[p]) self.assertIn(True, conv.world.dynamics[p])
def test_non_fluent_multi_param_init2(self): objs = { ('x1', 'y1'): 1, ('x1', 'y2'): 2, ('x2', 'y1'): 3, ('x2', 'y2'): 4 } rddl = f''' domain my_test {{ types {{ x_obj : object; y_obj : object; }}; pvariables {{ C(x_obj, y_obj) : {{ non-fluent, int, default = -1}}; p : {{ state-fluent, int, default = 1 }}; a : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p' = p + 1; }}; reward = 0; }} non-fluents my_test_nf {{ domain = my_test; non-fluents {{ {'; '.join(f'C({",".join(o)}) = {v}' for o, v in objs.items())}; }}; objects {{ x_obj : {{{', '.join({x for x, _ in objs.keys()})}}}; y_obj : {{{', '.join({y for _, y in objs.keys()})}}}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ a; }}; }} ''' conv = Converter() conv.convert_str(rddl) for o, v in objs.items(): self.assertIn(Converter.get_feature_name(('C', *o)), conv.constants) self.assertEqual( conv.constants[Converter.get_feature_name(('C', *o))], v)
def test_fluent_multi_param2(self): agents = {'John': 1.22, 'Paul': 3.75, 'George': -1.14, 'Ringo': 4.73} rddl = f''' domain my_test {{ types {{ agent : object; obj: object; }}; pvariables {{ p(agent, obj) : {{ state-fluent, real, default = 0 }}; a : {{ action-fluent, bool, default = false }}; }}; cpfs {{ p'(?a, ?o) = p(?a, ?o) + 1; }}; reward = 0; }} non-fluents my_test_empty {{ domain = my_test; objects {{ agent : {{ {", ".join(agents.keys())} }}; obj: {{ o1 }}; }}; }} instance my_test_inst {{ domain = my_test; init-state {{ {'; '.join(f'p({a}, o1) = {v}' for a, v in agents.items())}; }}; }} ''' conv = Converter() conv.convert_str(rddl) for a, v in agents.items(): p = conv.world.getState(a, Converter.get_feature_name(('p', 'o1')), unique=True) self.assertEqual(p, v) conv.world.step() for a, v in agents.items(): p = conv.world.getState(a, Converter.get_feature_name(('p', 'o1')), unique=True) self.assertEqual(p, v + 1)
def print_state(): objs = dict(conv.model.non_fluents.objects) x_objs = objs['x_pos'] y_objs = objs['y_pos'] state = np.zeros((len(x_objs), len(y_objs)), dtype=int) for x in range(len(x_objs)): x_obj = x_objs[x] for y in range(len(y_objs)): y_obj = y_objs[y] f_name = Converter.get_feature_name(('alive', x_obj, y_obj)) state[x, y] = int(conv.world.getState(WORLD, f_name, unique=True)) agent = next(iter(conv.world.agents.keys())) logging.info( f'Action: {conv.world.getFeature(actionKey(agent), unique=True)}') logging.info( f'Reward: {conv.world.getFeature(rewardKey(agent), unique=True)}') logging.info(f'\n{state}')