本文整理汇总了Python中util.manhattanDistance函数的典型用法代码示例。如果您正苦于以下问题:Python manhattanDistance函数的具体用法?Python manhattanDistance怎么用?Python manhattanDistance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了manhattanDistance函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: evaluationFunction
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (oldFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a GameState (pacman.py)
successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = successorGameState.getPacmanPosition()
oldFood = currentGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
"*** YOUR CODE HERE ***"
foodDistance = [manhattanDistance(newPos, foodPos) for foodPos in oldFood.asList()]
minFoodDistance = min(foodDistance)
ghostsPositions = [ghost.getPosition() for ghost in newGhostStates]
ghostsDistance = [manhattanDistance(newPos, ghostPos) for ghostPos in ghostsPositions]
minGhostDistance = min(ghostsDistance)
return 1/(minFoodDistance+0.1) - 1/(minGhostDistance+0.1)
开发者ID:pedrofelipefroes,项目名称:labia-tp2,代码行数:32,代码来源:multiAgents.py
示例2: evaluationFunction
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (newFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a GameState (pacman.py)
successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = successorGameState.getPacmanPosition()
newFood = successorGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
"*** YOUR CODE HERE ***"
Foodlist = newFood.asList()
listDis1 = []
listDis2 = []
for elem in Foodlist:
listDis1.append(1.0/util.manhattanDistance(newPos, elem))
for elem in newGhostStates:
dis = util.manhattanDistance(newPos, elem.getPosition())
if dis!=0.0 and dis<3:
listDis2.append(1.0/dis)
foodTotal = sum(listDis1)
ghostTotal = sum(listDis2)
return successorGameState.getScore()+foodTotal-28*ghostTotal
开发者ID:keroro824,项目名称:Artificial-Intelligence-,代码行数:35,代码来源:multiAgents.py
示例3: cornersHeuristic
def cornersHeuristic(state, problem):
"""
A heuristic for the CornersProblem that you defined.
state: The current search state
(a data structure you chose in your search problem)
problem: The CornersProblem instance for this layout.
This function should always return a number that is a lower bound
on the shortest path from the state to a goal of the problem; i.e.
it should be admissible (as well as consistent).
"""
corners = problem.corners # These are the corner coordinates
walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
"*** YOUR CODE HERE ***"
cornersBoolean = state[1]
currentPosition = state[0]
maxManhattan = 0
for cornerPosition, visited in cornersBoolean.iteritems():
if (visited == False) & (util.manhattanDistance(currentPosition, cornerPosition) > maxManhattan):
maxManhattan = util.manhattanDistance(currentPosition, cornerPosition)
return maxManhattan
开发者ID:quinnzshen,项目名称:cs188,代码行数:26,代码来源:searchAgents.py
示例4: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
"*** YOUR CODE HERE ***"
newPos = currentGameState.getPacmanPosition()
newFood = currentGameState.getFood()
newGhostStates = currentGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
newFoodPositions = newFood.asList()
score = 0
if newFoodPositions:
closestFoodPos, closestFoodDist = min([(foodPos,util.manhattanDistance(newPos, foodPos)) for foodPos in newFoodPositions], key = lambda x: x[1])
closestGhostPos, closestGhostDist = min([(ghostState.getPosition(), util.manhattanDistance(newPos, ghostState.getPosition()) ) for ghostState in newGhostStates], key = lambda x: x[1])
if closestFoodDist >= 2*closestGhostDist:
a = 100.0
b = 2.0
else:
a = 5.0
b = 2.0
score = (a/(closestFoodDist + 1)) - (b/(closestGhostDist + 1))
return currentGameState.getScore() + score
开发者ID:thegyro,项目名称:PacMan-Berkeley-Projects,代码行数:30,代码来源:multiAgents.py
示例5: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
"*** YOUR CODE HERE ***"
newPos = currentGameState.getPacmanPosition()
newFood = currentGameState.getFood()
newGhostStates = currentGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
ghostDis = util.manhattanDistance(newPos, newGhostStates[0].getPosition())
disFood = dict()
foodDis = 0
if (len(newFood.asList())!=0):
for f in newFood.asList():
disFood[f] = util.manhattanDistance(newPos, f)
minFood = min(disFood, key=disFood.get)
foodDis = disFood[minFood]
return -0.05*foodDis-len(newFood.asList())-2**(2-ghostDis)
#return currentGameState.getScore()
util.raiseNotDefined()
开发者ID:sean51623,项目名称:AI_pacman,代码行数:25,代码来源:multiAgents.py
示例6: evaluationFunction
def evaluationFunction(self, currentgameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
gameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (newFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a gameState (pacman.py)
successorgameState = currentgameState.generatePacmanSuccessor(action)
newPos = successorgameState.getPacmanPosition()
newFood = successorgameState.getFood()
newGhostStates = successorgameState.getGhostStates()
foodNum = currentgameState.getFood().count()
if len(newFood.asList()) == foodNum: # if this action does not eat a food
dis = BIGNUM
for pt in newFood.asList():
if manhattanDistance(pt , newPos) < dis :
dis = manhattanDistance(pt, newPos)
else:
dis = 0
for ghost in newGhostStates: # the impact of ghost surges as distance get close
dis += 4 ** (2 - manhattanDistance(ghost.getPosition(), newPos))
return -dis
开发者ID:fredzqm,项目名称:pacman,代码行数:31,代码来源:multiAgents.py
示例7: evaluationFunction
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (oldFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a GameState (pacman.py)
successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = successorGameState.getPacmanPosition()
oldFood = currentGameState.getFood().asList()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
capsules = currentGameState.getCapsules()
oldFood += capsules
ghostDistances = [manhattanDistance(newPos, ghost.getPosition()) for ghost in newGhostStates]
closestFood = min([manhattanDistance(newPos, food) for food in oldFood])
minIndex = 0
for i in range(len(ghostDistances)):
if ghostDistances[i] < ghostDistances[minIndex]:
minIndex = i
time = newScaredTimes[minIndex]
eval = time + ghostDistances[minIndex]/float(closestFood + 1)
return eval
开发者ID:mishelle123,项目名称:ArtificialIntelligence,代码行数:35,代码来源:multiAgents.py
示例8: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION:
Score more if we have less food left
Score more if the distance to all the remaining food is less
Score more if the nerest ghost is further away (up to a point, effectively ignore them once they get far enough away)
"""
food = currentGameState.getFood()
foodList = food.asList()
foodDistance = 0.001 # Make sure we don't have divide by zero
pacmanPos = currentGameState.getPacmanPosition()
for food in foodList:
foodDistance += manhattanDistance(pacmanPos, food)
minGhostDistance = None
for ghostPos in currentGameState.getGhostPositions():
ghostDistance = manhattanDistance(pacmanPos, ghostPos)
if minGhostDistance == None or minGhostDistance > ghostDistance:
minGhostDistance = ghostDistance
if minGhostDistance > 3:
minGhostDistance = 0
return currentGameState.getScore() + 10.0 * (1.0 / foodDistance) + 10.0 * (2.0 / (currentGameState.getNumFood() + 1.0)) + 0.1 * minGhostDistance
开发者ID:clive-bunting,项目名称:artificial-intelligence,代码行数:26,代码来源:multiAgents.py
示例9: evaluationFunction
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (newFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a GameState (pacman.py)
successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = successorGameState.getPacmanPosition()
newFood = successorGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
"*** YOUR CODE HERE ***"
foodList = newFood.asList()
minFoodDist, minGhostDist = 0, 0
if len(foodList) > 0:
minFoodDist, closestFood = min((manhattanDistance(newPos, food), food) for food in foodList)
if len(newGhostStates) > 0:
minGhostDist, closestGhost = min((manhattanDistance(newPos, ghost.getPosition()), ghost) for ghost in newGhostStates)
ghostScore = 0
if minGhostDist < 3 and closestGhost.scaredTimer <= 0:
ghostScore = -100
score = 1.0 / (minFoodDist + len(foodList) + 1) + ghostScore
return score + successorGameState.getScore()
开发者ID:XiaolanLin,项目名称:AI_Pacman_2,代码行数:34,代码来源:multiAgents.py
示例10: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
"*** YOUR CODE HERE ***"
# precalculamos algunas variables
pacmanPos = currentGameState.getPacmanPosition()
ghostStates = currentGameState.getGhostStates()
food = currentGameState.getFood()
# por defect retornamos el score propio del estado
score = currentGameState.getScore()
# si el estado es ganador retornamos directamente un score muy bueno
if currentGameState.isWin(): return 100 + score
# cuanta mas comida queda por comer menos score
if food.count() > 0: score += (1.0 / food.count()) * 20
# si un fantasma esta asustado nos lo comemos, sino huimos
for ghost in ghostStates:
ghostPos = ghost.getPosition()
if ghost.scaredTimer < 1:
if manhattanDistance(pacmanPos, ghostPos) == 1:
score = -100
else:
score += (1.0 / manhattanDistance(pacmanPos, ghostPos)) * 10
# cuanto mas cerca estemos de la comida mas cercana mas score
d2f = float("inf")
for f in food.asList():
d2f = min(d2f, manhattanDistance(pacmanPos, f))
score -= d2f
return score
开发者ID:septimusss,项目名称:CILN-P2,代码行数:33,代码来源:multiAgents.py
示例11: cornersHeuristic
def cornersHeuristic(state, problem):
"""
A heuristic for the CornersProblem that you defined.
state: The current search state
(a data structure you chose in your search problem)
problem: The CornersProblem instance for this layout.
This function should always return a number that is a lower bound on the
shortest path from the state to a goal of the problem; i.e. it should be
admissible (as well as consistent).
"""
corners = problem.corners
walls = problem.walls
currPos, cornersLeft = state
dist = []
weights = [ (util.manhattanDistance(currPos, corner), corner) for corner in cornersLeft ]
while weights:
curMin = min(weights, key=lambda a: a[0])
weights.remove(curMin)
dist.append(curMin[0])
weights = [ (util.manhattanDistance(curMin[1], corner), corner) for w, corner in weights ]
return sum(dist)
开发者ID:jacnik,项目名称:BerkeleyXAI,代码行数:25,代码来源:searchAgents.py
示例12: evaluationFunction
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (newFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a GameState (pacman.py)
successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = successorGameState.getPacmanPosition()
newFood = successorGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
ghostContrib = -1*min([1.0/(util.manhattanDistance(newPos, ghost.getPosition()) + 1) for ghost in newGhostStates])
foodArray = [1.0/(util.manhattanDistance(newPos, food) + 1) for food in newFood.asList()]
if not foodArray:
foodArray = [1]
foodContrib = max(foodArray)
scoreContrib = (successorGameState.getScore() - currentGameState.getScore())
value = scoreContrib + ghostContrib + foodContrib
return value
开发者ID:qswitcher,项目名称:CS188_projects,代码行数:31,代码来源:multiAgents.py
示例13: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
pos = currentGameState.getPacmanPosition()
foodGrid = currentGameState.getFood()
ghostStates = currentGameState.getGhostStates()
closet_ghost_pos = None
ghostContrib = 0
for ghost in ghostStates:
ghost_pos = util.manhattanDistance(pos, ghost.getPosition())
closet_ghost_pos = ghost_pos if not closet_ghost_pos or ghost_pos < closet_ghost_pos else closet_ghost_pos
ghostContrib = -0.5*1.0/(closet_ghost_pos + 1)
if ghost.scaredTimer:
ghostContrib *= -2
# compute food contrib
closest_food = None
closest_food_distance = None
for food in foodGrid.asList():
food_distance = util.manhattanDistance(pos, food)
if not closest_food or closest_food_distance > food_distance:
closest_food_distance = food_distance
closest_food = food
if not closest_food_distance:
closest_food_distance = 1000000
foodContrib = 1.0/closest_food_distance - 2.1*len(foodGrid.asList())
# is a wall in our way?
wall_contrib = 0
if closest_food:
if closest_food[0] < pos[0]:
if currentGameState.hasWall(pos[0] - 1, pos[1]):
wall_contrib -= 1
elif closest_food[0] > pos[0]:
if currentGameState.hasWall(pos[0] + 1, pos[1]):
wall_contrib -= 1
elif closest_food[1] < pos[1]:
if currentGameState.hasWall(pos[0], pos[1] - 1):
wall_contrib -= 1
elif closest_food[1] > pos[1]:
if currentGameState.hasWall(pos[0], pos[1] + 1):
wall_contrib -= 1
# find closest power pellet
powerPelletContrib = [1.0/util.manhattanDistance(pos, pellet) for pellet in currentGameState.getCapsules()]
if not powerPelletContrib:
powerPelletContrib = 0
else:
powerPelletContrib = 0.9*min(powerPelletContrib)
value = 0.5*wall_contrib + foodContrib + ghostContrib + 10*currentGameState.getScore() + powerPelletContrib
return value
开发者ID:qswitcher,项目名称:CS188_projects,代码行数:60,代码来源:multiAgents.py
示例14: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
"*** YOUR CODE HERE ***"
#util.raiseNotDefined()
#successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = currentGameState.getPacmanPosition()
newFood = currentGameState.getFood()
newGhostStates = currentGameState.getGhostStates()
#newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
ghostScore = 0
foodDistance = []
minFoodDistance = 10000000
for food in newFood.asList():
foodDistance.append(manhattanDistance(food, newPos))
if len(foodDistance)>=1:
minFoodDistance = min(foodDistance)
foodScore = 1.0/(100+minFoodDistance)
foodScore += 5.0/(len(newFood.asList())+1)
for ghost in newGhostStates:
dist = manhattanDistance(newPos, ghost.getPosition())
if dist <= 1 and ghost.scaredTimer <= 0:
return -100000000000
elif dist < 3 and ghost.scaredTimer > 0:
ghostScore += 1000000
return foodScore + ghostScore
开发者ID:PMX10,项目名称:projects,代码行数:32,代码来源:multiAgents+-+19+out+of+20.py
示例15: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
"*** YOUR CODE HERE ***"
newPos = currentGameState.getPacmanPosition()
newFood = currentGameState.getFood()
newGhostStates = currentGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
score = 0
min_dis = float('inf')
for pos in newFood.asList():
temp = manhattanDistance(pos, newPos)
if temp < min_dis:
min_dis = temp
if (min_dis < float('inf')):
score -= min_dis
score -= 1000*currentGameState.getNumFood()
score -= len(currentGameState.getCapsules())*10
for pos in currentGameState.getGhostPositions():
dis = manhattanDistance(pos, newPos)
if (dis <= 3): score = -float('inf')
score += currentGameState.getScore()*10
return score
开发者ID:yeahyy,项目名称:CSE-573-multiagent,代码行数:29,代码来源:multiAgents.py
示例16: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
"*** YOUR CODE HERE ***"
newPos = currentGameState.getPacmanPosition()
newFood = currentGameState.getFood().asList()
newGhostStates = currentGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
newCapsules = currentGameState.getCapsules()
score = currentGameState.getScore()
scared = sum(newScaredTimes)
dist2food = 100
if len(newFood) > 0:
dist2food = min(dist2food, min([manhattanDistance(newPos, food) for food in newFood]))
dist2food = 1.0/dist2food
dist2cap = 100
if len(newCapsules) > 0:
dist2cap = min(dist2cap, min([manhattanDistance(newPos, cap) for cap in newCapsules]))
dist2cap = 1.0/dist2cap
return score + dist2food + dist2cap + scared
开发者ID:mandary,项目名称:AI-Pacman,代码行数:32,代码来源:multiAgents.py
示例17: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
newPos = currentGameState.getPacmanPosition()
newFood = currentGameState.getFood()
newGhostStates = currentGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
farest_food = 0
nearest_food = 99999
if newFood.count() > 0:
for food in newFood.asList():
farest_food = max(farest_food, manhattanDistance(newPos, food))
nearest_food = min(nearest_food, manhattanDistance(newPos, food))
else:
nearest_food = 0
ng = 99999
fg = 0
for ghost in newGhostStates:
ng = min(ng, manhattanDistance(newPos, ghost.getPosition()))
fg = max(fg, manhattanDistance(newPos, ghost.getPosition()))
evaluation = currentGameState.getScore() - newFood.count(False) - 0.7 * nearest_food + 0.5 * ng
return evaluation
开发者ID:andersonfontes,项目名称:cs188x,代码行数:31,代码来源:multiAgents.py
示例18: evaluationFunction
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (newFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a GameState (pacman.py)
successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = successorGameState.getPacmanPosition()
newFood = successorGameState.getFood().asList()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
"*** YOUR CODE HERE ***"
dist2ghost = min([manhattanDistance(newPos, ghost.getPosition()) for ghost in newGhostStates])
dist2food = newFood and min([manhattanDistance(newPos, food) for food in newFood]) or 0
scaredScore = sum(newScaredTimes)
dist2food = (1.0/dist2food) if (dist2food) else dist2food
return successorGameState.getScore() + dist2food * dist2ghost + scaredScore
开发者ID:mandary,项目名称:AI-Pacman,代码行数:30,代码来源:multiAgents.py
示例19: evaluationFunction
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (newFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a GameState (pacman.py)
successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = successorGameState.getPacmanPosition()
newFood = successorGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
food=[]
for x in newFood.asList():
food.append(manhattanDistance(newPos,x))
score=0
if len(food)>0 :
score=1/sum(food)+5/min(food)+successorGameState.getScore()
for x in newGhostStates:
if manhattanDistance(newPos,x.getPosition())<2:
return -10
if score > 0:
return score
return successorGameState.getScore()
开发者ID:jmanalus,项目名称:CS-188,代码行数:34,代码来源:multiAgents.py
示例20: betterEvaluationFunction
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
I want to incentivize winning so I return infinity if that is a win state.
A large part of my strategy was "don't die." Dying gives you usually ~300 or less points, which will hurt your average. Winning usually gives
you more points than losing.
Here are the things I considered, along with some commentary on my decision making regarding them:1
distance to the closest ghost: (disttoghost): I wanted pacman to run away from ghosts if they got too close. I did this
by doing score += max(disttoghost, 4) * 2. If the ghost is more than distance 4 away, I didn't really care, and there is no difference between
the ghost being 5 away and 500 away, because it's not close enough to threaten pacman. I added the max because my pacman would sometimes try to get
farther away from a ghost already a long way away instead of going for food. That led to a lot of pacman idling in corners, wasting time and points. I added
the * 2 so that the penalty for being near a ghost would be more severe and hopefully cause pacman to tend away from getting too close.
closest food: I wanted to reward heading towards food, but not to the extent that it overrode the penalty of getting too close to a ghost. Thus, the score
will be higher the closer pacman goes towards a food. By subtracting 1.5 * the distance, I got further distances to food to receive lower scores. This, however,
introduced a problem where sometimes pacman would refuse to eat an isolated food because that would make the next turn's distance to food much higher. Thus, I had
to add the next thing.
bonus for eating food: I subtracted 4 times the number of remaining foods, so that eating food would cause a more markedly higher score. You can only eat one food
per move, so this made eating food preferable over not eating food.
capsules: I thought that eating ghosts would increase my score, so I tried to slightly incentivize moving onto a capsule so that eating ghosts could increase my score.
Thus, I subtracted 3.5 from scores for each existing capsule. This would only make a difference near the capsule.
I then watched pacman's actions and adjusted the weights and numbers to visible strategic flaws.
When I last submitted this, this averaged slightly over 1000. Let's hope it does so again.
"""
"*** YOUR CODE HERE ***"
if currentGameState.isWin():
return float("inf")
if currentGameState.isLose():
return -float("inf")
score = scoreEvaluationFunction(currentGameState)
newFood = currentGameState.getFood()
foodPos = newFood.asList()
closestfood = float("inf")
for pos in foodPos:
thisdist = util.manhattanDistance(pos, currentGameState.getPacmanPosition())
if thisdist < closestfood:
closestfood = thisdist
numghosts = currentGameState.getNumAgents() - 1
i = 1
disttoghost = float("inf")
while i <= numghosts:
nextdist = util.manhattanDistance(currentGameState.getPacmanPosition(), currentGameState.getGhostPosition(i))
disttoghost = min(disttoghost, nextdist)
i += 1
score += max(disttoghost, 4) * 2
score -= closestfood * 1.5
capsulelocations = currentGameState.getCapsules()
score -= 4 * len(foodPos)
score -= 3.5 * len(capsulelocations)
return score
开发者ID:douglaschan32167,项目名称:multiagent,代码行数:60,代码来源:multiAgents.py
注:本文中的util.manhattanDistance函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论