Esempio n. 1
0
    def generate(self):

        oLearners = list(self.learners)
        oTeams = list(self.teams)

        # multiActs for action pool for multiaction mutation
        if self.multiAction:
            multiActs = []
            for learner in oLearners:
                if learner.isActionAtomic():
                    multiActs.append(list(learner.action))
        else:
            multiActs = None

        while (len(self.teams) < self.teamPopSize
               or self.countRootTeams() < self.rTeamPopSize):

            # get parent root team, and child to be based on that
            parent = random.choice(self.rootTeams)
            child = Team()

            # child starts just like parent
            for learner in parent.learners:
                child.addLearner(learner)

            if self.uniqueProgThresh > 0:
                inputs, outputs = self.getLearnersInsOuts(oLearners)
            else:
                inputs = None
                outputs = None
            # then mutates
            child.mutate(self.pDelLrn,
                         self.pAddLrn,
                         self.pMutLrn,
                         oLearners,
                         self.pMutProg,
                         self.pMutAct,
                         self.pActAtom,
                         self.actions,
                         oTeams,
                         self.pDelInst,
                         self.pAddInst,
                         self.pSwpInst,
                         self.pMutInst,
                         multiActs,
                         self.pSwapMultiAct,
                         self.pChangeMultiAct,
                         self.uniqueProgThresh,
                         inputs=inputs,
                         outputs=outputs,
                         update=True)

            self.teams.append(child)
            self.rootTeams.append(child)
Esempio n. 2
0
    def generate(self, extraTeams=None):

        # extras who are already part of the team population
        protectedExtras = []
        extrasAdded = 0

        # add extras into the population
        if extraTeams is not None:
            for team in extraTeams:
                if team not in self.teams:
                    self.teams.append(team)
                    extrasAdded += 1
                else:
                    protectedExtras.append(team)

        oLearners = list(self.learners)
        oTeams = list(self.teams)

        # update generation in mutateParams
        self.mutateParams["generation"] = self.generation

        # get all the current root teams to be parents
        while (len(self.teams) < self.teamPopSize + extrasAdded or
                (self.rootBasedPop and self.countRootTeams() < self.teamPopSize)):
            # get parent root team, and child to be based on that
            parent = random.choice(self.rootTeams)
            child = Team(initParams=self.mutateParams)

            # child starts just like parent
            for learner in parent.learners:
                child.addLearner(learner)

            # then mutates
            child.mutate(self.mutateParams, oLearners, oTeams)

            self.teams.append(child)

        # remove unused extras
        if extraTeams is not None:
            for team in extraTeams:
                if team.numLearnersReferencing() == 0 and team not in protectedExtras:
                    self.teams.remove(team)