def follow(self, instructions):
     now = time.time()
     result = instructions
     print 'Instructions =', instructions
     if '-' in instructions:
         prefix, instructionID = instructions.split('-')
     else:
         instructionID = instructions
     setLogs(instructionID)
     logger.runComplete('Following %s', instructionID)
     RunResults = []
     for label, step in self.stages:
         startTime = now
         logger.stageComplete('Starting %s', label)
         result, CaughtError, CaughtErrorTxt = step(result, instructionID)
         if self.NLUQueue: self.NLUQueue.put((label, result))
         logger.stageComplete('Ending %s', label)
         if isinstance(result, list):
             for rst in result:
                 logger.stageComplete("%r", rst)
         else:
             logger.stageComplete('%r', result)
         now = time.time()
         logger.info('%s took %d', label, now - startTime)
         RunResults.append(result)
         if CaughtError == 'EOFError':
             return RunResults, CaughtError, CaughtErrorTxt
     logger.flush()
     return RunResults, CaughtError, CaughtErrorTxt
Beispiel #2
0
 def planRoute(self):
     self.getRoute()
     logger.runComplete('Replaying route trace %s/%s', self.directory,
                        self.route)
     return [
         self.Actions[act] for act in file(
             os.path.join(self.directory, 'ActTrace-' + self.route))
         if act in self.Actions
     ]
class Executor:
    def __init__(self):
        self.NLUQueue = None
    
    def initialize(self,instructID):
        director,env,start,dest,txt,set = instructID.split('_')
        if self.NLUQueue:
            self.NLUQueue.put(('Director',director))
            self.NLUQueue.put(('Start Place',start))
            self.NLUQueue.put(('Destination',dest))
            self.NLUQueue.put(('Set',set))
        self.robot.setRoute(start,dest)

    def setRobot(self,Robot):
        self.robot = Robot

    def _execute(self,Model): pass
    
    def execute(self,Model,instructID):
        CaughtError = None
        self.initialize(instructID)
        try:
            self._execute(Model)
        except (KeyError,LookupError,ValueError,AttributeError,RuntimeError,TypeError),e:
            logger.error('%s on %s', e.__class__.__name__, e)
            CaughtError = e
        if not self.robot.completed:
            reward,obs = self.robot.declareGoal()
            logger.stageComplete('Declared Goal to complete instruction execution %s',(reward,obs))
        ResultTxt = self.robot.completed and 'Success' or 'Failure'
        logger.runComplete("%s in direction following.", ResultTxt)
        return self.robot.completed, CaughtError, ResultTxt
 def run(self,
         files,
         instructionQ=None,
         successes=0,
         attempts=0,
         Results={},
         Conditions=[]):
     global InstructionText
     for file in files:
         while (instructionQ and not instructionQ.empty()):
             type, message = instructionQ.get()
             print 'TimedFollower.run :: got', type, message
             if type == 'State': print 'Got state message'
             elif type == 'ID': file = message
             elif type == 'Text': InstructionText = message
             else:
                 logger.error(
                     'TimedFollower.run::instructionQ:'
                     '  Unknown message type: %s %s.', type, message)
         Options.reset()
         results, error, resultTxt = self.follow(file)
         if Options.Statistics:
             logger.runComplete('Statistics: %s', Options.stats())
             name = file.split('-')[1]
             stats = open(
                 'MarcoLogs/Statistics/' + '-'.join(Conditions) + '-' +
                 logger.timeStamp + '-' + name, 'w')
             for opt, count in Options.stats().items():
                 stats.write('%s: %d\n' % (opt, count))
             stats.close()
         attempts += 1
         if resultTxt == Success:
             successList = Results.setdefault(resultTxt, [])
             successList.append((resultTxt, file))
             Results[resultTxt] = successList
             successes += 1
         else:
             errorList = Results.setdefault(error, [])
             errorList.append((resultTxt, file))
             Results[error] = errorList
def printResults(Results, startTime, successes, attempts, Conditions=[]):
    endTime = time.time()
    blank = 0
    for result, explanations in Results.items():
        logger.runComplete("Result: %s %d", result, len(explanations))
        if result == 'EOFError':
            blank = len(explanations)
            continue
        # Sort by exception
        explanations.sort(lambda a, b: cmp(a[1], b[1]))
        count = {}
        for explanation, riSet in explanations:
            exceptText = str(explanation)
            exceptText = exceptText.replace(' field structure\\n\\n', ':')
            exceptText = exceptText.replace('  ', ' ')
            exceptText = exceptText.replace('\t', ' ')
            exceptText = exceptText.replace('\\', '')
            if len(exceptText) > 85: exceptText = exceptText[:85] + '...'
            logger.runComplete('%s\t%s\t%s', result, exceptText, riSet)
            director = riSet.split('-')[1].split('_')[0]
            count.setdefault(director, 0)
            count[director] += 1
        logger.runComplete(
            "Result: %s %s", result,
            ''.join(['%s: %d; ' % (d, c) for d, c in count.items()]))
    recordResults(Results,
                  time.strftime("%Y-%m-%d-%H-%M", time.localtime(startTime)),
                  Conditions)
    logger.runComplete(
        'Failures on %d of %d',
        sum([len(l) for res, l in Results.items() if res != Success]) - blank,
        attempts - blank)
    count = attempts - blank
    if count:
        logger.runComplete('Success on %d of %d: %3.1f%%', successes, count,
                           100.0 * successes / count)
    logger.runComplete('Tests took %.2f seconds', endTime - startTime)
    logger.runComplete('Time Stamp %s-%d', logger.timeStamp, Orientation)
        follower = TimedFollower(
            wrapper(grabInput, DirectionCorpus.readCleaned),
            wrapper(parser.parse, ParseDirections.readCorrParse),
            wrapper(Sense.trees2frames, Sense.readCorrFrame),
            CompoundAction.model, executor)
    from POMDP.MarkovLoc_Grid import pomdp as Grid_pomdp
    from POMDP.MarkovLoc_Jelly import pomdp as Jelly_pomdp
    from POMDP.MarkovLoc_L import pomdp as L_pomdp
    POMDPs = {'Grid': Grid_pomdp, 'Jelly': Jelly_pomdp, 'L': L_pomdp}
    for pomdp in POMDPs.values():
        pomdp.trace = trace
        pomdp.PosSet = int(PosSet)
        pomdp.Positions = pomdp.PositionSet[pomdp.PosSet]

    if Ablate:
        logger.runComplete("Ablating condition: %s", ', '.join(Ablate))
        for ablateOpt in Ablate:
            if ablateOpt != 'Default':
                setattr(Options, ablateOpt, not getattr(Options, ablateOpt))
                ## ResetCache options are 3 choices for resetting the cache.
                ## At least one must be set, or memory will explode.
                if ablateOpt == 'TurnTermResetCache':
                    Options.TurnPostResetCache = True
                else:
                    Options.TurnTermResetCache = False
                if ablateOpt + 'List' in dir(Options):
                    for option in getattr(Options, ablateOpt + 'List'):
                        setattr(Options, option, False)
        for pomdp in POMDPs.values():
            pomdp.getDirectionVal = Orientation
        if not Options.ViewMemory:
Beispiel #7
0
    def execute(self, (Map, Start, Target)):
        CaughtError = None
        self.initializeRoute(Map, Start, Target)
        try:
            self.executeRoute(self.planRoute())
        except (KeyError, LookupError, ValueError), e:  #,AttributeError
            logger.error('%s on %s', e.__class__.__name__, e)
            CaughtError = e
        if not self.robot.completed:
            reward, obs = self.robot.declareGoal()
            logger.stageComplete(
                'Declared Goal to complete instruction execution %s',
                (reward, obs))
        ResultTxt = self.robot.completed and 'Success' or 'Failure'
        logger.runComplete("%s in direction following.", ResultTxt)
        return self.robot.completed, CaughtError, ResultTxt

    #handles dynamic functionality. not sure if this is the best place for this function
    def userInterface(self):
        #breaks when user inputs "AutoNav" only
        while self.instructionQ:
            if not self.instructionQ.empty():
                (ID, newCommand) = self.instructionQ.get()
                if ID == "NavID":
                    self.robot.previousMode = self.robot.userInterfaceMode
                    if newCommand == "AutoNav":
                        self.robot.userInterfaceMode = "AutoNav"
                        return
                    elif newCommand == "Stop":
                        self.robot.userInterfaceMode = "Stop"