Spaces:
Sleeping
Sleeping
Create env.py
Browse files
env.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
|
| 3 |
+
class DeliveryEnv:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
self.grid_size = 5
|
| 6 |
+
self.reset()
|
| 7 |
+
|
| 8 |
+
def reset(self):
|
| 9 |
+
self.agent_pos = [0, 0]
|
| 10 |
+
self.pickup = [2, 2]
|
| 11 |
+
self.drop = [4, 4]
|
| 12 |
+
self.has_item = False
|
| 13 |
+
self.steps = 0
|
| 14 |
+
return self.state()
|
| 15 |
+
|
| 16 |
+
def state(self):
|
| 17 |
+
return {
|
| 18 |
+
"agent_pos": self.agent_pos,
|
| 19 |
+
"has_item": self.has_item,
|
| 20 |
+
"pickup": self.pickup,
|
| 21 |
+
"drop": self.drop
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def step(self, action):
|
| 25 |
+
reward = -1 # step penalty
|
| 26 |
+
done = False
|
| 27 |
+
|
| 28 |
+
# movement
|
| 29 |
+
if action == "up":
|
| 30 |
+
self.agent_pos[0] = max(0, self.agent_pos[0] - 1)
|
| 31 |
+
elif action == "down":
|
| 32 |
+
self.agent_pos[0] = min(self.grid_size - 1, self.agent_pos[0] + 1)
|
| 33 |
+
elif action == "left":
|
| 34 |
+
self.agent_pos[1] = max(0, self.agent_pos[1] - 1)
|
| 35 |
+
elif action == "right":
|
| 36 |
+
self.agent_pos[1] = min(self.grid_size - 1, self.agent_pos[1] + 1)
|
| 37 |
+
|
| 38 |
+
# pickup
|
| 39 |
+
elif action == "pickup":
|
| 40 |
+
if self.agent_pos == self.pickup and not self.has_item:
|
| 41 |
+
self.has_item = True
|
| 42 |
+
reward += 10
|
| 43 |
+
|
| 44 |
+
# drop
|
| 45 |
+
elif action == "drop":
|
| 46 |
+
if self.agent_pos == self.drop and self.has_item:
|
| 47 |
+
reward += 20
|
| 48 |
+
done = True
|
| 49 |
+
|
| 50 |
+
self.steps += 1
|
| 51 |
+
|
| 52 |
+
if self.steps > 50:
|
| 53 |
+
done = True
|
| 54 |
+
|
| 55 |
+
return self.state(), reward, done, {}
|