Example #1
0
def evolve(pop, data, controls, generation=1):
    """This function examines each generation of programs, and if none meet
    the termination criterion, evolves a new generation and calls itself
    until an individual is found which satisfies the termination criterion,
    at which point it returns a dictionary containing the solution program and
    other info.
    """
    print("Generation:", generation)
    best_in_gen = pygp.termination_test(pop, data)
    
    if best_in_gen[1] < controls["target_fitness"]:
        return {"best":best_in_gen[0], "score":best_in_gen[1],
                "gen": generation}

    next_gen = []
    for i in range(len(pop)):
        choice = random()
        if choice < controls["cross_rate"]:
            child = pygp.subtree_crossover(pop, controls["tourn_size"], data)
        elif choice < controls["rep_rate"]:
            child = pygp.reproduction(pop, controls["tourn_size"], data)
        elif choice < controls["mut_rate"]:
            child = pygp.subtree_mutation(pop[i], controls["max_depth"])

        next_gen.append(child)

    return evolve(next_gen, data, controls, generation+1)
Example #2
0
def evolve(pop, generation=1):
    """This function examines each generation of programs, and if none meet
    the termination criterion, evolves a new generation and calls itself
    until an individual is found which satisfies the termination criterion,
    at which point it returns a dictionary with the solution program and
    other info. This function can also be found in the majorelements module, but
    is shown here for illustration.
    """
    print(generation)
    best_in_gen = pygp.termination_test(pop, data) # include a program return

    if best_in_gen[1] < target_fitness:
        return {"best":best_in_gen[0], "score":best_in_gen[1],
                "gen": generation}

    # if above fitness test fails, produce a new generation
    next_gen = []
    for i in range(len(pop)):
        choice = random()
        if choice < cross_rate:
            child = pygp.subtree_crossover(pop, tourn_size, data)
        elif choice < rep_rate:
            child = pygp.reproduction(pop, tourn_size, data)
        elif choice < mut_rate:
            child = pygp.subtree_mutation(pop[i], max_depth)

        next_gen.append(child)

    # After producing a new generation, call recursively
    return evolve(next_gen, generation+1)