示例#1
0
def printEquationPopulation(population, generation):
    size = len(population)
    equation_dict = {}
    #print (colored("Size of population: %d", 'blue') %(len(population)))

    for tree in population:
        path = []
        recombination.loadPaths(tree.root, path)
        equation = fitness.createEquation(path)
        error = fitness.fitnessValue(tree)
        if equation not in set(equation_dict.keys()):
            equation_dict[equation] = {}
            equation_dict[equation][error] = 1
        else:
            equation_dict[equation][error] += 1

    #Output Generation Number
    first_part = colored("\nGENERATION: %d ", 'magenta') % (generation)
    boundary = "=" * 100
    second_part = colored(boundary, 'magenta')
    print first_part + second_part

    #Sort based on value of dictionary
    sorted_equation = sorted(equation_dict.iteritems(), key=lambda x: x[1])
    for i, equation_and_error in enumerate(sorted_equation):
        for j in range(0, equation_and_error[1].values()[0]):
            equation = (colored("%s", 'green') % (equation_and_error[0]))
            error = (colored("Fitness: %s", 'blue') %
                     (equation_and_error[1].keys()[0]))
            print equation + " === " + error

    population_fitness = map(lambda x: x.fitness, population)
    mean_fitness = sum(population_fitness) / float(size)
    best_fitness = min(population_fitness)
    output_mean_fitness = colored("Mean Fitness: %f",
                                  'yellow') % (mean_fitness)
    output_best_fitness = colored("Best Fitness: %f", 'red') % (best_fitness)
    mean_best.write(str(mean_fitness) + " " + str(best_fitness) +
                    "\n")  #Write to file
    print output_mean_fitness + "\n" + output_best_fitness
def printEquationPopulation(population, generation):
	size = len(population)
	equation_dict = {}
	#print (colored("Size of population: %d", 'blue') %(len(population)))
	
        for tree in population:
		path = []
                recombination.loadPaths(tree.root, path)
                equation = fitness.createEquation(path)
                error = fitness.fitnessValue(tree)
		if equation not in set(equation_dict.keys()):
			equation_dict[equation] = {}
			equation_dict[equation][error] = 1
		else:
			equation_dict[equation][error] += 1
		
	#Output Generation Number
	first_part = colored("\nGENERATION: %d ", 'magenta') %(generation)
	boundary = "=" * 100
	second_part = colored(boundary, 'magenta')
	print first_part + second_part
	
	#Sort based on value of dictionary
	sorted_equation = sorted(equation_dict.iteritems(), key=lambda x: x[1])
	for i, equation_and_error in enumerate(sorted_equation):
		for j in range(0, equation_and_error[1].values()[0]):
			equation = (colored("%s", 'green') %(equation_and_error[0]))
			error = (colored("Fitness: %s", 'blue') %(equation_and_error[1].keys()[0]))
			print equation + " === " + error
			
	population_fitness = map(lambda x: x.fitness, population)
	mean_fitness = sum(population_fitness)/float(size)
	best_fitness = min(population_fitness)	
	output_mean_fitness = colored("Mean Fitness: %f", 'yellow') %(mean_fitness)	
	output_best_fitness = colored("Best Fitness: %f", 'red') %(best_fitness)
	mean_best.write(str(mean_fitness) +  " " + str(best_fitness) + "\n")  #Write to file
	print output_mean_fitness + "\n" + output_best_fitness
示例#3
0
def printEquation(tree):
    path = []
    recombination.loadPaths(tree.root, path)
    equation = fitness.createEquation(path)
    print equation
def printEquation(tree):
	path = []
	recombination.loadPaths(tree.root, path)
	equation = fitness.createEquation(path)
	print equation