Example #1
0
    errors = []
    for x in range(10):
        # Create the push interpreter and run program
        interpreter = PushInterpreter()
        y_hat = interpreter.run(program, inputs=[x],
                                output_types=['_integer'])[0]
        # Get output
        if y_hat is not None:
            # compare to target output
            target_int = target_function(x)
            # calculate error
            errors.append((y_hat - target_int)**2)
        else:
            errors.append(1e5)
    return errors


# Genetic operators
mut = UniformMutation()
alt = Alternation()
ops = [(Alternation(), 0.6), (UniformMutation(), 0.2),
       (VariationOperatorPipeline((alt, mut)), 0.2)]

if __name__ == "__main__":
    evo = SimplePushGPEvolver(n_jobs=-1,
                              verbose=1,
                              operators=ops,
                              selection_method='epsilon_lexicase',
                              atom_generators=list(all_instructions))
    evo.fit(error_func, 1, ['_integer'])
Example #2
0
            errors.append(1e5)
        else:
            errors.append(int(result != case[1]))
    return errors


atom_generators = list(merge_sets(
    get_instructions_by_pysh_type("_integer"),
    get_instructions_by_pysh_type("_boolean"),
    get_instructions_by_pysh_type("_print"),
    get_instructions_by_pysh_type("_exec"),
    [lambda: random.randint(-100, 100)]
))


alternation = Alternation(rate=0.01, alignment_deviation=5)
mutation = UniformMutation(rate=0.01)
genetic_operators = [
    (alternation, 0.2),
    (mutation, 0.2),
    (PerturbCloseMutation(rate=0.1), 0.1),
    (VariationOperatorPipeline((alternation, mutation)), 0.5)
]


if __name__ == "__main__":
    evo = SimplePushGPEvolver(n_jobs=-1, verbose=2,
                              atom_generators=atom_generators,
                              initial_max_genome_size=400,
                              operators=genetic_operators,
                              population_size=1000,
Example #3
0
        e = 0

        if output[0] is None:
            e += 1000
        elif output[0] != targets[0]:
            e += 1

        if output[1] is None:
            e += 1000
        elif output[1] != targets[1]:
            e += 1

        errors.append(e)
    return errors


atom_generators = list(merge_sets(get_instructions_by_pysh_type('_boolean'),
                                  get_instructions_by_pysh_type('_exec')))
mut = UniformMutation(rate=0.1)
alt = Alternation(rate=0.1, alignment_deviation=10)
ops = [(alt, 0.2), (mut, 0.3), (VariationOperatorPipeline((mut, alt)), 0.5)]


if __name__ == "__main__":
    evo = SimplePushGPEvolver(n_jobs=-1, verbose=1, operators=ops,
                              atom_generators=atom_generators,
                              initial_max_genome_size=300,
                              population_size=500, max_generations=300,
                              simplification_steps=5000)
    evo.fit(error_function, 3, ['_boolean', '_boolean'])