File size: 3,250 Bytes
7cec401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'''

This script processes `office_changes.json` to generate a dataset of office manipulation tasks.



The `CHANGES_DESCRIPTION_FILE` file contains a list of tasks, each with:

- "name": A natural language description.

- "init_conditions": An array of conditions to define the starting world state. null if no changes

- "goal_conditions": An array of conditions that describe the target world state.

- "actions": A sequence of actions to transition from the initial to the goal state.

'''
import json
import pickle
from office_graph import office_graph, add_edges
import copy

CHANGES_DESCRIPTION_FILE = 'office_changes.json'
OUT_FILENAME = 'office29.pkl'

def change_node(node, condition, graph):
    if condition.get("holding", False):
        graph.nodes[node]['holding'] = (condition["holding"]['name'], condition["holding"]['id'])

    if condition["relation"]:
        graph.nodes[node]['related_to'] = (condition["related_to"]["name"], condition["related_to"]["id"])
        graph.nodes[node]['relation'] = condition["relation"]

    if condition["states"]:
        states = condition.get("states", [])
        for state in states:
            if state not in graph.nodes[node]['states']:
                graph.nodes[node]['states'].append(state)
                if state == 'off':
                    graph.nodes[node]['states'].remove('on')
                if state == 'on':
                    graph.nodes[node]['states'].remove('off')
                if state == 'closed':
                    graph.nodes[node]['states'].remove('open')
                if state == 'open':
                    graph.nodes[node]['states'].remove('closed')

if __name__ == "__main__":
    # Load the JSON file
    with open(CHANGES_DESCRIPTION_FILE, "r") as file:
        tasks = json.load(file)

    dataset = []
    i=0
    for task in tasks:
        i+=1
        name = task["name"]
        init_graph = copy.deepcopy(office_graph)
        goal_graph = copy.deepcopy(office_graph)
        # Process initial conditions
        if task['init_conditions']:
            for condition in task.get("init_conditions", []):
                node = (condition["node"]["name"], condition["node"]["id"])
                change_node(node, condition, init_graph)
        # Process goal conditions
        for condition in task.get("goal_conditions", []):
            node = (condition["node"]["name"], condition["node"]["id"])
            change_node(node, condition, goal_graph)
        # Process actions
        actions = []
        for action in task.get("actions", []):
            actions.append((action['action'], (action['node']["name"],action['node']["id"])))
        # Reset graph edges
        init_graph.remove_edges_from(list(office_graph.edges))
        goal_graph.remove_edges_from(list(office_graph.edges))
        add_edges(init_graph)
        add_edges(goal_graph)

        entity = {
            "id": i,
            "name": name,
            "task": name,
            "init": init_graph,
            "goal": goal_graph,
            "actions": actions,
        }
        dataset.append(entity)

    with open(OUT_FILENAME, "wb") as file:
        pickle.dump(dataset, file)