def test_binary_val_default_params(): pop_shape = (6, 4) population = randint_population(pop_shape, 2) population = RandomMutations1D(max_gene_value=1, debug=1)(population) print(population) assert B.max(population) == 1 assert not B.min(population)
def test_randintpop(): shape = (100, 100, 10) pop = randint_population(shape, 42, 1) assert pop.shape == shape assert B.max(pop) == 42 assert B.min(pop) == 1 assert pop.dtype == B.intx()
def solve_onmax_2d(args): """Solve the onemax problem """ population_shape, generations = args population = randint_population(population_shape, 1) inputs = Input(population_shape) x = RandomMutations2D(max_gene_value=1)(inputs) outputs = UniformCrossover2D()(x) gf = EvoFlow(inputs, outputs, debug=0) fitness_function = Sum(max_sum_value=10000000) evolution_strategy = SelectFittest() gf.compile(evolution_strategy, fitness_function) gf.evolve(population, generations=generations, verbose=0)
def test_shuffle(): SHAPES = [(2, 4), (2, 4, 4), (2, 4, 4, 4)] for shape in SHAPES: population = randint_population(shape, max_value=255) previous_population = B.copy(population) population = Shuffle(1, debug=True)(population) diff = B.clip(abs(population - previous_population), 0, 1) assert B.is_tensor(population) assert population.shape == shape sum_diff = B.sum(diff) shape_size = int(B.prod(shape) / 2) assert sum_diff >= shape_size
def test_direct_2d(): NUM_EVOLUTIONS = 2 POPULATION_SIZE = 3 GENE_SIZE = 4 MAX_VAL = 10 SHAPE = (POPULATION_SIZE, GENE_SIZE, GENE_SIZE) population = randint_population(SHAPE, MAX_VAL) fitness_function = Sum(max_sum_value=GENE_SIZE) evolution_strategy = SelectFittest() inputs = Input(shape=SHAPE) # x = RandomMutations2D(max_gene_value=1, min_gene_value=0)(inputs) outputs = UniformCrossover2D()(inputs) ef = EvoFlow(inputs, outputs, debug=True) ef.compile(evolution_strategy, fitness_function) ef.evolve(population, generations=NUM_EVOLUTIONS, verbose=0)
def test_helloworld(): "Solve the MAXONE problem" NUM_EVOLUTIONS = 10 SHAPE = (20, 20) MAX_VAL = 1 population = randint_population(SHAPE, MAX_VAL) inputs = Input(SHAPE) x = RandomMutations1D(max_gene_value=1)(inputs) outputs = UniformCrossover1D()(x) gf = EvoFlow(inputs, outputs, debug=0) fitness_function = Sum(max_sum_value=SHAPE[1]) evolution_strategy = SelectFittest() gf.compile(evolution_strategy, fitness_function) gf.summary() results = gf.evolve(population, generations=NUM_EVOLUTIONS, callbacks=[DummyCallback()], verbose=0) assert results metrics = results.get_metrics_history() # check metrics for metric_grp, data in metrics.items(): for metric_name, vals in data.items(): assert isinstance(vals, list) if metric_grp == 'Fitness function': assert len(vals) == 10 assert isinstance(vals[9], float) assert 'mean' in data assert 'max' in data # assert the engine solved the (Trivial) problem max_fitness = metrics['Fitness function']['max'] assert max(max_fitness) == 1 assert max_fitness[9] == 1 assert min(max_fitness) < 1 # max sure we did improve # check population value population = results.get_populations() assert (population.shape) == SHAPE assert B.tensor_equal(population[0], B.tensor([1] * SHAPE[1]))
def test_mutation2d_eager(): pop_shape = (10, 4, 4) max_gene_value = 10 min_gene_value = 0 population_fraction = 1 mutations_probability = (0.5, 0.5) min_mutation_value = 1 max_mutation_value = 1 population = randint_population(pop_shape, max_gene_value) # save original original_population = B.copy(population) cprint('[Initial genepool]', 'blue') cprint(original_population, 'blue') for _ in range(3): RM = RandomMutations2D(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value, debug=True) population = RM(population) cprint('\n[Mutated genepool]', 'yellow') cprint(population, 'yellow') cprint('\n[Diff]', 'magenta') diff = population - original_population cprint(diff, 'magenta') assert B.is_tensor(population) assert population.shape == pop_shape assert B.max(diff) <= max_mutation_value ok = 0 for chromosome in diff: if B.sum(chromosome) >= 2: ok += 1 if (ok / len(diff)) > 0.5: break assert (ok / len(diff)) > 0.5
def test_ND(): "test various tensor size random" TEST_INPUTS = [ [RandomMutations1D, (2, 4), 0.5], [RandomMutations2D, (2, 4, 4), (0.5, 0.5)], [RandomMutations3D, (2, 4, 4, 4), (0.5, 0.5, 0.5)], ] for inputs in TEST_INPUTS: OP = inputs[0] pop_shape = inputs[1] mutations_probability = inputs[2] max_gene_value = 10 min_gene_value = 0 population_fraction = 1 min_mutation_value = 1 max_mutation_value = 1 population = randint_population(pop_shape, max_gene_value) # eager RM = OP(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value) population = RM(population) assert B.is_tensor(population) assert population.shape == pop_shape # graph RM = OP(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value) population = RM._call_from_graph(population) assert B.is_tensor(population) assert population.shape == pop_shape
def build_op_test(shape): from evoflow.population import randint_population from .ops.random_mutation import bench_random_mutation from .ops.uniform_crossover import bench_uniform_crossover from .ops.single_crossover import bench_single_crossover from .ops.dual_crossover import bench_dual_crossover from .ops.reverse import bench_reverse from .ops.shuffle import bench_shuffle from .utils import shape2opdim TESTS = [] population = randint_population(shape, max_value=255) OP_DIM = shape2opdim(shape) TESTS.append(['OP', 'Shuffle', OP_DIM, bench_shuffle, population, shape]) TESTS.append([ 'OP', 'RandomMutation', OP_DIM, bench_random_mutation, population, shape ]) TESTS.append([ 'OP', 'UniformCrossover', OP_DIM, bench_uniform_crossover, population, shape ]) TESTS.append([ 'OP', 'SingleCrossover', OP_DIM, bench_single_crossover, population, shape ]) TESTS.append([ 'OP', 'DualCrossover', OP_DIM, bench_dual_crossover, population, shape ]) TESTS.append(['OP', 'Reverse', OP_DIM, bench_reverse, population, shape]) return TESTS
def test_clip(): pop_shape = (100, 100) population = randint_population(pop_shape, 100) population = RandomMutations1D(max_gene_value=100, debug=1)(population) assert B.max(population) <= 100 assert not B.min(population)
def build_fittest_test(shape): population = randint_population(shape, 256) # test_type, group, name, fn, args, shape = test # single test for now but still return a list(test) OP_DIM = shape2opdim(shape) return [['Selection', 'Fittest', OP_DIM, bench_fittest, population, shape]]