一、项目介绍
- 项目介绍网页
- 项目代码下载
- 本项目是采用Berkeley的CS188课程内容实习二的内容,在这个项目中,我们将为经典版本的Pacman 设计自动算法,包括幽灵。在此过程中,我们将实现 minimax 和 expectimax 搜索并尝试评估函数设计
- 完成作业只需要完成5个题目,按照项目介绍的步骤进行完成,主要是在multiAgents.py文件中进行补充代码
二、代码详情
1、Question1:Reflex Agent
略
2、Question2:Minimax
- 套用ppt上的minmax算法伪代码。
- pacman作为max玩家,要在各ghost做出对于ghost最优的action下找到最优应对。
- 核心的是min_value和max_value函数互相递归调用。
- 麻烦的是本游戏中ghost可能不止一个,min_value函数中我采用递归的方式先得到每个ghost各走一步后的所有state,并对每个state评max_value,选出最小者。
- 另外要注意pacman不应该采取"stop"动作,否则无法通过测试(因为autograder认为总有比stop更优的解)
class MinimaxAgent(MultiAgentSearchAgent):
"""
Your minimax agent (question 2)
"""
def getAction(self, gameState):
"""
Returns the minimax action from the current gameState using self.depth
and self.evaluationFunction.
Here are some method calls that might be useful when implementing minimax.
gameState.getLegalActions(agentIndex):
Returns a list of legal actions for an agent
agentIndex=0 means Pacman, ghosts are >= 1
gameState.generateSuccessor(agentIndex, action):
Returns the successor game state after an agent takes an action
gameState.getNumAgents():
Returns the total number of agents in the game
gameState.isWin():
Returns whether or not the game state is a winning state
gameState.isLose():
Returns whether or not the game state is a losing state
"""
"*** YOUR CODE HERE ***"
GhostIndex = [i for i in range(1, gameState.getNumAgents())]
#pacman作为max玩家,要在各ghost做出对于ghost最优的action下找到最优应对。
#核心的minvalue和maxvalue函数互相递归调用。
#麻烦的是本游戏中ghost可能不止一个,
#minvalue函数中我采用递归的方式先得到每个ghost各走一步后的所有state,并对每个state评maxvalue,选出最小者。
# 根据当前状态,判断游戏是否胜利或者结束
def term(state, d):
return state.isWin() or state.isLose() or d == self.depth
# 采用递归方式把ghost每个走过的状态检查一遍
def min_value(state, d, ghost): # minimizer
if term(state, d):
return self.evaluationFunction(state)
v = 10000000000000000 # β初始值为无穷大
for action in state.getLegalActions(ghost):
if ghost == GhostIndex[-1]: # 递归的查找最小值,如果ghost==ghostindex[-1]的话,则在大数中进行查找,反之
v = min(v, max_value(state.generateSuccessor(ghost, action), d + 1))
else:
v = min(v, min_value(state.generateSuccessor(ghost, action), d, ghost + 1))
# print(v)
return v
def max_value(state, d): # maximizer
if term(state, d):
return self.evaluationFunction(state)
v = -10000000000000000 # α初始值为无穷小
for action in state.getLegalActions(0):
# 递归查找最大值
v = max(v, min_value(state.generateSuccessor(0, action), d, 1))
# print(v)
return v
res = [(action, min_value(gameState.generateSuccessor(0, action), 0, 1)

本文介绍了如何利用minimax, alpha-beta pruning和expectimax算法为经典版Pacman设计自动策略,包括对幽灵的处理。通过实例展示了代码实现和优化,以及不同算法在时间效率和胜率上的对比。重点在于评分函数设计和算法性能评估。
最低0.47元/天 解锁文章
1667

被折叠的 条评论
为什么被折叠?



