Berkeley Intro to AI学习笔记(一)MultiSearch

从今天开始,记录一下这门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
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值