Exemplo n.º 1
0
def test_mutate():
    parent = ClassicGRN()
    parent.random(5, 5, 5)
    config.MUTATION_ADD_RATE = 1.0
    child = evolution.mutate(parent)
    assert parent.size() == child.size() - 1
    assert parent.distance_to(child) < 1 / (child.size() + 2)
    config.MUTATION_ADD_RATE = 0.0
    config.MUTATION_DEL_RATE = 1.0
    child = evolution.mutate(parent)
    assert parent.size() == child.size() + 1
    assert parent.distance_to(child) < 1 / (child.size() + 2)
    config.MUTATION_DEL_RATE = 0.0
    child = evolution.mutate(parent)
    assert parent.size() == child.size()
    assert parent.distance_to(child) < 1 / (child.size() + 2)
Exemplo n.º 2
0
    def make_offspring(self):
        self.offspring = []
        for sp in self.species:
            if sp.num_offspring == 0:
                continue
            species_offspring = []
            # Create children with crossover
            for k in range(int(sp.num_offspring * config.CROSSOVER_RATE)):
                parent1 = sp.tournament_select()
                parent2 = sp.tournament_select()
                # Don't use the same parent 2x
                num_tries = 0
                while ((parent1 == parent2) and
                       (num_tries < config.MAX_SELECTION_TRIES)):
                    parent2 = sp.tournament_select()
                    num_tries += 1
                if parent1 != parent2:
                    species_offspring += [Individual(
                        crossover(parent1.grn, parent2.grn))]
            # Create children with mutation
            for k in range(int(sp.num_offspring * config.MUTATION_RATE)):
                parent = sp.tournament_select()
                species_offspring += [Individual(mutate(parent.grn))]
            # Add elite
            if len(species_offspring) == sp.num_offspring:
                species_offspring[np.random.randint(
                    len(species_offspring))] = sp.get_best_individual()
            else:
                species_offspring += [sp.get_best_individual()]
            # Fill with tournament champions if extra space
            while len(species_offspring) < sp.num_offspring:
                species_offspring += [sp.tournament_select().clone()]

            self.offspring += species_offspring
Exemplo n.º 3
0
def test_representative_distances():
    species = evolution.Species()
    assert species.sum_adjusted_fitness is None
    problem = Random()
    rep = evolution.Individual(ClassicGRN())
    rep.grn.random(problem.nin, problem.nout, 1)
    species.representative = rep
    for i in range(10):
        ind = evolution.Individual(evolution.mutate(rep.grn))
        species.individuals += [ind]
    dists0 = species.get_representative_distances()
    assert np.sum(dists0) > 0
    rep.grn.random(problem.nin, problem.nout, 1)
    dists1 = species.get_representative_distances()
    assert np.sum(dists1) > 0
    assert np.sum(dists1) > np.sum(dists0)
Exemplo n.º 4
0
    def speciation(self):
        for sp in self.species:
            sp.representative = random.choice(sp.individuals).clone()
            sp.reset()

        for ind in self.offspring:
            species_match = None
            min_dist = np.inf
            for spi in range(len(self.species)):
                sp_dist = self.species[spi].representative.grn.distance_to(
                    ind.grn)
                if ((sp_dist < min_dist) and
                    (sp_dist < self.species[spi].species_threshold)):
                    species_match = spi
                    min_dist = sp_dist
            if species_match is not None:
                self.species[species_match].individuals += [ind]
            else:
                new_sp = Species()
                new_sp.individuals += [ind]
                new_sp.representative = ind.clone()
                self.species += [new_sp]

        self.offspring = []

        num_removed = 0
        species_to_remove = []
        for sp in self.species:
            if len(sp.individuals) < config.MIN_SPECIES_SIZE:
                num_removed += len(sp.individuals)
                species_to_remove += [sp]
                del sp.representative
                for ind in sp.individuals:
                    del ind

        self.species = list(set(self.species) - set(species_to_remove))

        if len(self.species) == 0:
            raise Exception(('The entire population was removed. '
                             'Try again with a smaller MIN_SPECIES_SIZE '
                             'or a larger INITIALIZATION_DUPLICATION'))

        while num_removed > 0:
            sp = random.choice(self.species)
            ind = sp.tournament_select()

            tries = 0
            has_added = False
            while not has_added:
                child_grn = mutate(ind.grn)

                if (child_grn.distance_to(sp.representative.grn) <
                    sp.species_threshold):
                    sp.individuals += [Individual(child_grn)]
                    num_removed -= 1
                    has_added = True
                tries += 1
                if tries == config.MAX_SELECTION_TRIES:
                    ind = Individual(child_grn)
                    new_sp = Species()
                    new_sp.individuals += [ind]
                    new_sp.representative = ind.clone()
                    self.species += [new_sp]
                    num_removed -= 1
                    has_added = True