Beispiel #1
0
    def _fitnessEvaluations(self, pacmen, ghosts):
        totalGames = max(len(pacmen), len(ghosts))
        pacManNumber = 0
        ghostNumber = 0
        for evaluation in range(totalGames):
            pacMan = None
            ghost = None
            if pacManNumber > len(pacmen) - 1:
                pacManNumber = 0
            if ghostNumber > len(ghosts) - 1:
                ghostNumber = 0
            pacMan = pacmen[pacManNumber]
            ghost = ghosts[ghostNumber]

            engine = GameEngine(self.configDict, pacMan.parseTree,
                                ghost.parseTree)
            engine.runGame()
            pacMan, ghost = self._evaluateFitness(engine, pacMan, ghost)
            pacmen[pacManNumber] = pacMan
            ghosts[ghostNumber] = ghost

            pacManNumber += 1
            ghostNumber += 1

        return pacmen, ghosts
Beispiel #2
0
    def _initializePopulation(self):
        newPacMen = []
        numberOfPacMen = self.configDict["pacmanPopulationSize"]
        if self.totalEvals < numberOfPacMen:
            numberOfPacMen = self.totalEvals
        newGhosts = []
        numberOfGhosts = self.configDict["ghostPopulationSize"]
        if self.totalEvals < numberOfGhosts:
            numberOfGhosts = self.totalEvals

        totalGames = max(numberOfPacMen, numberOfGhosts)
        for evaluation in range(totalGames):
            pacMan = None
            ghost = None
            # Random number passed to _generateSolution implements ramped half and half.
            if numberOfPacMen > 0:
                pacMan = self._generateSolution("pac-man",
                                                random.randint(0, 1))
                numberOfPacMen -= 1
            if numberOfGhosts > 0:
                ghost = self._generateSolution("ghost", random.randint(0, 1))
                numberOfGhosts -= 1

            reusedPacManInd = -1
            reusedGhostInd = -1
            if not pacMan:
                reusedPacManInd = random.randint(0, len(newPacMen) - 1)
                pacMan = newPacMen[reusedPacManInd]
            elif not ghost:
                reusedGhostInd = random.randint(0, len(newGhosts) - 1)
                ghost = newGhosts[reusedGhostInd]

            engine = GameEngine(self.configDict, pacMan.parseTree,
                                ghost.parseTree)
            engine.runGame()
            pacMan, ghost = self._evaluateFitness(engine, pacMan, ghost)\

            if reusedPacManInd != -1:
                newPacMen[reusedPacManInd] = pacMan
            else:
                newPacMen.append(pacMan)

            if reusedGhostInd != -1:
                newGhosts[reusedGhostInd] = ghost
            else:
                newGhosts.append(ghost)

        self.pacmanPopulation = newPacMen
        self.ghostPopulation = newGhosts