import lib601.sm as sm class RobotMoves(sm.SM): legalInputs = ['left','right','down','up'] startState = None def __init__(self,s): self.s = s self.goal = (3,4) def getNextValues(self, state, inp): if state == None: state = self.s nextState = list(state) if inp not in self.legalInputs: return (state,state) else: if inp == 'left': nextState[0] -= 1 elif inp == 'right': nextState[0] += 1 elif inp == 'up': nextState[1] += 1 elif inp == 'down': nextState[1] -= 1 nextState = tuple(nextState) return (nextState, nextState) def done(self, state): return state == self.goal sm = RobotMoves((0,0)) sm.transduce(['up','right','right','up','up','right','up','right','up'],verbose = True)
if self.doesAnyoneDie(nextState): # action led to illegal state return state return tuple(nextState) def farmerTakesItem(self,nextState,itemIndex): if nextState [0] == nextState[itemIndex]: if nextState[0] == 'R': nextState[0] = 'L' nextState[itemIndex] = 'L' elif nextState[0] == 'L': nextState[0] = 'R' nextState[itemIndex] = 'R' return nextState def doesAnyoneDie(self,nextState): if nextState[1] == nextState[3] and nextState[0] != nextState[1]: # farmer left the goat with the cabbage return True elif nextState[1] == nextState[2] and nextState[0] != nextState[1]: # farmer left the goat with the wolf return True else: return False def getNextValues(self, state, action): nextState = self.nextState(state,action) return (nextState, nextState) def done(self, state): return state == self.goal sm = FarmerGoatWolfCabbage() sm.transduce(['takeGoat','takeNone','takeNone'],verbose = True)
return tuple(nextState) def farmerTakesItem(self, nextState, itemIndex): if nextState[0] == nextState[itemIndex]: if nextState[0] == 'R': nextState[0] = 'L' nextState[itemIndex] = 'L' elif nextState[0] == 'L': nextState[0] = 'R' nextState[itemIndex] = 'R' return nextState def doesAnyoneDie(self, nextState): if nextState[1] == nextState[3] and nextState[0] != nextState[1]: return True elif nextState[1] == nextState[2] and nextState[0] != nextState[1]: return True else: return False def getNextValues(self, state, action): nextState = self.nextState(state, action) return (nextState, nextState) def done(self, state): return state == self.goal sm = FarmerGoatWolfCabbage() sm.transduce(['takeGoat', 'takeNone', 'takeNone'], verbose=True)