Rev 424 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/env python__author__ = "Ira W. Snyder (devel@irasnyder.com)"__copyright__ = "Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)"__license__ = "GNU GPL v2 (or, at your option, any later version)"from PyCompat import *from Actions import *import sys(WALK, PUSH, CLIMB, GRAB) = ('Walk', 'Push', 'Climb', 'Grab')def findNextMove (State):"""Prioritized Search for the best next move"""monkeyPosition = Action(State).findMonkeyPosition ()boxPosition = Action(State).findBoxPosition ()# Try to Grabif Grab(State).meetsPreconditions():return (GRAB,)# Try to push box to middleif Push(State, boxPosition, Push.MIDDLE).meetsPreconditions():if boxPosition != Push.MIDDLE:return (PUSH, boxPosition, Push.MIDDLE)# Try to walk to boxif Walk(State, monkeyPosition, boxPosition).meetsPreconditions():if monkeyPosition != boxPosition:return (WALK, monkeyPosition, boxPosition)# Try to climb the boxif Climb(State, Climb.UP).meetsPreconditions():if not Action(State).monkeyOnBox ():return (CLIMB, Climb.UP)# Try to climb down from the boxif Climb(State, Climb.DOWN).meetsPreconditions():if Action(State).monkeyOnBox ():return (CLIMB, Climb.DOWN)def isValidInitialState (State):monkeyPos = Action(State).findMonkeyPosition()boxPos = Action(State).findBoxPosition()onBox = Action(State).monkeyOnBox()if onBox and (monkeyPos != boxPos):return Falsereturn Truedef main ():#State = set(['At(Monkey,Door)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])State = set(['At(Monkey,Middle)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnBox()', 'NoBanana()'])#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnBox()', 'HasBanana()'])#State = set(['At(Monkey,Door)', 'At(Box,Window)', 'OnBox()', 'HasBanana()'])#State = set(['At(Monkey,Window)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])#State = set(['At(Monkey,Door)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])#State = set(['At(Monkey,Middle)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])#State = set(['At(Monkey,Middle)', 'At(Box,Middle)', 'OnBox()', 'NoBanana()'])#State = set(['At(Monkey,Middle)', 'At(Box,Door)', 'OnFloor()', 'NoBanana()'])if not isValidInitialState (State):print 'Bad initial state'sys.exit (1)while not Action(State).isGoal():next = findNextMove (State)action = next[0]if action == WALK:print '%s from %s to %s' % nextState = Walk(State, next[1], next[2]).takeAction()elif action == PUSH:print '%s from %s to %s' % nextState = Push(State, next[1], next[2]).takeAction()elif action == CLIMB:print '%s %s' % nextState = Climb(State, next[1]).takeAction()elif action == GRAB:print '%s Banana' % nextState = Grab(State).takeAction()else:print 'BAD STATE'sys.exit (2)print 'Reached the Goal!'if __name__ == '__main__':main ()# vim: set ts=4 sts=4 sw=4: