forked from applied-ai-collective/Pacman-Deep-Q-Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
74 lines (68 loc) · 2.57 KB
/
environment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import layout
import os
from typing import List
class Environment():
"""
Base class for environments
Args:
difficulty: Difficulty of the environment
id: Unique identifier for the environment
"""
def __init__(self,difficulty:int,id:str):
self.difficulty = difficulty
self.id = id
class PacmanEnvironment(Environment):
"""
Pacman environment
Args:
difficulty: Difficulty of the environment
id: Unique identifier for the environment
numGhosts: Number of ghosts in the environment
randomStart: Whether to start the agents at random locations
ghostType: Type of ghosts to use
"""
def __init__(self,difficulty:int,
id:str,
random_start:bool,
lay:layout,
ghosts:List[object]):
super().__init__(difficulty,id)
self.random_start = random_start
self.ghosts = ghosts
self.lay = lay
def loadAgent(pacman, nographics):
# Looks through all pythonPath Directories for the right module,
pythonPathStr = os.path.expandvars("$PYTHONPATH")
if pythonPathStr.find(';') == -1:
pythonPathDirs = pythonPathStr.split(':')
else:
pythonPathDirs = pythonPathStr.split(';')
pythonPathDirs.append('.')
for moduleDir in pythonPathDirs:
if not os.path.isdir(moduleDir):
continue
moduleNames = [f for f in os.listdir(
moduleDir) if f.endswith('gents.py')]
for modulename in moduleNames:
try:
module = __import__(modulename[:-3])
except ImportError:
continue
if pacman in dir(module):
if nographics and modulename == 'keyboardAgents.py':
raise Exception(
'Using the keyboard requires graphics (not text display)')
return getattr(module, pacman)
raise Exception('The agent ' + pacman +
' is not specified in any *Agents.py.')
def parsePacmanEnv(noKeyboard,**envArg):
ghostType = loadAgent(envArg["ghost"],noKeyboard)
ghosts = [ghostType(i + 1) for i in range(envArg["numGhosts"])]
lay = layout.getLayout(envArg["layout"])
if lay == None:
raise Exception("The layout " + envArg['layout'] + " cannot be found")
return PacmanEnvironment(difficulty=envArg["difficulty"],
id=envArg["layout"],
random_start=envArg["randomStartPos"],
lay=lay,
ghosts=ghosts)