資源簡介
使用A*,a-b剪枝等搜索算法,求解吃豆人的一些簡單問題。

代碼片段和文件信息
#?multiAgents.py
#?--------------
#?Licensing?Information:??You?are?free?to?use?or?extend?these?projects?for
#?educational?purposes?provided?that?(1)?you?do?not?distribute?or?publish
#?solutions?(2)?you?retain?this?notice?and?(3)?you?provide?clear
#?attribution?to?UC?Berkeley?including?a?link?to?http://ai.berkeley.edu.
#?
#?Attribution?Information:?The?Pacman?AI?projects?were?developed?at?UC?Berkeley.
#?The?core?projects?and?autograders?were?primarily?created?by?John?DeNero
#?(denero@cs.berkeley.edu)?and?Dan?Klein?(klein@cs.berkeley.edu).
#?Student?side?autograding?was?added?by?Brad?Miller?Nick?Hay?and
#?Pieter?Abbeel?(pabbeel@cs.berkeley.edu).
from?util?import?manhattanDistance
from?game?import?Directions
import?random?util
from?game?import?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()
????????n
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????22973??2017-10-22?13:12??searchAgents.py
?????文件??????12043??2017-10-22?13:13??multiAgents.py
?????文件???????5320??2017-10-22?13:12??search.py
-----------?---------??----------?-----??----
????????????????40336????????????????????3
- 上一篇:license_R2015b.lic
- 下一篇:C數據結構最小生成樹的構造
評論
共有 條評論