Subversion Repositories programming

Rev

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 Grab
        if Grab(State).meetsPreconditions():
                return (GRAB,)

        # Try to push box to middle
        if Push(State, boxPosition, Push.MIDDLE).meetsPreconditions():
                if boxPosition != Push.MIDDLE:
                        return (PUSH, boxPosition, Push.MIDDLE)

        # Try to walk to box
        if Walk(State, monkeyPosition, boxPosition).meetsPreconditions():
                if monkeyPosition != boxPosition:
                        return (WALK, monkeyPosition, boxPosition)

        # Try to climb the box
        if Climb(State, Climb.UP).meetsPreconditions():
                if not Action(State).monkeyOnBox ():
                        return (CLIMB, Climb.UP)

        # Try to climb down from the box
        if 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 False

        return True

def 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' % next
                        State = Walk(State, next[1], next[2]).takeAction()
                elif action == PUSH:
                        print '%s from %s to %s' % next
                        State = Push(State, next[1], next[2]).takeAction()
                elif action == CLIMB:
                        print '%s %s' % next
                        State = Climb(State, next[1]).takeAction()
                elif action == GRAB:
                        print '%s Banana' % next
                        State = 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: