diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..23815ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +__pycache__ +/.settings +/.pydevproject +/.project +/.settings diff --git a/Core/CMazeEnviroment.py b/Core/CMazeEnviroment.py new file mode 100644 index 0000000..11f665c --- /dev/null +++ b/Core/CMazeEnviroment.py @@ -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 \ No newline at end of file diff --git a/Core/__init__.py b/Core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..ab3419c --- /dev/null +++ b/main.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def main(): + pass + +if __name__ == '__main__': + main() diff --git a/tests/Test_CMazeEnviroment.py b/tests/Test_CMazeEnviroment.py new file mode 100644 index 0000000..527ca1b --- /dev/null +++ b/tests/Test_CMazeEnviroment.py @@ -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) + \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29