#!/usr/bin/python ## C463 Artificial Intelligence ## Dana Vrajitoru ## An agent class for the Pacman implementing a fuzzy controller. from game import Directions from game import Agent from game import Actions from util import manhattanDistance import util import time import search import random # Implements an agent with a perfectly random behavior. class RandomAgent(Agent): """ An agent with perfectly random behavior. """ def getAction(self, state): dir = state.getLegalPacmanActions() return random.choice(dir) # Implements an agent with a behavior determined by a fuzzy logic # controller. class FuzzyAgent(Agent): """ An agent implementing a fuzzy logic controller. """ # Constructor where we define the object's attributes. We store # the past action for possible reuse in determining future # actions. def __init__(self): self.pastAction = 0 self.pastValue = 0 # The main agent function that will be called by the game to move # from one state to the next. It is supposed to return the next # direction of movement. def getAction(self, state): actions = state.getLegalPacmanActions() fuzzyVals = [] for act in actions: fuzzyVals.append(self.fuzzify(state, act)) index = self.defuzzify(actions, fuzzyVals) self.pastAction = actions[index] self.pastValue = fuzzyVals[index] return actions[index] # A function that returns a fuzzy value representing how much of a # good idea it is to go in the direction specified by the action # parameter. def fuzzify(self, state, action): # To be implemented by the student return 0 # This function takes several actions with fuzzy values associated # with them and returns a crisp decision in terms of the next # action (direction). def defuzzify(self, actions, values): # To be implemented by the student return 0