Coder Social home page Coder Social logo

chiragradhakrishna43-7 / pacman_multiagents Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 110 KB

Pacman games with multi agents. Evaluating the performance of Pacman and the ghosts.

Python 100.00%
minimax-algorithm adverserial-search multi-agent-path-finding reward-shaping

pacman_multiagents's Introduction

Pacman_MultiAgents

Pacman games with multi agents. Evaluating the performance of Pacman and the ghosts.

Attribution Information: The Pacman AI projects were developed at UC Berkeley. The core projects and autograders were primarily created by John DeNero ([email protected]) and Dan Klein ([email protected]). Student side autograding was added by Brad Miller, Nick Hay, and Pieter Abbeel ([email protected]).

Task: Minimax Agent


FUNCTION CODE:
class MinimaxAgent(MultiAgentSearchAgent):
def getAction(self, gameState):
def maxValue_fun(state, player_index, depth):
if state.isWin() or state.isLose() or depth == self.depth:
return scoreEvaluationFunction(state), None
alpha = float('-inf')
best_actn = None
for i in state.getLegalActions(player_index):
successor = state.generateSuccessor(player_index, i)
next_index = (player_index+1) % state.getNumAgents()
val = minValue_fun(successor, next_index, depth)
if val > alpha:
alpha = val
best_actn = i
return alpha, best_actn
def minValue_fun(state, player_index, depth):
if state.isWin() or state.isLose() or depth == self.depth:
return scoreEvaluationFunction(state)
beta = float('inf')
for j in state.getLegalActions(player_index):
successor = state.generateSuccessor(player_index, j)
next_index = (player_index+1) % state.getNumAgents()
if next_index == 0:
beta = min(beta, maxValue_fun(successor, next_index, depth+1) [0])
else:
beta = min(beta, minValue_fun(successor, next_index, depth))
return beta

bst_score, bst_actn = maxValue_fun(gameState, 0, 0)
return bst_actn

------------------------------------------------------------------------------------

COMMENTS:

We have implemented two functions, maxValue_fun and minValue_fun for the required minimax problem. The maxValue_fun function helps maximize Pacman’s turn in the game as part of the minimax algorithm. The maxValue_fun identifies and iterates over all the legal actions possible by making a function call to the minValue_fun function. The alpha value and best action variables are regularly updated if a higher value is found.The minValue_fun minimizes the player’s turn in the game sequence (here ghosts). It goes over all possible actions for the current state and computes the value by recursively either calling the maxValue_fun (if the next player is Pacman) or itself (if the next player is the ghost). Ultimately, it returns the minimum value found for the current state. Finally, we pass the gameState and the root node to the maxValue_fun to obtain the best action for Pacman.

How does the “max” function work?
The max function essentially helps obtain the maximum value and the best action for Pacman to take. If the current state examined is the win state/loss state or if we have reached the maximum depth, then we just return the score for that state. The function examines all possible legal actions for the player and calls the min for each successor that is generated. It keeps track of the maximum value and ultimately returns it.

How does the “min” function work?
Like the max function, the min function also examines each possible action that can be taken from the current state. The function makes recursive calls to the max function or itself based on the next player move. It keeps track and returns the minimum value.

How to recursively call the “max” function and “min” function?
Both functions make recursive calls to each other/themselves respectively based on the move in the minimax algorithm.
In function max:
val = minValue_fun(successor, next_index, depth)
The max function calls min to minimize the opponents’ (ghosts’) moves.
In function min:
if next_index == 0:
beta = min(beta, maxValue_fun(successor, next_index, depth+1) [0])
else:
beta = min(beta, minValue_fun(successor, next_index, depth))

Min function calls max to maximize Pacman’s moves. It calls itself when the move is to be played by the ghosts.

How is depth=4 reached?
We increment the depth during a recursive call. The depth will be incremented until the desired depth is reached. Once we reach the desired depth, we evaluate the score at that state.
min(beta, maxValue_fun(successor, next_index, depth+1) [0])

How does the root node return the best action?
bst_score, bst_actn = maxValue_fun(gameState, 0, 0)
We call the max function on the initial game state, the starting index of Pacman and depth 0. It will assist the minimax agent to return the best action for Pacman.

How do you set the PlayerIndex for Pacman and for three ghosts?
The player_index and next_index variable helps keep track of the turn of Pacman and the ghosts. Pacman’s player index is set to 0. The index for each ghost is determined by the number of agents in the game. When depth<4, how to go from the 3rd ghost to the next player-MAX, and at the same time increase the depth by 1?
if next_index == 0:
beta = min(beta, maxValue_fun(successor, next_index, depth+1) [0])
else:
beta = min(beta, minValue_fun(successor, next_index, depth))

The above code snippet will determine if the player after the current ghost is Pacman. If it is Pacman, we call max with an incremented depth value. Basically, we are maximizing Pacman’s turn. This increases the depth by 1 while we move from the 3rd ghost to P.

pacman_multiagents's People

Contributors

chiragradhakrishna43-7 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.