def main(): goals = [101, 150] routes = [(1, goals[0]), (1, goals[1])] raw_graph = Graph(mapfile__name='./graph/waterloo.map') # path = [1, 23, 45, 66, 88, 89, 67, 87, 66, 45, 24, 23, 45, 67, 68, 67, 89, 111, 131, 111, 133, 154, 155, 156, 135, 136, 156, 177, 156, 135, 157, 178, 179, 178, 179, 180, 159, 160, 182, 181, 161, 183, 184, 206, 228, 207, 228, 207, 206, 228, 208, 230] # raw_graph.plotMovementsMultiPaths([path], [(1, 230)], './result/before_ga.png') # path = remove_cycle(path) # print path # raw_graph.plotMovementsMultiPaths([path], [(1, 230)], './result/after_ga.png') # return graph = raw_graph.toGraphNode() # generate ecegraph # IOManager.export_graph(graph, '../aco/waterloo.ecegraph') # graph = IOManager.import_graph('../aco/waterloo.ecegraph') solver = ACOSolver.from_graph(graph, goals) score, solutions, order = solver.solve() solutions = [soln[0] for soln in solutions] paths = [] ga_scores = [] removed = {} for goal in goals: end = goal population = Population(graph[START_NODE], end, removed) path = population.evolve() path = remove_cycle(path) new_cost = Chromosome.calc_costs(path, graph, goal, removed) print 'new path: ' + str(path) print 'new cost: ' + str(new_cost) ga_scores.append(new_cost) paths.append(path) # remove path from graph for p in path: removed[p] = p if raw_graph != None: raw_graph.plotMovementsMultiPaths(solutions, routes, './result/aco.png') raw_graph.plotMovementsMultiPaths(paths, routes, './result/ga.png') print 'GA:' print 'scores: ' + str(ga_scores) print 'paths: ' + str(paths) print 'ACO:' print('Best overall score: {}'.format(score)) print('Best order of goal nodes: {}'.format(order)) print('Best path solutions: {}'.format(solutions))
def __generate_cities(map, hyperparameters, simulation): max_pop_size = Population.max_pop_size(hyperparameters['pop_size'], hyperparameters['elitism_n'], hyperparameters['truncation_percentage']) print('Maximum population size:', max_pop_size) return [City.generate(map['size'], map['size'], map['intersections'], simulation['max_cars'], simulation['max_cars_spawn'], map['seed']) for _ in range(max_pop_size)]
def __init__(self): if Handler.__instance is not None: raise Exception("This is a Singleton!") else: Handler.__instance = self self.is_running = True self.window_size = (510, 600) self.play_area_size = [(10, 10), (10, 500), (500, 500), (500, 10)] self.play_area_boundaries = { 'top border': 10, 'bottom border': 490, 'left border': 10, 'right border': 490 } self.directions = { 'up': [0, -10], 'down': [0, 10], 'left': [-10, 0], 'right': [10, 0] } self.current_direction = self.directions['right'] self.run_speed = 100 self.fuel = 130 self.points = 0 self.time_lasted = 0 self.edible = Edible() self.snake = Snake() self.ga = GeneticAlgorithm() self.population = Population(50, True) self.current_snake = 0 self.current_generation = 0
def plot_net(): node_ordering = node_ordering = { 0: 'asia', 1: 'tub', 2: 'smoke', 3: 'bronc', 4: 'lung', 5: 'either', 6: 'dysp', 7: 'xray' } Population.init_state(POPULATION_SIZE, DATA.columns, node_ordering) new = as_net( np.array([ 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0 ])) new.plot()
def main(): env = gym.make('SlimeVolley-v0') env.seed(123) POPULATION_SIZE = 20 MAX_GENERATION = 100 MUTATION_RATE = 0.4 CROSSOVER_RATE = 0.7 INPUT_SIZE = 12 OUTPUT_SIZE = 3 assert POPULATION_SIZE % 2 == 0 p = Population(DeepMLPTorchIndividual(INPUT_SIZE, 0, OUTPUT_SIZE), POPULATION_SIZE, MAX_GENERATION, MUTATION_RATE, CROSSOVER_RATE, 0.0) p.set_population([ DeepMLPTorchIndividual(INPUT_SIZE, 0, OUTPUT_SIZE) for _ in range(POPULATION_SIZE) ]) p.run( env, generation, verbose=True, log=True, output_folder=f'model-layers={INPUT_SIZE}-{HIDDEN_SIZE}-{OUTPUT_SIZE}', save_as_pytorch=False)
def generate(self) -> Population: """ 种群生成算法 """ individuals = [] for _ in range(self.individual_num): # setup phenotype phenotype = Phenotype() values = [] for val_range in self.individual_meta.range_list: val = random() * (val_range[1] - val_range[0]) + val_range[0] values.append(val) phenotype.phenotype = values individual = Individual() individual.meta = self.individual_meta individual.phenotype = phenotype individuals.append(individual) population = Population() population.individuals = individuals return population
def evolve(self, population): new_population = Population(len(population.individuals), False) new_population.individuals.append(population.get_fittest()) for i in range(len(population.individuals) - self.elitism_offset): if i <= self.elitism_offset: continue mother = self.tournament(population) father = self.tournament(population) baby = self.reproduce(mother, father) new_population.individuals.append(baby) for i in range(len(new_population.individuals)): if i <= self.elitism_offset: continue self.mutate(new_population.individuals[i]) return new_population
class PopulationTestCase(TestCase): def setUp(self): self.population = Population() def testMutateCross(self): fake_cm_plugin = FakeCmPlugin() self.population.individuals = [1, 2, 3] self.population(fake_cm_plugin) self.population.mutate() self.population.cross() self.assertEqual(self.population.cm_plugin, fake_cm_plugin) self.assertEqual( fake_cm_plugin.c_count, fake_cm_plugin.m_count) def testSelect(self): fake_selector = FakeSelector() self.population.use_selector(fake_selector) self.population.individuals = [1, 2, 3] self.population.select() self.assertEqual( fake_selector.i_count, len(self.population.individuals))
# If children fitness is greater thant parents update population if child1.fitness + child2.fitness > parent1.fitness + parent2.fitness: new_population[i] = child1 new_population[i + 1] = child2 else: new_population[i] = parent1 new_population[i + 1] = parent2 if __name__ == '__main__': env = gym.make('CarRacing-v0') POPULATION_SIZE = 100 MAX_GENERATION = 2 MUTATION_RATE = 0.1 CROSSOVER_RATE = 0.8 p = Population(ConvNetTorchIndividal(None, None, None), POPULATION_SIZE, MAX_GENERATION, MUTATION_RATE, CROSSOVER_RATE, 0) p.run(env, generation, verbose=False, output_folder='') env.close() ''' 초기화 for in max_generation (총 세대수){ for old_population : 적합성 계산 ( 100 episode * population_size ) run_generation(env, old, new, mutation, crossover){ for old population / 2 parent1,parent2 = 적합성 기준 2개 select
def ga(maxPop, mutation_rate, chromosome_length, maxGen): pop = Population(maxPop, mutation_rate, chromosome_length=chromosome_length) # populating with maxPop elements pop.populate() i = 0 for i in range(maxGen): # calculating fitness pop.calc_fitness() # natural selection parents = pop.selection() # crossover + mutation pop.crossover(parents) pop.mutate() # printing print('Gen ' + str(i) + ": ", pop.getFittest()[0], pop.getAvgFitness()) pop.population[pop.getFittest()[1]].plot() pop.population[pop.getFittest()[1]].plot()
def main(dataset): # node_ordering = order_based_on_conditional_entropy_sum_on_line(dataset) #un dictionar de forma{i:nume variabila}. variabila[i] poate fi parinte pentru orice j>i # print(node_ordering) # node_ordering = {0: 'asia', 1: 'tub', 2: 'bronc', 3: 'xray', 4: 'lung', 5: 'smoke', 6: 'dysp', 7: 'either'} # node_ordering ={0: 'asia', 1: 'tub', 2: 'smoke', 3: 'bronc', 4: 'lung', 5: 'either', 6: 'dysp', 7: 'xray'} #perfect node ordering # node_ordering = {0: 'diabetes', 1: 'flatulence', 2: 'upper_pain', 3: 'hepatotoxic', 4: 'anorexia', 5: 'nausea', 6: 'hepatomegaly', 7: 'hepatalgia', 8: 'bleeding', 9: 'vh_amn', 10: 'hospital', 11: 'fat', 12: 'transfusion', 13: 'joints', 14: 'proteins', 15: 'hbsag', 16: 'density', 17: 'le_cells', 18: 'ascites', 19: 'skin', 20: 'alcoholism', 21: 'surgery', 22: 'spiders', 23: 'hcv_anti', 24: 'RHepatitis', 25: 'hbsag_anti', 26: 'choledocholithotomy', 27: 'fatigue', 28: 'hbc_anti', 29: 'THepatitis', 30: 'palms', 31: 'edema', 32: 'pain', 33: 'consciousness', 34: 'pain_ruq', 35: 'obesity', 36: 'alcohol', 37: 'edge', 38: 'jaundice', 39: 'itching', 40: 'gallstones', 41: 'injections', 42: 'spleen', 43: 'sex', 44: 'pressure_ruq', 45: 'Hyperbilirubinemia', 46: 'irregular_liver', 47: 'hbeag', 48: 'encephalopathy', 49: 'ama', 50: 'fibrosis', 51: 'amylase', 52: 'PBC', 53: 'Steatosis', 54: 'urea', 55: 'alt', 56: 'ESR', 57: 'triglycerides', 58: 'inr', 59: 'albumin', 60: 'ChHepatitis', 61: 'phosphatase', 62: 'cholesterol', 63: 'ast', 64: 'carcinoma', 65: 'platelet', 66: 'bilirubin', 67: 'Cirrhosis', 68: 'age', 69: 'ggtp'} node_ordering = perfect_order_alarm() Population.init_state(POPULATION_SIZE, dataset.columns, node_ordering) generation = Population() generation.random() generation.load() print('Population shape:', generation.shape) no_generation = 0 log = open(LOG_FILE, 'a') init_writer() no_same_score = 0 old_fitness = 0 ttt = time.time() while no_generation < MAX_NO_GENERATION and no_same_score < MAX_NO_GENERATION_SAME_SCORE: print('Generation: ', no_generation, generation.shape) t=time.time() fitness = np.array(get_fitness(generation.population_as_nets)) print(" after fitness computation", time.time() - t) # t = time.time() indexes_of_best_fitnesses = np.argsort(fitness)[-ELITIST:] new_generation = Population( generation.population_as_array[indexes_of_best_fitnesses], generation.population_as_nets[indexes_of_best_fitnesses] ) # print(" after elitism", time.time() - t) # t = time.time() log.write(str(np.max(fitness)) + '\n') log.flush() print(str(np.max(fitness)) + '\n') if np.max(fitness) == old_fitness: no_same_score+=1 else: no_same_score = 0 old_fitness = np.max(fitness) no_pairs = int((POPULATION_SIZE - ELITIST) / 2) for _ in range(no_pairs): index_p1, index_p2 = select(fitness) offsprings = crossover(generation.get(index_p1), generation.get(index_p2)) new_generation.add_individs(offsprings) # print(" after crossover", time.time() - t) # t=time.time() for i in range(POPULATION_SIZE): new_generation.add_individs([mutate(generation.get(i), Population.index_to_feature,dataset)]) print(" after mutation") generation = copy.deepcopy(new_generation) no_generation += 1 if no_generation % INTERMEDIAR_SAVE == 0: fitness = np.array(get_fitness(generation.population_as_nets)) write(fitness, generation) if no_generation%25 ==0: checkpoint(generation) # print(" end generation", time.time() - t) else: fitness = np.array(get_fitness(generation.population_as_nets)) write(fitness, generation) checkpoint(generation) print('final time: ', time.time() - ttt)
def revolution(self, args): # Config if (args.crossmode == "one"): cross_func = Crossover.onePoint elif (args.crossmode == "two"): cross_func = Crossover.twoPoints else: cross_func = Crossover.randomPoints # Prepare dataset dataset = readDataset(args.dpath) # Create individuals & population ppl = Population() for i in range(args.individuals): individual = Individual() individual.createGene(dataset, args.gene) ppl.addInd(individual) # Evolution for i in range(args.revolution): # Evaluation population in individuals ppl.calcFitness(self.evaluate, args) if ((i % 10) == 0): ppl.show() # Select parents if (args.elite): parents = Select.Elite(ppl, args.esize, args.mode) parents.extend( Select.Tournament(ppl, args.individuals - args.esize, args.tornsize, args.mode)) else: parents = Select.Tournament(ppl, args.individuals, args.tornsize, args.mode) # Clossover children = cross_func(parents, args.individuals, args.gene, args.crossrate) # Mutation children = Mutation.mutation(children, args.mutaterate, dataset) # Swap children ppl.setPpl(children) # show result ppl.calcFitness(self.evaluate, args) ppl.show()
from ga.population import Population import numpy as np dataHandler = DataHandler(['apple_test_data.csv', 'NASDAQ_test_data.csv']) #dataHandler = DataHandler(['apple_data.csv', 'NASDAQ_data.csv']) data = dataHandler.get_signal_list() signal_ranges = dataHandler.signal_ranges() #print signal_ranges data_element = len(data[0]) #print "data[0]" #print data[0] population = Population(100 , signal_ranges, data) #for i in population: # print i.transform_individual(signal_ranges) initial_fitess_values = map(population.fitness_function, population) #print initial_fitess_values number_generations = 100 population.sus_sampler(initial_fitess_values) max_list = [] mean_list = [] for i in xrange(number_generations): #print "initial population" #for i in population:
if __name__ == '__main__': env = gym.make('BipedalWalker-v2') env.seed(123) POPULATION_SIZE = 10 MAX_GENERATION = 10 MUTATION_RATE = 0.01 CROSSOVER_RATE = 0.8 INPUT_SIZE = 24 OUTPUT_SIZE = 4 assert POPULATION_SIZE % 2 == 0 p = Population(DeepBipedalWalkerIndividual(INPUT_SIZE, 0, OUTPUT_SIZE), POPULATION_SIZE, MAX_GENERATION, MUTATION_RATE, CROSSOVER_RATE, 0.0) p.set_population([ DeepBipedalWalkerIndividual(INPUT_SIZE, 0, OUTPUT_SIZE) for _ in range(POPULATION_SIZE) ]) p.run( env, generation, verbose=True, log=True, output_folder=f'model-layers={INPUT_SIZE}-{HIDDEN_SIZE}-{OUTPUT_SIZE}', save_as_pytorch=False) env.close()
# 4. if np.random.rand() < p_inversion: child1.weights_biases = inversion(child1.weights_biases) child2.weights_biases = inversion(child2.weights_biases) new_population.append(child1) new_population.append(child2) if __name__ == '__main__': env = gym.make('BipedalWalker-v2') env.seed(123) POPULATION_SIZE = 10 MAX_GENERATION = 20 MUTATION_RATE = 0.01 CROSSOVER_RATE = 0.8 INVERSION_RATE = 0.02 INPUT_SIZE = 10 HIDDEN_SIZE = 16 OUTPUT_SIZE = 4 p = Population(MLPTorchIndividual(INPUT_SIZE, HIDDEN_SIZE, OUTPUT_SIZE), POPULATION_SIZE, MAX_GENERATION, MUTATION_RATE, CROSSOVER_RATE, INVERSION_RATE) p.run(env, generation_new, verbose=True, log=True, output_folder='') env.close()
def setUp(self): self.population = Population()
child2.calculate_fitness(env) # If children fitness is greater thant parents update population if child1.fitness + child2.fitness > parent1.fitness + parent2.fitness: new_population[i] = child1 new_population[i + 1] = child2 else: new_population[i] = parent1 new_population[i + 1] = parent2 if __name__ == '__main__': env = gym.make('BipedalWalker-v2') env.seed(123) POPULATION_SIZE = 10 MAX_GENERATION = 10 MUTATION_RATE = 0.2 CROSSOVER_RATE = 0.9 INPUT_SIZE = 24 HIDDEN_SIZE = 16 OUTPUT_SIZE = 4 p = Population(RNNIndividual(INPUT_SIZE, HIDDEN_SIZE, OUTPUT_SIZE), POPULATION_SIZE, MAX_GENERATION, MUTATION_RATE, CROSSOVER_RATE) p.run(env, generation, verbose=True, output_folder='') env.close()
def run_genetics(map, hyperparameters, simulation): init = time() cities = __generate_cities(map, hyperparameters, simulation) number_of_lights = len(cities[0].grid.roads_with_lights) lights_steps = ceil(simulation['sim_steps'] / simulation['light_duration_steps']) + 1 # Todo +1 should not be needed dna_size = lights_steps * number_of_lights population = Population(pop_size=hyperparameters['pop_size'], dna_size=dna_size, elitism_n=hyperparameters['elitism_n'], truncation_percentage=hyperparameters['truncation_percentage'], crossover_type=hyperparameters['crossover_type'], crossover_points=hyperparameters['crossover_points'], crossover_probability=hyperparameters['crossover_probability'], mutation_probability=hyperparameters['mutation_probability'], spread_mutation=hyperparameters['spread_mutation'], objects_codified=number_of_lights, multiprocessing=False) best_performance_steps = [] while population.generation_id < hyperparameters['max_generations'] \ and not population.convergence_criteria(): # Print information print('Step: {:3d}'.format(population.generation_id), end=' ') init = time() # Generate arguments to run individuals args = __generate_args(cities, population, simulation['light_duration_steps'], hyperparameters['sim_per_individual'], simulation['sim_steps']) # Run all individuals if hyperparameters['multiprocessing']: pool = Pool(hyperparameters['processes']) scores = pool.starmap(__run_gene, args) pool.close() for i, score in enumerate(scores): population.genes[i].score = score else: for i, gene in enumerate(population.genes): gene.score = __run_gene(cities[i], gene, hyperparameters['sim_per_individual'], simulation['sim_steps'], simulation['light_duration_steps']) # Update genes best_performance, _, pop_size = population.update_genes() best_performance_steps.append(best_performance) # Print information print('| New pop size: {:3d} | Fitness: [Best step: {:3.2f}, Best all: {:3.2f}] | Gene size {:d} | Took {:s}' .format(pop_size, best_performance, population.best_historical_performance, dna_size, str(timedelta(seconds=(time() - init))))) best_performance = population.best_historical_performance best_gene = population.best_historical_individual print('Done !, took', timedelta(seconds=(time() - init)), 'Saving best...') name = generate_filename(simulation, map, hyperparameters) np.save('../data/{:d}_best_{:s}.npy'.format(int(time()), name), best_gene) np.savetxt('../data/{:d}_fitness_{:s}.txt'.format(int(time()), name), np.array(best_performance_steps), delimiter="\n", fmt="%s") return best_performance, best_gene, best_performance_steps
from data.read_stock import DataHandler from ga.individual import Individual from ga.population import Population import numpy as np dataHandler = DataHandler(['apple_test_data.csv', 'NASDAQ_test_data.csv']) #dataHandler = DataHandler(['apple_data.csv', 'NASDAQ_data.csv']) data = dataHandler.get_signal_list() signal_ranges = dataHandler.signal_ranges() #print signal_ranges data_element = len(data[0]) #print "data[0]" #print data[0] population = Population(100, signal_ranges, data) #for i in population: # print i.transform_individual(signal_ranges) initial_fitess_values = map(population.fitness_function, population) #print initial_fitess_values number_generations = 100 population.sus_sampler(initial_fitess_values) max_list = [] mean_list = [] for i in xrange(number_generations): #print "initial population" #for i in population: # print i.rep
def tournament(self, population): tournament_population = Population(self.tournament_size, False) for i in range(self.tournament_size): tournament_population.individuals.append( random.choice(population.individuals)) return tournament_population.get_fittest()
new_population[i + 1] = child2 else: new_population[i] = parent1 new_population[i + 1] = parent2 if __name__ == '__main__': env = gym.make('CartPole-v1') env.seed(123) POPULATION_SIZE = 100 MAX_GENERATION = 20 MUTATION_RATE = 0.4 CROSSOVER_RATE = 0.9 INPUT_SIZE = 4 HIDDEN_SIZE = 2 OUTPUT_SIZE = 1 p = Population(MLPIndividual(INPUT_SIZE, HIDDEN_SIZE, OUTPUT_SIZE), POPULATION_SIZE, MAX_GENERATION, MUTATION_RATE, CROSSOVER_RATE, 0) p.run(env, generation, verbose=True, output_folder='../../models/cartpole', log=True) env.close()