Esempio n. 1
0
class Runner:
    def __init__(self, numEpochs, numItterations, agentCode):
        self._numEpochs = numEpochs
        self._numItterations = numItterations
        self._agentCode = agentCode

        self._gamegrid = GameGrid()
        self._gamegrid.hide()

        self._trainingRecord = []

    def createAgent(self,
                    gameSessionFile=None,
                    trainName=None,
                    trainData=None,
                    trainDataPickle=None,
                    existingAgent=None):
        if (self._agentCode == 0):
            self._gamegrid.setAgent(RandomAgent(None, waitTime=0))
        elif (self._agentCode == 1):
            self._gamegrid.setAgent(PatternAgentULRD(None, waitTime=0))
        elif (self._agentCode == 2):
            self._gamegrid.setAgent(PatternAgentLURD(None, waitTime=0))
        elif (self._agentCode == 3):
            self._gamegrid.setAgent(ManualAgent(None, waitTime=0))
        elif (self._agentCode == 4):
            self._gamegrid.setAgent(
                DNNAgent(None,
                         waitTime=0,
                         gameSessionFile=gameSessionFile,
                         trainName=trainName,
                         trainData=trainData,
                         trainDataPickle=trainDataPickle))

        self._gamegrid.getAgent().setGameGrid(self._gamegrid)

    def refreshGameGrid(self):
        agent = self._gamegrid.getAgent()
        self._gamegrid = GameGrid()
        self._gamegrid.hide()

        agent.reset()

        self._gamegrid.setAgent(agent)
        self._gamegrid.getAgent().setGameGrid(self._gamegrid)

    def runTraining(self):
        for epochNum in range(0, self._numEpochs):
            for itterNum in range(0, self._numItterations):
                self.refreshGameGrid()
                self._gamegrid.setAgent(self._agent)

                print("Epoch: ", epochNum, " Iteration: ", itterNum)
                self._gamegrid.mainloop()

                # print(gamegrid.matrix)
                print("Score: ", self._gamegrid.scoreMatrix())
                self._agent.setScore(self._gamegrid.scoreMatrix())

                # The current code running AI games needs to know the current epochNum for encoding filename
                (boards, moves, score) = self._agent.getGameRecord()
                #                 self._agent.pikPakGame()
                self._trainingRecord.append(
                    (epochNum, itterNum, boards, moves, score))
        return self._trainingRecord
Esempio n. 2
0
def main():
    existingAgent1 = None
    with open("TrainingPartialCountRunner_100_20_4_5.pickle", 'rb') as f:
        existingAgent0 = pickle.load(f)

    with open("ULRD_trained_model_20_game_layers_32_16.pickle", 'rb') as f:
        existingAgent1 = pickle.load(f)

    with open("ULRD_trained_model_20_game_layers_64_16.pickle", 'rb') as f:
        existingAgent2 = pickle.load(f)

    with open("ULRD_trained_model_20_game_layers_64_16_8.pickle", 'rb') as f:
        existingAgent3 = pickle.load(f)

    with open("ULRD_trained_model_20_game_layers_64_32_8.pickle", 'rb') as f:
        existingAgent4 = pickle.load(f)

    with open("ULRD_trained_model_20_game_layers_64.pickle", 'rb') as f:
        existingAgent5 = pickle.load(f)

    agentDict = {
        1: RandomAgent(None, waitTime=0),
        2: PatternAgentULRD(None, waitTime=0),
        0: existingAgent0,
        3: DNNAgent(None, waitTime=0, trainName="ULRD_train.pickle"),
        4: existingAgent1,
        5: existingAgent2,
        6: existingAgent2,
        7: existingAgent2,
        8: existingAgent2
    }
    agentDescription = {
        1: "Random",
        2: "Up-Left-Right-Down",
        0: "Online learning NN",
        3: "DNN Agent",
        4: "DNN Agent with layers [32, 16]",
        5: "DNN Agent with layers [64, 16]",
        6: "DNN Agent with layers [64, 16, 8]",
        7: "DNN Agent with layers [64, 32, 8]",
        8: "DNN Agent with layers [64]"
    }
    agentScoreDict = {
        1: [],
        2: [],
        0: [],
        3: [],
        4: [],
        5: [],
        6: [],
        7: [],
        8: []
    }
    agentColors = {
        1: "b",
        2: "r",
        0: "#1f004d",
        3: "g",
        4: "c",
        5: "m",
        6: "y",
        7: "k",
        8: "#3CFE6E"
    }

    gameIDs = []
    for i in range(0, 15):
        gameIDs.append(i)
        random.seed(i)
        for (agentKey, agent) in agentDict.items():
            gamegrid = GameGrid()
            gamegrid.hide()
            gamegrid.setAgent(agent)
            agent.setGameGrid(gamegrid)
            gamegrid.mainloop()
            agentScoreDict[agentKey].append(sumScoreMatrix(gamegrid.matrix))
            print(agentScoreDict[agentKey])
            agent.reset()

    plotTrainingRecord(gameIDs, agentDict, agentDescription, agentScoreDict,
                       agentColors)