コード例 #1
0
ファイル: Simulator.py プロジェクト: rondelion/Lingadrome
class Simulator(object):
    '''
    classdocs
    '''
    __cnt = 0

    def __init__(self, name, rewardActDescription):
        '''
        Constructor
        '''
        self.logFP = None
        self.ll = LanguageLearner(name)
        self.lu = LanguageUser(rewardActDescription)

    def loop(self, maxLoop):
        llUtt = ""
        llAct = ""
        while self.__cnt < maxLoop:
            self.__cnt = self.__cnt + 1
            self.lu.setInput("llUtterance", llUtt)
            self.lu.setInput("llAction", llAct)
            self.lu.loop()
            luUtt = self.lu.getOutput("utterance")
            luAct = self.lu.getOutput("action")
            luRew = self.lu.getOutput("reward")
            self.ll.setInput("luUtterance", luUtt)
            self.ll.setInput("luAction", luAct)
            self.ll.setReward(luRew)
            self.ll.loop()
            luMode = self.ll.getOutput("mode")
            llUtt = self.ll.getOutput("utterance")
            llAct = self.ll.getOutput("action")
            llRew = self.ll.getOutput("internalReward")
            self.printActivities(luUtt, luAct, luRew, llUtt, llAct, llRew)
        self.logFP.close()

    def printActivities(self, luUtt, luAct, luRew, llUtt, llAct, llRew):
        print >> self.logFP, "[LU Rew]: ", luRew
        print >> self.logFP, "[LU Utt]: ", luUtt
        print >> self.logFP, "[LU Act]: ", luAct
        print >> self.logFP, "[LL Rew]: ", llRew
        print >> self.logFP, "[LL Utt]: ", llUtt
        print >> self.logFP, "[LL Act]: ", llAct

    def setLogFile(self, path):
        try:
            self.logFP = open(path, 'w')
        except:
            print >> sys.stderr, "Fatal: error opening the log file."
            exit()

    def setRewardLog(self, path):
        rewardLog = None
        try:
            rewardLog = open(path, 'w')
        except:
            print >> sys.stderr, "Fatal: error opening the reward log file."
            exit()
        self.ll.setRewardLog(rewardLog)

    def setLearnerLog(self, path):
        learnerLog = None
        try:
            learnerLog = open(path, 'w')
        except:
            print >> sys.stderr, "Fatal: error opening the learner log file."
            exit()
        self.ll.setLearnerLog(learnerLog)