示例#1
0
def test_non_tensor_input():
    shape = (2, 4)
    population = [[1, 2, 3, 4], [1, 2, 3, 4]]
    inputs = Input(shape)
    inputs.assign(population)
    res = inputs.get()
    assert B.is_tensor(res)
示例#2
0
def solve_tsp(args):
    population_shape, generations, cities, chart_data, distances, idx2city = args  # noqa

    NUM_CITIES = len(cities)
    NUM_REVERSE_OPERATIONS = 4
    MAX_REVERSE_PROBABILITY = 0.3
    REVERSE_POPULATION_FRACTION = 0.3
    MIN_REVERSE_PROBABILITY = 0.1

    SHUFFLE_POPULATION_FRACTION = 0.2

    population = uniform_population(population_shape)
    rpi = MAX_REVERSE_PROBABILITY / NUM_REVERSE_OPERATIONS
    reverse_probabilty = 1 - rpi

    # Evolution model
    inputs = Input(shape=population.shape)
    x = inputs
    for idx in range(NUM_REVERSE_OPERATIONS):
        x = Reverse1D(population_fraction=REVERSE_POPULATION_FRACTION,
                      max_reverse_probability=reverse_probabilty)(x)
        reverse_probabilty = max(reverse_probabilty - rpi,
                                 MIN_REVERSE_PROBABILITY)

    x = Shuffle(population_fraction=SHUFFLE_POPULATION_FRACTION)(x)
    outputs = x
    ef = EvoFlow(inputs, outputs, debug=False)

    evolution_strategy = SelectFittest(mode='min')
    fitness_fn = TSPFitness(distances, NUM_CITIES)
    ef.compile(evolution_strategy, fitness_fn)
    ef.evolve(population, generations=generations, verbose=0)
示例#3
0
def test_graph():
    "When passing OPs GF is supposed to return a graph"
    i = Input((42, 1))
    d1 = Dummy()(i)
    d2 = Dummy()(i)
    r = Dummy()([d1, d2])
    assert issubclass(type(r), Dummy)
    assert r.call(42) == 42  # explicitly execute the graph op
示例#4
0
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)
示例#6
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]))
示例#7
0
def test_solve_tsp():
    population_shape = (150, 10)
    generations = 40

    cities, chart_data, distances, idx2city = tsp_setup(10)
    BASELINE = 6000
    NUM_CITIES = len(cities)
    NUM_REVERSE_OPERATIONS = 4
    MAX_REVERSE_PROBABILITY = 0.3
    REVERSE_POPULATION_FRACTION = 0.3
    MIN_REVERSE_PROBABILITY = 0.1

    SHUFFLE_POPULATION_FRACTION = 0.2

    population = uniform_population(population_shape)
    rpi = MAX_REVERSE_PROBABILITY / NUM_REVERSE_OPERATIONS
    reverse_probabilty = 1 - rpi

    # Evolution model

    inputs = Input(shape=population.shape)
    x = inputs
    for idx in range(NUM_REVERSE_OPERATIONS):
        x = Reverse1D(population_fraction=REVERSE_POPULATION_FRACTION,
                      max_reverse_probability=reverse_probabilty)(x)
        reverse_probabilty = max(reverse_probabilty - rpi,
                                 MIN_REVERSE_PROBABILITY)

    x = Shuffle(population_fraction=SHUFFLE_POPULATION_FRACTION)(x)
    outputs = x
    ef = EvoFlow(inputs, outputs, debug=False)

    evolution_strategy = SelectFittest(mode='min')
    fitness_fn = TSPFitness(distances, NUM_CITIES)
    ef.compile(evolution_strategy, fitness_fn)
    results = ef.evolve(population, generations=generations, verbose=0)
    metrics = results.get_latest_metrics()
    assert metrics['Fitness function']['min'] < BASELINE
示例#8
0
def test_2d():
    shape = (128, 64, 64)
    population = B.randint(1, 10, shape=shape)
    inputs = Input(shape)
    inputs.assign(population)
    assert B.tensor_equal(inputs.get(), population)
示例#9
0
def test_call_vs_get():
    shape = (128, 64)
    population = B.randint(1, 10, shape=shape)
    inputs = Input(shape)
    inputs.assign(population)
    assert B.tensor_equal(inputs.get(), inputs.call(''))