def test_max_gene_val_2d(): MAX_VAL = 10 t = B.randint(0, MAX_VAL + 1, (10, 10, 10)) max_sum_value = MAX_VAL * 10 * 10 v = Sum(max_sum_value=max_sum_value).call(t) assert v.shape == (10, ) for t in v: assert t < 1
def test_sum2d(): t = [[[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]]] inputs = B.tensor(t) print(inputs) result = Sum().call(inputs) assert result.shape == (3, ) print(result) expected = B.tensor([9, 9, 9]) assert B.tensor_equal(result, expected)
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_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 bench_fittest(population): fitness_function = Sum() selector = SelectFittest() selected_pop, fitness_scores, metrics = selector(fitness_function, population, population)