Skip to content

Commit

Permalink
basic environment
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Aug 23, 2020
1 parent b41b6b2 commit 00fd8ad
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__
/.settings
/.pydevproject
/.project
/.settings
52 changes: 52 additions & 0 deletions Core/CMazeEnviroment.py
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 added Core/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions main.py
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()
87 changes: 87 additions & 0 deletions tests/Test_CMazeEnviroment.py
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 added tests/__init__.py
Empty file.

0 comments on commit 00fd8ad

Please sign in to comment.