示例#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)
示例#2
0
def test_evaluate():
    ind = evolution.Individual(ClassicGRN())
    problem = Random()
    ind.grn.random(problem.nin, problem.nout, 1)
    assert ind.fitness == 0.0
    fitness = ind.get_fitness(problem)
    assert ind.fitness == fitness
    assert fitness != 0
示例#3
0
 def __init__(self, problem, new_grn_function=lambda: ClassicGRN(),
              run_id=str(uuid4()), grn_dir='grns', log_dir='logs'):
     pathlib.Path(grn_dir).mkdir(parents=True, exist_ok=True)
     pathlib.Path(log_dir).mkdir(parents=True, exist_ok=True)
     self.grn_file = os.path.join(grn_dir, 'grns_' + run_id + '.log')
     self.log_file = os.path.join(log_dir, 'fits_' + run_id + '.log')
     self.problem = problem
     self.population = Population(new_grn_function, problem.nin,
                                  problem.nout)
     self.generation = 0
示例#4
0
def test_thresholds():
    problem = Random()
    config.INITIALIZATION_DUPLICATION = 8
    pop = evolution.Population(lambda: ClassicGRN(),
                               problem.nin, problem.nout)
    pop.evaluate(problem)
    pop.speciation()
    pop.adjust_thresholds()
    assert np.any([sp.species_threshold for sp in pop.species] !=
                  config.START_SPECIES_THRESHOLD)
示例#5
0
def test_best():
    problem = Counter()
    pop = evolution.Population(lambda: ClassicGRN(),
                               problem.nin, problem.nout)
    pop.evaluate(problem)
    assert problem.count == config.POPULATION_SIZE
    pop.speciation()
    best_fit, best_ind = pop.get_best()
    assert problem.count == config.POPULATION_SIZE
    assert best_fit > 0
示例#6
0
def test_mutate_remove():
    parent = ClassicGRN()
    parent.random(5, 5, 5)
    child = evolution.mutate_remove(parent)
    assert len(parent.inhibitors) == len(child.inhibitors) + 1
    assert len(parent.enhancers) == len(child.enhancers) + 1
    assert len(parent.identifiers) == len(child.identifiers) + 1
    assert parent.size() == child.size() + 1
    assert parent.distance_to(child) < 1 / (parent.size() + 2)
示例#7
0
def test_offspring_count():
    problem = Random()
    config.INITIALIZATION_DUPLICATION = 8
    pop = evolution.Population(lambda: ClassicGRN(),
                               problem.nin, problem.nout)
    pop.evaluate(problem)
    pop.speciation()
    pop.adjust_thresholds()
    pop.set_offspring_count()
    assert (np.sum([sp.num_offspring for sp in pop.species]) ==
            config.POPULATION_SIZE)
示例#8
0
def test_speciation():
    problem = Counter()
    config.INITIALIZATION_DUPLICATION = 8
    pop = evolution.Population(lambda: ClassicGRN(),
                               problem.nin, problem.nout)
    pop.evaluate(problem)
    pop.speciation()
    assert len(pop.offspring) == 0
    assert len(pop.species) > 0
    print([len(sp.individuals) for sp in pop.species])
    assert problem.count == config.POPULATION_SIZE
示例#9
0
def test_fit_increase():
    problem = Counter()
    problem.cacheable = True
    evo = Evolution(problem, lambda: ClassicGRN())
    evo.step()
    assert np.any([sp.sum_adjusted_fitness > 0.0
                   for sp in evo.population.species])
    best_fit, best_ind = evo.population.get_best()
    evo.run(10)
    new_best_fit, new_best_ind = evo.population.get_best()
    assert np.any([sp.sum_adjusted_fitness > 0.0
                   for sp in evo.population.species])
    assert new_best_fit >= best_fit
示例#10
0
def test_sum_adjusted_fitness():
    species = evolution.Species()
    assert species.sum_adjusted_fitness is None
    problem = Random()
    for i in range(10):
        ind = evolution.Individual(ClassicGRN())
        ind.grn.random(problem.nin, problem.nout, 1)
        species.individuals += [ind]
    assert species.get_adjusted_fitness(0.0, 1.0) == 0.0
    assert species.sum_adjusted_fitness == 0.0
    for ind in species.individuals:
        ind.get_fitness(problem)
    assert species.get_adjusted_fitness(0.0, 1.0) > 0.0
    assert species.sum_adjusted_fitness > 0.0
示例#11
0
def test_make_offspring():
    problem = Counter()
    config.INITIALIZATION_DUPLICATION = 8
    pop = evolution.Population(lambda: ClassicGRN(),
                               problem.nin, problem.nout)
    pop.evaluate(problem)
    pop.speciation()
    pop.adjust_thresholds()
    pop.set_offspring_count()
    pop.make_offspring()
    assert problem.count == config.POPULATION_SIZE
    assert len(pop.offspring) == config.POPULATION_SIZE
    assert np.any([ind.evaluated for ind in pop.offspring])
    assert np.any([~ind.evaluated for ind in pop.offspring])
示例#12
0
def test_mutate_modify():
    parent = ClassicGRN()
    parent.random(5, 5, 5)
    child = evolution.mutate_modify(parent)
    if parent.beta == child.beta and parent.delta == child.delta:
        assert (np.sum(parent.inhibitors != child.inhibitors) +
                np.sum(parent.enhancers != child.enhancers) +
                np.sum(parent.identifiers != child.identifiers)) == 1
    assert len(parent.inhibitors) == len(child.inhibitors)
    assert len(parent.enhancers) == len(child.enhancers)
    assert len(parent.identifiers) == len(child.identifiers)
    assert parent.size() == child.size()
    assert parent.distance_to(child) < 1 / (parent.size() + 2)
示例#13
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)
示例#14
0
def test_noncacheable_counter():
    problem = Counter()
    problem.cacheable = False
    evo = Evolution(problem, lambda: ClassicGRN())
    evo.step()
    assert np.any([sp.sum_adjusted_fitness > 0.0
                   for sp in evo.population.species])
    assert problem.count == config.POPULATION_SIZE
    evo.step()
    assert np.any([sp.sum_adjusted_fitness > 0.0
                   for sp in evo.population.species])
    assert problem.count == 2 * config.POPULATION_SIZE
    evo.step()
    assert np.any([sp.sum_adjusted_fitness > 0.0
                   for sp in evo.population.species])
    assert problem.count == 3 * config.POPULATION_SIZE
示例#15
0
def test_mutate_add():
    parent = ClassicGRN()
    parent.random(5, 5, 5)
    child = evolution.mutate_add(parent)
    assert np.all(parent.inhibitors == child.inhibitors[0:-1])
    assert np.all(parent.enhancers == child.enhancers[0:-1])
    assert np.all(parent.identifiers == child.identifiers[0:-1])
    assert len(parent.inhibitors) == len(child.inhibitors) - 1
    assert len(parent.enhancers) == len(child.enhancers) - 1
    assert len(parent.identifiers) == len(child.identifiers) - 1
    assert parent.size() == child.size() - 1
    assert parent.distance_to(child) < 1 / (child.size() + 2)
示例#16
0
def test_evaluation():
    problem = Counter()
    pop = evolution.Population(lambda: ClassicGRN(),
                               problem.nin, problem.nout)
    pop.evaluate(problem)
    assert problem.count == config.POPULATION_SIZE
示例#17
0
def test_eq_size_crossover():
    parent1 = ClassicGRN()
    parent2 = ClassicGRN()
    parent1.random(5, 5, 5)
    parent2.random(5, 5, 5)
    child = evolution.crossover(parent1, parent2)
    assert (child.beta == parent1.beta) or (child.beta == parent2.beta)
    assert (child.delta == parent1.delta) or (child.delta == parent2.delta)
    assert parent1.size() == child.size()
    assert parent2.size() == child.size()
    assert parent1.distance_to(child) < parent1.distance_to(parent2)
    assert parent2.distance_to(child) < parent2.distance_to(parent1)
    assert parent1.distance_to(child) > 0
    assert parent2.distance_to(child) > 0
示例#18
0
def test_init():
    problem = Counter()
    pop = evolution.Population(lambda: ClassicGRN(),
                               problem.nin, problem.nout)
    assert len(pop.offspring) == config.POPULATION_SIZE
    assert len(pop.species) == 0