def fitnessValue(tree): path = [] recombination.loadPaths(tree.root, path) #Load market data and stores in respective dictionaries apple_data = scrape.getAppleData() nasdaq_data = scrape.getNasdaqData() sp500_data = scrape.getSP500Data() nodeReplace(path) equation = createEquation(path) data_size = range(0, len(apple_data)) predicted_prices = map( lambda i: evaluateEquation(i, nasdaq_data, apple_data, sp500_data, equation), data_size) next_day_prices = map(lambda data: data['apple_close'], apple_data) #next_day_price is the actual price predicted_prices.pop() #Removes the last item in the list next_day_prices.pop(0) #Removes the first item in the list zipped = zip(predicted_prices, next_day_prices) errors = map(lambda pair: abs(pair[1] - pair[0]), zipped) fitness = sum(errors) / float(len(errors)) tree.fitness = fitness #Adds fitness to tree object in-place return fitness #For printEquationPopulation function
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
def fitnessValue(tree): path = [] recombination.loadPaths(tree.root, path) #Load market data and stores in respective dictionaries apple_data = scrape.getAppleData() nasdaq_data = scrape.getNasdaqData() sp500_data = scrape.getSP500Data() nodeReplace(path) equation = createEquation(path) data_size = range(0, len(apple_data)) predicted_prices = map(lambda i: evaluateEquation(i, nasdaq_data, apple_data, sp500_data, equation), data_size) next_day_prices = map(lambda data: data['apple_close'], apple_data) #next_day_price is the actual price predicted_prices.pop() #Removes the last item in the list next_day_prices.pop(0) #Removes the first item in the list zipped = zip(predicted_prices, next_day_prices) errors = map(lambda pair: abs(pair[1] - pair[0]), zipped) fitness = sum(errors)/float(len(errors)) tree.fitness = fitness #Adds fitness to tree object in-place return fitness #For printEquationPopulation function
def printEquation(tree): path = [] recombination.loadPaths(tree.root, path) equation = fitness.createEquation(path) print equation