CS188 项目二

Question 1:Reflex Agent

class ReflexAgent(Agent):
    """
    A reflex agent chooses an action at each choice point by examining
    its alternatives via a state evaluation function.

    The code below is provided as a guide.  You are welcome to change
    it in any way you see fit, so long as you don't touch our method
    headers.
    """


    def getAction(self, gameState):
        """
        You do not need to change this method, but you're welcome to.

        getAction chooses among the best options according to the evaluation function.

        Just like in the previous project, getAction takes a GameState and returns
        some Directions.X for some X in the set {NORTH, SOUTH, WEST, EAST, STOP}
        """
        # Collect legal moves and successor states
        legalMoves = gameState.getLegalActions()

        # Choose one of the best actions
        scores = [self.evaluationFunction(gameState, action) for action in legalMoves]
        bestScore = max(scores)
        bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore]
        chosenIndex = random.choice(bestIndices) # Pick randomly among the best

        "Add more of your code here if you want to"

        return legalMoves[chosenIndex]

    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 ***"
        # return Value [-1,1]

        newFood = newFood.asList()
        ghostPos = [(G.getPosition()[0], G.getPosition()[1]) for G in newGhostStates]
        scared = min(newScaredTimes) > 0

        # if not new ScaredTimes new state is ghost: return lowest value

        if not scared and (newPos in ghostPos):
            return -1.0

        if newPos in currentGameState.getFood().asList():
            return 1

        closestFoodDist = sorted(newFood, key=lambda fDist: util.manhattanDistance(fDist, newPos))
        cl
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
CS188项目2是一个与人工智能相关的项目,目标是实现一个基于搜索算法的迷宫问题求解器。 迷宫问题是在一个维网格中寻找从起点到终点的最短路径问题。在这个项目中,我们需要设计和实现用于解决迷宫问题的算法。 首先,我们需要定义迷宫的数据结构。迷宫可以表示为一个维数组,其中每个元素表示迷宫的一个方块,包括墙壁、通路和起点终点等。我们需要编写一个函数来读取迷宫的输入文件,并将其转换为我们定义的数据结构。 接下来,我们需要实现搜索算法来找到从起点到终点的最短路径。基本的搜索算法包括深度优先搜索(Depth First Search, DFS)和广度优先搜索(Breadth First Search, BFS)。这些算法可以通过递归或队列来实现,逐步探索迷宫的可能路径并找到最短路径。 在计算机科学中,搜索算法中的另一个重要概念是启发式搜索(Heuristic Search)。启发式搜索基于一个启发函数,通过评估每个可能路径的潜在价值来指导搜索过程。在这个项目中,我们可以使用A*搜索算法来求解迷宫问题。A*算法综合考虑了起点到当前位置的实际移动成本和当前位置到终点的估计成本,以选择下一个最有可能的路径。 最后,我们要评估实现的算法在不同迷宫问题上的性能。我们可以使用一些标准的迷宫问题来验证算法的正确性和效率,比如小型迷宫、大型迷宫等等。我们可以通过比较算法找到的最短路径长度和实际最短路径长度来评估算法的正确性,也可以通过计算算法的运行时间来评估算法的效率。 总之,CS188项目2是一个关于迷宫问题求解器的项目。通过实现搜索算法和评估算法性能,我们可以提高我们在处理类似问题时的问题求解能力和性能优化能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I will be here

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值