Beispiel #1
0
    def runEvolution(self):
        # Start evolution
        logging.info("Start of evolution")
        for self.gIdx in range(self.gIdx + 1, self.gIdx + self.traj.NGEN):
            # ------- Weed out the invalid individuals and replace them by random new indivuals -------- #
            validpop = self.getValidPopulation(self.pop)

            # add all valid individuals to the population history
            self.popHist[self.gIdx] = self.getValidPopulation(self.pop)

            # replace invalid individuals
            nanpop = self.getInvalidPopulation(self.pop)
            logging.info("Replacing {} invalid individuals.".format(
                len(nanpop)))
            newpop = self.toolbox.population(n=len(nanpop))
            newpop = self.tagPopulation(newpop)

            # self.pop = validpop + newpop

            # ------- Create the next generation by crossover and mutation -------- #
            ### Select parents using rank selection and clone them ###
            offspring = list(
                map(self.toolbox.clone,
                    self.toolbox.selRank(self.pop, self.POP_SIZE)))

            ##### cross-over ####
            for i in range(1, len(offspring), 2):
                offspring[i - 1], offspring[i] = self.toolbox.mate(
                    offspring[i - 1], offspring[i], indpb=self.CXPB)
                # del offspring[i - 1].fitness, offspring[i].fitness
                del offspring[i -
                              1].fitness.values, offspring[i].fitness.values
                del offspring[i -
                              1].fitness.wvalues, offspring[i].fitness.wvalues
                # print("Deleting ID of {} and {}".format(offspring[i - 1].id, offspring[i].id))
                del offspring[i - 1].id, offspring[i].id

            ##### Mutation ####
            # Apply adaptive mutation
            du.mutateUntilValid(offspring, self.paramInterval, self.toolbox)

            offspring = self.tagPopulation(offspring)

            # ------- Evaluate next generation -------- #

            logging.info("----------- Generation %i -----------" % self.gIdx)

            self.pop = offspring + newpop
            self.evalPopulationUsingPypet(self.traj, self.toolbox,
                                          offspring + newpop, self.gIdx)
            # ------- Select surviving population -------- #

            # select next population
            self.pop = self.toolbox.selBest(validpop + offspring + newpop,
                                            k=self.traj.popsize)

            self.best_ind = self.toolbox.selBest(self.pop, 1)[0]

            if self.verbose:
                print("----------- Generation %i -----------" % self.gIdx)
                print("Best individual is {}".format(self.best_ind))
                print("Score: {}".format(self.best_ind.fitness.score))
                print("Fitness: {}".format(self.best_ind.fitness.values))
                print("--- Population statistics ---")
                eu.printParamDist(self.pop, self.paramInterval, self.gIdx)
                eu.plotPopulation(self.pop,
                                  self.paramInterval,
                                  self.gIdx,
                                  draw_scattermatrix=True,
                                  save_plots=self.trajectoryName)

            # save all simulation data to pypet
            try:
                self.pop = eu.saveToPypet(self.traj, self.pop, self.gIdx)
            except:
                logging.warn("Error: Write to pypet failed!")

        logging.info("--- End of evolution ---")
        self.best_ind = self.toolbox.selBest(self.pop, 1)[0]
        logging.info("Best individual is %s, %s" %
                     (self.best_ind, self.best_ind.fitness.values))
        logging.info("--- End of evolution ---")

        self.traj.f_store(
        )  # We switched off automatic storing, so we need to store manually
 def test_mutateUntilValid(self):
     du.mutateUntilValid(self.pop,
                         self.evolution.paramInterval,
                         self.evolution.toolbox,
                         maxTries=10)