Skip to content

Commit f548185

Browse files
author
Timur Gilmullin
committed
#14: refactor maze.py
1 parent 01b463d commit f548185

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

  • pybrain/rl/environments/mazes

pybrain/rl/environments/mazes/maze.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
__author__ = 'Tom Schaul, tom@idsia.ch'
1+
# -*- coding: utf-8 -*-
22

33
from random import random, choice
44
from scipy import zeros
55

66
from pybrain.utilities import Named
77
from pybrain.rl.environments.environment import Environment
88

9-
# TODO: mazes can have any number of dimensions?
9+
__author__ = 'Tom Schaul, tom@idsia.ch'
1010

1111

1212
class Maze(Environment, Named):
@@ -52,9 +52,12 @@ def __init__(self, topology, goal, **args):
5252
self.setArgs(**args)
5353
self.mazeTable = topology
5454
self.goal = goal
55-
if self.initPos == None:
55+
self.bang = False
56+
57+
if self.initPos is None:
5658
self.initPos = self._freePos()
5759
self.initPos.remove(self.goal)
60+
5861
self.reset()
5962

6063
def reset(self):
@@ -65,24 +68,29 @@ def reset(self):
6568
def _freePos(self):
6669
""" produce a list of the free positions. """
6770
res = []
71+
6872
for i, row in enumerate(self.mazeTable):
6973
for j, p in enumerate(row):
70-
if p == False:
74+
if p is False:
7175
res.append((i, j))
76+
7277
return res
7378

74-
def _moveInDir(self, pos, dir):
79+
def _moveInDir(self, pos, direction):
7580
""" the new state after the movement in one direction. """
76-
return (pos[0] + dir[0], pos[1] + dir[1])
81+
return (pos[0] + direction[0], pos[1] + direction[1])
7782

7883
def performAction(self, action):
7984
if self.stochAction > 0:
8085
if random() < self.stochAction:
8186
action = choice(range(len(self.allActions)))
87+
8288
tmp = self._moveInDir(self.perseus, self.allActions[action])
83-
if self.mazeTable[tmp] == False:
89+
90+
if self.mazeTable[tmp] is False:
8491
self.perseus = tmp
8592
self.bang = False
93+
8694
else:
8795
self.bang = True
8896

0 commit comments

Comments
 (0)