File size: 2,411 Bytes
c5997d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from environment.file_system import FileSystem
from actions.file_actions import execute_action
from grader.grader import grade_state
from utils.helpers import load_initial_state
from utils.helpers import load_task_criteria


class CloudDataJanitorEnv:

    def __init__(self, task_name="easy"):
        self.task_name = task_name
        self.max_steps = 10

        self.current_step = 0
        self.done = False

        self.fs = None

    # REQUIRED
    def reset(self):

        self.current_step = 0
        self.done = False

        initial_state = load_initial_state()

        criteria = load_task_criteria(
            self.task_name
        )

        self.fs = FileSystem(
            initial_state,
            criteria
        )

        # REQUIRED folders for grading
        self.fs.create_folder("Documents")
        self.fs.create_folder("Images")
        self.fs.create_folder("Misc")

        return self.state()

    # REQUIRED
    def state(self):

        return {
            "files": self.fs.list_files(),
            "step": self.current_step,
            "task": self.task_name
        }

    # REQUIRED
    def step(self, action):

        if self.done:
            return self.state(), 0.0, True, {}

        self.current_step += 1

        error = None

        # Handle done action
        if action.get("action") == "done":

            self.done = True

            return (
                self.state(),
                1.0,
                True,
                {
                    "last_action_error": None
                }
            )

        try:

            action_type = action.get("action")

            params = {
                k: v
                for k, v in action.items()
                if k != "action"
            }

            execute_action(
                self.fs,
                action_type,
                params
            )

        except Exception as e:

            error = str(e)

        score = grade_state(
            self.fs,
            self.task_name
        )

        reward = float(score)

        if score >= 1.0:
            self.done = True

        if self.current_step >= self.max_steps:
            self.done = True

        return (
            self.state(),
            reward,
            self.done,
            {
                "last_action_error": error
            }
        )

    def close(self):

        pass