def main(): #Get the sequence to be searched using FitnessCalculator.set_solution(str(raw_input("SEQUENCE: "))) #Define the population pop_size = int(raw_input("Enter population size: ")) elite_option = str(raw_input("Elitism enabled?(y/n): ")) #Elite switch if elite_option == 'y': elitism_enabled = True else: elitism_enabled = False #Create a starting popultation population = Population(pop_size, True) #Generation count generation = 0 evolution = Evolution(elitism_enabled) #Loop until we dont have a solution while population.get_fittest().get_fitness() < FitnessCalculator.get_max_fitness(): generation += 1 print "GENERATION : " + str(generation) + " FITTEST: " + str(population.get_fittest().get_fitness()) #Evolve the population population = evolution.evolve(population) print "SOLUTION FOUND..." print "GENERATION: " + str(generation) print "GENES: " + population.get_fittest().get_string() return
def make_new_evolution(name, confusion, mask): print('Making new evolution') evolution = Evolution(confusion, mask) evolution.run_tests() ranked_preys = evolution.rank_preys() ranked_predators = evolution.rank_predators() evolution.update_pools(ranked_predators, ranked_preys) evolution.save(name) return evolution
def runParallelEvolutions(threads_number=2): print(f"Runing {threads_number} paralel evolutions") evolutions, evolution_threads = [], [] for h in range(threads_number): evolutions.append(Evolution()) evolution_threads.append(Thread(target=evolutions[h].runEvolutionLoop)) evolution_threads[h].start() for evolution_thread in evolution_threads: if evolution_thread.isAlive(): evolution_thread.join() best_of_them_all = evolutions[0] for evolution in evolutions: print( f"Verifying evolution best fit with fitness score of {evolution.best_fit.fitness_score}" ) best_of_them_all = evolution if evolution.best_fit.fitness_score > best_of_them_all.best_fit.fitness_score else best_of_them_all return best_of_them_all
def run_evolution(self): tronEvo = Evolution() tronEvo.run() return tronEvo
#Viz Stuff viz_row = 3 viz_col = 3 Viz = Vizulization(viz_row, viz_col) energy = [] force = [] fit_history = [] if showViz: FitnessGraph = Viz.createGraph() Viz.labelGraph(FitnessGraph, "FitnessGraph") Viz.legendGraph(FitnessGraph, "Max Fitness", c="red") Viz.setStruts(num_bars) #smaller stru ts, less play but faster convergence..... GA = Evolution(num_struts = num_bars, strut_length = 10, max_gen=1000,init_size =population, pop_size=population, mutation_rate=m_rate, \ selection_rate = s_rate, selection_pressure = s_pressure,elite_rate=e_rate) #Essentailly just have an autmated hill climber rn, b/c mutation rate is so highself. GA.load(load) GA.eps = 1 while GA.alive(): if GA.current_gen % 25 == 0: GA.save(population) print("----------------------") print("Curr Gen: ", GA.current_gen) print("Pop Sicze", len(GA.pop)) for i in np.arange(len(GA.pop)): drone_structure = GA.pop[i][2] # loop through current population Solver.solve(drone_structure, i) # drones have already been solved so they go very fast fit = GA.fitness(drone_structure) # evaluate drone
def reward(predictions, _y_train): score = 0 if len(predictions) != len(_y_train): raise ValueError('Lengths are not the same') for pred, correct in zip(predictions, _y_train): if pred[0] == correct: score += correct_score return score """THIS FORM IS ALWAYS USED""" def solve(predict, _X_train): predictions = list() for sample in _X_train: predictions.append(predict(sample)) return predictions if __name__ == '__main__': X_train, y_train = read_data('../data/xor_train.txt') evolution = Evolution(reward, solve) evolution.start_simulation(X_train, y_train, int(len(X_train) * correct_score * 0.95)) X_test, y_test = read_data('../data/xor_test.txt')