def test_permutation_mutation(self): """ Chromosome: float array containing x and y values Selection: roulette-wheel Crossover operator: uniform crossover Mutation operator: permutation Elitism is disabled Termination criteria: number of generations = 100 Parameters: population_size: 27 reproduction rate: 0.125 crossover rate: 0.75 mutation rate: 0.125 """ sys.stdout.write("Starting test_permutation_mutation: ONE-POINT CROSSOVER, PERMUTATION MUTATION, ELITISM DISABLED\n") reproduction = 0.125 crossover = 0.75 mutation = 0.125 population_size = 27 individual_factory = optimization.XAbsoluteSquareFloatIndividualFactory(crossover_method='one_point', mutation_method='permutation') termination_criteria = ga.NumberOfGenerationsTerminationCriteria(number_of_generations=100) solver = ga.GeneticAlgorithm(individual_factory, population_size=population_size, reproduction=reproduction, crossover=crossover, mutation=mutation, elitism=False, termination_criteria=termination_criteria) repeat = 1 info = pandas.DataFrame([], columns=["generation", "mean", "max", "std", "execution"]) for idx in xrange(repeat): solver.set_params() solver.init_population() solver.evolve() i = solver.get_generation_info() i["execution"] = idx info = info.append(i) matplotlib.pyplot.plot(i['generation'], i['max'], "coral", label="melhor - permutation", linewidth=2) matplotlib.pyplot.plot(i['generation'], i['mean'], "cadetblue", label="media - permutation", linewidth=2) matplotlib.pyplot.plot(i['generation'], i['std'], ".", label="desvio - permutation") info.to_csv('/home/fabio/sin5006/tests/results/xabsolutesquare/test_permutation_mutation.csv', sep=',', index=False) base = pandas.read_csv('/home/fabio/sin5006/tests/results/xabsolutesquare/test_base.csv') matplotlib.pyplot.plot(base["generation"], base["max"], "r", label="melhor - basic mutation", linewidth=2) matplotlib.pyplot.plot(base["generation"], base["mean"], "b", label="media - basic mutation", linewidth=2) matplotlib.pyplot.plot(base["generation"], base["std"], "k.", label="desvio - basic mutation") legend = matplotlib.pyplot.legend(loc='lower right', numpoints=1) matplotlib.pyplot.xlabel("geracoes") matplotlib.pyplot.ylabel("fitness") matplotlib.pyplot.show() sys.stdout.write("Finished. Results are at: /home/fabio/sin5006/tests/results/xabsolutesquare/test_permutation_mutation.csv\n") assert True
def main(): if not options.series == None: series = read(options.series) fname = options.series elif not options.ks == None: print('Running series ks = {}...\n\n'.format(options.ks)) series = read(options.ks) fname = options.ks else: raise Exception('Unknown arguments!') model = ga.GeneticAlgorithm(series, ks=options.ks, progress=True) model.run() # Basic info fig, ax = plt.subplots( nrows=1, ncols=1 ) ax.plot(model.fitseries) fig.savefig('../tests/fitness/fitseries_{}.png'.format(fname), bbox_inches='tight') plt.close(fig) fig, ax = plt.subplots( nrows=1, ncols=1 ) model.fitness(model.Xbest, True, ax) fig.savefig('../tests/best/best_strand_{}.png'.format(fname), bbox_inches='tight') plt.close(fig) # Analysis score = model.score() fig, ax = plt.subplots( nrows=1, ncols=1 ) ax.plot(score, color='red') fig.savefig('../tests/ROC/score_{}.png'.format(fname), bbox_inches='tight') plt.close(fig) tpr, fpr, roc_auc = ROC(score) fig, ax = plt.subplots( nrows=1, ncols=1 ) ax.plot(fpr, tpr, color='darkorange', label='ROC curve (area = {})'.format(roc_auc)) ax.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.legend(loc="lower right") fig.savefig('../tests/ROC/roc_{}.png'.format(fname), bbox_inches='tight') plt.close(fig)
def run(self): print(" [MANAGER_THREAD {0} START] Running manager".format( self.name)) # Initialize for i in range(self.problem.options.gaThreadsQuantity): self.gaThreads.append( ga.GeneticAlgorithm(self.CreateRandomPopulation(), self.problem)) for i in range(self.problem.options.tsThreadsQuantity): self.tsThreads.append( ts.TabooSearch(None, self.tsGlobalMemory, i + 1, self.problem.CreateRandomRandomKeys(), self.problem, None, self.DrawRandomTabooTenure())) for thread in self.gaThreads: thread.start() for thread in self.tsThreads: thread.start() # Start search print(" [MANAGER_THREAD {0} FINISH] Finishing manager".format( self.name))
option = input('Enter the number : ') if option == '1': # hill climbing alg = hc.HillClimbing(listPawn) alg.board.output() elif option == '2': # simulated annealing alg = sa.SimulatedAnnealing(listPawn) alg.board.output() elif option == '3': # genetic algorithm while True: popNum = int(input("Enter number of population: ")) if popNum > 1: break print('Population number must be greater than 1') while True: maxIter = int(input("Enter number of maximum iteration: ")) if maxIter > 0: break print('Maximum iteration at least 1') alg = ga.GeneticAlgorithm(listPawn, popNum, maxIter) alg.result.output() # for debug purpose # b.printListPawn(alg.result.listPawn) else: print('Wrong number') else: print("Too much pawns (max: 64)")
def test_4(self): """ Chromosome: sequence of clients separed by an 'X' when a new vehicle is assigned Selection: roulette-wheel Crossover operator: simple random crossover Mutation operator: simple random mutation Elitism is enabled Termination criteria: number of generations = 100 Parameters: population_size: 97 reproduction rate: 0.25 crossover rate: 0.625 mutation rate: 0.125 """ fname = './input/A-n80-k10.vrp' nodes, capacity, distances, demand = self.load_test(fname) individual_factory = cvrp.CVRPIndividualFactory( nodes, capacity, distances, demand, individual_type='corrected') termination_criteria = ga.NumberOfGenerationsTerminationCriteria( number_of_generations=100) solver = ga.GeneticAlgorithm(individual_factory, population_size=97, reproduction=0.25, crossover=0.625, mutation=0.125, elitism=True, termination_criteria=termination_criteria) if self.grid_search: params = { "population_size": numpy.logspace(3, 12, base=2, num=6, dtype=int), "operators_rate": filter( lambda x: sum(x) == 1.0, itertools.product(numpy.arange(.125, 0.875, .125), repeat=3)), "elitism": [True], "termination_criteria": [ ga.NumberOfGenerationsTerminationCriteria( number_of_generations=100) ] } grid = grid_search.GridSearch(solver, params) grid.search(0.0) grid_scores = grid.get_grid_scores() fname = './results/correction_operator/A-n80-k10.vrp.grid.csv' grid_scores.to_csv(fname, sep=',', index=False) sys.stdout.write( "Finished. Results are at: ./results/correction_operator/A-n80-k10.vrp.grid.csv\n" ) else: sys.stdout.write( "Starting test_4: CORRECTION + SIMPLE RANDOM OPERATORS, ELITISM ENABLED\n" ) sys.stdout.write("Input: ./tests/vrp/A-n80-k10.vrp\n") solver.init_population() solver.evolve() info = solver.get_generation_info() fname = './results/correction_operator/A-n80-k10.vrp.csv' info.to_csv(fname, sep=',', index=False) plt.plot(info['generation'], info['min'], "r", label="melhor", linewidth=2) plt.plot(info['generation'], info['mean'], "b", label="media", linewidth=2) plt.plot(info['generation'], info['std'], "k.", label="desvio") legend = plt.legend(loc='lower right', numpoints=1) plt.xlabel("geracoes") plt.ylabel("fitness") plt.yscale('log') plt.show() sys.stdout.write( "Finished. Results are at: ./results/correction_operator/A-n80-k10.vrp.csv\n" ) assert True
if math.fabs(col2 - col1) == math.fabs(row2 - row1): satisfied = False break if not satisfied: break if satisfied: return True return False ga.TerminationCriteria.register(EightQueensTerminationCriteria) start_time = time.time() solver = ga.GeneticAlgorithm(population_size=300) solver.init_population(EightQueensIndividualFactory()) solver.evolve(EightQueensTerminationCriteria(), reproduction=0.3, crossover=0.6, mutation=0.1) individual = solver.result() genotype = individual.get_genotype() #genotype = numpy.asarray([ 3, 5, 7, 1, 6, 0, 2, 4]) for i in xrange(0, 8): for j in xrange(0, 8): if genotype[j] == i: sys.stdout.write('Q ') else: sys.stdout.write('# ') sys.stdout.write('\n')
# In[ ]: import warnings warnings.filterwarnings("ignore", category=FutureWarning) import ga from timeit import default_timer as timer print("Inicio de la creación del algoritmo") GA = ga.GeneticAlgorithm(fit_cifar, threads=2, population_size=50, tournament_size=3, chromosome_length=110, crossoverOperator='variableMultiPointCrossover', crossoverArgs={ 'minPoints': 3, 'maxPoints': 10 }, mutation_rate=0.015, initialize=True) print("Fin de la creación del algoritmo") # Fixes the random initialization in case it is zero. print("Limpieza de individuos defectuosos") while True: # Bucle infinito hasta que toda la población tenga un fitness distinto de cero valid = list(filter(lambda i: i.fitness != 0, GA.population)) print("Se tienen {} individuos de un total de {}".format( len(valid), GA.population_size)) if len(valid) == GA.population_size: break zeros = [
def noise_test(test_run): data_saver = DataSaver(test_run) # calcualte counts for SNR # SNR = sqrt(N) snr_min = np.sqrt(20) # minimum count level snr_max = np.sqrt(5000) # maximum count level snr_levels = np.linspace(snr_min, snr_max, 40) counts_list = [int(count) for count in snr_levels**2] # overwrite, sample a few counts for bootstrap method # counts_list = [20, 1833] for counts in counts_list: # ++++++++++Define run name++++++++++ run_name = test_run + str(counts) # ++++Get the Measured Trace+++++++++ measured_trace, measured_trace_phase, fake_axes = get_fake_measured_trace( counts=counts, plotting=True, run_name=run_name + "_fields") data_saver.collect_actual_phase_trace(measured_trace, measured_trace_phase, counts) for retrieval_type in ["proof", "autocorrelation", "normal"]: # +++++ run unsupervised learning retrieval+++++ unsupervised_retrieval = UnsupervisedRetrieval( run_name=run_name + "_unsupervised_" + retrieval_type, iterations=5000, retrieval=retrieval_type, modelname="xuv_ph_2", measured_trace=measured_trace, use_xuv_initial_output=False) nn_result = unsupervised_retrieval.retrieve() plt.close(unsupervised_retrieval.axes["fig"]) del unsupervised_retrieval tf.reset_default_graph() # +++++ run unsupervised learning retrieval INITIAL OUTPUT ONLY+++++ unsupervised_retrieval_initial = UnsupervisedRetrieval( run_name=run_name + "_unsupervised_initial_" + retrieval_type, iterations=0, retrieval=retrieval_type, modelname="xuv_ph_2", measured_trace=measured_trace, use_xuv_initial_output=False) nn_init_result = unsupervised_retrieval_initial.retrieve() plt.close(unsupervised_retrieval_initial.axes["fig"]) del unsupervised_retrieval_initial tf.reset_default_graph() # ++++++++++run genetic algorithm++++++++++ genetic_algorithm = genetic_alg.GeneticAlgorithm( generations=30, pop_size=5000, run_name=run_name + "_ga_" + retrieval_type, measured_trace=measured_trace, retrieval=retrieval_type) ga_result = genetic_algorithm.run() plt.close(genetic_algorithm.axes["fig"]) del genetic_algorithm tf.reset_default_graph() # get RMSE of retrieved phase curve # nn_result["nn_phase_rmse"] = calculate_rmse( # nn_result["nn_retrieved_phase"]["cropped"], measured_trace_phase) # nn_init_result["nn_init_phase_rmse"] = calculate_rmse( # nn_init_result["nn_retrieved_phase_init"]["cropped"], # measured_trace_phase) # ga_result["ga_phase_rmse"] = calculate_rmse( # ga_result["ga_retrieved_phase"]["cropped"], measured_trace_phase) # add data to collection data_saver.collect(counts=counts, retrieval_type=retrieval_type, nn=nn_result, nn_init=nn_init_result, ga=ga_result) # close the fake measured trace figure plt.close(fake_axes["fig"])
def bootstrap_retrievals(test_name): """ test_name: the name of the test set used in noise testing """ # open the data retrieval object to get the measured traces with open(test_name + ".p", "rb") as file: data_obj = pickle.load(file) # get the trace at specific noise level results = dict() n_samples = 20 noise_counts = [20, 1833] for noise_count in noise_counts: results["unsupervised_" + str(noise_count)] = list() results["ga_" + str(noise_count)] = list() for _ in range(n_samples): # do a retrieval with this trace measured_trace = data_obj["actual_values"]["measured_trace_" + str(noise_count)] # generate bootstrap indexes total_points = len(measured_trace.reshape(-1)) bootstrap = dict() # generate random indexes for bootstrap retrieval bootstrap["indexes"] = np.random.randint(low=0, high=total_points, size=int((2 / 3) * total_points)) # +++++++++++++++++++++++++++++++++++++ # ++++++++++genetic algorithm++++++++++ # +++++++++++++++++++++++++++++++++++++ genetic_algorithm = genetic_alg.GeneticAlgorithm( generations=30, pop_size=5000, run_name="bootstrap_normal_ga", measured_trace=measured_trace, retrieval="normal", bootstrap=bootstrap) result = genetic_algorithm.run() plt.close(genetic_algorithm.axes["fig"]) del genetic_algorithm tf.reset_default_graph() # append the results results["ga_" + str(noise_count)].append(result) # ++++++++++++++++++++++++++++++ # ++++++++++unsupervised++++++++ # ++++++++++++++++++++++++++++++ unsupervised_retrieval = UnsupervisedRetrieval( run_name="bootstrap_normal_unsupervised", iterations=5000, retrieval="normal", modelname="xuv_ph_2", measured_trace=measured_trace, use_xuv_initial_output=False, bootstrap=bootstrap) result = unsupervised_retrieval.retrieve() plt.close(unsupervised_retrieval.axes["fig"]) del unsupervised_retrieval tf.reset_default_graph() # append the results results["unsupervised_" + str(noise_count)].append(result) # save the results with open(test_name + "_bootstrap.p", "wb") as file: pickle.dump(results, file)
def test_base(self): """ Chromosome: float array containing x and y values Selection: roulette-wheel Crossover operator: one-point crossover Mutation operator: basic (replaces x or y by another valid value) Elitism is disabled Termination criteria: number of generations = 100 Parameters: population_size: 27 reproduction rate: 0.125 crossover rate: 0.75 mutation rate: 0.125 """ sys.stdout.write( "Starting test_elitism: ONE-POINT CROSSOVER, BASIC MUTATION, ELITISM ENABLED\n" ) reproduction = 0.125 crossover = 0.75 mutation = 0.125 population_size = 27 repeat = 1 info = pandas.DataFrame( [], columns=["generation", "mean", "max", "std", "execution"]) for idx in xrange(repeat): individual_factory = optimization.RastriginFloatIndividualFactory( crossover_method='one_point', mutation_method='basic_mutation') termination_criteria = ga.NumberOfGenerationsTerminationCriteria( number_of_generations=100) solver = ga.GeneticAlgorithm( individual_factory, population_size=population_size, reproduction=reproduction, crossover=crossover, mutation=mutation, elitism=False, termination_criteria=termination_criteria) solver.set_params() solver.init_population() solver.evolve() i = solver.get_generation_info() i["execution"] = idx info = info.append(i) matplotlib.pyplot.plot(i['generation'], i['max'], "r", label="melhor", linewidth=2) matplotlib.pyplot.plot(i['generation'], i['mean'], "b", label="media", linewidth=2) matplotlib.pyplot.plot(i['generation'], i['std'], "k.", label="desvio") info.to_csv( '/home/fabio/sin5006/tests/results/rastrigin/test_base.csv', sep=',', index=False) legend = matplotlib.pyplot.legend(loc='lower right') matplotlib.pyplot.xlabel("geracoes") matplotlib.pyplot.ylabel("fitness") matplotlib.pyplot.show() sys.stdout.write( "Finished. Results are at: /home/fabio/sin5006/tests/results/rastrigin/test_base.csv\n" ) assert True
class CompositeMutation(HasTraits): joint_position_mutation = Callable() element_material_mutation = Callable() def __call__(self, mutation_rate, world, individual): # apply mutation to joint positions gene individual.joint_positions_gene = self.joint_position_mutation( mutation_rate, world, individual.joint_positions_gene) # apply mutation to element material gene individual.element_material_gene = self.element_material_mutation( mutation_rate, world, individual.element_material_gene) _validator = ValidateConstruction() default_genetic_algorithm = GA.GeneticAlgorithm( fitness_function=MassOfConstructionFitness(), validator=_validator, survival=ConstructionSurvival(), init_individual=RandomInitializer(validator=_validator), selection_operator=GA.TournamentSelection(arena_size=3), crossover_operator=CompositeCrossover( joint_position_crossover=uniform_joint_position_crossover, element_material_crossover=uniform_element_material_crossover), #element_material_crossover = twopoint_element_material_crossover ), mutation_operator=CompositeMutation( joint_position_mutation=JointPositionOffsetMutation( maximum_offset=5.0), element_material_mutation=uniform_random_material_selection_mutation), world=World(deleted_material_gene_places=len(data.steels), ))
target[x].append(pix) # new = Image.new("RGBA", (50,50), (128, 128, 128, 128)) ''' for x in range(new.size[0]): for y in range(new.size[1]): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) a = random.randint(0, 255) new.putpixel((x,y),(r,g,b)) ''' # new.save("output/" + name + "." + ext.lower(), ext.upper()) # Main part of the code begins here experiment = ga.GeneticAlgorithm() numGenerations = ((experiment.configInfo.numberOfEvals - experiment.configInfo.mu)/(experiment.configInfo.lamb)) + 1 childList, matingPool, fitnessList, generationList, generationList2 = [], [], [], [], [] print(experiment.configInfo.numberOfEvals, numGenerations) for k in range(0, int(numGenerations)+3): generationList.append([]) generationList2.append([]) #For each run in the range for run in range(1,experiment.configInfo.numberOfRuns+1): experiment.initializeRun(run) ga.firstGenerationTree(experiment, generationList, target, img.size) #The other generations' lambda evaluations
# -*-coding:utf-8 -*- import ga import time import numpy as np gaAlgorithm = ga.GeneticAlgorithm() # 初始化种群 随机赋值 0110值 gaAlgorithm.initPopulation() # debug gaAlgorithm.debug('population') maxGN = gaAlgorithm.getMaxNumberIteration() start = time.process_time() for i in range(maxGN): # 计算适应度函数 gaAlgorithm.fitness() # gaAlgorithm.debug('fitnessValue') gaAlgorithm.rank() # gaAlgorithm.debug('rank') gaAlgorithm.selection() # gaAlgorithm.debug('population') gaAlgorithm.crossOver() gaAlgorithm.mutation() if (i % 10 == 0): print(i, end=' ') elapsed = (time.process_time() - start) print("Time used:", elapsed) gaAlgorithm.plotGA() bestFitness, bestGeneration, bestIndividual = gaAlgorithm.getBestThreeAttr() # decCode = gaAlgorithm.decode(bestIndividual) decCode = 0 for j in range(17): if (bestIndividual[j] == "1"):
class QuadraticEquation: a, b, c, length, min_value, max_value = 12, 31, 15, 6, 0, 100 x = [[ random.uniform(min_value, max_value), random.uniform(min_value, max_value), random.uniform(min_value, max_value) ] for i in range(length)] y = [a * d[0] + b * d[1] + c * d[2] for d in x] @staticmethod def get_correct_values(): return QuadraticEquation.a, QuadraticEquation.b, QuadraticEquation.c @staticmethod def fitness_calculator(weights): a, b, c, diff = weights[0], weights[1], weights[2], 0.0 x, y = QuadraticEquation.x, QuadraticEquation.y for i in range(len(QuadraticEquation.y)): v = (a * x[i][0] + b * x[i][1] + c * x[i][2] - y[i]) v = v**2 diff += v diff = math.sqrt(diff) return 1.0 / diff if __name__ == '__main__': p = ga.GeneticAlgorithm(QuadraticEquation.fitness_calculator, 3) p.run() print 'estimated values ...: ', p.winner() print 'correct values .....: ', QuadraticEquation.get_correct_values()
import ga import csv import main series = main.read() model = ga.GeneticAlgorithm(series, ks=4, verbose=1) model.run() model.fitness(model.Xbest, True) B = ['*'] * len(series) B[42] = 42 B[270] = 270 B[676] = 676 B[821] = 821 i = ga.Individual(series, 4) i.B = B i._make_attr_() model.fitness(i, True)
def run(self): background_image = pygame.image.load('assets/background.png') clock = pygame.time.Clock() TOTAL = 250 savedDoodler = [] GA = ga.GeneticAlgorithm() doodler = GA.populate(TOTAL, None) run = True self.generateplatforms(True) highestScore = 0 while run: self.screen.fill((255, 255, 255)) self.screen.blit(background_image, [0, 0]) clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False currentTime = time.time() # Clear when stuck if (currentTime - self.time > 15): self.time = time.time() for d in doodler: d.fitness = self.score d.fitnessExpo() doodler.clear() # When all doodlers are dead, create new generation if (len(doodler) == 0): self.camera = 0 self.time = time.time() self.score = 0 doodler.clear() self.platforms.clear() self.generateplatforms(True) # Stagnation (No improvement) if ((self.generation > 100 and highestScore < 4000)): print("RESET") self.generation = 0 doodler = GA.populate(TOTAL, None) else: self.generation += 1 GA.nextGeneration(TOTAL, savedDoodler) doodler = GA.doodler savedDoodler.clear() self.update() for d in doodler: d.fitness = self.score d.move(d.think(self.platforms)) self.drawPlayer(d) self.playerUpdate(d) self.updateplatforms(d) #pygame.draw.rect(self.screen, (255,0,0),(d.x + 50, d.y, 1, 800)) #pygame.draw.rect(self.screen, (255,0,0), (d.x-600, d.y +50, 600, 1)) if (d.y - self.camera > 800): #d.fitness = self.score # Not sure if it matters d.fitnessExpo() savedDoodler.append(d) doodler.remove(d) if (self.score > highestScore): highestScore = self.score self.screen.blit( self.font.render("Count: " + str(len(doodler)), -1, (0, 0, 0)), (25, 120)) self.screen.blit( self.font.render("High Score: " + str(highestScore), -1, (0, 0, 0)), (25, 90)) pygame.display.update()