Ejemplo n.º 1
0
    def initPops(self):
        # create teams to fill population
        for i in range(self.teamPopSize):
            # take two distinct initial actions for each of two learners on team
            if not self.multiAction:  # choose single number
                ac1 = self.rand.choice(self.actions)
                ac2 = self.rand.choice([a for a in self.actions if a != ac1])
            else:  # choose list of length self.actions within range
                minv = self.actionRange[0]
                maxv = self.actionRange[1]
                ac1 = [
                    self.rand.uniform(minv, maxv) for i in range(self.actions)
                ]
                ac2 = [
                    self.rand.uniform(minv, maxv) for i in range(self.actions)
                ]

            team = Team()  # create new team

            # add/create first learner
            learner = Learner(ac1, self.maxProgramSize, randSeed=self.randSeed)
            team.addLearner(learner)
            self.learners.append(learner)

            # add/create second learner
            learner = Learner(ac2, self.maxProgramSize, randSeed=self.randSeed)
            team.addLearner(learner)
            self.learners.append(learner)

            # add other random learners
            learnerMax = self.rand.randint(0, self.maxTeamSize - 2)
            for i in range(learnerMax):
                if not self.multiAction:  # choose single number
                    ac = self.rand.choice(self.actions)
                else:  # choose list of length self.actions within range
                    ac = [
                        self.rand.uniform(minv, maxv)
                        for i in range(self.actions)
                    ]
                learner = Learner(ac,
                                  maxProgSize=self.maxProgramSize,
                                  randSeed=self.randSeed)
                team.addLearner(learner)
                self.learners.append(learner)

            team.uid = TpgTrainer.teamIdCounter
            TpgTrainer.teamIdCounter += 1

            # add into population
            self.teams.append(team)
            self.rootTeams.append(team)
Ejemplo n.º 2
0
    def generateNewTeams(self, parents=None):
        if parents is None:
            parents = list(
                self.rootTeams)  # parents are all original root teams
        # add teams until maxed size
        while (len(self.teams) < self.teamPopSize
               or (self.rTeamPopSize > 0
                   and self.getRootTeamsSize() < self.rTeamPopSize)):
            # choose 2 random teams as parents
            par1 = self.rand.choice(parents)
            par2 = self.rand.choice(
                [par for par in parents if par is not par1])

            # get learners
            par1Lrns = set(par1.learners)
            par2Lrns = set(par2.learners)

            # make 2 children at a time
            child1 = Team(birthGen=self.curGen)
            child2 = Team(birthGen=self.curGen)

            # new children get intersection of parents learners
            for learner in par1Lrns.intersection(par2Lrns):
                child1.addLearner(learner)
                child2.addLearner(learner)

            # learners unique to a parent goes to one child or other, with one
            # child having higher priority for a learner
            for learner in par1Lrns.symmetric_difference(par2Lrns):
                superChild = None
                subChild = None
                if self.rand.choice([True, False]) == True:
                    superChild = child1
                    subChild = child2
                else:
                    superChild = child2
                    subChild = child1

                # apply learner to child if can,
                # if not, give to other child if can
                if (len(superChild.learners) < self.maxTeamSize
                        and (len(superChild.learners) < 2
                             or len(subChild.learners) >= 2)):
                    superChild.addLearner(learner)
                else:
                    subChild.addLearner(learner)

            self.mutate(child1)  # attempt a mutation
            if (set(child1.learners) == set(par1.learners)
                    or set(child1.learners) == set(par2.learners)):
                while not self.mutate(
                        child1):  # attempt mutation untill it works
                    continue

            self.mutate(child2)  # attempt a mutation
            if (set(child2.learners) == set(par1.learners)
                    or set(child2.learners) == set(par2.learners)):
                while not self.mutate(
                        child2):  # attempt mutation untill it works
                    continue

            child1.uid = TpgTrainer.teamIdCounter
            TpgTrainer.teamIdCounter += 1
            child2.uid = TpgTrainer.teamIdCounter
            TpgTrainer.teamIdCounter += 1

            # add children to team populations
            self.teams.append(child1)
            self.teams.append(child2)
            self.rootTeams.append(child1)
            self.rootTeams.append(child2)