Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
migration to python3
  • Loading branch information
johnnynunez committed Apr 28, 2026
commit e6ce94e8c749261c632aba10a55a78f6e32e7f75
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
<<<<<<< HEAD
# PacmanAI
# UC-Berkeley-AI-Pacman-Project

Project Link :
http://ai.berkeley.edu/project_overview.html
Artificial Intelligence project designed by UC Berkeley to develop game agents for Pacman using search algorithms and reinforcement learning.

Sections Of the Project Covered are:
Project link: http://ai.berkeley.edu/project_overview.html

Search:
Implement depth-first, breadth-first, uniform cost, and A* search algorithms. These algorithms are used to solve navigation and traveling salesman problems in the Pacman world.
## Sections

- **Search**: Implement depth-first, breadth-first, uniform cost, and A* search algorithms. These algorithms are used to solve navigation and traveling salesman problems in the Pacman world.
- **Multi-Agent Search**: Classic Pacman is modeled as both an adversarial and a stochastic search problem. Implement multi-agent minimax and expectimax algorithms, as well as designing evaluation functions.
- **Reinforcement Learning**: Implement model-based and model-free reinforcement learning algorithms, applied to the AIMA textbook's Gridworld, Pacman, and a simulated crawling robot.
- **Ghostbusters**: Probabilistic inference in a hidden Markov model tracks the movement of hidden ghosts in the Pacman world. Implement exact inference using the forward algorithm and approximate inference via particle filters.

Multi-Agent Search:
Classic Pacman is modeled as both an adversarial and a stochastic search problem. Implement multiagent minimax and expectimax algorithms, as well as designing evaluation functions.
## Python 3

This repository has been migrated to Python 3 (tested with CPython 3.12). A `uv`-managed virtual environment lives in `.berkeley/`.

Reinforcement Learning:
Implement model-based and model-free reinforcement learning algorithms, applied to the AIMA textbook's Gridworld, Pacman, and a simulated crawling robot.
```bash
# Activate the bundled environment
source .berkeley/bin/activate

# Or invoke the interpreter directly
.berkeley/bin/python search/pacman.py
```

Ghostbusters:
Probabilistic inference in a hidden Markov model tracks the movement of hidden ghosts in the Pacman world. Implement exact inference using the forward algorithm and approximate inference via particle filters.
=======
# UC-Berkeley-AI-Pacman-Project
Artificial Intelligence project designed by UC Berkeley to develop game agents for Pacman using search algorithms and reinforcement learning
>>>>>>> b747ae092562a96c8c2ac70466fc10707a0f6865
Run any of the autograders from inside its project directory, e.g.:

```bash
cd search && ../.berkeley/bin/python autograder.py
```
39 changes: 21 additions & 18 deletions multiagent/autograder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

# imports from python standard library
import grading
import imp
import importlib.util
import types as _types
import optparse
import os
import re
Expand Down Expand Up @@ -82,16 +83,16 @@ def readCommand(argv):

# confirm we should author solution files
def confirmGenerate():
print 'WARNING: this action will overwrite any solution files.'
print 'Are you sure you want to proceed? (yes/no)'
print('WARNING: this action will overwrite any solution files.')
print('Are you sure you want to proceed? (yes/no)')
while True:
ans = sys.stdin.readline().strip()
if ans == 'yes':
break
elif ans == 'no':
sys.exit(0)
else:
print 'please answer either "yes" or "no"'
print('please answer either "yes" or "no"')


# TODO: Fix this so that it tracebacks work correctly
Expand Down Expand Up @@ -122,16 +123,18 @@ def loadModuleString(moduleSource):
#
#f = StringIO(moduleCodeDict[k])
#tmp = imp.load_module(k, f, k, (".py", "r", imp.PY_SOURCE))
tmp = imp.new_module(k)
exec moduleCodeDict[k] in tmp.__dict__
tmp = _types.ModuleType(k)
exec(moduleCodeDict[k], tmp.__dict__)
setModuleName(tmp, k)
return tmp

import py_compile

def loadModuleFile(moduleName, filePath):
with open(filePath, 'r') as f:
return imp.load_module(moduleName, f, "%s.py" % moduleName, (".py", "r", imp.PY_SOURCE))
spec = importlib.util.spec_from_file_location(moduleName, filePath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module


def readFile(path, root=""):
Expand Down Expand Up @@ -184,12 +187,12 @@ def splitStrings(d):

def printTest(testDict, solutionDict):
pp = pprint.PrettyPrinter(indent=4)
print "Test case:"
print("Test case:")
for line in testDict["__raw_lines__"]:
print " |", line
print "Solution:"
print(" |", line)
print("Solution:")
for line in solutionDict["__raw_lines__"]:
print " |", line
print(" |", line)


def runTest(testName, moduleDict, printTestCase=False, display=None):
Expand Down Expand Up @@ -233,7 +236,7 @@ def getTestSubdirs(testParser, testRoot, questionToGrade):
if questionToGrade != None:
questions = getDepends(testParser, testRoot, questionToGrade)
if len(questions) > 1:
print 'Note: due to dependencies, the following tests will be run: %s' % ' '.join(questions)
print('Note: due to dependencies, the following tests will be run: %s' % ' '.join(questions))
return questions
if 'order' in problemDict:
return problemDict['order'].split()
Expand Down Expand Up @@ -265,8 +268,8 @@ def evaluate(generateSolutions, testRoot, moduleDict, exceptionMap=ERROR_HINT_MA
questionDicts[q] = questionDict

# load test cases into question
tests = filter(lambda t: re.match('[^#~.].*\.test\Z', t), os.listdir(subdir_path))
tests = map(lambda t: re.match('(.*)\.test\Z', t).group(1), tests)
tests = [t for t in os.listdir(subdir_path) if re.match(r'[^#~.].*\.test\Z', t)]
tests = [re.match(r'(.*)\.test\Z', t).group(1) for t in tests]
for t in sorted(tests):
test_file = os.path.join(subdir_path, '%s.test' % t)
solution_file = os.path.join(subdir_path, '%s.solution' % t)
Expand Down Expand Up @@ -331,16 +334,16 @@ def getDisplay(graphicsByDefault, options=None):
codePaths = options.studentCode.split(',')
# moduleCodeDict = {}
# for cp in codePaths:
# moduleName = re.match('.*?([^/]*)\.py', cp).group(1)
# moduleName = re.match(r'.*?([^/]*)\.py', cp).group(1)
# moduleCodeDict[moduleName] = readFile(cp, root=options.codeRoot)
# moduleCodeDict['projectTestClasses'] = readFile(options.testCaseCode, root=options.codeRoot)
# moduleDict = loadModuleDict(moduleCodeDict)

moduleDict = {}
for cp in codePaths:
moduleName = re.match('.*?([^/]*)\.py', cp).group(1)
moduleName = re.match(r'.*?([^/]*)\.py', cp).group(1)
moduleDict[moduleName] = loadModuleFile(moduleName, os.path.join(options.codeRoot, cp))
moduleName = re.match('.*?([^/]*)\.py', options.testCaseCode).group(1)
moduleName = re.match(r'.*?([^/]*)\.py', options.testCaseCode).group(1)
moduleDict['projectTestClasses'] = loadModuleFile(moduleName, os.path.join(options.codeRoot, options.testCaseCode))


Expand Down
38 changes: 19 additions & 19 deletions multiagent/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Directions:
WEST: SOUTH,
STOP: STOP}

RIGHT = dict([(y,x) for x, y in LEFT.items()])
RIGHT = dict([(y,x) for x, y in list(LEFT.items())])

REVERSE = {NORTH: SOUTH,
SOUTH: NORTH,
Expand Down Expand Up @@ -265,7 +265,7 @@ def _unpackBits(self, bits):

def _unpackInt(self, packed, size):
bools = []
if packed < 0: raise ValueError, "must be a positive integer"
if packed < 0: raise ValueError("must be a positive integer")
for i in range(size):
n = 2 ** (self.CELLS_PER_INT - i - 1)
if packed >= n:
Expand Down Expand Up @@ -296,7 +296,7 @@ class Actions:
Directions.WEST: (-1, 0),
Directions.STOP: (0, 0)}

_directionsAsList = _directions.items()
_directionsAsList = list(_directions.items())

TOLERANCE = .001

Expand Down Expand Up @@ -428,8 +428,8 @@ def __hash__( self ):
for i, state in enumerate( self.agentStates ):
try:
int(hash(state))
except TypeError, e:
print e
except TypeError as e:
print(e)
#hash(state)
return int((hash(tuple(self.agentStates)) + 13*hash(self.food) + 113* hash(tuple(self.capsules)) + 7 * hash(self.score)) % 1048575 )

Expand Down Expand Up @@ -529,8 +529,8 @@ def __init__( self, agents, display, rules, startingIndex=0, muteAgents=False, c
self.totalAgentTimes = [0 for agent in agents]
self.totalAgentTimeWarnings = [0 for agent in agents]
self.agentTimeout = False
import cStringIO
self.agentOutput = [cStringIO.StringIO() for agent in agents]
import io
self.agentOutput = [io.StringIO() for agent in agents]

def getProgress(self):
if self.gameOver:
Expand All @@ -551,7 +551,7 @@ def _agentCrash( self, agentIndex, quiet=False):
def mute(self, agentIndex):
if not self.muteAgents: return
global OLD_STDOUT, OLD_STDERR
import cStringIO
import io
OLD_STDOUT = sys.stdout
OLD_STDERR = sys.stderr
sys.stdout = self.agentOutput[agentIndex]
Expand Down Expand Up @@ -580,7 +580,7 @@ def run( self ):
self.mute(i)
# this is a null agent, meaning it failed to load
# the other team wins
print >>sys.stderr, "Agent %d failed to load" % i
print("Agent %d failed to load" % i, file=sys.stderr)
self.unmute()
self._agentCrash(i, quiet=True)
return
Expand All @@ -595,12 +595,12 @@ def run( self ):
time_taken = time.time() - start_time
self.totalAgentTimes[i] += time_taken
except TimeoutFunctionException:
print >>sys.stderr, "Agent %d ran out of time on startup!" % i
print("Agent %d ran out of time on startup!" % i, file=sys.stderr)
self.unmute()
self.agentTimeout = True
self._agentCrash(i, quiet=True)
return
except Exception,data:
except Exception as data:
self._agentCrash(i, quiet=False)
self.unmute()
return
Expand Down Expand Up @@ -630,7 +630,7 @@ def run( self ):
skip_action = True
move_time += time.time() - start_time
self.unmute()
except Exception,data:
except Exception as data:
self._agentCrash(agentIndex, quiet=False)
self.unmute()
return
Expand All @@ -652,7 +652,7 @@ def run( self ):
raise TimeoutFunctionException()
action = timed_func( observation )
except TimeoutFunctionException:
print >>sys.stderr, "Agent %d timed out on a single move!" % agentIndex
print("Agent %d timed out on a single move!" % agentIndex, file=sys.stderr)
self.agentTimeout = True
self._agentCrash(agentIndex, quiet=True)
self.unmute()
Expand All @@ -662,9 +662,9 @@ def run( self ):

if move_time > self.rules.getMoveWarningTime(agentIndex):
self.totalAgentTimeWarnings[agentIndex] += 1
print >>sys.stderr, "Agent %d took too long to make a move! This is warning %d" % (agentIndex, self.totalAgentTimeWarnings[agentIndex])
print("Agent %d took too long to make a move! This is warning %d" % (agentIndex, self.totalAgentTimeWarnings[agentIndex]), file=sys.stderr)
if self.totalAgentTimeWarnings[agentIndex] > self.rules.getMaxTimeWarnings(agentIndex):
print >>sys.stderr, "Agent %d exceeded the maximum number of warnings: %d" % (agentIndex, self.totalAgentTimeWarnings[agentIndex])
print("Agent %d exceeded the maximum number of warnings: %d" % (agentIndex, self.totalAgentTimeWarnings[agentIndex]), file=sys.stderr)
self.agentTimeout = True
self._agentCrash(agentIndex, quiet=True)
self.unmute()
Expand All @@ -673,13 +673,13 @@ def run( self ):
self.totalAgentTimes[agentIndex] += move_time
#print "Agent: %d, time: %f, total: %f" % (agentIndex, move_time, self.totalAgentTimes[agentIndex])
if self.totalAgentTimes[agentIndex] > self.rules.getMaxTotalTime(agentIndex):
print >>sys.stderr, "Agent %d ran out of time! (time: %1.2f)" % (agentIndex, self.totalAgentTimes[agentIndex])
print("Agent %d ran out of time! (time: %1.2f)" % (agentIndex, self.totalAgentTimes[agentIndex]), file=sys.stderr)
self.agentTimeout = True
self._agentCrash(agentIndex, quiet=True)
self.unmute()
return
self.unmute()
except Exception,data:
except Exception as data:
self._agentCrash(agentIndex)
self.unmute()
return
Expand All @@ -692,7 +692,7 @@ def run( self ):
if self.catchExceptions:
try:
self.state = self.state.generateSuccessor( agentIndex, action )
except Exception,data:
except Exception as data:
self.mute(agentIndex)
self._agentCrash(agentIndex)
self.unmute()
Expand Down Expand Up @@ -722,7 +722,7 @@ def run( self ):
self.mute(agentIndex)
agent.final( self.state )
self.unmute()
except Exception,data:
except Exception as data:
if not self.catchExceptions: raise
self._agentCrash(agentIndex)
self.unmute()
Expand Down
Loading