You are on page 1of 5

def enhancedPacmanFeatures(state, action): # from pacmitan_clonadorDeAgenteVirt.

py

###################################################################
# FEATURES #
# STOP: 50 si la accion es STOP. 0 Si es otra cosa. #
# NEAREST_GHOST: MD al fantasma mas Cercano #
# NEAREST_CAPSULE: MD a la capsula mas cercana #
# FOOD: Lista de MDs a las 5 comidas mas cercanas #
# CAPSULE COUNT: # de Capsulas que existen #
# SCORE: Score actual #
# ASUSTADITOS: # de Fantasmas asustados #
###################################################################

features = util.Counter()
# Feature de STOP
features["STOP"] = int(action == Directions.STOP)

# Generar arreglos y listas de los atributos del estado


successor = state.generateSuccessor(0, action)
pac_pos = successor.getPacmanPosition()
ghosts = successor.getGhostPositions()
ghostStates = successor.getGhostStates()
# Fantasma Arriba
features["win"] = int(successor.isWin())
features["lose"] = int(successor.isLose())*(-10)

x,y = pac_pos
meanghosts=[]
for ghostState in ghostStates:
if ghostState.scaredTimer == 0:
meanghosts.append(ghostState.getPosition())

if (x,y+1) in meanghosts:
features["UP"] = 0
else:
features["UP"] = 1

if (x,y-1) in meanghosts:
features["DOWN"] = 0
else:
features["DOWN"] = 1

if (x+1,y) in meanghosts:
features["RIGHT"] = 0
else:
features["RIGHT"] = 1

if (x-1,y) in meanghosts:
features["LEFT"] = 0
else:
features["LEFT"] = 1

capsules = successor.getCapsules()
state_food = state.getFood()
food = [(x, y) #enlista las posiciones donde hay comida
for x, row in enumerate(state_food)
for y, food in enumerate(row)
if food]
nearest_ghosts = sorted([util.manhattanDistance(pac_pos, i) for i in ghosts])
# Feature de Fantasmita Mas Cercano #a cuanta distancia manhattan esta el
fantasma mas cercano
features["nearest_ghost"] = nearest_ghosts[0]

# Feature de Pildora mas cercana #a cuanta distancia manhattan esta la capsula


mas cercana
nearest_caps = sorted([util.manhattanDistance(pac_pos, i) for i in capsules])
if nearest_caps:
features["nearest_capsule"] = nearest_caps[0]
else:
features["nearest_capsule"] = 0

# Feature de MD a las 5 comidas mas cercanas


nearest_food = sorted([(util.manhattanDistance(pac_pos, i),i) for i in food])
nearest_food = nearest_food[:5]
for i in range(min(len(nearest_food), 5)):
nearest_food[i]=searchAgents.mazeDistance(pac_pos,nearest_food[i][1],state)

for i, weight in zip(range(min(len(nearest_food), 5)), [1.1,1.2,1.3,1.4,1.5]):


features[("food", i)] = weight * nearest_food[i]

# Feature de cantidad de capsulas


if len(capsules) != 0:
features["capsule count"] = 1/float(len(capsules))
else:
features["capsule count"]=1

# Feature de Score
features["score"] = float(state.getScore())/2000

# Feature de cantidad de Fantasmitas Asustaditos


ghostStates = successor.getGhostStates()
numOfScaredGhosts = 0
for ghostState in ghostStates:
if ghostState.scaredTimer > 0:
numOfScaredGhosts += 1
if numOfScaredGhosts != 0:
features["asustaditos"]=1/float(numOfScaredGhosts)
else:
features["asustaditos"]=1

return features

#===========================================================
class LeftTurnAgent(game.Agent): # from pacmanAgents.py
"An agent that turns left at every opportunity"

def getAction(self, state):


legal = state.getLegalPacmanActions()
current = state.getPacmanState().configuration.direction
if current == Directions.STOP:
current = Directions.NORTH
left = Directions.LEFT[current]
if left in legal:
return left
if current in legal:
return current
if Directions.RIGHT[current] in legal:
return Directions.RIGHT[current]
if Directions.LEFT[left] in legal:
return Directions.LEFT[left]
return Directions.STOP
#==============================================================
class GreedyAgent(Agent): # from pacmanAgents.py

def __init__(self, evalFn="scoreEvaluation"):


self.evaluationFunction = util.lookup(evalFn, globals())
assert self.evaluationFunction != None

def getAction(self, state):


# Generate candidate actions
legal = state.getLegalPacmanActions()
if Directions.STOP in legal:
legal.remove(Directions.STOP)

successors = [(state.generateSuccessor(0, action), action)


for action in legal]
scored = [(self.evaluationFunction(state), action)
for state, action in successors]
bestScore = max(scored)[0]
bestActions = [pair[1] for pair in scored if pair[0] == bestScore]
return random.choice(bestActions)
#===========================================================
import search
#from searchAgents import PositionSearchProblem
import searchAgents

def mazeDistanceAndFirstAction(point1, point2, gameState): # from pacman_iapucp.py


"""
Returns the maze distance between any two points, using the search functions
you have already built. The gameState can be any game state -- Pacman's
position in that state is ignored.

Example usage: mazeDistance( (2,4), (5,6), gameState)

This might be a useful helper function for your ApproximateSearchAgent.


"""
x1, y1 = point1
x2, y2 = point2
walls = gameState.getWalls()
assert not walls[x1][y1], 'point1 is a wall: ' + str(point1)
assert not walls[x2][y2], 'point2 is a wall: ' + str(point2)
prob = PositionSearchProblem(gameState, start=point1, goal=point2, warn=False,
visualize=False)
acciones = search.astar(prob)
return len(acciones), acciones[0]

#================================================================
def enhancedPacmanFeatures(state, action):

###################################################################
# FEATURES #
# STOP: 50 si la accion es STOP. 0 Si es otra cosa. #
# NEAREST_GHOST: MD al fantasma mas Cercano #
# NEAREST_CAPSULE: MD a la capsula mas cercana #
# FOOD: Lista de MDs a las 5 comidas mas cercanas #
# CAPSULE COUNT: # de Capsulas que existen #
# SCORE: Score actual #
# ASUSTADITOS: # de Fantasmas asustados #
###################################################################

features = util.Counter()
# Feature de STOP
features["STOP"] = int(action == Directions.STOP)

# Generar arreglos y listas de los atributos del estado


successor = state.generateSuccessor(0, action)
pac_pos = successor.getPacmanPosition()
ghosts = successor.getGhostPositions()
ghostStates = successor.getGhostStates()
# Fantasma Arriba
features["win"] = int(successor.isWin())
features["lose"] = int(successor.isLose())*(-10)

x,y = pac_pos
meanghosts=[]
for ghostState in ghostStates:
if ghostState.scaredTimer == 0:
meanghosts.append(ghostState.getPosition())

if (x,y+1) in meanghosts:
features["UP"] = 0
else:
features["UP"] = 1

if (x,y-1) in meanghosts:
features["DOWN"] = 0
else:
features["DOWN"] = 1

if (x+1,y) in meanghosts:
features["RIGHT"] = 0
else:
features["RIGHT"] = 1

if (x-1,y) in meanghosts:
features["LEFT"] = 0
else:
features["LEFT"] = 1

capsules = successor.getCapsules()
state_food = state.getFood()
food = [(x, y) #enlista las posiciones donde hay comida
for x, row in enumerate(state_food)
for y, food in enumerate(row)
if food]
nearest_ghosts = sorted([util.manhattanDistance(pac_pos, i) for i in ghosts])

# Feature de Fantasmita Mas Cercano #a cuanta distancia manhattan esta el


fantasma mas cercano
features["nearest_ghost"] = nearest_ghosts[0]

# Feature de Pildora mas cercana #a cuanta distancia manhattan esta la capsula


mas cercana
nearest_caps = sorted([util.manhattanDistance(pac_pos, i) for i in capsules])
if nearest_caps:
features["nearest_capsule"] = nearest_caps[0]
else:
features["nearest_capsule"] = 0

# Feature de MD a las 5 comidas mas cercanas


nearest_food = sorted([(util.manhattanDistance(pac_pos, i),i) for i in food])
nearest_food = nearest_food[:5]
for i in range(min(len(nearest_food), 5)):
nearest_food[i]=searchAgents.mazeDistance(pac_pos,nearest_food[i][1],state)

for i, weight in zip(range(min(len(nearest_food), 5)), [1.1,1.2,1.3,1.4,1.5]):


features[("food", i)] = weight * nearest_food[i]

# Feature de cantidad de capsulas


if len(capsules) != 0:
features["capsule count"] = 1/float(len(capsules))
else:
features["capsule count"]=1

# Feature de Score
features["score"] = float(state.getScore())/2000

# Feature de cantidad de Fantasmitas Asustaditos


ghostStates = successor.getGhostStates()
numOfScaredGhosts = 0
for ghostState in ghostStates:
if ghostState.scaredTimer > 0:
numOfScaredGhosts += 1
if numOfScaredGhosts != 0:
features["asustaditos"]=1/float(numOfScaredGhosts)
else:
features["asustaditos"]=1

return features

You might also like