fitness = -sys.maxint

    else:
        new_stdout.seek(0)
        output = new_stdout.read()
        print output

        for (output_char, target_char) in zip(output, TARGET_PROGRAM_OUTPUT):
            fitness += character_fitness(output_char, target_char)

    return fitness


def breed_programs(prog1, prog2):
    return breed_strings(prog1, prog2, WEIGHTED_COMMANDS, MUTATION_RATE)


def stop_condition(candidate):
    if candidate.fitness == len(TARGET_PROGRAM_OUTPUT):
        return True
    else:
        return False


if __name__ == "__main__":
    ga.run_genetic_algorithm(spawn_func=generate_random_program,
                             breed_func=breed_programs,
                             fitness_func=calculate_fitness,
                             stop_condition=stop_condition,
                             population_size=POPULATION_SIZE)
    return child


def calc_fitness(func):
    try:
        x_vals = xrange(-100, 100, 1)
        reference_vals = map(target, x_vals)
        tested_vals = map(func, x_vals)
        differences = [(r - t) for (r, t) in zip(reference_vals, tested_vals)]
        sum_of_squares = sum([a*a for a in differences])
        return -sum_of_squares

    except ZeroDivisionError:
        return -sys.maxint


def stop_condition(candidate):
    if candidate.fitness == 0:
        return True
    else:
        return False


if __name__ == "__main__":
    ga.run_genetic_algorithm(spawn_func=generate_tree,
                             breed_func=breed,
                             fitness_func=calc_fitness,
                             stop_condition=stop_condition,
                             population_size=POPULATION_SIZE)
Exemplo n.º 3
0
    return generate_random_string(CHARACTERS, MAX_STRING_LENGTH)


def calculate_fitness(dna):
    fitness = 0
    for (a, b) in zip(dna, TARGET):
        if a == b:
            fitness += 1
    diff_length = abs(len(dna) - len(TARGET))
    fitness -= (diff_length*1.1)
    return fitness


def crossover(string1, string2):
    return breed_strings(string1, string2, CHARACTERS, MUTATION_RATE)


def stop_condition(candidate):
    if candidate.this == TARGET:
        return True
    else:
        return False


if __name__ == "__main__":
    ga.run_genetic_algorithm(spawn_func=generate_candidate,
                             breed_func=crossover,
                             fitness_func=calculate_fitness,
                             stop_condition=stop_condition,
                             population_size=POPULATION_SIZE)
Exemplo n.º 4
0
        fitness = -sys.maxint

    else:
        new_stdout.seek(0)
        output = new_stdout.read()
        print output

        for (output_char, target_char) in zip(output, TARGET_PROGRAM_OUTPUT):
            fitness += character_fitness(output_char, target_char)

    return fitness


def breed_programs(prog1, prog2):
    return breed_strings(prog1, prog2, WEIGHTED_COMMANDS, MUTATION_RATE)


def stop_condition(candidate):
    if candidate.fitness == len(TARGET_PROGRAM_OUTPUT):
        return True
    else:
        return False


if __name__ == "__main__":
    ga.run_genetic_algorithm(spawn_func=generate_random_program,
                             breed_func=breed_programs,
                             fitness_func=calculate_fitness,
                             stop_condition=stop_condition,
                             population_size=POPULATION_SIZE)
    return child


def calc_fitness(func):
    try:
        x_vals = xrange(-100, 100, 1)
        reference_vals = map(target, x_vals)
        tested_vals = map(func, x_vals)
        differences = [(r - t) for (r, t) in zip(reference_vals, tested_vals)]
        sum_of_squares = sum([a * a for a in differences])
        return -sum_of_squares

    except ZeroDivisionError:
        return -sys.maxint


def stop_condition(candidate):
    if candidate.fitness == 0:
        return True
    else:
        return False


if __name__ == "__main__":
    ga.run_genetic_algorithm(spawn_func=generate_tree,
                             breed_func=breed,
                             fitness_func=calc_fitness,
                             stop_condition=stop_condition,
                             population_size=POPULATION_SIZE)