-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b41b6b2
commit 00fd8ad
Showing
6 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
__pycache__ | ||
/.settings | ||
/.pydevproject | ||
/.project | ||
/.settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from enum import Enum | ||
import numpy as np | ||
|
||
class MazeActions(Enum): | ||
LEFT = (-1, 0) | ||
RIGHT = (1, 0) | ||
UP = (0, -1) | ||
DOWN = (0, 1) | ||
|
||
class CMazeEnviroment: | ||
def __init__(self, maze, pos, FOV): | ||
self._maze = np.pad(np.array(maze), FOV, constant_values=(1,)) | ||
self._pos = np.array(pos) + FOV | ||
self._fov = FOV | ||
|
||
self._fog = np.zeros_like(self._maze) | ||
self._updateFog() | ||
|
||
def _updateFog(self): | ||
y, x = self._pos | ||
self._fog[ | ||
x - self._fov:x + self._fov + 1, | ||
y - self._fov:y + self._fov + 1 | ||
] = 1 | ||
return | ||
|
||
def apply(self, action): | ||
self._pos += action.value | ||
self._updateFog() | ||
return | ||
|
||
def vision(self): | ||
y, x = self._pos | ||
return self._maze[ | ||
x - self._fov:x + self._fov + 1, | ||
y - self._fov:y + self._fov + 1 | ||
] | ||
|
||
@property | ||
def state(self): | ||
return ((self.vision(), self._fog, ), self.score, self.done) | ||
|
||
@property | ||
def done(self): | ||
y, x = self._pos | ||
return 1 < self._maze[x, y] | ||
|
||
@property | ||
def score(self): | ||
h, w = self._fog.shape | ||
total = h * w | ||
return np.count_nonzero(self._fog) / total |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
def main(): | ||
pass | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from verify import expect | ||
from Core.CMazeEnviroment import CMazeEnviroment, MazeActions | ||
import numpy as np | ||
|
||
class Test_CMazeEnviroment: | ||
def test_vision(self): | ||
env = CMazeEnviroment( | ||
maze=[ | ||
[0, 0, 1, 0], | ||
[0, 0, 0, 0], | ||
[0, 1, 0, 0], | ||
[0, 1, 0, 1], | ||
[0, 0, 2, 0], | ||
], | ||
pos=(0, 0), | ||
FOV=1 | ||
) | ||
|
||
valid = np.array([ | ||
[1, 1, 1], | ||
[1, 0, 0], | ||
[1, 0, 0], | ||
]) | ||
expect(str(env.vision())).is_equal(str(valid)) | ||
|
||
def test_apply(self): | ||
env = CMazeEnviroment( | ||
maze=[ | ||
[0, 0, 1, 0], | ||
[0, 0, 0, 0], | ||
[0, 1, 0, 0], | ||
[0, 1, 0, 1], | ||
[0, 0, 2, 0], | ||
], | ||
pos=(0, 0), | ||
FOV=1 | ||
) | ||
env.apply(MazeActions.RIGHT) | ||
env.apply(MazeActions.DOWN) | ||
env.apply(MazeActions.RIGHT) | ||
env.apply(MazeActions.DOWN) | ||
env.apply(MazeActions.RIGHT) | ||
|
||
valid = np.array([ | ||
[0, 0, 1], | ||
[0, 0, 1], | ||
[0, 1, 1], | ||
]) | ||
expect(str(env.vision())).is_equal(str(valid)) | ||
|
||
def test_increasingScoreWhileExploring(self): | ||
env = CMazeEnviroment( | ||
maze=[ | ||
[0, 0, 1, 0], | ||
[0, 0, 0, 0], | ||
[0, 1, 0, 0], | ||
[0, 1, 0, 1], | ||
[0, 0, 2, 0], | ||
], | ||
pos=(0, 0), | ||
FOV=1 | ||
) | ||
|
||
oldScore = env.score | ||
env.apply(MazeActions.RIGHT) | ||
newScore = env.score | ||
expect(oldScore).is_less(newScore) | ||
|
||
def test_scoreNotChanged(self): | ||
env = CMazeEnviroment( | ||
maze=[ | ||
[0, 0, 1, 0], | ||
[0, 0, 0, 0], | ||
[0, 1, 0, 0], | ||
[0, 1, 0, 1], | ||
[0, 0, 2, 0], | ||
], | ||
pos=(0, 0), | ||
FOV=1 | ||
) | ||
|
||
env.apply(MazeActions.RIGHT) | ||
oldScore = env.score | ||
env.apply(MazeActions.LEFT) | ||
newScore = env.score | ||
expect(oldScore).is_equal(newScore) | ||
|
Empty file.