getting right starter files
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
# educational purposes provided that (1) you do not distribute or publish
|
||||
# solutions, (2) you retain this notice, and (3) you provide clear
|
||||
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
|
||||
#
|
||||
#
|
||||
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
|
||||
# The core projects and autograders were primarily created by John DeNero
|
||||
# ([email protected]) and Dan Klein ([email protected]).
|
||||
@@ -21,7 +21,8 @@
|
||||
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
|
||||
|
||||
from util import *
|
||||
import time, os
|
||||
import time
|
||||
import os
|
||||
import traceback
|
||||
import sys
|
||||
|
||||
@@ -29,6 +30,7 @@ import sys
|
||||
# Parts worth reading #
|
||||
#######################
|
||||
|
||||
|
||||
class Agent:
|
||||
"""
|
||||
An agent must define a getAction method, but may also define the
|
||||
@@ -36,6 +38,7 @@ class Agent:
|
||||
|
||||
def registerInitialState(self, state): # inspects the starting state
|
||||
"""
|
||||
|
||||
def __init__(self, index=0):
|
||||
self.index = index
|
||||
|
||||
@@ -46,6 +49,7 @@ class Agent:
|
||||
"""
|
||||
raiseNotDefined()
|
||||
|
||||
|
||||
class Directions:
|
||||
NORTH = 'North'
|
||||
SOUTH = 'South'
|
||||
@@ -53,13 +57,13 @@ class Directions:
|
||||
WEST = 'West'
|
||||
STOP = 'Stop'
|
||||
|
||||
LEFT = {NORTH: WEST,
|
||||
SOUTH: EAST,
|
||||
EAST: NORTH,
|
||||
WEST: SOUTH,
|
||||
STOP: STOP}
|
||||
LEFT = {NORTH: WEST,
|
||||
SOUTH: EAST,
|
||||
EAST: NORTH,
|
||||
WEST: SOUTH,
|
||||
STOP: STOP}
|
||||
|
||||
RIGHT = dict([(y,x) for x, y in LEFT.items()])
|
||||
RIGHT = dict([(y, x) for x, y in list(LEFT.items())])
|
||||
|
||||
REVERSE = {NORTH: SOUTH,
|
||||
SOUTH: NORTH,
|
||||
@@ -67,6 +71,7 @@ class Directions:
|
||||
WEST: EAST,
|
||||
STOP: STOP}
|
||||
|
||||
|
||||
class Configuration:
|
||||
"""
|
||||
A Configuration holds the (x,y) coordinate of a character, along with its
|
||||
@@ -87,11 +92,12 @@ class Configuration:
|
||||
return self.direction
|
||||
|
||||
def isInteger(self):
|
||||
x,y = self.pos
|
||||
x, y = self.pos
|
||||
return x == int(x) and y == int(y)
|
||||
|
||||
def __eq__(self, other):
|
||||
if other == None: return False
|
||||
if other == None:
|
||||
return False
|
||||
return (self.pos == other.pos and self.direction == other.direction)
|
||||
|
||||
def __hash__(self):
|
||||
@@ -110,33 +116,35 @@ class Configuration:
|
||||
|
||||
Actions are movement vectors.
|
||||
"""
|
||||
x, y= self.pos
|
||||
x, y = self.pos
|
||||
dx, dy = vector
|
||||
direction = Actions.vectorToDirection(vector)
|
||||
if direction == Directions.STOP:
|
||||
direction = self.direction # There is no stop direction
|
||||
direction = self.direction # There is no stop direction
|
||||
return Configuration((x + dx, y+dy), direction)
|
||||
|
||||
|
||||
class AgentState:
|
||||
"""
|
||||
AgentStates hold the state of an agent (configuration, speed, scared, etc).
|
||||
"""
|
||||
|
||||
def __init__( self, startConfiguration, isPacman ):
|
||||
def __init__(self, startConfiguration, isPacman):
|
||||
self.start = startConfiguration
|
||||
self.configuration = startConfiguration
|
||||
self.isPacman = isPacman
|
||||
self.scaredTimer = 0
|
||||
# state below potentially used for contest only
|
||||
self.numCarrying = 0
|
||||
self.numReturned = 0
|
||||
|
||||
def __str__( self ):
|
||||
def __str__(self):
|
||||
if self.isPacman:
|
||||
return "Pacman: " + str( self.configuration )
|
||||
return "Pacman: " + str(self.configuration)
|
||||
else:
|
||||
return "Ghost: " + str( self.configuration )
|
||||
return "Ghost: " + str(self.configuration)
|
||||
|
||||
def __eq__( self, other ):
|
||||
def __eq__(self, other):
|
||||
if other == None:
|
||||
return False
|
||||
return self.configuration == other.configuration and self.scaredTimer == other.scaredTimer
|
||||
@@ -144,8 +152,8 @@ class AgentState:
|
||||
def __hash__(self):
|
||||
return hash(hash(self.configuration) + 13 * hash(self.scaredTimer))
|
||||
|
||||
def copy( self ):
|
||||
state = AgentState( self.start, self.isPacman )
|
||||
def copy(self):
|
||||
state = AgentState(self.start, self.isPacman)
|
||||
state.configuration = self.configuration
|
||||
state.scaredTimer = self.scaredTimer
|
||||
state.numCarrying = self.numCarrying
|
||||
@@ -153,12 +161,14 @@ class AgentState:
|
||||
return state
|
||||
|
||||
def getPosition(self):
|
||||
if self.configuration == None: return None
|
||||
if self.configuration == None:
|
||||
return None
|
||||
return self.configuration.getPosition()
|
||||
|
||||
def getDirection(self):
|
||||
return self.configuration.getDirection()
|
||||
|
||||
|
||||
class Grid:
|
||||
"""
|
||||
A 2-dimensional array of objects backed by a list of lists. Data is accessed
|
||||
@@ -167,13 +177,16 @@ class Grid:
|
||||
|
||||
The __str__ method constructs an output that is oriented like a pacman board.
|
||||
"""
|
||||
|
||||
def __init__(self, width, height, initialValue=False, bitRepresentation=None):
|
||||
if initialValue not in [False, True]: raise Exception('Grids can only contain booleans')
|
||||
if initialValue not in [False, True]:
|
||||
raise Exception('Grids can only contain booleans')
|
||||
self.CELLS_PER_INT = 30
|
||||
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.data = [[initialValue for y in range(height)] for x in range(width)]
|
||||
self.data = [[initialValue for y in range(
|
||||
height)] for x in range(width)]
|
||||
if bitRepresentation:
|
||||
self._unpackBits(bitRepresentation)
|
||||
|
||||
@@ -184,12 +197,14 @@ class Grid:
|
||||
self.data[key] = item
|
||||
|
||||
def __str__(self):
|
||||
out = [[str(self.data[x][y])[0] for x in range(self.width)] for y in range(self.height)]
|
||||
out = [[str(self.data[x][y])[0] for x in range(self.width)]
|
||||
for y in range(self.height)]
|
||||
out.reverse()
|
||||
return '\n'.join([''.join(x) for x in out])
|
||||
|
||||
def __eq__(self, other):
|
||||
if other == None: return False
|
||||
if other == None:
|
||||
return False
|
||||
return self.data == other.data
|
||||
|
||||
def __hash__(self):
|
||||
@@ -216,14 +231,15 @@ class Grid:
|
||||
g.data = self.data
|
||||
return g
|
||||
|
||||
def count(self, item =True ):
|
||||
def count(self, item=True):
|
||||
return sum([x.count(item) for x in self.data])
|
||||
|
||||
def asList(self, key = True):
|
||||
def asList(self, key=True):
|
||||
list = []
|
||||
for x in range(self.width):
|
||||
for y in range(self.height):
|
||||
if self[x][y] == key: list.append( (x,y) )
|
||||
if self[x][y] == key:
|
||||
list.append((x, y))
|
||||
return list
|
||||
|
||||
def packBits(self):
|
||||
@@ -246,7 +262,7 @@ class Grid:
|
||||
return tuple(bits)
|
||||
|
||||
def _cellIndexToPosition(self, index):
|
||||
x = index // self.height
|
||||
x = index / self.height
|
||||
y = index % self.height
|
||||
return x, y
|
||||
|
||||
@@ -257,14 +273,16 @@ class Grid:
|
||||
cell = 0
|
||||
for packed in bits:
|
||||
for bit in self._unpackInt(packed, self.CELLS_PER_INT):
|
||||
if cell == self.width * self.height: break
|
||||
if cell == self.width * self.height:
|
||||
break
|
||||
x, y = self._cellIndexToPosition(cell)
|
||||
self[x][y] = bit
|
||||
cell += 1
|
||||
|
||||
def _unpackInt(self, packed, size):
|
||||
bools = []
|
||||
if packed < 0: raise ValueError("must be a positive integer")
|
||||
if packed < 0:
|
||||
raise ValueError("must be a positive integer")
|
||||
for i in range(size):
|
||||
n = 2 ** (self.CELLS_PER_INT - i - 1)
|
||||
if packed >= n:
|
||||
@@ -274,28 +292,30 @@ class Grid:
|
||||
bools.append(False)
|
||||
return bools
|
||||
|
||||
|
||||
def reconstituteGrid(bitRep):
|
||||
if type(bitRep) is not type((1,2)):
|
||||
if type(bitRep) is not type((1, 2)):
|
||||
return bitRep
|
||||
width, height = bitRep[:2]
|
||||
return Grid(width, height, bitRepresentation= bitRep[2:])
|
||||
return Grid(width, height, bitRepresentation=bitRep[2:])
|
||||
|
||||
####################################
|
||||
# Parts you shouldn't have to read #
|
||||
####################################
|
||||
|
||||
|
||||
class Actions:
|
||||
"""
|
||||
A collection of static methods for manipulating move actions.
|
||||
"""
|
||||
# Directions
|
||||
_directions = {Directions.NORTH: (0, 1),
|
||||
Directions.SOUTH: (0, -1),
|
||||
_directions = {Directions.WEST: (-1, 0),
|
||||
Directions.STOP: (0, 0),
|
||||
Directions.EAST: (1, 0),
|
||||
Directions.WEST: (-1, 0),
|
||||
Directions.STOP: (0, 0)}
|
||||
Directions.NORTH: (0, 1),
|
||||
Directions.SOUTH: (0, -1)}
|
||||
|
||||
_directionsAsList = _directions.items()
|
||||
_directionsAsList = [('West', (-1, 0)), ('Stop', (0, 0)), ('East', (1, 0)), ('North', (0, 1)), ('South', (0, -1))]
|
||||
|
||||
TOLERANCE = .001
|
||||
|
||||
@@ -324,8 +344,8 @@ class Actions:
|
||||
return Directions.STOP
|
||||
vectorToDirection = staticmethod(vectorToDirection)
|
||||
|
||||
def directionToVector(direction, speed = 1.0):
|
||||
dx, dy = Actions._directions[direction]
|
||||
def directionToVector(direction, speed=1.0):
|
||||
dx, dy = Actions._directions[direction]
|
||||
return (dx * speed, dy * speed)
|
||||
directionToVector = staticmethod(directionToVector)
|
||||
|
||||
@@ -335,30 +355,34 @@ class Actions:
|
||||
x_int, y_int = int(x + 0.5), int(y + 0.5)
|
||||
|
||||
# In between grid points, all agents must continue straight
|
||||
if (abs(x - x_int) + abs(y - y_int) > Actions.TOLERANCE):
|
||||
if (abs(x - x_int) + abs(y - y_int) > Actions.TOLERANCE):
|
||||
return [config.getDirection()]
|
||||
|
||||
for dir, vec in Actions._directionsAsList:
|
||||
dx, dy = vec
|
||||
next_y = y_int + dy
|
||||
next_x = x_int + dx
|
||||
if not walls[next_x][next_y]: possible.append(dir)
|
||||
if not walls[next_x][next_y]:
|
||||
possible.append(dir)
|
||||
|
||||
return possible
|
||||
|
||||
getPossibleActions = staticmethod(getPossibleActions)
|
||||
|
||||
def getLegalNeighbors(position, walls):
|
||||
x,y = position
|
||||
x, y = position
|
||||
x_int, y_int = int(x + 0.5), int(y + 0.5)
|
||||
neighbors = []
|
||||
for dir, vec in Actions._directionsAsList:
|
||||
dx, dy = vec
|
||||
next_x = x_int + dx
|
||||
if next_x < 0 or next_x == walls.width: continue
|
||||
if next_x < 0 or next_x == walls.width:
|
||||
continue
|
||||
next_y = y_int + dy
|
||||
if next_y < 0 or next_y == walls.height: continue
|
||||
if not walls[next_x][next_y]: neighbors.append((next_x, next_y))
|
||||
if next_y < 0 or next_y == walls.height:
|
||||
continue
|
||||
if not walls[next_x][next_y]:
|
||||
neighbors.append((next_x, next_y))
|
||||
return neighbors
|
||||
getLegalNeighbors = staticmethod(getLegalNeighbors)
|
||||
|
||||
@@ -368,18 +392,17 @@ class Actions:
|
||||
return (x + dx, y + dy)
|
||||
getSuccessor = staticmethod(getSuccessor)
|
||||
|
||||
class GameStateData:
|
||||
"""
|
||||
|
||||
"""
|
||||
def __init__( self, prevState = None ):
|
||||
class GameStateData:
|
||||
|
||||
def __init__(self, prevState=None):
|
||||
"""
|
||||
Generates a new data packet by copying information from its predecessor.
|
||||
"""
|
||||
if prevState != None:
|
||||
self.food = prevState.food.shallowCopy()
|
||||
self.capsules = prevState.capsules[:]
|
||||
self.agentStates = self.copyAgentStates( prevState.agentStates )
|
||||
self.agentStates = self.copyAgentStates(prevState.agentStates)
|
||||
self.layout = prevState.layout
|
||||
self._eaten = prevState._eaten
|
||||
self.score = prevState.score
|
||||
@@ -392,8 +415,8 @@ class GameStateData:
|
||||
self._win = False
|
||||
self.scoreChange = 0
|
||||
|
||||
def deepCopy( self ):
|
||||
state = GameStateData( self )
|
||||
def deepCopy(self):
|
||||
state = GameStateData(self)
|
||||
state.food = self.food.deepCopy()
|
||||
state.layout = self.layout.deepCopy()
|
||||
state._agentMoved = self._agentMoved
|
||||
@@ -402,40 +425,45 @@ class GameStateData:
|
||||
state._capsuleEaten = self._capsuleEaten
|
||||
return state
|
||||
|
||||
def copyAgentStates( self, agentStates ):
|
||||
def copyAgentStates(self, agentStates):
|
||||
copiedStates = []
|
||||
for agentState in agentStates:
|
||||
copiedStates.append( agentState.copy() )
|
||||
copiedStates.append(agentState.copy())
|
||||
return copiedStates
|
||||
|
||||
def __eq__( self, other ):
|
||||
def __eq__(self, other):
|
||||
"""
|
||||
Allows two states to be compared.
|
||||
"""
|
||||
if other == None: return False
|
||||
if other == None:
|
||||
return False
|
||||
# TODO Check for type of other
|
||||
if not self.agentStates == other.agentStates: return False
|
||||
if not self.food == other.food: return False
|
||||
if not self.capsules == other.capsules: return False
|
||||
if not self.score == other.score: return False
|
||||
if not self.agentStates == other.agentStates:
|
||||
return False
|
||||
if not self.food == other.food:
|
||||
return False
|
||||
if not self.capsules == other.capsules:
|
||||
return False
|
||||
if not self.score == other.score:
|
||||
return False
|
||||
return True
|
||||
|
||||
def __hash__( self ):
|
||||
def __hash__(self):
|
||||
"""
|
||||
Allows states to be keys of dictionaries.
|
||||
"""
|
||||
for i, state in enumerate( self.agentStates ):
|
||||
for i, state in enumerate(self.agentStates):
|
||||
try:
|
||||
int(hash(state))
|
||||
except TypeError as e:
|
||||
print(e)
|
||||
#hash(state)
|
||||
return int((hash(tuple(self.agentStates)) + 13*hash(self.food) + 113* hash(tuple(self.capsules)) + 7 * hash(self.score)) % 1048575 )
|
||||
# hash(state)
|
||||
return int((hash(tuple(self.agentStates)) + 13*hash(self.food) + 113 * hash(tuple(self.capsules)) + 7 * hash(self.score)) % 1048575)
|
||||
|
||||
def __str__( self ):
|
||||
def __str__(self):
|
||||
width, height = self.layout.width, self.layout.height
|
||||
map = Grid(width, height)
|
||||
if type(self.food) == type((1,2)):
|
||||
if type(self.food) == type((1, 2)):
|
||||
self.food = reconstituteGrid(self.food)
|
||||
for x in range(width):
|
||||
for y in range(height):
|
||||
@@ -443,21 +471,23 @@ class GameStateData:
|
||||
map[x][y] = self._foodWallStr(food[x][y], walls[x][y])
|
||||
|
||||
for agentState in self.agentStates:
|
||||
if agentState == None: continue
|
||||
if agentState.configuration == None: continue
|
||||
x,y = [int( i ) for i in nearestPoint( agentState.configuration.pos )]
|
||||
if agentState == None:
|
||||
continue
|
||||
if agentState.configuration == None:
|
||||
continue
|
||||
x, y = [int(i) for i in nearestPoint(agentState.configuration.pos)]
|
||||
agent_dir = agentState.configuration.direction
|
||||
if agentState.isPacman:
|
||||
map[x][y] = self._pacStr( agent_dir )
|
||||
map[x][y] = self._pacStr(agent_dir)
|
||||
else:
|
||||
map[x][y] = self._ghostStr( agent_dir )
|
||||
map[x][y] = self._ghostStr(agent_dir)
|
||||
|
||||
for x, y in self.capsules:
|
||||
map[x][y] = 'o'
|
||||
|
||||
return str(map) + ("\nScore: %d\n" % self.score)
|
||||
|
||||
def _foodWallStr( self, hasFood, hasWall ):
|
||||
def _foodWallStr(self, hasFood, hasWall):
|
||||
if hasFood:
|
||||
return '.'
|
||||
elif hasWall:
|
||||
@@ -465,7 +495,7 @@ class GameStateData:
|
||||
else:
|
||||
return ' '
|
||||
|
||||
def _pacStr( self, dir ):
|
||||
def _pacStr(self, dir):
|
||||
if dir == Directions.NORTH:
|
||||
return 'v'
|
||||
if dir == Directions.SOUTH:
|
||||
@@ -474,7 +504,7 @@ class GameStateData:
|
||||
return '>'
|
||||
return '<'
|
||||
|
||||
def _ghostStr( self, dir ):
|
||||
def _ghostStr(self, dir):
|
||||
return 'G'
|
||||
if dir == Directions.NORTH:
|
||||
return 'M'
|
||||
@@ -484,7 +514,7 @@ class GameStateData:
|
||||
return '3'
|
||||
return 'E'
|
||||
|
||||
def initialize( self, layout, numGhostAgents ):
|
||||
def initialize(self, layout, numGhostAgents):
|
||||
"""
|
||||
Creates an initial game state from a layout array (see layout.py).
|
||||
"""
|
||||
@@ -499,23 +529,28 @@ class GameStateData:
|
||||
numGhosts = 0
|
||||
for isPacman, pos in layout.agentPositions:
|
||||
if not isPacman:
|
||||
if numGhosts == numGhostAgents: continue # Max ghosts reached already
|
||||
else: numGhosts += 1
|
||||
self.agentStates.append( AgentState( Configuration( pos, Directions.STOP), isPacman) )
|
||||
if numGhosts == numGhostAgents:
|
||||
continue # Max ghosts reached already
|
||||
else:
|
||||
numGhosts += 1
|
||||
self.agentStates.append(AgentState(
|
||||
Configuration(pos, Directions.STOP), isPacman))
|
||||
self._eaten = [False for a in self.agentStates]
|
||||
|
||||
|
||||
try:
|
||||
import boinc
|
||||
_BOINC_ENABLED = True
|
||||
except:
|
||||
_BOINC_ENABLED = False
|
||||
|
||||
|
||||
class Game:
|
||||
"""
|
||||
The Game manages the control flow, soliciting actions from agents.
|
||||
"""
|
||||
|
||||
def __init__( self, agents, display, rules, startingIndex=0, muteAgents=False, catchExceptions=False ):
|
||||
def __init__(self, agents, display, rules, startingIndex=0, muteAgents=False, catchExceptions=False):
|
||||
self.agentCrashed = False
|
||||
self.agents = agents
|
||||
self.display = display
|
||||
@@ -537,9 +572,10 @@ class Game:
|
||||
else:
|
||||
return self.rules.getProgress(self)
|
||||
|
||||
def _agentCrash( self, agentIndex, quiet=False):
|
||||
def _agentCrash(self, agentIndex, quiet=False):
|
||||
"Helper method for handling agent crashes"
|
||||
if not quiet: traceback.print_exc()
|
||||
if not quiet:
|
||||
traceback.print_exc()
|
||||
self.gameOver = True
|
||||
self.agentCrashed = True
|
||||
self.rules.agentCrash(self, agentIndex)
|
||||
@@ -548,7 +584,8 @@ class Game:
|
||||
OLD_STDERR = None
|
||||
|
||||
def mute(self, agentIndex):
|
||||
if not self.muteAgents: return
|
||||
if not self.muteAgents:
|
||||
return
|
||||
global OLD_STDOUT, OLD_STDERR
|
||||
import io
|
||||
OLD_STDOUT = sys.stdout
|
||||
@@ -557,21 +594,21 @@ class Game:
|
||||
sys.stderr = self.agentOutput[agentIndex]
|
||||
|
||||
def unmute(self):
|
||||
if not self.muteAgents: return
|
||||
if not self.muteAgents:
|
||||
return
|
||||
global OLD_STDOUT, OLD_STDERR
|
||||
# Revert stdout/stderr to originals
|
||||
sys.stdout = OLD_STDOUT
|
||||
sys.stderr = OLD_STDERR
|
||||
|
||||
|
||||
def run( self ):
|
||||
def run(self):
|
||||
"""
|
||||
Main control loop for game play.
|
||||
"""
|
||||
self.display.initialize(self.state.data)
|
||||
self.numMoves = 0
|
||||
|
||||
###self.display.initialize(self.state.makeObservation(1).data)
|
||||
# self.display.initialize(self.state.makeObservation(1).data)
|
||||
# inform learning agents of the game start
|
||||
for i in range(len(self.agents)):
|
||||
agent = self.agents[i]
|
||||
@@ -587,14 +624,16 @@ class Game:
|
||||
self.mute(i)
|
||||
if self.catchExceptions:
|
||||
try:
|
||||
timed_func = TimeoutFunction(agent.registerInitialState, int(self.rules.getMaxStartupTime(i)))
|
||||
timed_func = TimeoutFunction(
|
||||
agent.registerInitialState, int(self.rules.getMaxStartupTime(i)))
|
||||
try:
|
||||
start_time = time.time()
|
||||
timed_func(self.state.deepCopy())
|
||||
time_taken = time.time() - start_time
|
||||
self.totalAgentTimes[i] += time_taken
|
||||
except TimeoutFunctionException:
|
||||
print("Agent %d ran out of time on startup!" % i, file=sys.stderr)
|
||||
print("Agent %d ran out of time on startup!" %
|
||||
i, file=sys.stderr)
|
||||
self.unmute()
|
||||
self.agentTimeout = True
|
||||
self._agentCrash(i, quiet=True)
|
||||
@@ -605,11 +644,11 @@ class Game:
|
||||
return
|
||||
else:
|
||||
agent.registerInitialState(self.state.deepCopy())
|
||||
## TODO: could this exceed the total time
|
||||
# TODO: could this exceed the total time
|
||||
self.unmute()
|
||||
|
||||
agentIndex = self.startingIndex
|
||||
numAgents = len( self.agents )
|
||||
numAgents = len(self.agents)
|
||||
|
||||
while not self.gameOver:
|
||||
# Fetch the next agent
|
||||
@@ -617,11 +656,12 @@ class Game:
|
||||
move_time = 0
|
||||
skip_action = False
|
||||
# Generate an observation of the state
|
||||
if 'observationFunction' in dir( agent ):
|
||||
if 'observationFunction' in dir(agent):
|
||||
self.mute(agentIndex)
|
||||
if self.catchExceptions:
|
||||
try:
|
||||
timed_func = TimeoutFunction(agent.observationFunction, int(self.rules.getMoveTimeout(agentIndex)))
|
||||
timed_func = TimeoutFunction(agent.observationFunction, int(
|
||||
self.rules.getMoveTimeout(agentIndex)))
|
||||
try:
|
||||
start_time = time.time()
|
||||
observation = timed_func(self.state.deepCopy())
|
||||
@@ -634,7 +674,8 @@ class Game:
|
||||
self.unmute()
|
||||
return
|
||||
else:
|
||||
observation = agent.observationFunction(self.state.deepCopy())
|
||||
observation = agent.observationFunction(
|
||||
self.state.deepCopy())
|
||||
self.unmute()
|
||||
else:
|
||||
observation = self.state.deepCopy()
|
||||
@@ -644,14 +685,16 @@ class Game:
|
||||
self.mute(agentIndex)
|
||||
if self.catchExceptions:
|
||||
try:
|
||||
timed_func = TimeoutFunction(agent.getAction, int(self.rules.getMoveTimeout(agentIndex)) - int(move_time))
|
||||
timed_func = TimeoutFunction(agent.getAction, int(
|
||||
self.rules.getMoveTimeout(agentIndex)) - int(move_time))
|
||||
try:
|
||||
start_time = time.time()
|
||||
if skip_action:
|
||||
raise TimeoutFunctionException()
|
||||
action = timed_func( observation )
|
||||
action = timed_func(observation)
|
||||
except TimeoutFunctionException:
|
||||
print("Agent %d timed out on a single move!" % agentIndex, file=sys.stderr)
|
||||
print("Agent %d timed out on a single move!" %
|
||||
agentIndex, file=sys.stderr)
|
||||
self.agentTimeout = True
|
||||
self._agentCrash(agentIndex, quiet=True)
|
||||
self.unmute()
|
||||
@@ -661,18 +704,21 @@ class Game:
|
||||
|
||||
if move_time > self.rules.getMoveWarningTime(agentIndex):
|
||||
self.totalAgentTimeWarnings[agentIndex] += 1
|
||||
print("Agent %d took too long to make a move! This is warning %d" % (agentIndex, self.totalAgentTimeWarnings[agentIndex]), file=sys.stderr)
|
||||
print("Agent %d took too long to make a move! This is warning %d" % (
|
||||
agentIndex, self.totalAgentTimeWarnings[agentIndex]), file=sys.stderr)
|
||||
if self.totalAgentTimeWarnings[agentIndex] > self.rules.getMaxTimeWarnings(agentIndex):
|
||||
print("Agent %d exceeded the maximum number of warnings: %d" % (agentIndex, self.totalAgentTimeWarnings[agentIndex]), file=sys.stderr)
|
||||
print("Agent %d exceeded the maximum number of warnings: %d" % (
|
||||
agentIndex, self.totalAgentTimeWarnings[agentIndex]), file=sys.stderr)
|
||||
self.agentTimeout = True
|
||||
self._agentCrash(agentIndex, quiet=True)
|
||||
self.unmute()
|
||||
return
|
||||
|
||||
self.totalAgentTimes[agentIndex] += move_time
|
||||
#print("Agent: %d, time: %f, total: %f" % (agentIndex, move_time, self.totalAgentTimes[agentIndex]))
|
||||
# print "Agent: %d, time: %f, total: %f" % (agentIndex, move_time, self.totalAgentTimes[agentIndex])
|
||||
if self.totalAgentTimes[agentIndex] > self.rules.getMaxTotalTime(agentIndex):
|
||||
print("Agent %d ran out of time! (time: %1.2f)" % (agentIndex, self.totalAgentTimes[agentIndex]), file=sys.stderr)
|
||||
print("Agent %d ran out of time! (time: %1.2f)" % (
|
||||
agentIndex, self.totalAgentTimes[agentIndex]), file=sys.stderr)
|
||||
self.agentTimeout = True
|
||||
self._agentCrash(agentIndex, quiet=True)
|
||||
self.unmute()
|
||||
@@ -687,42 +733,45 @@ class Game:
|
||||
self.unmute()
|
||||
|
||||
# Execute the action
|
||||
self.moveHistory.append( (agentIndex, action) )
|
||||
self.moveHistory.append((agentIndex, action))
|
||||
if self.catchExceptions:
|
||||
try:
|
||||
self.state = self.state.generateSuccessor( agentIndex, action )
|
||||
self.state = self.state.generateSuccessor(
|
||||
agentIndex, action)
|
||||
except Exception as data:
|
||||
self.mute(agentIndex)
|
||||
self._agentCrash(agentIndex)
|
||||
self.unmute()
|
||||
return
|
||||
else:
|
||||
self.state = self.state.generateSuccessor( agentIndex, action )
|
||||
self.state = self.state.generateSuccessor(agentIndex, action)
|
||||
|
||||
# Change the display
|
||||
self.display.update( self.state.data )
|
||||
self.display.update(self.state.data)
|
||||
###idx = agentIndex - agentIndex % 2 + 1
|
||||
###self.display.update( self.state.makeObservation(idx).data )
|
||||
|
||||
# Allow for game specific conditions (winning, losing, etc.)
|
||||
self.rules.process(self.state, self)
|
||||
# Track progress
|
||||
if agentIndex == numAgents + 1: self.numMoves += 1
|
||||
if agentIndex == numAgents + 1:
|
||||
self.numMoves += 1
|
||||
# Next agent
|
||||
agentIndex = ( agentIndex + 1 ) % numAgents
|
||||
agentIndex = (agentIndex + 1) % numAgents
|
||||
|
||||
if _BOINC_ENABLED:
|
||||
boinc.set_fraction_done(self.getProgress())
|
||||
|
||||
# inform a learning agent of the game result
|
||||
for agentIndex, agent in enumerate(self.agents):
|
||||
if "final" in dir( agent ) :
|
||||
if "final" in dir(agent):
|
||||
try:
|
||||
self.mute(agentIndex)
|
||||
agent.final( self.state )
|
||||
agent.final(self.state)
|
||||
self.unmute()
|
||||
except Exception as data:
|
||||
if not self.catchExceptions: raise data
|
||||
if not self.catchExceptions:
|
||||
raise
|
||||
self._agentCrash(agentIndex)
|
||||
self.unmute()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user