def example_2(): print() print('Example 2') print('---------') graph = utils.load_graph('../example_graphs/TestScene6_graph.json') script = read_script('example_scripts/example_script_2.txt') name_equivalence = utils.load_name_equivalence() object_placing = utils.load_object_placing() properties_data = utils.load_properties_data() executor = ScriptExecutor(graph, name_equivalence) # Execute script; fails due to a missing object state_enum = executor.find_solutions(script) state = next(state_enum, None) print('Script is {0}executable'.format('not ' if state is None else '')) # Add missing objects (random) prepare_1 = AddMissingScriptObjects(name_equivalence, properties_data, object_placing) # Add 10 random objects prepare_2 = AddRandomObjects(properties_data, object_placing, choices=10) # Change states of "can_open" and "has_switch" objects to # open/closed, on/off) prepare_3 = ChangeObjectStates(properties_data) state_enum = executor.find_solutions(script, [prepare_1, prepare_2, prepare_3]) state = next(state_enum, None) print('Script is {0}executable'.format('not ' if state is None else ''))
def example_1(): print('Example 1') print('---------') graph = utils.load_graph('../example_graphs/TestScene6_graph.json') name_equivalence = utils.load_name_equivalence() script = read_script('example_scripts/example_script_1.txt') executor = ScriptExecutor(graph, name_equivalence) state_enum = executor.find_solutions(script) state = next(state_enum, None) if state is None: print('Script is not executable.') else: print('Script is executable') fridge_nodes = state.get_nodes_by_attr('class_name', 'microwave') if len(fridge_nodes) > 0: print("Microwave states are:", fridge_nodes[0].states) chars = state.get_nodes_by_attr('class_name', 'character') if len(chars) > 0: char = chars[0] print("Character holds:") print_node_names(state.get_nodes_from(char, Relation.HOLDS_RH)) print_node_names(state.get_nodes_from(char, Relation.HOLDS_LH)) print("Character is on:") print_node_names(state.get_nodes_from(char, Relation.ON)) print("Character is in:") print_node_names(state.get_nodes_from(char, Relation.INSIDE)) print("Character states are:", char.states)
def example_4(): print() print('Example 4') print('---------') graph = utils.load_graph('../example_graphs/TestScene6_graph.json') script = read_script('example_scripts/example_script_3.txt') name_equivalence = utils.load_name_equivalence() executor = ScriptExecutor(graph, name_equivalence) state = executor.execute(script) if state is None: print('Script is not executable, since {}'.format( executor.info.get_error_string())) else: print('Script is executable')
def check_executability(input): script, graph_dict = input if len(script.split(', ')) == 1: final_state = graph_dict return True, True, final_state string = modify_script(script) able_to_be_parsed = False able_to_be_executed = False try: script = read_script_from_string(string) able_to_be_parsed = True except ScriptParseException: return able_to_be_parsed, able_to_be_executed, None graph = EnvironmentGraph(graph_dict) name_equivalence = utils.load_name_equivalence() executor = ScriptExecutor(graph, name_equivalence) try: executable, final_state, _ = executor.execute(script) except AttributeError: print("Attribute error") print("Program:") programs = string.split(', ') for p in programs: print(p) return able_to_be_parsed, able_to_be_executed, None except: print("Unexpected error:", sys.exc_info()[0]) print("Program:") programs = string.split(', ') for p in programs: print(p) return able_to_be_parsed, able_to_be_executed, None if executable: able_to_be_executed = True return able_to_be_parsed, able_to_be_executed, final_state.to_dict() else: return able_to_be_parsed, able_to_be_executed, None
def example_3(): print() print('Example 3') print('---------') graph = utils.load_graph('../example_graphs/TestScene6_graph.json') script = read_script('example_scripts/example_script_2.txt') name_equivalence = utils.load_name_equivalence() properties_data = utils.load_properties_data() executor = ScriptExecutor(graph, name_equivalence) # "Manual" changes: # * add object named kettle to a stove # * add a vacummcleaner on the floor in the livingroom and turn it on # * turn on all lights (lightswitches) prepare_1 = StatePrepare(properties_data, [ AddObject('kettle', Destination.on('stove')), AddObject('vacuumcleaner', Destination.on('floor', 'livingroom'), [State.ON]), ChangeState('lightswitch', [State.ON]) ]) state_enum = executor.find_solutions(script, [prepare_1]) state = next(state_enum, None) print('Script is {0}executable'.format('not ' if state is None else ''))
def __init__(self, n_chars=1, max_nodes=200): self.graph_helper = graph_dict_helper() self.n_chars = n_chars self.name_equivalence = load_name_equivalence() self.state = None self.observable_state_n = [None for i in range(self.n_chars)] self.character_n = [None for i in range(self.n_chars)] self.tasks_n = [None for i in range(self.n_chars)] self.prev_progress_n = [None for i in range(self.n_chars)] self.rooms = None self.rooms_ids = None self.observable_object_ids_n = [None for i in range(self.n_chars)] self.pomdp = False self.executor_n = [ ScriptExecutor(EnvironmentGraph(self.state), self.name_equivalence, i) for i in range(self.n_chars) ]
def reset(self, state, task_goals_n): ############ State ############ state = self._remove_house_obj(state) # Fill out the missing states self.fill_missing_states(state) for i in range(self.n_chars): self.executor = ScriptExecutor(EnvironmentGraph(state), self.name_equivalence, i) self.character_n = [None for i in range(self.n_chars)] chars = [ node for node in state["nodes"] if node["category"] == "Characters" ] chars.sort(key=lambda node: node['id']) self.character_n = chars self.rooms = [] for node in state["nodes"]: if node["category"] == "Rooms": self.rooms.append(node) self.rooms_ids = [n["id"] for n in self.rooms] self.state = state self.vh_state = self.get_vh_state(state) ############ Reward ############ observable_state_n = [ self._mask_state(state, i) if self.pomdp else state for i in range(self.n_chars) ] self.observable_state_n = observable_state_n self.observable_object_ids_n = [[ node['id'] for node in obs_state['nodes'] ] for obs_state in observable_state_n] return observable_state_n
def check_one_program(helper, script, precond, graph_dict, w_graph_list, modify_graph=True, place_other_objects=True, id_mapping={}, **info): helper.initialize(graph_dict) script, precond = modify_objects_unity2script(helper, script, precond) if modify_graph: ## add missing object from scripts (id from 1000) and set them to default setting ## id mapping can specify the objects that already specify in the graphs helper.set_to_default_state(graph_dict, None, id_checker=lambda v: True) id_mapping, first_room, room_mapping = helper.add_missing_object_from_script( script, precond, graph_dict, id_mapping) info = {'room_mapping': room_mapping} objects_id_in_script = [v for v in id_mapping.values()] helper.set_to_default_state( graph_dict, first_room, id_checker=lambda v: v in objects_id_in_script) ## place the random objects (id from 2000) if place_other_objects: max_node_to_place = max_nodes - len(graph_dict["nodes"]) n = random.randint(max_node_to_place - 20, max_node_to_place) helper.add_random_objs_graph_dict(graph_dict, n=max(n, 0)) helper.set_to_default_state(graph_dict, None, id_checker=lambda v: v >= 2000) helper.random_change_object_state( id_mapping, graph_dict, id_checker=lambda v: v not in objects_id_in_script) ## set relation and state from precondition helper.check_binary(graph_dict, id_checker=lambda v: True, verbose=False) random_objects_id = helper.random_objects_id helper.prepare_from_precondition(precond, id_mapping, graph_dict) helper.open_all_doors(graph_dict) helper.ensure_light_on( graph_dict, id_checker=lambda v: v not in objects_id_in_script) helper.check_binary(graph_dict, id_checker=lambda v: v >= random_objects_id, verbose=False) helper.check_binary(graph_dict, id_checker=lambda v: True, verbose=True) assert len(graph_dict["nodes"]) <= max_nodes elif len(id_mapping) != 0: # Assume that object mapping specify all the objects in the scripts helper.modify_script_with_specified_id(script, id_mapping, **info) graph = EnvironmentGraph(graph_dict) name_equivalence = utils.load_name_equivalence() executor = ScriptExecutor(graph, name_equivalence) executable, final_state, graph_state_list = executor.execute( script, w_graph_list=w_graph_list) if executable: message = 'Script is executable' else: message = 'Script is not executable, since {}'.format( executor.info.get_error_string()) return message, executable, final_state, graph_state_list, id_mapping, info, script
def example_5(): properties_data = utils.load_properties_data() object_states = utils.load_object_states() object_placing = utils.load_object_placing() graph_dict = { 'nodes': [ { "id": 1, 'class_name': "kitchen", "category": "Rooms", "properties": [], "states": [] }, { "id": 2, 'class_name': "bedroom", "category": "Rooms", "properties": [], "states": [] }, { "id": 3, 'class_name': "home_office", "category": "Rooms", "properties": [], "states": [] }, { "id": 4, 'class_name': "bathroom", "category": "Rooms", "properties": [], "states": [] }, { "id": 5, 'class_name': "character", "category": "", "properties": [], "states": [] }, { "id": 6, 'class_name': "door", "category": "", "properties": ["CAN_OPEN"], "states": ["OPEN"] }, { "id": 7, 'class_name': "door", "category": "", "properties": ["CAN_OPEN"], "states": ["OPEN"] }, { "id": 8, 'class_name': "door", "category": "", "properties": ["CAN_OPEN"], "states": ["OPEN"] }, ], 'edges': [ { "from_id": 5, "to_id": 2, "relation_type": "INSIDE" }, { "from_id": 6, "to_id": 1, "relation_type": "BETWEEN" }, { "from_id": 6, "to_id": 2, "relation_type": "BETWEEN" }, { "from_id": 7, "to_id": 1, "relation_type": "BETWEEN" }, { "from_id": 7, "to_id": 3, "relation_type": "BETWEEN" }, { "from_id": 8, "to_id": 3, "relation_type": "BETWEEN" }, { "from_id": 8, "to_id": 4, "relation_type": "BETWEEN" }, ] } helper = utils.graph_dict_helper(properties_data, object_placing, object_states, max_nodes=15) helper.initialize(graph_dict) print() print('Example 5') print('---------') script = read_script('example_scripts/example_script_4.txt') with open('example_scripts/example_precond_script_4.json', 'r') as f: precond = json.load(f) id_mapping = {} id_mapping, first_room, room_mapping = helper.add_missing_object_from_script( script, precond, graph_dict, id_mapping) print(id_mapping) # add random objects graph = EnvironmentGraph(graph_dict) name_equivalence = utils.load_name_equivalence() executor = ScriptExecutor(graph, name_equivalence) state = executor.execute(script) if state is None: print('Script is not executable, since {}'.format( executor.info.get_error_string())) else: print('Script is executable')