Skip to content

Commit 00fd8ad

Browse files
basic environment
1 parent b41b6b2 commit 00fd8ad

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__
2+
/.settings
3+
/.pydevproject
4+
/.project
5+
/.settings

Core/CMazeEnviroment.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from enum import Enum
2+
import numpy as np
3+
4+
class MazeActions(Enum):
5+
LEFT = (-1, 0)
6+
RIGHT = (1, 0)
7+
UP = (0, -1)
8+
DOWN = (0, 1)
9+
10+
class CMazeEnviroment:
11+
def __init__(self, maze, pos, FOV):
12+
self._maze = np.pad(np.array(maze), FOV, constant_values=(1,))
13+
self._pos = np.array(pos) + FOV
14+
self._fov = FOV
15+
16+
self._fog = np.zeros_like(self._maze)
17+
self._updateFog()
18+
19+
def _updateFog(self):
20+
y, x = self._pos
21+
self._fog[
22+
x - self._fov:x + self._fov + 1,
23+
y - self._fov:y + self._fov + 1
24+
] = 1
25+
return
26+
27+
def apply(self, action):
28+
self._pos += action.value
29+
self._updateFog()
30+
return
31+
32+
def vision(self):
33+
y, x = self._pos
34+
return self._maze[
35+
x - self._fov:x + self._fov + 1,
36+
y - self._fov:y + self._fov + 1
37+
]
38+
39+
@property
40+
def state(self):
41+
return ((self.vision(), self._fog, ), self.score, self.done)
42+
43+
@property
44+
def done(self):
45+
y, x = self._pos
46+
return 1 < self._maze[x, y]
47+
48+
@property
49+
def score(self):
50+
h, w = self._fog.shape
51+
total = h * w
52+
return np.count_nonzero(self._fog) / total

Core/__init__.py

Whitespace-only changes.

main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
def main():
5+
pass
6+
7+
if __name__ == '__main__':
8+
main()

tests/Test_CMazeEnviroment.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from verify import expect
2+
from Core.CMazeEnviroment import CMazeEnviroment, MazeActions
3+
import numpy as np
4+
5+
class Test_CMazeEnviroment:
6+
def test_vision(self):
7+
env = CMazeEnviroment(
8+
maze=[
9+
[0, 0, 1, 0],
10+
[0, 0, 0, 0],
11+
[0, 1, 0, 0],
12+
[0, 1, 0, 1],
13+
[0, 0, 2, 0],
14+
],
15+
pos=(0, 0),
16+
FOV=1
17+
)
18+
19+
valid = np.array([
20+
[1, 1, 1],
21+
[1, 0, 0],
22+
[1, 0, 0],
23+
])
24+
expect(str(env.vision())).is_equal(str(valid))
25+
26+
def test_apply(self):
27+
env = CMazeEnviroment(
28+
maze=[
29+
[0, 0, 1, 0],
30+
[0, 0, 0, 0],
31+
[0, 1, 0, 0],
32+
[0, 1, 0, 1],
33+
[0, 0, 2, 0],
34+
],
35+
pos=(0, 0),
36+
FOV=1
37+
)
38+
env.apply(MazeActions.RIGHT)
39+
env.apply(MazeActions.DOWN)
40+
env.apply(MazeActions.RIGHT)
41+
env.apply(MazeActions.DOWN)
42+
env.apply(MazeActions.RIGHT)
43+
44+
valid = np.array([
45+
[0, 0, 1],
46+
[0, 0, 1],
47+
[0, 1, 1],
48+
])
49+
expect(str(env.vision())).is_equal(str(valid))
50+
51+
def test_increasingScoreWhileExploring(self):
52+
env = CMazeEnviroment(
53+
maze=[
54+
[0, 0, 1, 0],
55+
[0, 0, 0, 0],
56+
[0, 1, 0, 0],
57+
[0, 1, 0, 1],
58+
[0, 0, 2, 0],
59+
],
60+
pos=(0, 0),
61+
FOV=1
62+
)
63+
64+
oldScore = env.score
65+
env.apply(MazeActions.RIGHT)
66+
newScore = env.score
67+
expect(oldScore).is_less(newScore)
68+
69+
def test_scoreNotChanged(self):
70+
env = CMazeEnviroment(
71+
maze=[
72+
[0, 0, 1, 0],
73+
[0, 0, 0, 0],
74+
[0, 1, 0, 0],
75+
[0, 1, 0, 1],
76+
[0, 0, 2, 0],
77+
],
78+
pos=(0, 0),
79+
FOV=1
80+
)
81+
82+
env.apply(MazeActions.RIGHT)
83+
oldScore = env.score
84+
env.apply(MazeActions.LEFT)
85+
newScore = env.score
86+
expect(oldScore).is_equal(newScore)
87+

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)