Example #1
0
def single_fit(g, initial_num=42):
    """Returns the fitness given a specific gene."""
    return float((1.0 / (1.0 + abs(initial_num - gene.evaluate(g)))))
Example #2
0
def generate_fitness(gene_array, initial_num=42):
    """Generates a list of fitnesses for a given array of genes."""
    return [
        float((0.00001 / (0.00001 + abs(initial_num - gene.evaluate(g)))))
        for g in gene_array
    ]
Example #3
0
def generate_fitness(gene_array, initial_num=42):
    """Generates a list of fitnesses for a given array of genes."""
    return [float((0.00001 / (0.00001 +
                          abs(initial_num - gene.evaluate(g)))))
            for g in gene_array]
Example #4
0
def single_fit(g, initial_num=42):
    """Returns the fitness given a specific gene."""
    return float((1.0 / (1.0 +
                         abs(initial_num - gene.evaluate(g)))))
Example #5
0
# Keep track of the previous and max fit to prevent cluttering of the console.
prev_gene = ""

while not gene_pool[-1][1] == 1:
# When the Max fitted gene pool is 1, this is when a solution is found.
    # ALWAYS write to the file, but don't print to stdout
    f.write("\nGENERATION: %d\n" % (generation))
    for g, fit in gene_pool:
        f.write("%s  %1.8f  %s\n" % (g, fit, gene.show_eq(g)))

    # If there is a new maximum, do the verbose output
    if not (prev_gene == gene_pool[-1][0]):
        os.system('clear')
        print "GENERATION: %d\n" % (generation)
        print "Best Gene\n\t - Fitness: %1.5f\n" % (gene_pool[-1][1])
        print "\t - Value: %d\n" % (gene.evaluate(gene_pool[-1][0]))
        for g, fit in gene_pool:
            print "%s  %1.8f  %s" % (g, fit, gene.show_eq(g))

    # Reproduce 2 genes and add the resulting ones to the list of genes
    ng1, ng2 = manage.reproduce(gene_pool, REPRODUCTION_RATE, MUTATION_RATE)
    manage.add(gene_pool, ng1, FINAL_NUMBER, MAX_GENE_POOL)
    manage.add(gene_pool, ng2, FINAL_NUMBER, MAX_GENE_POOL)

    generation += 1
    prev_gene = gene_pool[-1][1]


# Pretty print things when the genes have solved the problem
f.write("\nGENERATION: %d\n" % (generation))
for g, fit in gene_pool: