예제 #1
0
    def test_add_learners(self):

        team = Team(dummy_init_params)

        original_learners = team.learners

        l1 = create_dummy_learner()
        l2 = create_dummy_learner()

        l1_original_team_references = l1.numTeamsReferencing()
        l2_original_team_references = l2.numTeamsReferencing()

        team.addLearner(l1)
        team.addLearner(l2)

        #Ensure learner ref counts incremented
        self.assertEqual(l1_original_team_references + 1,
                         l1.numTeamsReferencing())
        self.assertEqual(l2_original_team_references + 1,
                         l2.numTeamsReferencing())

        #Ensure learner list grew
        self.assertEqual(2, len(team.learners))

        #Ensure no incomming learners appear in the team's inlearner list
        self.assertEqual(0, len(team.inLearners))
예제 #2
0
파일: trainer.py 프로젝트: mattthelee/PyTPG
    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)
예제 #3
0
def create_dummy_team(num_learners=2):

    team = Team(dummy_init_params)
    learners = []

    for x in range(0, num_learners):
        learner = create_dummy_learner()
        learners.append(learner)
        team.addLearner(learner)

    return team, learners
예제 #4
0
    def initializePopulations(self):
        for i in range(self.teamPopSize):
            # create 2 unique actions and learners
            a1,a2 = random.sample(range(len(self.actionCodes)), 2)

            l1 = Learner(self.mutateParams,
                        program=Program(maxProgramLength=self.initMaxProgSize,
                                         nOperations=self.nOperations,
                                         nDestinations=self.nRegisters,
                                         inputSize=self.inputSize,
                                         initParams=self.mutateParams),
                        actionObj=ActionObject(action=a1, initParams=self.mutateParams),
                        numRegisters=self.nRegisters)
            l2 = Learner(self.mutateParams,
                        program=Program(maxProgramLength=self.initMaxProgSize,
                                         nOperations=self.nOperations,
                                         nDestinations=self.nRegisters,
                                         inputSize=self.inputSize,
                                         initParams=self.mutateParams),
                        actionObj=ActionObject(action=a2, initParams=self.mutateParams),
                        numRegisters=self.nRegisters)

            # save learner population
            self.learners.append(l1)
            self.learners.append(l2)

            # create team and add initial learners
            team = Team(initParams=self.mutateParams)
            team.addLearner(l1)
            team.addLearner(l2)

            # add more learners
            moreLearners = random.randint(0, self.initMaxTeamSize-2)
            for i in range(moreLearners):
                # select action
                act = random.choice(range(len(self.actionCodes)))

                # create new learner
                learner = Learner(initParams=self.mutateParams,
                            program=Program(maxProgramLength=self.initMaxProgSize,
                                             nOperations=self.nOperations,
                                             nDestinations=self.nRegisters,
                                             inputSize=self.inputSize,
                                             initParams=self.mutateParams),
                            actionObj=ActionObject(action=act, initParams=self.mutateParams),
                            numRegisters=self.nRegisters)

                team.addLearner(learner)
                self.learners.append(learner)

            # save to team populations
            self.teams.append(team)
            self.rootTeams.append(team)
예제 #5
0
    def test_no_duplicate_learners(self):

        program = create_dummy_program()
        l1 = create_dummy_learner()
        l2 = create_dummy_learner()

        # Set the program to be the same for both learners
        l1.program = copy.deepcopy(program)
        l2.program = copy.deepcopy(program)

        # Create a team
        team = Team(dummy_init_params)

        # Add the first learner
        team.addLearner(l1)
예제 #6
0
    def initializePopulations(self, initMaxTeamSize, initMaxProgSize,
                              registerSize):
        for i in range(self.teamPopSize):
            # create 2 unique actions and learners
            if self.multiAction == False:
                a1, a2 = random.sample(self.actions, 2)
            else:
                a1 = [random.uniform(0, 1) for _ in range(self.actions)]
                a2 = [random.uniform(0, 1) for _ in range(self.actions)]
            l1 = Learner(program=Program(maxProgramLength=initMaxProgSize),
                         action=a1,
                         numRegisters=registerSize)
            l2 = Learner(program=Program(maxProgramLength=initMaxProgSize),
                         action=a2,
                         numRegisters=registerSize)

            # save learner population
            self.learners.append(l1)
            self.learners.append(l2)

            # create team and add initial learners
            team = Team()
            team.addLearner(l1)
            team.addLearner(l2)

            # add more learners
            moreLearners = random.randint(0, initMaxTeamSize - 2)
            for i in range(moreLearners):
                # select action
                if self.multiAction == False:
                    act = random.choice(self.actions)
                else:
                    act = [random.uniform(0, 1) for _ in range(self.actions)]
                # create new learner
                learner = Learner(
                    program=Program(maxProgramLength=initMaxProgSize),
                    action=act,
                    numRegisters=registerSize)
                team.addLearner(learner)
                self.learners.append(learner)

            # save to team populations
            self.teams.append(team)
            self.rootTeams.append(team)
예제 #7
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)
예제 #8
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)
예제 #9
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)