从今天开始,记录一下这门CBU的神课。
一方面是准备加入电子化笔记大军,另一方面嘛
是因为觉得课程视屏太枯燥(啰嗦)了,就直接从大作业写起,
为了防止忘记、便于查询,就开这个专题笔记记录一下吧。
好了,废话不多说,直接开始正题
由于Project1基本和AI没什么太大关系,就是简单的搜索算法,那么就直接从P2开始。
第一题也没什么内容,就跳过不叙述好咯。
看第二题
**
Minmax
**
(图片来自:https://blog.csdn.net/witnessai1/article/details/78377544)
这道题考察极大极小化算法
说白了,就是要考虑不同阵营的策略
假如你在一场竞赛中,
我方数值化一个评分,不妨将策略看做最大化这个数值
那么敌方就要最小化这个数值
我们可以通过选择不同的预测深度(比如你来我往一次,记为深度=1)来实现这个算法,完成比赛的策略转移。
如上图所示,我们如何选择我们的策略,当然是从敌人的策略开始,估计敌人的最小化策略选择(最底一层选择最小数值),然后以此比较选出最大化数值。
这作为一层。
根据这个思路不难写出代码,github上面也可以找参考(CS188)
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
Directions.STOP:
The stop direction, which is always legal
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
"""
"*** YOUR CODE HERE ***"
curDepth = 0
currentAgentIndex = 0
val = self.value(gameState, currentAgentIndex, curDepth)
print "Returning %s" % str(val)
return val[0]
def value(self, gameState, currentAgentIndex, curDepth):
if currentAgentIndex >= gameState.getNumAgents():
currentAgentIndex = 0
curDepth += 1
if curDepth == self.depth:
return self.evaluationFunction(gameState)
if currentAgentIndex == self.pacmanIndex:
return self.maxValue(gameState, currentAgentIndex, curDepth)
else:
return self.minValue(gameState, currentAgentIndex, curDepth)
def minValue(self, gameState, currentAgentIndex, curDepth):
v = ("unknown", float("inf"))
if not gameState.getLegalActions(currentAgentIndex):
return self.evaluationFunction(gameState)
for action in gameState.getLegalActions(currentAgentIndex):
if action == "Stop":
continue
retVal = self.value(gameState.generateSuccessor(currentAgentIndex, action), currentAgentIndex + 1, curDepth)
if type(retVal) is tuple:
retVal = retVal