Example #1
0
class RLCommander(Commander):
    """
        Rename and modify this class to create your own commander and add 
        mycmd.Placeholder to the execution command you use to run the 
        competition.
    """
    
    
    #LEARNING_PATH = '/media/jayden/Storage/Linux/AIGameDev/CaptureTheFlag/aisbx-0.20.6.linux-x86_64/' 
    LEARNING_PATH = '/home/jayden/AIGameDev/CaptureTheFlag/aisbx-0.20.5.linux-x86_64/'


    def initialize(self):
        """
            Use this function to setup your bot before the game starts.
        """
        self.verbose = True #display the command descriptions next to the bot.
        #Get the important game data to represent the current state.
        self.gameData = GameData(self.game, self.level) 
        #Get important data for each bot to represent the current state.
        self.bots = [BotData(name, self.game, self.level) for name in\
                     [bot.name for bot in self.game.team.members]]
        self.actions = Actions(self.game, self.level, self)
        #Set up reinforcement learning.
        self.agent = CTFAgent() 
        self.env = CTFEnvironment(self.actions.returnFunctions(), self.gameData, 
                                  self.bots, [bot for bot in self.game.team.members]) 
        self.rl = RLinterface(self.agent, self.env)
        self.checkForPastLearning() #Check for previous learning.
        self.rl.RL_start() #Start episode.
        self.tc = 0 #Count until reinforcement learning begins. Get bots out of spawn...       
        print 'Finished Commander Initialization...'


    def tick(self):
        """
            Override this function for your own bots.  
            Here you can access all the information in self.game,
            which includes game information, and self.level which includes 
            information about the level.
        """
        #if self.tc % 10 == 0:
        self.updateGameState()
        #self.gameData.printProperties()
        #print '\n\n\n'
        self.runRL()
        self.tc += 1


    def shutdown(self):
        """
            Use this function to teardown your bot after the game is over, 
            or perform an analysis of the data accumulated during the game.
        """
        print 'Writing out State Action Values...'
        self.agent.sendToFile_QValues()
        #self.agent.sendToFile_ActionValues()
        print 'Fin.'



    
    ######################################################################
    #Utility
    ######################################################################  

    def printStars(self):
        """
            Print stars for formatting output nicely.
        """
        print '*' * 80

    
    def updateGameState(self):
        """
            Update the data structures with the new information.
        """
        self.gameData.updateGameState(self.game)
        for bot in self.bots:
            bot.updateGameState(self.game, self.level)
 

    def printStateProperties(self):
        """
            Print the value of all the properties of our state.
        """
        self.printStars()
        print 'Game Properties'
        self.printStars()
        self.gameData.printProperties()
        self.printStars()
        print 'Bot Properties'
        for bot in self.bots:
            self.printStars()
            bot.printProperties()
            self.printStars()
        print '\n\n\n'


    def checkForPastLearning(self):
        """
            Checks whether learning has occured before and if so
            reads in this information and passes it to the agent.
        """
        print 'Checking for previous learning...'
        if os.path.exists(RLCommander.LEARNING_PATH) and \
        os.path.isfile(os.path.join(RLCommander.LEARNING_PATH, 'data.pkl')):
           print 'Found previous learning. Reading in data.'
           self.agent.readFromFile_QValues()
        else:
            print 'No previous learning. Starting with defaults.'


    def runRL(self):
        """
            Iterate through the bots on the team
            and run one step.
        """
        if self.tc > 0:
            for bot in self.game.team.members:
                self.rl.RL_step_two()
                #Update the botData and bot references.
                botData = [b for b in self.bots if b.botName == bot.name][0]
                self.env.botUpdate(bot, botData)
                self.rl.RL_step_one()
        else:
            print 'Taking a step - First tick iteration.'
            for bot in self.game.team.members:
                botData = [b for b in self.bots if b.botName == bot.name][0]
                self.env.botUpdate(bot, botData)
                self.rl.RL_step_one()
            print 'First tick iteration ending.'