-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid_world.py
127 lines (106 loc) · 3.63 KB
/
grid_world.py
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
121
122
123
124
125
126
127
import numpy as np
import gym
from abc import ABC, abstractmethod
from pathlib import Path
from enum import IntEnum
from termcolor import colored
from el2805.envs.tabular_mdp import TabularMDP
class Move(IntEnum):
UP = 0
DOWN = 1
RIGHT = 2
LEFT = 3
NOP = 4
def __str__(self):
if self is Move.UP:
s = "\u2191"
elif self is Move.DOWN:
s = "\u2193"
elif self is Move.LEFT:
s = "\u2190"
elif self is Move.RIGHT:
s = "\u2192"
elif self is Move.NOP:
s = "X"
else:
raise ValueError
return s
Position = tuple[int, int]
class GridWorld(TabularMDP, ABC):
action_space = gym.spaces.Discrete(len(Move))
def __init__(self, map_filepath: Path, horizon: int | None = None):
super().__init__(horizon)
self._states = None
self._n_steps = None
self._current_state = None
self._initial_state = None
self.map = None
self._load_map(map_filepath)
assert isinstance(self.map, np.ndarray)
self.observation_space = gym.spaces.MultiDiscrete(self.map.shape)
@property
def states(self) -> list[Position]:
return self._states
@abstractmethod
def _load_map(self, filepath: Path) -> None:
raise NotImplementedError
def step(self, action: int) -> tuple[Position, float, bool, dict]:
# update state
previous_state = self._current_state
new_state = self._next_state(previous_state, action)
self._current_state = new_state
# calculate reward
reward = self.reward(previous_state, action)
# check end of episode
self._n_steps += 1
done = self._horizon_reached() or self.terminal_state(self._current_state)
# additional info
info = {}
return self._current_state, reward, done, info
def reset(self) -> Position:
self._current_state = self._initial_state
self._n_steps = 0
return self._current_state
def render(self, mode: str = "human", policy: np.ndarray = None) -> None:
assert mode == "human" or (mode == "policy" and policy is not None)
map_ = self.map.copy()
if mode == "human":
map_[self._current_state] = colored("P", color="blue")
elif mode == "policy":
for s, action in enumerate(policy):
state = self.states[s]
action = Move(action)
map_[state] = str(action)
else:
raise ValueError
self._render(map_)
def next_states(self, state: Position, action: int) -> tuple[list[Position], np.ndarray]:
next_state = self._next_state(state, action)
return ([next_state]), np.asarray([1]) # deterministic
def _next_state(self, state: Position, action: int) -> Position:
x, y = state
if action == Move.UP:
x -= 1
elif action == Move.DOWN:
x += 1
elif action == Move.LEFT:
y -= 1
elif action == Move.RIGHT:
y += 1
elif action == Move.NOP:
pass
else:
raise ValueError(f"Invalid move {action}")
state = (x, y)
return state
def _horizon_reached(self):
horizon_reached = self._n_steps >= self.horizon if self.finite_horizon() else False
return horizon_reached
@staticmethod
def _render(map_):
print("=" * 8 * map_.shape[0])
for i in range(map_.shape[0]):
for j in range(map_.shape[1]):
print(map_[i, j], end="\t")
print()
print("=" * 8 * map_.shape[0])