Ejemplo n.º 1
0
    def _cross_over(self, positive_progress_only=False):
        if len(self.population) <= 1:
            return

        cross_over_occur = math.ceil(len(self.population) * self.cross_over_rate)

        result = []
        for i in range(int(cross_over_occur)):
            a, b = random.sample(self.population, 2)
            offspring = Chromosome.cross_over(a, b)
            if not positive_progress_only or (offspring.Fitness > max(a.Fitness, b.Fitness)):
                result.append(offspring)
        self.population.extend(result)
Ejemplo n.º 2
0
    def _cross_over(self, always=False):
        if len(self.population) < 2:
            return

        cross_over_occur = math.ceil(len(self.population) * self.cross_over_rate)
        list_of_fertile_genes = [i for i in range(len(self.population))]

        for i in range(int(cross_over_occur)):
            a, b = random.sample(list_of_fertile_genes, 2)
            for idx, k in enumerate(list_of_fertile_genes):
                if k == b or k == a:
                    del list_of_fertile_genes[idx]

            offspring_a, offspring_b = Chromosome.cross_over(self.population[a], self.population[b])
            if (offspring_a.error < min(self.population[a].error, self.population[b].error) and \
               offspring_b.error < min(self.population[a].error, self.population[b].error)) or always:
                self.population[a] = offspring_a
                self.population[b] = offspring_b