<s> import testClasses <EOL> import random <EOL> import math <EOL> import traceback <EOL> import sys <EOL> import os <EOL> import layout <EOL> import textDisplay <EOL> import pacman <EOL> import gridworld <EOL> import time <EOL> from util import Counter , TimeoutFunction , FixedRandom <EOL> from collections import defaultdict <EOL> from pprint import PrettyPrinter <EOL> from hashlib import sha1 <EOL> from functools import reduce <EOL> pp = PrettyPrinter ( ) <EOL> VERBOSE = False <EOL> import gridworld <EOL> LIVINGREWARD = - <NUM_LIT:0.1> <EOL> NOISE = <NUM_LIT> <EOL> class ValueIterationTest ( testClasses . TestCase ) : <EOL> def __init__ ( self , question , testDict ) : <EOL> super ( ValueIterationTest , self ) . __init__ ( question , testDict ) <EOL> self . discount = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . grid = gridworld . Gridworld ( parseGrid ( testDict [ '<STR_LIT>' ] ) ) <EOL> iterations = int ( testDict [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in testDict : <EOL> self . grid . setNoise ( float ( testDict [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in testDict : <EOL> self . grid . setLivingReward ( float ( testDict [ '<STR_LIT>' ] ) ) <EOL> maxPreIterations = <NUM_LIT:10> <EOL> self . numsIterationsForDisplay = range ( <EOL> min ( iterations , maxPreIterations ) ) <EOL> self . testOutFile = testDict [ '<STR_LIT>' ] <EOL> if maxPreIterations < iterations : <EOL> self . numsIterationsForDisplay . append ( iterations ) <EOL> def writeFailureFile ( self , string ) : <EOL> with open ( self . testOutFile , '<STR_LIT:w>' ) as handle : <EOL> handle . write ( string ) <EOL> def removeFailureFileIfExists ( self ) : <EOL> if os . path . exists ( self . testOutFile ) : <EOL> os . remove ( self . testOutFile ) <EOL> def execute ( self , grades , moduleDict , solutionDict ) : <EOL> failureOutputFileString = '<STR_LIT>' <EOL> failureOutputStdString = '<STR_LIT>' <EOL> for n in self . numsIterationsForDisplay : <EOL> checkPolicy = ( n == self . numsIterationsForDisplay [ - <NUM_LIT:1> ] ) <EOL> testPass , stdOutString , fileOutString = self . executeNIterations ( <EOL> grades , moduleDict , solutionDict , n , checkPolicy ) <EOL> failureOutputStdString += stdOutString <EOL> failureOutputFileString += fileOutString <EOL> if not testPass : <EOL> self . addMessage ( failureOutputStdString ) <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> self . testOutFile ) <EOL> self . writeFailureFile ( failureOutputFileString ) <EOL> return self . testFail ( grades ) <EOL> self . removeFailureFileIfExists ( ) <EOL> return self . testPass ( grades ) <EOL> def executeNIterations ( self , grades , moduleDict , <EOL> solutionDict , n , checkPolicy ) : <EOL> testPass = True <EOL> valuesPretty , qValuesPretty , actions , policyPretty = self . runAgent ( <EOL> moduleDict , n ) <EOL> stdOutString = '<STR_LIT>' <EOL> fileOutString = '<STR_LIT>' <EOL> valuesKey = "<STR_LIT>" % n <EOL> if self . comparePrettyValues ( valuesPretty , solutionDict [ valuesKey ] ) : <EOL> fileOutString += "<STR_LIT>" % n <EOL> fileOutString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> valuesKey , <EOL> valuesPretty ) <EOL> else : <EOL> testPass = False <EOL> outString = "<STR_LIT>" % n <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> valuesKey , <EOL> valuesPretty ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> valuesKey , <EOL> solutionDict [ valuesKey ] ) <EOL> stdOutString += outString <EOL> fileOutString += outString <EOL> for action in actions : <EOL> qValuesKey = '<STR_LIT>' % ( n , action ) <EOL> qValues = qValuesPretty [ action ] <EOL> if self . comparePrettyValues ( qValues , solutionDict [ qValuesKey ] ) : <EOL> fileOutString += "<STR_LIT>" % ( <EOL> n , <EOL> action ) <EOL> fileOutString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> qValues ) <EOL> else : <EOL> testPass = False <EOL> outString = "<STR_LIT>" % ( <EOL> n , <EOL> action ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> qValues ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> solutionDict [ qValuesKey ] ) <EOL> stdOutString += outString <EOL> fileOutString += outString <EOL> if checkPolicy : <EOL> if not self . comparePrettyValues ( policyPretty , solutionDict [ '<STR_LIT>' ] ) : <EOL> testPass = False <EOL> outString = "<STR_LIT>" <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> policyPretty ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> solutionDict [ '<STR_LIT>' ] ) <EOL> stdOutString += outString <EOL> fileOutString += outString <EOL> return testPass , stdOutString , fileOutString <EOL> def writeSolution ( self , moduleDict , filePath ) : <EOL> with open ( filePath , '<STR_LIT:w>' ) as handle : <EOL> policyPretty = '<STR_LIT>' <EOL> actions = [ ] <EOL> for n in self . numsIterationsForDisplay : <EOL> valuesPretty , qValuesPretty , actions , policyPretty = self . runAgent ( <EOL> moduleDict , n ) <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( <EOL> '<STR_LIT>' % <EOL> n , <EOL> valuesPretty ) ) <EOL> for action in actions : <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( <EOL> '<STR_LIT>' % <EOL> ( n , action ) , qValuesPretty [ action ] ) ) <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> policyPretty ) ) <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( '<STR_LIT>' , <EOL> '<STR_LIT:\n>' . join ( actions ) + '<STR_LIT:\n>' ) ) <EOL> return True <EOL> def runAgent ( self , moduleDict , numIterations ) : <EOL> agent = moduleDict [ '<STR_LIT>' ] . ValueIterationAgent ( <EOL> self . grid , <EOL> discount = self . discount , <EOL> iterations = numIterations ) <EOL> states = self . grid . getStates ( ) <EOL> actions = list ( <EOL> reduce ( lambda a , <EOL> b : set ( a ) . union ( b ) , <EOL> [ self . grid . getPossibleActions ( state ) for state in states ] ) ) <EOL> values = { } <EOL> qValues = { } <EOL> policy = { } <EOL> for state in states : <EOL> values [ state ] = agent . getValue ( state ) <EOL> policy [ state ] = agent . computeActionFromValues ( state ) <EOL> possibleActions = self . grid . getPossibleActions ( state ) <EOL> for action in actions : <EOL> if action not in qValues : <EOL> qValues [ action ] = { } <EOL> if action in possibleActions : <EOL> qValues [ <EOL> action ] [ <EOL> state ] = agent . computeQValueFromValues ( <EOL> state , <EOL> action ) <EOL> else : <EOL> qValues [ action ] [ state ] = None <EOL> valuesPretty = self . prettyValues ( values ) <EOL> policyPretty = self . prettyPolicy ( policy ) <EOL> qValuesPretty = { } <EOL> for action in actions : <EOL> qValuesPretty [ action ] = self . prettyValues ( qValues [ action ] ) <EOL> return ( valuesPretty , qValuesPretty , actions , policyPretty ) <EOL> def prettyPrint ( self , elements , formatString ) : <EOL> pretty = '<STR_LIT>' <EOL> states = self . grid . getStates ( ) <EOL> for ybar in range ( self . grid . grid . height ) : <EOL> y = self . grid . grid . height - <NUM_LIT:1> - ybar <EOL> row = [ ] <EOL> for x in range ( self . grid . grid . width ) : <EOL> if ( x , y ) in states : <EOL> value = elements [ ( x , y ) ] <EOL> if value is None : <EOL> row . append ( '<STR_LIT>' ) <EOL> else : <EOL> row . append ( formatString . format ( elements [ ( x , y ) ] ) ) <EOL> else : <EOL> row . append ( '<STR_LIT:_>' * <NUM_LIT:10> ) <EOL> pretty += '<STR_LIT>' % ( "<STR_LIT:U+0020>" . join ( row ) , ) <EOL> pretty += '<STR_LIT:\n>' <EOL> return pretty <EOL> def prettyValues ( self , values ) : <EOL> return self . prettyPrint ( values , '<STR_LIT>' ) <EOL> def prettyPolicy ( self , policy ) : <EOL> return self . prettyPrint ( policy , '<STR_LIT>' ) <EOL> def prettyValueSolutionString ( self , name , pretty ) : <EOL> return '<STR_LIT>' % ( name , pretty . rstrip ( ) ) <EOL> def comparePrettyValues ( self , aPretty , bPretty , tolerance = <NUM_LIT> ) : <EOL> aList = self . parsePrettyValues ( aPretty ) <EOL> bList = self . parsePrettyValues ( bPretty ) <EOL> if len ( aList ) != len ( bList ) : <EOL> return False <EOL> for a , b in zip ( aList , bList ) : <EOL> try : <EOL> aNum = float ( a ) <EOL> bNum = float ( b ) <EOL> error = abs ( aNum - bNum ) <EOL> if error > tolerance : <EOL> return False <EOL> except ValueError : <EOL> if a . strip ( ) != b . strip ( ) : <EOL> return False <EOL> return True <EOL> def parsePrettyValues ( self , pretty ) : <EOL> values = pretty . split ( ) <EOL> return values <EOL> class ApproximateQLearningTest ( testClasses . TestCase ) : <EOL> def __init__ ( self , question , testDict ) : <EOL> super ( ApproximateQLearningTest , self ) . __init__ ( question , testDict ) <EOL> self . discount = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . grid = gridworld . Gridworld ( parseGrid ( testDict [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in testDict : <EOL> self . grid . setNoise ( float ( testDict [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in testDict : <EOL> self . grid . setLivingReward ( float ( testDict [ '<STR_LIT>' ] ) ) <EOL> self . grid = gridworld . Gridworld ( parseGrid ( testDict [ '<STR_LIT>' ] ) ) <EOL> self . env = gridworld . GridworldEnvironment ( self . grid ) <EOL> self . epsilon = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . learningRate = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . extractor = '<STR_LIT>' <EOL> if '<STR_LIT>' in testDict : <EOL> self . extractor = testDict [ '<STR_LIT>' ] <EOL> self . opts = { <EOL> '<STR_LIT>' : self . env . getPossibleActions , <EOL> '<STR_LIT>' : self . epsilon , <EOL> '<STR_LIT>' : self . discount , <EOL> '<STR_LIT>' : self . learningRate } <EOL> numExperiences = int ( testDict [ '<STR_LIT>' ] ) <EOL> maxPreExperiences = <NUM_LIT:10> <EOL> self . numsExperiencesForDisplay = range ( <EOL> min ( numExperiences , maxPreExperiences ) ) <EOL> self . testOutFile = testDict [ '<STR_LIT>' ] <EOL> if maxPreExperiences < numExperiences : <EOL> self . numsExperiencesForDisplay . append ( numExperiences ) <EOL> def writeFailureFile ( self , string ) : <EOL> with open ( self . testOutFile , '<STR_LIT:w>' ) as handle : <EOL> handle . write ( string ) <EOL> def removeFailureFileIfExists ( self ) : <EOL> if os . path . exists ( self . testOutFile ) : <EOL> os . remove ( self . testOutFile ) <EOL> def execute ( self , grades , moduleDict , solutionDict ) : <EOL> failureOutputFileString = '<STR_LIT>' <EOL> failureOutputStdString = '<STR_LIT>' <EOL> for n in self . numsExperiencesForDisplay : <EOL> testPass , stdOutString , fileOutString = self . executeNExperiences ( <EOL> grades , moduleDict , solutionDict , n ) <EOL> failureOutputStdString += stdOutString <EOL> failureOutputFileString += fileOutString <EOL> if not testPass : <EOL> self . addMessage ( failureOutputStdString ) <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> self . testOutFile ) <EOL> self . writeFailureFile ( failureOutputFileString ) <EOL> return self . testFail ( grades ) <EOL> self . removeFailureFileIfExists ( ) <EOL> return self . testPass ( grades ) <EOL> def executeNExperiences ( self , grades , moduleDict , solutionDict , n ) : <EOL> testPass = True <EOL> qValuesPretty , weights , actions , lastExperience = self . runAgent ( <EOL> moduleDict , n ) <EOL> stdOutString = '<STR_LIT>' <EOL> fileOutString = "<STR_LIT>" % n <EOL> if lastExperience is not None : <EOL> fileOutString += "<STR_LIT>" % lastExperience <EOL> weightsKey = '<STR_LIT>' % n <EOL> if weights == eval ( solutionDict [ weightsKey ] ) : <EOL> fileOutString += "<STR_LIT>" % n <EOL> fileOutString += "<STR_LIT>" % pp . pformat ( weights ) <EOL> for action in actions : <EOL> qValuesKey = '<STR_LIT>' % ( n , action ) <EOL> qValues = qValuesPretty [ action ] <EOL> if self . comparePrettyValues ( qValues , solutionDict [ qValuesKey ] ) : <EOL> fileOutString += "<STR_LIT>" % ( <EOL> n , <EOL> action ) <EOL> fileOutString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> qValues ) <EOL> else : <EOL> testPass = False <EOL> outString = "<STR_LIT>" % ( <EOL> n , <EOL> action ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> qValues ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> solutionDict [ qValuesKey ] ) <EOL> stdOutString += outString <EOL> fileOutString += outString <EOL> return testPass , stdOutString , fileOutString <EOL> def writeSolution ( self , moduleDict , filePath ) : <EOL> with open ( filePath , '<STR_LIT:w>' ) as handle : <EOL> for n in self . numsExperiencesForDisplay : <EOL> qValuesPretty , weights , actions , _ = self . runAgent ( <EOL> moduleDict , n ) <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( '<STR_LIT>' % <EOL> n , pp . pformat ( weights ) ) ) <EOL> for action in actions : <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( <EOL> '<STR_LIT>' % <EOL> ( n , action ) , qValuesPretty [ action ] ) ) <EOL> return True <EOL> def runAgent ( self , moduleDict , numExperiences ) : <EOL> agent = moduleDict [ '<STR_LIT>' ] . ApproximateQAgent ( <EOL> extractor = self . extractor , <EOL> ** self . opts ) <EOL> states = sorted ( <EOL> filter ( lambda state : len ( self . grid . getPossibleActions ( state ) ) > <NUM_LIT:0> , <EOL> self . grid . getStates ( ) ) ) <EOL> randObj = FixedRandom ( ) . random <EOL> lastExperience = None <EOL> for i in range ( numExperiences ) : <EOL> startState = randObj . choice ( states ) <EOL> action = randObj . choice ( self . grid . getPossibleActions ( startState ) ) <EOL> ( endState , reward ) = self . env . getRandomNextState ( <EOL> startState , action , randObj = randObj ) <EOL> lastExperience = ( startState , action , endState , reward ) <EOL> agent . update ( * lastExperience ) <EOL> actions = list ( <EOL> reduce ( lambda a , <EOL> b : set ( a ) . union ( b ) , <EOL> [ self . grid . getPossibleActions ( state ) for state in states ] ) ) <EOL> qValues = { } <EOL> weights = agent . getWeights ( ) <EOL> for state in states : <EOL> possibleActions = self . grid . getPossibleActions ( state ) <EOL> for action in actions : <EOL> if action not in qValues : <EOL> qValues [ action ] = { } <EOL> if action in possibleActions : <EOL> qValues [ action ] [ state ] = agent . getQValue ( state , action ) <EOL> else : <EOL> qValues [ action ] [ state ] = None <EOL> qValuesPretty = { } <EOL> for action in actions : <EOL> qValuesPretty [ action ] = self . prettyValues ( qValues [ action ] ) <EOL> return ( qValuesPretty , weights , actions , lastExperience ) <EOL> def prettyPrint ( self , elements , formatString ) : <EOL> pretty = '<STR_LIT>' <EOL> states = self . grid . getStates ( ) <EOL> for ybar in range ( self . grid . grid . height ) : <EOL> y = self . grid . grid . height - <NUM_LIT:1> - ybar <EOL> row = [ ] <EOL> for x in range ( self . grid . grid . width ) : <EOL> if ( x , y ) in states : <EOL> value = elements [ ( x , y ) ] <EOL> if value is None : <EOL> row . append ( '<STR_LIT>' ) <EOL> else : <EOL> row . append ( formatString . format ( elements [ ( x , y ) ] ) ) <EOL> else : <EOL> row . append ( '<STR_LIT:_>' * <NUM_LIT:10> ) <EOL> pretty += '<STR_LIT>' % ( "<STR_LIT:U+0020>" . join ( row ) , ) <EOL> pretty += '<STR_LIT:\n>' <EOL> return pretty <EOL> def prettyValues ( self , values ) : <EOL> return self . prettyPrint ( values , '<STR_LIT>' ) <EOL> def prettyPolicy ( self , policy ) : <EOL> return self . prettyPrint ( policy , '<STR_LIT>' ) <EOL> def prettyValueSolutionString ( self , name , pretty ) : <EOL> return '<STR_LIT>' % ( name , pretty . rstrip ( ) ) <EOL> def comparePrettyValues ( self , aPretty , bPretty , tolerance = <NUM_LIT> ) : <EOL> aList = self . parsePrettyValues ( aPretty ) <EOL> bList = self . parsePrettyValues ( bPretty ) <EOL> if len ( aList ) != len ( bList ) : <EOL> return False <EOL> for a , b in zip ( aList , bList ) : <EOL> try : <EOL> aNum = float ( a ) <EOL> bNum = float ( b ) <EOL> error = abs ( aNum - bNum ) <EOL> if error > tolerance : <EOL> return False <EOL> except ValueError : <EOL> if a . strip ( ) != b . strip ( ) : <EOL> return False <EOL> return True <EOL> def parsePrettyValues ( self , pretty ) : <EOL> values = pretty . split ( ) <EOL> return values <EOL> class QLearningTest ( testClasses . TestCase ) : <EOL> def __init__ ( self , question , testDict ) : <EOL> super ( QLearningTest , self ) . __init__ ( question , testDict ) <EOL> self . discount = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . grid = gridworld . Gridworld ( parseGrid ( testDict [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in testDict : <EOL> self . grid . setNoise ( float ( testDict [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in testDict : <EOL> self . grid . setLivingReward ( float ( testDict [ '<STR_LIT>' ] ) ) <EOL> self . grid = gridworld . Gridworld ( parseGrid ( testDict [ '<STR_LIT>' ] ) ) <EOL> self . env = gridworld . GridworldEnvironment ( self . grid ) <EOL> self . epsilon = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . learningRate = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . opts = { <EOL> '<STR_LIT>' : self . env . getPossibleActions , <EOL> '<STR_LIT>' : self . epsilon , <EOL> '<STR_LIT>' : self . discount , <EOL> '<STR_LIT>' : self . learningRate } <EOL> numExperiences = int ( testDict [ '<STR_LIT>' ] ) <EOL> maxPreExperiences = <NUM_LIT:10> <EOL> self . numsExperiencesForDisplay = range ( <EOL> min ( numExperiences , maxPreExperiences ) ) <EOL> self . testOutFile = testDict [ '<STR_LIT>' ] <EOL> if maxPreExperiences < numExperiences : <EOL> self . numsExperiencesForDisplay . append ( numExperiences ) <EOL> def writeFailureFile ( self , string ) : <EOL> with open ( self . testOutFile , '<STR_LIT:w>' ) as handle : <EOL> handle . write ( string ) <EOL> def removeFailureFileIfExists ( self ) : <EOL> if os . path . exists ( self . testOutFile ) : <EOL> os . remove ( self . testOutFile ) <EOL> def execute ( self , grades , moduleDict , solutionDict ) : <EOL> failureOutputFileString = '<STR_LIT>' <EOL> failureOutputStdString = '<STR_LIT>' <EOL> for n in self . numsExperiencesForDisplay : <EOL> checkValuesAndPolicy = ( n == self . numsExperiencesForDisplay [ - <NUM_LIT:1> ] ) <EOL> testPass , stdOutString , fileOutString = self . executeNExperiences ( <EOL> grades , moduleDict , solutionDict , n , checkValuesAndPolicy ) <EOL> failureOutputStdString += stdOutString <EOL> failureOutputFileString += fileOutString <EOL> if not testPass : <EOL> self . addMessage ( failureOutputStdString ) <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> self . testOutFile ) <EOL> self . writeFailureFile ( failureOutputFileString ) <EOL> return self . testFail ( grades ) <EOL> self . removeFailureFileIfExists ( ) <EOL> return self . testPass ( grades ) <EOL> def executeNExperiences ( self , grades , moduleDict , <EOL> solutionDict , n , checkValuesAndPolicy ) : <EOL> testPass = True <EOL> valuesPretty , qValuesPretty , actions , policyPretty , lastExperience = self . runAgent ( <EOL> moduleDict , n ) <EOL> stdOutString = '<STR_LIT>' <EOL> fileOutString = "<STR_LIT>" % n <EOL> if lastExperience is not None : <EOL> fileOutString += "<STR_LIT>" % lastExperience <EOL> for action in actions : <EOL> qValuesKey = '<STR_LIT>' % ( n , action ) <EOL> qValues = qValuesPretty [ action ] <EOL> if self . comparePrettyValues ( qValues , solutionDict [ qValuesKey ] ) : <EOL> fileOutString += "<STR_LIT>" % ( <EOL> n , <EOL> action ) <EOL> fileOutString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> qValues ) <EOL> else : <EOL> testPass = False <EOL> outString = "<STR_LIT>" % ( <EOL> n , <EOL> action ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> qValues ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> qValuesKey , <EOL> solutionDict [ qValuesKey ] ) <EOL> stdOutString += outString <EOL> fileOutString += outString <EOL> if checkValuesAndPolicy : <EOL> if not self . comparePrettyValues ( valuesPretty , solutionDict [ '<STR_LIT>' ] ) : <EOL> testPass = False <EOL> outString = "<STR_LIT>" <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> valuesPretty ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> solutionDict [ '<STR_LIT>' ] ) <EOL> stdOutString += outString <EOL> fileOutString += outString <EOL> if not self . comparePrettyValues ( policyPretty , solutionDict [ '<STR_LIT>' ] ) : <EOL> testPass = False <EOL> outString = "<STR_LIT>" <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> policyPretty ) <EOL> outString += "<STR_LIT>" % self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> solutionDict [ '<STR_LIT>' ] ) <EOL> stdOutString += outString <EOL> fileOutString += outString <EOL> return testPass , stdOutString , fileOutString <EOL> def writeSolution ( self , moduleDict , filePath ) : <EOL> with open ( filePath , '<STR_LIT:w>' ) as handle : <EOL> valuesPretty = '<STR_LIT>' <EOL> policyPretty = '<STR_LIT>' <EOL> for n in self . numsExperiencesForDisplay : <EOL> valuesPretty , qValuesPretty , actions , policyPretty , _ = self . runAgent ( <EOL> moduleDict , n ) <EOL> for action in actions : <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( <EOL> '<STR_LIT>' % <EOL> ( n , action ) , qValuesPretty [ action ] ) ) <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> valuesPretty ) ) <EOL> handle . write ( <EOL> self . prettyValueSolutionString ( <EOL> '<STR_LIT>' , <EOL> policyPretty ) ) <EOL> return True <EOL> def runAgent ( self , moduleDict , numExperiences ) : <EOL> agent = moduleDict [ '<STR_LIT>' ] . QLearningAgent ( ** self . opts ) <EOL> states = sorted ( <EOL> filter ( lambda state : len ( self . grid . getPossibleActions ( state ) ) > <NUM_LIT:0> , <EOL> self . grid . getStates ( ) ) ) <EOL> randObj = FixedRandom ( ) . random <EOL> lastExperience = None <EOL> for i in range ( numExperiences ) : <EOL> startState = randObj . choice ( states ) <EOL> action = randObj . choice ( self . grid . getPossibleActions ( startState ) ) <EOL> ( endState , reward ) = self . env . getRandomNextState ( <EOL> startState , action , randObj = randObj ) <EOL> lastExperience = ( startState , action , endState , reward ) <EOL> agent . update ( * lastExperience ) <EOL> actions = list ( <EOL> reduce ( lambda a , <EOL> b : set ( a ) . union ( b ) , <EOL> [ self . grid . getPossibleActions ( state ) for state in states ] ) ) <EOL> values = { } <EOL> qValues = { } <EOL> policy = { } <EOL> for state in states : <EOL> values [ state ] = agent . computeValueFromQValues ( state ) <EOL> policy [ state ] = agent . computeActionFromQValues ( state ) <EOL> possibleActions = self . grid . getPossibleActions ( state ) <EOL> for action in actions : <EOL> if action not in qValues : <EOL> qValues [ action ] = { } <EOL> if action in possibleActions : <EOL> qValues [ action ] [ state ] = agent . getQValue ( state , action ) <EOL> else : <EOL> qValues [ action ] [ state ] = None <EOL> valuesPretty = self . prettyValues ( values ) <EOL> policyPretty = self . prettyPolicy ( policy ) <EOL> qValuesPretty = { } <EOL> for action in actions : <EOL> qValuesPretty [ action ] = self . prettyValues ( qValues [ action ] ) <EOL> return ( <EOL> ( valuesPretty , qValuesPretty , actions , <EOL> policyPretty , lastExperience ) <EOL> ) <EOL> def prettyPrint ( self , elements , formatString ) : <EOL> pretty = '<STR_LIT>' <EOL> states = self . grid . getStates ( ) <EOL> for ybar in range ( self . grid . grid . height ) : <EOL> y = self . grid . grid . height - <NUM_LIT:1> - ybar <EOL> row = [ ] <EOL> for x in range ( self . grid . grid . width ) : <EOL> if ( x , y ) in states : <EOL> value = elements [ ( x , y ) ] <EOL> if value is None : <EOL> row . append ( '<STR_LIT>' ) <EOL> else : <EOL> row . append ( formatString . format ( elements [ ( x , y ) ] ) ) <EOL> else : <EOL> row . append ( '<STR_LIT:_>' * <NUM_LIT:10> ) <EOL> pretty += '<STR_LIT>' % ( "<STR_LIT:U+0020>" . join ( row ) , ) <EOL> pretty += '<STR_LIT:\n>' <EOL> return pretty <EOL> def prettyValues ( self , values ) : <EOL> return self . prettyPrint ( values , '<STR_LIT>' ) <EOL> def prettyPolicy ( self , policy ) : <EOL> return self . prettyPrint ( policy , '<STR_LIT>' ) <EOL> def prettyValueSolutionString ( self , name , pretty ) : <EOL> return '<STR_LIT>' % ( name , pretty . rstrip ( ) ) <EOL> def comparePrettyValues ( self , aPretty , bPretty , tolerance = <NUM_LIT> ) : <EOL> aList = self . parsePrettyValues ( aPretty ) <EOL> bList = self . parsePrettyValues ( bPretty ) <EOL> if len ( aList ) != len ( bList ) : <EOL> return False <EOL> for a , b in zip ( aList , bList ) : <EOL> try : <EOL> aNum = float ( a ) <EOL> bNum = float ( b ) <EOL> error = abs ( aNum - bNum ) <EOL> if error > tolerance : <EOL> return False <EOL> except ValueError : <EOL> if a . strip ( ) != b . strip ( ) : <EOL> return False <EOL> return True <EOL> def parsePrettyValues ( self , pretty ) : <EOL> values = pretty . split ( ) <EOL> return values <EOL> class EpsilonGreedyTest ( testClasses . TestCase ) : <EOL> def __init__ ( self , question , testDict ) : <EOL> super ( EpsilonGreedyTest , self ) . __init__ ( question , testDict ) <EOL> self . discount = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . grid = gridworld . Gridworld ( parseGrid ( testDict [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in testDict : <EOL> self . grid . setNoise ( float ( testDict [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in testDict : <EOL> self . grid . setLivingReward ( float ( testDict [ '<STR_LIT>' ] ) ) <EOL> self . grid = gridworld . Gridworld ( parseGrid ( testDict [ '<STR_LIT>' ] ) ) <EOL> self . env = gridworld . GridworldEnvironment ( self . grid ) <EOL> self . epsilon = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . learningRate = float ( testDict [ '<STR_LIT>' ] ) <EOL> self . numExperiences = int ( testDict [ '<STR_LIT>' ] ) <EOL> self . numIterations = int ( testDict [ '<STR_LIT>' ] ) <EOL> self . opts = { <EOL> '<STR_LIT>' : self . env . getPossibleActions , <EOL> '<STR_LIT>' : self . epsilon , <EOL> '<STR_LIT>' : self . discount , <EOL> '<STR_LIT>' : self . learningRate } <EOL> def execute ( self , grades , moduleDict , solutionDict ) : <EOL> if self . testEpsilonGreedy ( moduleDict ) : <EOL> return self . testPass ( grades ) <EOL> else : <EOL> return self . testFail ( grades ) <EOL> def writeSolution ( self , moduleDict , filePath ) : <EOL> with open ( filePath , '<STR_LIT:w>' ) as handle : <EOL> handle . write ( '<STR_LIT>' % self . path ) <EOL> handle . write ( '<STR_LIT>' ) <EOL> return True <EOL> def runAgent ( self , moduleDict ) : <EOL> agent = moduleDict [ '<STR_LIT>' ] . QLearningAgent ( ** self . opts ) <EOL> states = sorted ( <EOL> filter ( lambda state : len ( self . grid . getPossibleActions ( state ) ) > <NUM_LIT:0> , <EOL> self . grid . getStates ( ) ) ) <EOL> randObj = FixedRandom ( ) . random <EOL> for i in range ( self . numExperiences ) : <EOL> startState = randObj . choice ( states ) <EOL> action = randObj . choice ( self . grid . getPossibleActions ( startState ) ) <EOL> ( endState , reward ) = self . env . getRandomNextState ( <EOL> startState , action , randObj = randObj ) <EOL> agent . update ( startState , action , endState , reward ) <EOL> return agent <EOL> def testEpsilonGreedy ( self , moduleDict , tolerance = <NUM_LIT> ) : <EOL> agent = self . runAgent ( moduleDict ) <EOL> for state in self . grid . getStates ( ) : <EOL> numLegalActions = len ( agent . getLegalActions ( state ) ) <EOL> if numLegalActions <= <NUM_LIT:1> : <EOL> continue <EOL> numGreedyChoices = <NUM_LIT:0> <EOL> optimalAction = agent . computeActionFromQValues ( state ) <EOL> for iteration in range ( self . numIterations ) : <EOL> if agent . getAction ( state ) == optimalAction : <EOL> numGreedyChoices += <NUM_LIT:1> <EOL> empiricalEpsilonNumerator = self . numIterations - numGreedyChoices <EOL> empiricalEpsilonDenominator = self . numIterations - self . numIterations / float ( numLegalActions ) <EOL> empiricalEpsilon = empiricalEpsilonNumerator / empiricalEpsilonDenominator <EOL> error = abs ( empiricalEpsilon - self . epsilon ) <EOL> if error > tolerance : <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" % <EOL> ( self . epsilon , empiricalEpsilon , error , tolerance ) ) <EOL> return False <EOL> return True <EOL> class Question6Test ( testClasses . TestCase ) : <EOL> def __init__ ( self , question , testDict ) : <EOL> super ( Question6Test , self ) . __init__ ( question , testDict ) <EOL> def execute ( self , grades , moduleDict , solutionDict ) : <EOL> studentSolution = moduleDict [ '<STR_LIT>' ] . question6 ( ) <EOL> studentSolution = str ( studentSolution ) . strip ( ) . lower ( ) <EOL> hashedSolution = sha1 ( studentSolution ) . hexdigest ( ) <EOL> if hashedSolution == '<STR_LIT>' : <EOL> return self . testPass ( grades ) <EOL> else : <EOL> self . addMessage ( "<STR_LIT>" ) <EOL> self . addMessage ( "<STR_LIT>" % ( studentSolution , ) ) <EOL> return self . testFail ( grades ) <EOL> def writeSolution ( self , moduleDict , filePath ) : <EOL> handle = open ( filePath , '<STR_LIT:w>' ) <EOL> handle . write ( '<STR_LIT>' % self . path ) <EOL> handle . write ( '<STR_LIT>' ) <EOL> handle . close ( ) <EOL> return True <EOL> class EvalAgentTest ( testClasses . TestCase ) : <EOL> def __init__ ( self , question , testDict ) : <EOL> super ( EvalAgentTest , self ) . __init__ ( question , testDict ) <EOL> self . pacmanParams = testDict [ '<STR_LIT>' ] <EOL> self . scoreMinimum = int ( <EOL> testDict [ '<STR_LIT>' ] ) if '<STR_LIT>' in testDict else None <EOL> self . nonTimeoutMinimum = int ( <EOL> testDict [ '<STR_LIT>' ] ) if '<STR_LIT>' in testDict else None <EOL> self . winsMinimum = int ( <EOL> testDict [ '<STR_LIT>' ] ) if '<STR_LIT>' in testDict else None <EOL> self . scoreThresholds = [ int ( s ) <EOL> for s in testDict . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) ] <EOL> self . nonTimeoutThresholds = [ int ( s ) <EOL> for s in testDict . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) ] <EOL> self . winsThresholds = [ int ( s ) <EOL> for s in testDict . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) ] <EOL> self . maxPoints = sum ( <EOL> [ len ( t ) for t in [ self . scoreThresholds , <EOL> self . nonTimeoutThresholds , <EOL> self . winsThresholds ] ] ) <EOL> def execute ( self , grades , moduleDict , solutionDict ) : <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( self . pacmanParams , ) ) <EOL> startTime = time . time ( ) <EOL> games = pacman . runGames ( <EOL> ** pacman . readCommand ( self . pacmanParams . split ( '<STR_LIT:U+0020>' ) ) ) <EOL> totalTime = time . time ( ) - startTime <EOL> numGames = len ( games ) <EOL> stats = { <EOL> '<STR_LIT:time>' : totalTime , '<STR_LIT>' : <EOL> [ g . state . isWin ( ) for g in games ] . count ( True ) , <EOL> '<STR_LIT>' : games , '<STR_LIT>' : [ g . state . getScore ( ) for g in games ] , <EOL> '<STR_LIT>' : [ g . agentTimeout for g in games ] . count ( True ) , '<STR_LIT>' : [ g . agentCrashed for g in games ] . count ( True ) } <EOL> averageScore = sum ( stats [ '<STR_LIT>' ] ) / float ( len ( stats [ '<STR_LIT>' ] ) ) <EOL> nonTimeouts = numGames - stats [ '<STR_LIT>' ] <EOL> wins = stats [ '<STR_LIT>' ] <EOL> def gradeThreshold ( value , minimum , thresholds , name ) : <EOL> points = <NUM_LIT:0> <EOL> passed = ( minimum is None ) or ( value >= minimum ) <EOL> if passed : <EOL> for t in thresholds : <EOL> if value >= t : <EOL> points += <NUM_LIT:1> <EOL> return ( passed , points , value , minimum , thresholds , name ) <EOL> results = [ gradeThreshold ( <EOL> averageScore , self . scoreMinimum , self . scoreThresholds , "<STR_LIT>" ) , <EOL> gradeThreshold ( <EOL> nonTimeouts , <EOL> self . nonTimeoutMinimum , <EOL> self . nonTimeoutThresholds , <EOL> "<STR_LIT>" ) , <EOL> gradeThreshold ( wins , self . winsMinimum , self . winsThresholds , "<STR_LIT>" ) ] <EOL> totalPoints = <NUM_LIT:0> <EOL> for passed , points , value , minimum , thresholds , name in results : <EOL> if minimum is None and len ( thresholds ) == <NUM_LIT:0> : <EOL> continue <EOL> totalPoints += points <EOL> if not passed : <EOL> assert points == <NUM_LIT:0> <EOL> self . addMessage ( <EOL> "<STR_LIT>" % <EOL> ( value , name , minimum ) ) <EOL> else : <EOL> self . addMessage ( <EOL> "<STR_LIT>" % <EOL> ( value , name , points , len ( thresholds ) ) ) <EOL> if minimum is not None : <EOL> self . addMessage ( "<STR_LIT>" ) <EOL> self . addMessage ( "<STR_LIT>" % ( minimum , ) ) <EOL> if len ( thresholds ) == <NUM_LIT:0> or minimum != thresholds [ <NUM_LIT:0> ] : <EOL> self . addMessage ( "<STR_LIT>" % ( minimum , ) ) <EOL> for idx , threshold in enumerate ( thresholds ) : <EOL> self . addMessage ( <EOL> "<STR_LIT>" % <EOL> ( threshold , idx + <NUM_LIT:1> ) ) <EOL> elif len ( thresholds ) > <NUM_LIT:0> : <EOL> self . addMessage ( "<STR_LIT>" ) <EOL> self . addMessage ( "<STR_LIT>" % ( thresholds [ <NUM_LIT:0> ] , ) ) <EOL> for idx , threshold in enumerate ( thresholds ) : <EOL> self . addMessage ( <EOL> "<STR_LIT>" % <EOL> ( threshold , idx + <NUM_LIT:1> ) ) <EOL> if any ( [ not passed for passed , _ , _ , _ , _ , _ in results ] ) : <EOL> totalPoints = <NUM_LIT:0> <EOL> return self . testPartial ( grades , totalPoints , self . maxPoints ) <EOL> def writeSolution ( self , moduleDict , filePath ) : <EOL> with open ( filePath , '<STR_LIT:w>' ) as handle : <EOL> handle . write ( '<STR_LIT>' % self . path ) <EOL> handle . write ( '<STR_LIT>' ) <EOL> return True <EOL> def followPath ( policy , start , numSteps = <NUM_LIT:100> ) : <EOL> state = start <EOL> path = [ ] <EOL> for i in range ( numSteps ) : <EOL> if state not in policy : <EOL> break <EOL> action = policy [ state ] <EOL> path . append ( "<STR_LIT>" % state ) <EOL> if action == '<STR_LIT>' : <EOL> nextState = state [ <NUM_LIT:0> ] , state [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> if action == '<STR_LIT>' : <EOL> nextState = state [ <NUM_LIT:0> ] , state [ <NUM_LIT:1> ] - <NUM_LIT:1> <EOL> if action == '<STR_LIT>' : <EOL> nextState = state [ <NUM_LIT:0> ] + <NUM_LIT:1> , state [ <NUM_LIT:1> ] <EOL> if action == '<STR_LIT>' : <EOL> nextState = state [ <NUM_LIT:0> ] - <NUM_LIT:1> , state [ <NUM_LIT:1> ] <EOL> if action == '<STR_LIT>' or action is None : <EOL> path . append ( '<STR_LIT>' ) <EOL> break <EOL> state = nextState <EOL> return path <EOL> def parseGrid ( string ) : <EOL> grid = [ [ entry . strip ( ) for entry in line . split ( ) ] <EOL> for line in string . split ( '<STR_LIT:\n>' ) ] <EOL> for row in grid : <EOL> for x , col in enumerate ( row ) : <EOL> try : <EOL> col = int ( col ) <EOL> except : <EOL> pass <EOL> if col == "<STR_LIT:_>" : <EOL> col = '<STR_LIT:U+0020>' <EOL> row [ x ] = col <EOL> return gridworld . makeGrid ( grid ) <EOL> def computePolicy ( moduleDict , grid , discount ) : <EOL> valueIterator = moduleDict [ <EOL> '<STR_LIT>' ] . ValueIterationAgent ( <EOL> grid , <EOL> discount = discount ) <EOL> policy = { } <EOL> for state in grid . getStates ( ) : <EOL> policy [ state ] = valueIterator . computeActionFromValues ( state ) <EOL> return policy <EOL> class GridPolicyTest ( testClasses . TestCase ) : <EOL> def __init__ ( self , question , testDict ) : <EOL> super ( GridPolicyTest , self ) . __init__ ( question , testDict ) <EOL> self . parameterFn = testDict [ '<STR_LIT>' ] <EOL> self . question2 = testDict . get ( '<STR_LIT>' , '<STR_LIT:false>' ) . lower ( ) == '<STR_LIT:true>' <EOL> self . gridText = testDict [ '<STR_LIT>' ] <EOL> self . grid = gridworld . Gridworld ( parseGrid ( testDict [ '<STR_LIT>' ] ) ) <EOL> self . gridName = testDict [ '<STR_LIT>' ] <EOL> self . policy = parseGrid ( testDict [ '<STR_LIT>' ] ) <EOL> self . pathVisits = testDict . get ( '<STR_LIT>' , None ) <EOL> self . pathNotVisits = testDict . get ( '<STR_LIT>' , None ) <EOL> def execute ( self , grades , moduleDict , solutionDict ) : <EOL> if not hasattr ( moduleDict [ '<STR_LIT>' ] , self . parameterFn ) : <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( self . parameterFn , ) ) <EOL> return self . testFail ( grades ) <EOL> result = getattr ( moduleDict [ '<STR_LIT>' ] , self . parameterFn ) ( ) <EOL> if isinstance ( result , str ) and result . lower ( ) [ <NUM_LIT:0> : <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> self . addMessage ( '<STR_LIT>' ) <EOL> return self . testFail ( grades ) <EOL> if self . question2 : <EOL> livingReward = None <EOL> try : <EOL> discount , noise = result <EOL> discount = float ( discount ) <EOL> noise = float ( noise ) <EOL> except : <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( self . parameterFn , result ) ) <EOL> return self . testFail ( grades ) <EOL> if discount != <NUM_LIT> and noise != <NUM_LIT> : <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( result , ) ) <EOL> return self . testFail ( grades ) <EOL> else : <EOL> try : <EOL> discount , noise , livingReward = result <EOL> discount = float ( discount ) <EOL> noise = float ( noise ) <EOL> livingReward = float ( livingReward ) <EOL> except : <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( self . parameterFn , result ) ) <EOL> return self . testFail ( grades ) <EOL> self . grid . setNoise ( noise ) <EOL> if livingReward is not None : <EOL> self . grid . setLivingReward ( livingReward ) <EOL> start = self . grid . getStartState ( ) <EOL> policy = computePolicy ( moduleDict , self . grid , discount ) <EOL> actionMap = { <EOL> '<STR_LIT:N>' : '<STR_LIT>' , <EOL> '<STR_LIT:E>' : '<STR_LIT>' , <EOL> '<STR_LIT:S>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:X>' : '<STR_LIT>' } <EOL> width , height = self . policy . width , self . policy . height <EOL> policyPassed = True <EOL> for x in range ( width ) : <EOL> for y in range ( height ) : <EOL> if self . policy [ x ] [ y ] in actionMap and policy [ ( x , y ) ] != actionMap [ self . policy [ x ] [ y ] ] : <EOL> differPoint = ( x , y ) <EOL> policyPassed = False <EOL> if not policyPassed : <EOL> self . addMessage ( '<STR_LIT>' ) <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( differPoint , policy [ differPoint ] ) ) <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( differPoint , actionMap [ self . policy [ differPoint [ <NUM_LIT:0> ] ] [ differPoint [ <NUM_LIT:1> ] ] ] ) ) <EOL> self . addMessage ( '<STR_LIT>' ) <EOL> self . printPolicy ( policy , False ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . addMessage ( '<STR_LIT>' ) <EOL> self . printPolicy ( self . policy , True ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . printGridworld ( ) <EOL> return self . testFail ( grades ) <EOL> path = followPath ( policy , self . grid . getStartState ( ) ) <EOL> if self . pathVisits is not None and self . pathVisits not in path : <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( self . pathVisits , ) ) <EOL> self . addMessage ( '<STR_LIT>' % ( path , ) ) <EOL> self . addMessage ( '<STR_LIT>' ) <EOL> self . printPolicy ( policy , False ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . printGridworld ( ) <EOL> return self . testFail ( grades ) <EOL> if self . pathNotVisits is not None and self . pathNotVisits in path : <EOL> self . addMessage ( <EOL> '<STR_LIT>' % <EOL> ( self . pathNotVisits , ) ) <EOL> self . addMessage ( '<STR_LIT>' % ( path , ) ) <EOL> self . addMessage ( '<STR_LIT>' ) <EOL> self . printPolicy ( policy , False ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . addMessage ( <EOL> "<STR_LIT>" ) <EOL> self . printGridworld ( ) <EOL> return self . testFail ( grades ) <EOL> return self . testPass ( grades ) <EOL> def printGridworld ( self ) : <EOL> self . addMessage ( '<STR_LIT>' ) <EOL> for line in self . gridText . split ( '<STR_LIT:\n>' ) : <EOL> self . addMessage ( '<STR_LIT:U+0020>' + line ) <EOL> self . addMessage ( <EOL> '<STR_LIT>' ) <EOL> def printPolicy ( self , policy , policyTypeIsGrid ) : <EOL> if policyTypeIsGrid : <EOL> legend = { '<STR_LIT:N>' : '<STR_LIT:N>' , '<STR_LIT:E>' : '<STR_LIT:E>' , '<STR_LIT:S>' : '<STR_LIT:S>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:U+0020>' : '<STR_LIT:_>' } <EOL> else : <EOL> legend = { <EOL> '<STR_LIT>' : '<STR_LIT:N>' , <EOL> '<STR_LIT>' : '<STR_LIT:E>' , <EOL> '<STR_LIT>' : '<STR_LIT:S>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:X>' , <EOL> '<STR_LIT:.>' : '<STR_LIT:.>' , <EOL> '<STR_LIT:U+0020>' : '<STR_LIT:_>' } <EOL> for ybar in range ( self . grid . grid . height ) : <EOL> y = self . grid . grid . height - <NUM_LIT:1> - ybar <EOL> if policyTypeIsGrid : <EOL> self . addMessage ( <EOL> "<STR_LIT>" % <EOL> ( "<STR_LIT:U+0020>" . join ( [ legend [ policy [ x ] [ y ] ] for x in range ( self . grid . grid . width ) ] ) , ) ) <EOL> else : <EOL> self . addMessage ( <EOL> "<STR_LIT>" % <EOL> ( "<STR_LIT:U+0020>" . join ( [ legend [ policy . get ( ( x , y ) , '<STR_LIT:.>' ) ] for x in range ( self . grid . grid . width ) ] ) , ) ) <EOL> def writeSolution ( self , moduleDict , filePath ) : <EOL> with open ( filePath , '<STR_LIT:w>' ) as handle : <EOL> handle . write ( '<STR_LIT>' % self . path ) <EOL> handle . write ( '<STR_LIT>' ) <EOL> return True </s>
