Пример #1
0
    def run(self, ngen=10, population=None,startfile=None, checkpoint=None, cp_freq=1):
        """Run the genetic algorithm, updating the population over ngen number of generations.

        Keywork arguments:
        ngen -- number of generations to run the genetic algorithm.
        startfile -- File containing existing population (default None)
        checkpoint -- File containing existing checkpoint (default None)

        Output:
        population -- Resulting population after ngen generations.

        """
        
        if startfile:
            try:
                print(f"Reading in {startfile}")
                population = self.readpop(startfile)
            except FileNotFoundError:
                print("WARNING: Start file not found")
            except:
                raise
        
        if not population:
            print(f"Initializing a new random population")
            population = self.newpopulation()
            if checkpoint:
                self.writepop(population, filename=f"{checkpoint}")
        

        for cur_g in range(0, ngen+1):
            print(f"Generation {cur_g} of population size {len(population)}")
            
            histogram = Segmentors.popCounts(population)
            print(f"#HIST - {histogram}")
            
            _, population = self.popfitness(population)
            
            bestsofar = self.hof[0]
            seg = Segmentors.algoFromParams(bestsofar)
            mask = seg.evaluate(self.img)
            fitness = Segmentors.FitnessFunction(mask, self.mask)
            print(f"#BEST - {fitness} - {bestsofar}")

            if checkpoint and cur_g%cp_freq == 0:
                print(f"Writing Checkpoint file - {checkpoint}")
                copyfile(f"{checkpoint}", f"{checkpoint}.prev")
                self.writepop(population, filename=f"{checkpoint}")
                for cur_p in range(len(population)):
                    logging.getLogger().info(population[cur_p])
            if cur_g < ngen+1:          
                if bestsofar.fitness.values[0] >= 0.95:
                    population = self.newpopulation()
                  # if the best fitness value is at or above the
                  # threshold of 0.95, discard the entire current
                  # population and randomly select a new population
                  # for the next generation
                  # note: setting keep_prob = 0 and mutate_prob = 1
                  # as mutate arguments
                  # should have same result as self.new_population()
                else:                
                    population = self.mutate(population)
                  # if the best fitness value is below this threshold,
                  # proceed as normal, mutating the current population
                  # to get the next generation 
            
        if checkpoint:
            print(f"Writing Checkpoint file - {checkpoint}")
            copyfile(f"{checkpoint}", f"{checkpoint}.prev")
            self.writepop(population, filename=f"{checkpoint}")
            for cur_p in range(len(population)):
                logging.getLogger().info(population[cur_p])
        return population
Пример #2
0
    def run(self,
            ngen=10,
            population=None,
            startfile=None,
            checkpoint=None,
            cp_freq=1):
        """Run the genetic algorithm, updating the population over ngen number of generations.

        Keywork arguments:
        ngen -- number of generations to run the genetic algorithm.
        startfile -- File containing existing population (default None)
        checkpoint -- File containing existing checkpoint (default None)

        Output:
        population -- Resulting population after ngen generations.

        """

        if startfile:
            try:
                print(f"Reading in {startfile}")
                population = self.readpop(startfile)
            except FileNotFoundError:
                print("WARNING: Start file not found")
            except:
                raise

        if not population:
            print(f"Initializing a new random population")
            population = self.newpopulation()
            if checkpoint:
                self.writepop(population, filename=f"{checkpoint}")

        for cur_g in range(0, ngen + 1):
            print(f"Generation {cur_g} of population size {len(population)}")

            histogram = Segmentors.popCounts(population)
            print(f"#HIST - {histogram}")

            _, population = self.popfitness(population)

            bestsofar = self.hof[0]
            seg = Segmentors.algoFromParams(bestsofar)
            mask = seg.evaluate(self.img)
            fitness = Segmentors.FitnessFunction(mask, self.mask)
            print(f"#BEST - {fitness} - {bestsofar}")

            if checkpoint and cur_g % cp_freq == 0:
                print(f"Writing Checkpoint file - {checkpoint}")
                copyfile(f"{checkpoint}", f"{checkpoint}.prev")
                self.writepop(population, filename=f"{checkpoint}")
                for cur_p in range(len(population)):
                    logging.getLogger().info(population[cur_p])
            if cur_g < ngen + 1:
                population = self.mutate(population)

        if checkpoint:
            print(f"Writing Checkpoint file - {checkpoint}")
            copyfile(f"{checkpoint}", f"{checkpoint}.prev")
            self.writepop(population, filename=f"{checkpoint}")
            for cur_p in range(len(population)):
                logging.getLogger().info(population[cur_p])
        return population