def mutatePopulation(self, population): # Initialize new population newPopulation = Population(self.populationSize) for populationIndex in range(population.size()): individual = population.getFittest(populationIndex) for geneIndex in range(individual.getChromosomeLength()): # Skip mutation if this is an elite individual if (populationIndex >= self.elitismCount): # Does this gene need mutation? if (self.mutationRate > random.random()): # Get new gene newGene = 1 if (individual.getGene(geneIndex) == 1): newGene = 0 # Mutate gene individual.setGene(geneIndex, newGene) # Add individual to population newPopulation.setIndividual(populationIndex, individual) # Return mutated population return newPopulation
def larry_analysis(prevalence=0.01,household_size_dist=[1],group_test_name='Gollier', group_size_min = 10, group_size_max = 100, grid_size = 9, nreps = 100, verbose=True): doubling_time = 3. # number of days for epidemic to double in the absence of distancing alpha = 2**(1/doubling_time) SAR = 0.374 assert(np.isclose(sum(household_size_dist), 1)) pop = Population(n_households=10000, # Should be big relative to the largest group size household_size_dist=household_size_dist, target_prevalence=prevalence, disease_length=0, time_until_symptomatic=0, non_quarantine_alpha=alpha, daily_secondary_attack_rate=SAR, fatality_pct=0, daily_outside_infection_pct=0, outside_symptomatic_prob=0, initial_quarantine=0) if verbose: print("Done initializing population. Using initial prevalence {} for target prevalence {}".format( pop.initial_prevalence, pop.target_prevalence)) beta = 1 FNR = 0.3 FPR = 0.1 uspop = 328E6 tests_per_week_ub = 6E6 tests_per_week_per_person_ub = tests_per_week_ub / uspop best_group_size, best_quarantines_per_person, test_per_person_per_week, days_between_tests =\ optimize_gollier_group_size(pop,beta,FNR, FPR,tests_per_week_per_person_ub, group_size_min, group_size_max, grid_size, nreps, group_test_name, True) print('{} doubling time {} days, household size dist={} SAR={} test FNR={} FPR={} beta={}'.format( group_test_name, doubling_time,household_size_dist, SAR,FNR,FPR,beta)) print('Optimum @ prevalence={}: group size={} quarantines/person={:.2f} tests/week/person={:.3f} days between tests={:2f}'.format( prevalence,best_group_size,best_quarantines_per_person,test_per_person_per_week,days_between_tests))
def main(year, month, ga_start_date, ga_end_date, test_start_date, test_end_date, index): initiated_time = time.time() training_file_name = "Kospi_200_portfolio_optimization-" + str( datetime.datetime.now().strftime("%Y%m%d_%H_%M_%S")) + ".csv" day_list = scraper.days(KOSPI_TICKER, ga_start_date, ga_end_date) stock_price_list = scraper.stock_prices(day_list, KOSPI_TICKER, ga_start_date, ga_end_date) test_day_list = scraper.days(KOSPI_TICKER, test_start_date, test_end_date) test_stock_price_list = scraper.stock_prices(test_day_list, KOSPI_TICKER, test_start_date, test_end_date) population = Population(POPULATION_SIZE, day_list, stock_price_list) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) save.save_population(population, 0, year, month, training_file_name) generation_number = 1 while generation_number < GENERATION_SIZE: population = GeneticAlgorithm.evolve(population, day_list, stock_price_list, POPULATION_SIZE) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) save.save_population(population, generation_number, year, month, training_file_name) generation_number += 1 if generation_number == GENERATION_SIZE: completed_time = time.time() print(index + 1, "th GA GENERATION OF PORTFOLIO OPTIMIZATION FOR ", test_start_date, ",", test_end_date, "COMPLETED IN", completed_time - initiated_time, "SECONDS.") best_chromosome = population.get_chromosomes()[0] test_fund.test_best_population(best_chromosome, test_day_list, test_stock_price_list, year, month, ga_start_date, ga_end_date, test_start_date, test_end_date)
def main(training_file_name, year, month, ga_start_date, ga_end_date, test_start_date, test_end_date): day_list = scraper.days(KOSPI_TICKER, ga_start_date, ga_end_date) stock_price_list = scraper.stock_prices(day_list, KOSPI_TICKER, ga_start_date, ga_end_date) test_day_list = scraper.days(KOSPI_TICKER, test_start_date, test_end_date) test_stock_price_list = scraper.stock_prices(test_day_list, KOSPI_TICKER, test_start_date, test_end_date) population = Population(POPULATION_SIZE, day_list, stock_price_list) population.get_chromosomes().sort(key=lambda chromosome: chromosome.get_fitness(), reverse=True) save.save_population(population, 0, training_file_name, year, month) generation_number = 1 while generation_number < GENERATION_SIZE: population = GeneticAlgorithm.evolve(population, day_list, stock_price_list, POPULATION_SIZE) population.get_chromosomes().sort(key=lambda chromosome: chromosome.get_fitness(), reverse=True) save.save_population(population, generation_number, training_file_name, year, month) generation_number += 1 best_chromosome = population.get_chromosomes()[0] save.print_best_population(best_chromosome, year, month, training_file_name) if generation_number == GENERATION_SIZE: monthly_best_file_name = "Kospi_200_portfolio_optimization-Monthly Best.csv" save.save_best_generation(best_chromosome, year, month, monthly_best_file_name) test_fund.test_best_population(KOSPI_TICKER, best_chromosome, test_day_list, test_stock_price_list, year, month, monthly_best_file_name)
def select_tournament(population: Population, tournament_size) -> (Tour, Tour): selected = [] while len(selected) < 2: tournament = Population() for _ in range(tournament_size): random_index = random.randint(0, len(population)-1) tournament.append_tour(population.get_tour(random_index)) selected_tour = tournament.get_fittest() # def is_equal(t1, t2): # for i in range(Graph.num_nodes): # if t1.path[i] != t2.path[i]: # return False # return True # if len(selected) == 1 and is_equal(selected[0], selected_tour): # continue selected.append(selected_tour) return selected
def __init__(self): self.total_workers = 0 self.available_buildings = [House()] self.buildings = [] self.current_gold = 50000 self.resources = { "gold": 50000, "wood": 20000.0, "meat": 1000.0, "fish": 0.0, } self.population = Population() self.map = Map() self.gathering = Gathering(self.map, self.population) self.debug_mode = True self.game_is_running = True self.daytime_in_irl = 10 self.current_day = 0
def lambda_handler(param): # Creation de la population initiale data = Data() _genetic_algorithm = GeneticAlgorithm(data=data, param=param) _population = Population(size=param[0], data=data) while _population.schedules[0]._fitness != 1.0: # Calcul la fitness de chaque emploi du temps dans la population for schedule in _population.schedules: schedule._fitness = schedule.calculate_fitness() # Tri la population et evolution de cette population _population.sort_by_fitness() _population = _genetic_algorithm.evolve(population=_population) for schedule in _population.schedules: schedule._fitness = schedule.calculate_fitness() _population.sort_by_fitness() return _population.schedules[0]
def __init__(self): super().__init__() self.set_size(Const.WIDTH, Const.HEIGHT) self.set_caption('Floppy Bird') self.score = 0 self.population = Population(self) self.tubes = [Tube()] self.background = Sprite(img=Images.BACKGROUND_IMG) self.land = Land() self.score_label = Label(text=f'Score: {self.score}', x=Const.WIDTH - 10, y=Const.HEIGHT - 20, anchor_x='right', bold=True) self.fps_display = FPSDisplay(self) self.fps_display.label.font_size = 12 self.fps_display.label.color = (0, 255, 0, 255) self.fps_display.label.bold = False pyglet.clock.schedule_interval(self.on_timer, Const.FRAME_RATE)
def f_para(obj, new_pop, i): """Function to parallel""" obj.utils.fast_nondominated_sort(obj.population) for front in obj.population.fronts: obj.utils.calculate_crowding_distance(front) children = obj.utils.create_children(obj.population) obj.population.extend(children) obj.utils.fast_nondominated_sort(obj.population) new_population = Population() front_num = 0 while len(new_population) + len( obj.population.fronts[front_num]) <= obj.num_of_individuals: # print(obj.population.fronts[front_num]) obj.utils.calculate_crowding_distance(obj.population.fronts[front_num]) new_population.extend(obj.population.fronts[front_num]) front_num += 1 obj.utils.calculate_crowding_distance(obj.population.fronts[front_num]) obj.population.fronts[front_num].sort( key=lambda individual: individual.crowding_distance, reverse=True) new_population.extend( obj.population.fronts[front_num][0:obj.num_of_individuals - len(new_population)]) new_pop[i] = new_population
def select(population: Population): fitness = cal_fitness(population) # 采用轮盘赌选择法 ratio = [] for i in fitness: ratio.append(i / sum(fitness)) sum_ratio = [] for i in range(len(ratio)): sum_ratio.append(sum(ratio[:i + 1])) next_generation = [] for i in range(population.size): x = random.random() for j in range(population.size): if sum_ratio[j] > x: next_generation.append(population.chromosomes[j]) break p = Population(population.id + 1, next_generation) """ # 排序选择算法 # 对适应度降序排序 sorted_fitness = deepcopy(fitness) sorted_fitness.sort() sorted_fitness.reverse() # 将适应度前5%的个体代替掉后5%的个体 for i in range(int(percent * len(population.chromosomes) / 100)): n = fitness.index(sorted_fitness[i]) m = fitness.index(sorted_fitness[-i - 1]) x = population.chromosomes[n] population.remove(population.chromosomes[m]) population.insert(m, x) """ return p
def create_next_generation(p, xover, ff, log=None): random.shuffle(p.population) new_population = Population() updated = False selection_errors = 0 selection_correct = 0 for i in range(0, len(p.population), 2): parent1 = p.population[i] parent2 = p.population[i + 1] parent1.generation = "parent" parent2.generation = "parent" child1, child2 = xover(parent1, parent2, ff) best, update, error, correct = compete(parent1, parent2, child1, child2) selection_correct += correct selection_errors += error updated = updated or update new_population.population += best new_population.set_fitness() if log == "2": return new_population, updated, selection_errors, selection_correct return new_population, updated
def main(): generations, num_cities, population_size = parse_args() tour_manager = TourManager() tour_manager.create_tour_cities(num_cities) population = Population(population_size, True, tour_manager) print "Initial distance = %d" % (population.get_fittest().get_distance(), ) for i in range(0, generations): population = algorithm.evolve_population(population) print "generation=%d shortest_distance=%d" % ( i, population.get_fittest().get_distance()) fittest = population.get_fittest() print """ Finished Final distance=%d Solution -------- %s """ % (fittest.get_distance(), fittest)
def create_new_generation(self, current_population): self.current_population = current_population filhos = [] new_population = Population(current_population.getTamPop()) if self.elitismo == True: new_population.specimens.append(current_population.getSpeciemen(pos=0)) while len(new_population.specimens) < len(current_population.specimens): pais = self.torneio() if random.uniform(0,1) <= self.crossover_rate: filhos = self.crossover(pais) else: filhos = pais new_population.specimens.append(filhos[0]) new_population.specimens.append(filhos[1]) new_population.order_population() return new_population
def evolve_population(population): new_population = Population(population.size(), False, population.tour_manager) elitism_offset = 0 # save the fittest tour if elitism: new_population.save_tour(0, population.get_fittest()) elitism_offset = 1 # crossover for i in range(elitism_offset, new_population.size()): parent_1 = tournament_selection(population) parent_2 = tournament_selection(population) child = crossover(parent_1, parent_2) new_population.save_tour(i, child) # mutate population for i in range(elitism_offset, new_population.size()): mutate(new_population.get_tour(i)) return new_population
def main(args): ''' GA Project. Marcos Felipe Eipper @ 2018 | UDESC - CCT ''' # Checks encoding param. enc = args.enc.upper() if (enc != "INT" and enc != "REAL" and enc != "INT-PERM" and enc != "BIN"): raise Exception('Invalid encoding.') # Define seed, if provided if args.seed: np.random.seed(args.seed) # Create population pop = Population(args.enc.upper(), args.chromo, args.pop, args.lower_bounds, args.upper_bounds) # Prints out verbose fun stuff. if args.verbose: print("+====================================+") print("+=========== Welcome to ===========+") print("+=========== Verbose Mode ===========+") print("+====================================+\n") sleep(0.25) print("==> Arguments: " + str(args) + "\n") print("==> Population: \n" + str(pop) + "<==")
def evolution(population, mutation_probab, crossover_multiplier, tournament_participants): """ Returns population of N best-fit offsprings of previous population Process of evolution of population includes the following steps 1)selection for crossover (several pairs are selected) 2)each pair produces new offspring(s) 3)each offspring mutate with a certain probability(change of genes in genome) 4)next population is formed from offsprings, selecting best N elements among mutated """ N = population.__len__() offsprings = mutate(mut_probab=mutation_probab, chromosomes=crossover_chromosomes( mpl=crossover_multiplier, chromosomes=kth_partite_tournament_selection( k=tournament_participants, pop=population))) for ch in offsprings: ch.set_fitness() offsprings.sort(key=lambda ch: ch.get_fitness()) nextPopulation = Population().populate(offsprings[:N]) #a slice of N best fit chromosomes which form next generation population return nextPopulation #returns a slice of N best fit chromosomes
def evolve_population(cls, population: Population) -> Population: if cls.adaptable_mutation_rate: if cls.mutation_rate > 0.001: cls.mutation_rate -= 0.0005 logging.info('mutation rate: {}'.format(cls.mutation_rate)) new_population = Population() N = len(population) offset = 0 if cls.elitism: new_population.append_tour(population.get_fittest()) offset += 1 while offset < N: start_time = time.time() parents = cls._select(population) after_select = time.time() child1, child2 = cls._crossover(parents[0], parents[1]) after_crossover = time.time() cls._mutate(child1) cls._mutate(child2) after_mutation = time.time() new_population.append_tour(child1) new_population.append_tour(child2) offset += 2 return new_population
def main(self): tm = TourManager() cityList = self.readInCities() for i in range(0, len(cityList)): tm.addCity(cityList[i]) numCities = len(cityList) getPopSize = int(raw_input("Enter the population size... >")) getGenSize = int(raw_input("Enter the number of generations... >")) ti = time.clock() # pass true because we're initializing the population (parent generation) pop = Population(getPopSize, True, numCities) i = 0 ga = GA() distances = [] pop = ga.wiseEvolvePopulation(pop, numCities) for i in range(0, getGenSize): print "evolving generation " + str(i) pop = ga.wiseEvolvePopulation(pop, numCities) distances.append(pop.getFittest().getDistance()) winningDistance = pop.getFittest().getDistance() finalTime = time.clock() - ti print "------------------ FINISHED ------------------" print "time taken: " + str(finalTime) + " seconds." print "final distance: " + str(winningDistance) self.createGui(pop.getFittest(), winningDistance, getPopSize, getGenSize, finalTime) self.printToFile(distances)
schedule = [0] * totalTime else: for repEvent in range(numRepr): for learnEvent in range(numLearn): schedule.append(0) schedule.append(1) return schedule for rep in range(reps): # Reset the landscape for each repitition landscape = NKLandscape(n, k) for cond, (numLearn, numRepr) in enumerate(conditions): pop = Population(popsize, genesize, landscape, recombProb, mutatProb, eliteprop, lamarckian, steep_checks) schedule = constructSchedule(numLearn, numRepr, totalTime) for step, event in enumerate(schedule): if event == 0: pop.steepLearn() elif event == 1: pop.reproduce() best_fitnesses[rep][cond][step], avg_fitnesses[rep][cond][ step] = pop.stats() np.save(os.path.join(saveDir, "bh_{}_{}.npy".format(k, steep_checks)), best_fitnesses) np.save(os.path.join(saveDir, "ah_{}_{}.npy".format(k, steep_checks)), avg_fitnesses)
def generate(ngen, surveyList=None, age_max=1.0E9, pDistPars=[.3, .15], bFieldPars=[12.65, 0.55], birthVPars=[0.0, 180.], siDistPars=[-1.6, 0.35], alignModel='orthogonal', lumDistType='fk06', lumDistPars=[-1.5, 0.5], alignTime=None, spinModel='fk06', beamModel='tm98', birthVModel='gaussian', electronModel='ne2001', braking_index=0, zscale=0.05, duty=5., nodeathline=False, efficiencycut=None, nostdout=False, nospiralarms=False, keepdead=False): pop = Population() # set the parameters in the population object pop.pmean, pop.psigma = pDistPars pop.bmean, pop.bsigma = bFieldPars if lumDistType == 'pow': try: pop.lummin, pop.lummax, pop.lumpow = \ lumDistPars[0], lumDistPars[1], lumDistPars[2] except ValueError: raise EvolveException('Not enough lum distn parameters for "pow"') elif lumDistType == 'fk06': pop.lumPar1, pop.lumPar2 = lumDistPars[0], lumDistPars[1] if len(lumDistPars) == 3: pop.lumPar3 = lumDistPars[2] else: pop.lumPar3 = 0.18 else: pop.lumPar1, pop.lumPar2 = lumDistPars pop.simean, pop.sisigma = siDistPars pop.birthvmean, pop.birthvsigma = birthVPars pop.alignModel = alignModel pop.alignTime = alignTime pop.spinModel = spinModel pop.beamModel = beamModel pop.birthVModel = birthVModel pop.electronModel = electronModel pop.braking_index = braking_index pop.deathline = not nodeathline pop.nospiralarms = nospiralarms pop.zscale = zscale if not nostdout: print "\tGenerating evolved pulsars with parameters:" print "\t\tngen = {0}".format(ngen) print "\t\tUsing electron distn model {0}".format(pop.electronModel) print "\n\t\tPeriod mean, sigma = {0}, {1}".format( pop.pmean, pop.psigma) print "\t\tLuminosity mean, sigma = {0}, {1}".format( pop.lummean, pop.lumsigma) print "\t\tSpectral index mean, sigma = {0}, {1}".format( pop.simean, pop.sisigma) print "\t\tGalactic z scale height = {0} kpc".format(pop.zscale) print "\t\tWidth {0}% ".format(duty) # set up progress bar for fun :) prog = ProgressBar(min_value=0, max_value=ngen, width=65, mode='dynamic') # create survey objects here and put them in a list if surveyList is not None: surveys = [Survey(s) for s in surveyList] else: # make an empty list here - makes some code just a little # simpler - can still loop over an empty list (ie zero times) surveys = [] # initialise these counters to zero for surv in surveys: surv.ndet = 0 # number detected surv.nout = 0 # number outside survey region surv.nsmear = 0 # number smeared out surv.ntf = 0 # number too faint # this is the nitty-gritty loop for generating the pulsars while pop.ndet < ngen: pulsar = Pulsar() # initial age for pulsar pulsar.age = random.random() * age_max # initial period pulsar.p0 = -1. while pulsar.p0 <= 0.: pulsar.p0 = random.gauss(pop.pmean, pop.psigma) # initial magnetic field (in Gauss) pulsar.bfield_init = 10**random.gauss(pop.bmean, pop.bsigma) # aligment angle alignpulsar(pulsar, pop) # braking index if pop.braking_index == 0: pulsar.braking_index = 2.5 + 0.5 * random.random() else: pulsar.braking_index = float(pop.braking_index) #apply relevant spin down model pulsar.dead = False # pulsar should start alive! if pop.spinModel == 'fk06': spindown_fk06(pulsar) # apply deathline if relevant if pop.deathline: bhattacharya_deathperiod_92(pulsar) elif pop.spinModel == 'cs06': # contopoulos and spitkovsky spindown_cs06(pulsar, pop) # define pulse width (default = 6% = 18 degrees) width = (float(duty) / 100.) * pulsar.period**0.9 width = math.log10(width) width = dists.drawlnorm(width, 0.3) pulsar.width_degree = 360. * width / pulsar.period # plough on - only if the pulsar isn't dead! if not pulsar.dead or keepdead: # is the pulsar beaming? pulsar_beaming(pulsar, pop.beamModel) # if not, then skip onto next pulsar if not pulsar.beaming: continue # position of the pulsar galacticDistribute(pulsar, pop) # birthvelocity birthVelocity(pulsar, pop) # model the xyz velocity go.vxyz(pulsar) # luminosity if lumDistType == 'fk06': luminosity_fk06(pulsar, alpha=pop.lumPar1, beta=pop.lumPar2, gamma=pop.lumPar3) elif lumDistType == 'lnorm': pulsar.lum_1400 = dists.drawlnorm(pop.lumPar1, pop.lumPar2) elif lumDistType == 'pow': pulsar.lum_1400 = dists.powerlaw(pop.lummin, pop.lummax, pop.lumpow) else: # something's wrong! raise EvolveException('Invalid luminosity distn selected') # apply efficiency cutoff if efficiencycut is not None: if pulsar.efficiency() > efficiencycut: pulsar.dead = True if not keepdead: continue # spectral index pulsar.spindex = random.gauss(pop.simean, pop.sisigma) # calculate galactic coords and distance pulsar.gl, pulsar.gb = go.xyz_to_lb(pulsar.galCoords) pulsar.dtrue = go.calc_dtrue(pulsar.galCoords) # then calc DM using fortran libs if pop.electronModel == 'ne2001': pulsar.dm = go.ne2001_dist_to_dm(pulsar.dtrue, pulsar.gl, pulsar.gb) elif pop.electronModel == 'lmt85': pulsar.dm = go.lmt85_dist_to_dm(pulsar.dtrue, pulsar.gl, pulsar.gb) else: raise EvolveException('Invalid electron dist model selected') # if surveys are given, check if pulsar detected or not # in ANY of the surveys if surveyList is not None: detect_int = 0 # just a flag if pulsar is detected for surv in surveys: SNR = surv.SNRcalc(pulsar, pop) if SNR > surv.SNRlimit: # SNR is over threshold # increment the flag # and survey ndetected detect_int += 1 surv.ndet += 1 continue elif SNR == -1: # pulse is smeared out surv.nsmear += 1 continue elif SNR == -2: # pulsar is outside survey region surv.nout += 1 continue else: #pulsar is just too faint surv.ntf += 1 continue # if detected, increment ndet (for whole population) # and redraw the progress bar if detect_int: pop.ndet += 1 # update the counter if not nostdout: prog.increment_amount() print prog, '\r', sys.stdout.flush() else: # no survey list, just add the pulsar to population, # and increment number of pulsars pop.ndet += 1 # update the counter if not nostdout: prog.increment_amount() print prog, '\r', sys.stdout.flush() # pulsar isn't dead, add to population! pop.population.append(pulsar) else: # pulsar is dead. If no survey list, # just increment number of pulsars if surveyList is None: pop.ndet += 1 # update the counter if not nostdout: prog.increment_amount() print prog, '\r', sys.stdout.flush() if not nostdout: print "\n\n" print " Total pulsars = {0}".format(len(pop.population)) print " Total detected = {0}".format(pop.ndet) for surv in surveys: print "\n Results for survey '{0}'".format(surv.surveyName) print " Number detected = {0}".format(surv.ndet) print " Number too faint = {0}".format(surv.ntf) print " Number smeared = {0}".format(surv.nsmear) print " Number outside survey area = {0}".format(surv.nout) # save list of arguments into the pop argspec = inspect.getargspec(generate) key_values = [(arg, locals()[arg]) for arg in argspec.args] pop.arguments = {key: value for (key, value) in key_values} return pop
def generate(ngen, surveyList=None, pDistType='lnorm', radialDistType='lfl06', radialDistPars=7.5, electronModel='ne2001', pDistPars=[2.7, -0.34], siDistPars=[-1.6, 0.35], lumDistType='lnorm', lumDistPars=[-1.1, 0.9], zscaleType='exp', zscale=0.33, duty_percent=6., scindex=-3.86, gpsArgs=[-1, -1], doubleSpec=[-1, -1], nostdout=False, pattern='gaussian', orbits=False, dgf=None, singlepulse=False, giantpulse=False, dither=False, accelsearch=False, jerksearch=False, sig_factor=10.0, bns = False, orbparams = {'m': [1, 5], 'm1': [1.0, 2.4], 'm2': [0.2, 1e9], 'om': [0, 360.], 'inc': [0, 90], 'ec': [0., 1.], 'pod': [1e-3, 1e3]}, brDistType='log_unif'): """ Generate a population of pulsars. Keyword args: ngen -- the number of pulsars to generate (or detect) surveyList -- a list of surveys you want to use to try and detect the pulsars pDistType -- the pulsar period distribution model to use (def=lnorm) radialDistType -- radial distribution model electronModel -- mode to use for Galactic electron distribution pDistPars -- parameters to use for period distribution siDistPars -- parameters to use for spectral index distribution lumDistPars -- parameters to use for luminosity distribution radialDistPars -- parameters for radial distribution zscale -- if using exponential z height, set it here (in kpc) scindex -- spectral index of the scattering model gpsArgs -- add GPS-type spectrum sources doubleSpec -- add double-spectrum type sources nostdout -- (bool) switch off stdout """ pop = Population() # check that the distribution types are supported.... if 'd_g' in (pDistType,lumDistType,zscaleType,radialDistType,brDistType) and dgf is None: print("Provide the distribution generation file") sys.exit() elif dgf != None: try: f = open(dgf, 'rb') except IOError: print("Could not open file {0}.".format(dgf)) sys.exit() dgf_pop_load = cPickle.load(f) f.close() if lumDistType not in ['lnorm', 'pow', 'log_unif', 'd_g', 'log_st']: print("Unsupported luminosity distribution: {0}".format(lumDistType)) if brDistType not in ['log_unif', 'd_g']: print("Unsupported burst rate distribution: {0}".format(brDistType)) if pDistType not in ['lnorm', 'norm', 'cc97', 'lorimer12','unif', 'd_g']: print("Unsupported period distribution: {0}".format(pDistType)) if radialDistType not in ['lfl06', 'yk04', 'isotropic', 'slab', 'disk','unif' ,'gauss','d_g', 'gamma']: print("Unsupported radial distribution: {0}".format(radialDistType)) if electronModel not in ['ne2001', 'lmt85','ymw16']: print("Unsupported electron model: {0}".format(electronModel)) if pattern not in ['gaussian', 'airy']: print("Unsupported gain pattern: {0}".format(pattern)) if duty_percent < 0.: print("Unsupported value of duty cycle: {0}".format(duty_percent)) # need to use properties in this class so they're get/set-type props pop.pDistType = pDistType pop.radialDistType = radialDistType pop.electronModel = electronModel pop.lumDistType = lumDistType pop.rsigma = radialDistPars pop.pmean, pop.psigma = pDistPars pop.simean, pop.sisigma = siDistPars pop.gpsFrac, pop.gpsA = gpsArgs pop.brokenFrac, pop.brokenSI = doubleSpec #Set whether system is BNS: pop.bns = bns pop.orbparams = orbparams if pop.lumDistType == 'lnorm': pop.lummean, pop.lumsigma = \ lumDistPars[0], lumDistPars[1] elif pop.lumDistType == 'log_unif': pop.lumlow, pop.lumhigh = \ lumDistPars[0], lumDistPars[1] elif pop.lumDistType == 'log_st': try: pop.lumlow, pop.lumhigh, pop.lumslope = \ lumDistPars[0], lumDistPars[1], lumDistPars[2] except ValueError: raise PopulateException('Not enough lum distn parameters') elif pop.lumDistType == 'd_g': pass else: try: pop.lummin, pop.lummax, pop.lumpow = \ lumDistPars[0], lumDistPars[1], lumDistPars[2] except ValueError: raise PopulateException('Not enough lum distn parameters') pop.zscaleType = zscaleType pop.zscale = zscale # store the dict of arguments inside the model. Could be useful. try: if sys.version_info[0] > 3: argspec = inspect.getfullargspec(generate) else: argspec = inspect.getargspec(generate) lcl = locals() key_values = [(arg, lcl[arg]) for arg in argspec.args] #key_values = [(arg, locals()['argspec'][arg]) for arg in argspec.args] #pop.arguments = {key: value for (key, value) in key_values} except SyntaxError: pass if not nostdout: print("\tGenerating pulsars with parameters:") param_string_list = [] for key, value in key_values: s = ": ".join([key, str(value)]) param_string_list.append(s) # join this list of strings, and print it s = "\n\t\t".join(param_string_list) print("\t\t{0}".format(s)) # set up progress bar for fun :) prog = ProgressBar(min_value=0, max_value=ngen, width=65, mode='dynamic') # create survey objects here and put them in a list if surveyList is not None: surveys = [Survey(s, pattern) for s in surveyList] # initialise these counters to zero for surv in surveys: surv.ndet = 0 # number detected surv.nout = 0 # number outside survey region surv.nsmear = 0 # number smeared out surv.ntf = 0 # number too faint surv.nbr = 0 #number didn't burst # surv.gainpat=pattern else: # make an empty list here - makes some code just a little # simpler - can still loop over an empty list (ie zero times) surveys = [] while pop.ndet < ngen: # Declare new pulsar object p = Pulsar() # period, alpha, rho, width distribution calls # Start creating the pulsar! if pop.pDistType == 'lnorm': p.period = dists.drawlnorm(pop.pmean, pop.psigma) elif pop.pDistType == 'unif': p.period = dists.uniform(pop.pmean,pop.psigma) elif pop.pDistType == 'norm': p.period = random.gauss(pop.pmean, pop.psigma) elif pop.pDistType == 'cc97': p.period = _cc97() elif pop.pDistType == 'gamma': print("Gamma function not yet supported") sys.exit() elif pop.pDistType == 'd_g': Pbin_num=dists.draw1d(dgf_pop_load['pHist']) Pmin=dgf_pop_load['pBins'][0] Pmax=dgf_pop_load['pBins'][-1] p.period = Pmin + (Pmax-Pmin)*(Pbin_num+random.random())/len(dgf_pop_load['pHist']) # p.period = dg.gen_nos(dgf_pop_load['pHist'],dgf_pop_load['pBins']) elif pop.pDistType == 'lorimer12': p.period = _lorimer2012_msp_periods() if duty_percent > 0.: # use a simple duty cycle for each pulsar # with a log-normal scatter width = (float(duty_percent)/100.) * p.period**0.9 width = math.log10(width) width = dists.drawlnorm(width, 0.3) # following are the numbers for the RRATs # from the period pulse width relation if singlepulse: width = 1000 while width > 100: A1=random.gauss(0.49986684,0.13035004) A2=random.gauss(-0.77626305,0.4222085) width = 10**(A1*math.log10(p.period)+ A2) p.width_degree = width*360./p.period else: # use the model to caculate if beaming p.alpha = _genAlpha() p.rho, p.width_degree = _genRhoWidth(p) if p.width_degree == 0.0 and p.rho == 0.0: continue # is pulsar beaming at us? If not, move on! p.beaming = _beaming(p) if not p.beaming: continue # Spectral index stuff here # suppose it might be nice to be able to have GPS sources # AND double spectra. But for now I assume only have one or # none of these types. if random.random() > pop.gpsFrac: # This will evaluate true when gpsArgs[0] is -1 # might have to change in future p.gpsFlag = 0 else: p.gpsFlag = 1 p.gpsA = pop.gpsA if random.random() > pop.brokenFrac: p.brokenFlag = 0 else: p.brokenFlag = 1 p.brokenSI = pop.brokenSI p.spindex = random.gauss(pop.simean, pop.sisigma) # get galactic position # first, Galactic distribution models if pop.radialDistType == 'isotropic': # calculate gl and gb randomly p.gb = math.degrees(math.asin(random.random())) if random.random() < 0.5: p.gb = 0.0 - p.gb p.gl = random.random() * 360.0 # use gl and gb to compute galactic coordinates # pretend the pulsar is at distance of 1kpc # not sure why, ask Dunc! #Adam comment ,1 Kpc makes no sense here... p.galCoords = go.lb_to_xyz(p.gl, p.gb, 1.0) elif pop.radialDistType == 'slab': p.galCoords = go.slabdist() p.gl, p.gb = go.xyz_to_lb(p.galCoords) elif pop.radialDistType == 'disk': p.galCoords = go.diskdist() p.gl, p.gb = go.xyz_to_lb(p.galCoords) else: if pop.radialDistType == 'lfl06': p.r0 = go.lfl06() elif pop.radialDistType == 'gamma': fit_alpha, fit_loc, fit_beta=1050.312227, -8.12315263841, 0.00756010693478 p.r0 = 8.5*stats.gamma.rvs(fit_alpha, loc=fit_loc, scale=fit_beta, size=1) + 8.5 elif pop.radialDistType == 'd_g': Rbin_num=dists.draw1d(dgf_pop_load['RHist']) Rmin=dgf_pop_load['RBins'][0] Rmax=dgf_pop_load['RBins'][-1] #p.r0 = dists.yet_another_try(dgf_pop_load['RHist'], dgf_pop_load['RBins']) p.r0 = Rmin + (Rmax-Rmin)*(Rbin_num+random.random())/len(dgf_pop_load['RHist']) elif pop.radialDistType == 'yk04': p.r0 = go.ykr() elif pop.radialDistType == 'unif': p.r0 = random.uniform(0,12.3) elif pop.radialDistType == 'gauss': # guassian of mean 0 # and stdDev given by parameter (kpc) p.r0 = random.gauss(0., pop.rsigma) # then calc xyz,distance, l and b if pop.zscaleType == 'exp': zheight = go._double_sided_exp(zscale) elif pop.zscaleType == 'unif': zheight = random.uniform(-1.06489765758, 1.91849552866) elif pop.zscaleType == 'd_g': zbin_num=dists.draw1d(dgf_pop_load['ZHist']) logzmin=dgf_pop_load['ZBins'][0] logzmax=dgf_pop_load['ZBins'][-1] logz = logzmin + (logzmax-logzmin)*(zbin_num+random.random())/len(dgf_pop_load['ZHist']) zheight = logz #zheight = dg.gen_nos(dgf_pop_load['ZHist'],dgf_pop_load['ZBins']) else: zheight = random.gauss(0., zscale) gx, gy = go.calcXY(p.r0) p.galCoords = gx, gy, zheight p.gl, p.gb = go.xyz_to_lb(p.galCoords) p.dtrue = go.calc_dtrue(p.galCoords) if pop.lumDistType == 'lnorm': p.lum_1400 = dists.drawlnorm(pop.lummean, pop.lumsigma) elif pop.lumDistType == 'pow': p.lum_1400 = dists.powerlaw(pop.lummin, pop.lummax, pop.lumpow) elif pop.lumDistType == 'log_st': low_lim=pop.lumlow upr_lim=pop.lumhigh slope = pop.lumslope #-0.10227965 p.lum_1400= 10.0**(low_lim + dists.st_line(slope,(upr_lim-low_lim))) elif pop.lumDistType == 'log_unif': p.lum_1400 = 10.0**dists.uniform(pop.lumlow,pop.lumhigh) elif pop.lumDistType == 'd_g': lbin_num=dists.draw1d(dgf_pop_load['lHist']) lmin=dgf_pop_load['lBins'][0] lmax=dgf_pop_load['lBins'][-1] logl = lmin + (lmax-lmin)*(lbin_num+random.random())/len(dgf_pop_load['lHist']) p.lum_1400 = 10.0**logl p.lum_inj_mu=p.lum_1400 # add in orbital parameters if pop.bns: assert isinstance(orbparams, dict), "Orbital parameter distribution limits should be a dictionary of form {'name of orb_param': [min, max]}" #These represents the range in which NN was trained. #DO NOT GO OUTSIDE THESE BOUNDS!! default_orbparams = {'m': [1, 5], 'm1': [1.0, 2.4], 'm2': [0.2, 1e9], 'om': [0, 360.], 'inc': [0, 90], 'ec': [0., 1.], 'pod': [1e-3, 1e3]} if len(pop.orbparams) == 0: print("Warning: Supplied orbparams dict is empty; Setting ranges to default") pop.orbparams = default_orbparams else: temp_opd = dict(default_orbparams) temp_opd.update(orbparams) pop.orbparams = temp_opd #Draw a value for each of the orbital parameters from a uniform distribution p.m = np.int(np.random.uniform(pop.orbparams['m'][0], pop.orbparams['m'][1], size = 1)) #Should typically fix this to one value! p.m1 = np.random.uniform(pop.orbparams['m1'][0], pop.orbparams['m1'][1], size = 1) p.m2 = np.random.uniform(pop.orbparams['m2'][0], pop.orbparams['m2'][1], size = 1) p.om = np.random.uniform(pop.orbparams['om'][0], pop.orbparams['om'][1], size = 1) p.inc = np.random.uniform(pop.orbparams['inc'][0], pop.orbparams['inc'][1], size = 1) p.ec = np.random.uniform(pop.orbparams['ec'][0], pop.orbparams['ec'][1], size = 1) p.pod = np.random.uniform(pop.orbparams['pod'][0], pop.orbparams['pod'][1], size = 1) #dither the distance if dither: # find flux flux = p.lum_inj_mu/(p.dtrue**2) # dithered distance p.dold = p.dtrue p.dtrue += random.gauss(0.0,0.2*p.dtrue) # new luminosity p.lum_1400 = flux*p.dtrue**2 p.lum_inj_mu=p.lum_1400 # new R and z p.galCoords = go.lb_to_xyz(p.gl, p.gb, p.dtrue) p.r0=np.sqrt(p.galCoords[0]**2 + p.galCoords[1]**2) #define a burst rate if single pulse option is on if singlepulse: if brDistType == 'd_g': brbin_num=dists.draw1d(dgf_pop_load['brHist']) brmin=dgf_pop_load['brBins'][0] brmax=dgf_pop_load['brBins'][-1] p.br = 10**(brmin + (brmax-brmin)*(brbin_num+random.random())/len(dgf_pop_load['brHist'])) else: p.br=_burst() p.lum_sig=sig_factor p.det_nos=0 else: p.br=None p.det_nos=None # then calc DM using fortran libs if pop.electronModel == 'ne2001': p.dm = go.ne2001_dist_to_dm(p.dtrue, p.gl, p.gb) elif pop.electronModel == 'lmt85': p.dm = go.lmt85_dist_to_dm(p.dtrue, p.gl, p.gb) elif pop.electronModel == 'ymw16': p.dm = go.ymw16_dist_to_dm(p.dtrue, p.gl, p.gb) p.scindex = scindex # then calc scatter time p.t_scatter = go.scatter_bhat(p.dm, p.scindex) # if no surveys, just generate ngen pulsars if surveyList is None: pop.population.append(p) pop.ndet += 1 if not nostdout: prog.increment_amount() print(prog, '\r',) sys.stdout.flush() # if surveys are given, check if pulsar detected or not # in ANY of the surveys else: # just a flag to increment if pulsar is detected detect_int = 0 for surv in surveys: # do SNR calculation if singlepulse: #pop_time = int(p.br*surv.tobs) #if pop_time >= 1.0: SNR = surv.SNRcalc(p, pop,rratssearch=True) #else: # SNR = -3 elif accelsearch: SNR = surv.SNRcalc(p, pop, accelsearch=True) elif jerksearch: SNR = surv.SNRcalc(p, pop, jerksearch=True) else: SNR = surv.SNRcalc(p, pop, rratssearch=False) if SNR > surv.SNRlimit: # SNR is over threshold # increment the flag # and survey ndetected detect_int += 1 surv.ndet += 1 continue elif SNR == -1: # pulse is smeared out surv.nsmear += 1 continue elif SNR == -2: # pulsar is outside survey region surv.nout += 1 continue elif SNR == -3: # rrat didn't burst surv.nbr += 1 continue else: # pulsar is just too faint surv.ntf += 1 continue # add the pulsar to the population pop.population.append(p) # if detected, increment ndet (for whole population) # and redraw the progress bar if detect_int: pop.ndet += 1 if not nostdout: prog.increment_amount() print(prog, '\r',) sys.stdout.flush() # print info to stdout if not nostdout: print("\n") print(" Total pulsars = {0}".format(len(pop.population))) print(" Total detected = {0}".format(pop.ndet)) # print " Number not beaming = {0}".format(surv.nnb) for surv in surveys: print("\n Results for survey '{0}'".format(surv.surveyName)) print(" Number detected = {0}".format(surv.ndet)) print(" Number too faint = {0}".format(surv.ntf)) print(" Number smeared = {0}".format(surv.nsmear)) print(" Number outside survey area = {0}".format(surv.nout)) if singlepulse: print(" Number didn't burst = {0}".format(surv.nbr)) return pop
def run(pop, surveyList, nostdout=False, allsurveyfile=False, scint=False, accelsearch=False, jerksearch=False, rratssearch=False): """ Run the surveys and detect the pulsars.""" # print the population if not nostdout: print "Running doSurvey on population..." print pop # loop over the surveys we want to run on the pop file surveyPops = [] for surv in surveyList: s = Survey(surv) s.discoveries = 0 if not nostdout: print "\nRunning survey {0}".format(surv) # create a new population object to store discovered pulsars in survpop = Population() # HERE SHOULD INCLUDE THE PROPERTIES OF THE ORIGINAL POPULATION # counters nsmear = 0 nout = 0 ntf = 0 ndet = 0 nbr = 0 # loop over the pulsars in the population list for psr in pop.population: # pulsar could be dead (evolve!) - continue if so if psr.dead: continue # is the pulsar over the detection threshold? snr = s.SNRcalc(psr, pop, accelsearch, jerksearch, rratssearch) #print snr ###################################################################################### #This section is to add in degradation due to orbital motion of DNS pulsars. #Remove this if doing literally anything else. #Right now this is very ad-hoc and manual. Optimization possible, maybe not worth right now. deg_fac_1102 = { 'PALFA_one_v_older': 0.9997, 'PHSURV': 0.9997, 'HTRU_low_1757': 0.9912, '1534_survey': 0.9999, 'PMSURV': 0.4649 } #This is for 1913+1102 deg_fac_1913 = { 'PALFA_one_v_older': 0.9953, 'PHSURV': 0.9956, 'HTRU_low_1757': 0.9569, '1534_survey': 0.9999, 'PMSURV': 0.7915 } deg_fac_1946 = { 'PALFA_one_v_older': 0.9514, 'PHSURV': 0.9543, 'HTRU_low_1757': 0.6513, '1534_survey': 0.9999, 'PMSURV': 0.2368 } deg_fac_1757 = { 'PALFA_one_v_older': 0.9710, 'PHSURV': 0.9716, 'HTRU_low_1757': 0.9255, '1534_survey': 0.9999, 'PMSURV': 0.5080 } deg_fac_1534 = { 'PALFA_one_v_older': 0.9999, 'PHSURV': 0.9999, 'HTRU_low_1757': 0.9994, '1534_survey': 0.9999, 'PMSURV': 0.7759 } deg_fac_0737A = { 'PALFA_one_v_older': 0.9910, 'PHSURV': 0.9916, 'HTRU_low_1757': 0.8371, '1534_survey': 0.9999, 'PMSURV': 0.2991 } deg_fac_0737B = { 'PALFA_one_v_older': 0.9999, 'PHSURV': 0.9999, 'HTRU_low_1757': 0.9999, '1534_survey': 0.9999, 'PMSURV': 1 } deg_fac_1756 = { 'PALFA_one_v_older': 0.9999, 'PHSURV': 0.9999, 'HTRU_low_1757': 0.9982, '1534_survey': 0.9999, 'PMSURV': 0.5598 } deg_fac_1906 = { 'PALFA_one_v_older': 0.9999, 'PHSURV': 0.9999, 'HTRU_low_1757': 0.9994, '1534_survey': 0.9999, 'PMSURV': 0.7337 } snr = snr * (deg_fac_1913[surv]**2 ) #Please change this for each DNS ###################################################################################### # add scintillation, if required # modifying S/N rather than flux is sensible because then # a pulsar can have same flux but change S/N in repeated surveys if scint: snr = s.scint(psr, snr) if snr > s.SNRlimit: ndet += 1 psr.snr = snr survpop.population.append(psr) # check if the pulsar has been detected in other # surveys if not psr.detected: # if not, set to detected and increment # number of discoveries by the survey psr.detected = True s.discoveries += 1 elif snr == -1.0: nsmear += 1 elif snr == -2.0: nout += 1 elif snr == -3.0: nbr += 1 else: ntf += 1 # report the results if not nostdout: print "Total pulsars in model = {0}".format(len(pop.population)) print "Number detected by survey {0} = {1}".format(surv, ndet) print "Of which are discoveries = {0}".format(s.discoveries) print "Number too faint = {0}".format(ntf) print "Number smeared = {0}".format(nsmear) print "Number out = {0}".format(nout) if rratssearch: print "Number didn't burst = {0}".format(nbr) print "\n" d = Detections(ndet=ndet, ntf=ntf, nsmear=nsmear, nout=nout, nbr=nbr, ndisc=s.discoveries) surveyPops.append([surv, survpop, d]) if allsurveyfile: allsurvpop = Population() allsurvpop.population = [psr for psr in pop.population if psr.detected] surveyPops.append([None, allsurvpop, None]) return surveyPops
def game_loop(): pop = Population(constants.pop_size) pad = constants.pad #to avoid food getting to much at the boundaries #the first food x_food = random.randrange(pad, display_width - pad, step=food_width) y_food = random.randrange(pad, display_height - pad, step=food_height) print('food', x_food, y_food) score = 0 food_not_eaten = 0 for gen in range(constants.max_generation): game_over = False print('Generation', gen) while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() #moving the population pop.move(x_food, y_food) #checking if game_over game_over, ate_food = pop.check_status(x_food, y_food) #displaying it gameDisplay.fill(black) draw_food(x_food, y_food) update_gen(gen) pop.draw(gameDisplay) pygame.display.update() clock.tick(50) #updates if (ate_food): x_food = random.randrange(pad, display_width - pad, step=food_width) y_food = random.randrange(pad, display_height - pad, step=food_height) if (game_over): break #changing food coordinates after each gen in the hope of getting better training results if not ate_food: x_food = random.randrange(pad, display_width - pad, step=food_width) y_food = random.randrange(pad, display_height - pad, step=food_height) pop.increase_gen()
def population_evolution(num_iterations): ### The parameters choosen were ### # Pop size: 125 # Tournament size: 13, in fact 10% rounded up # Crossover rate: 1 # Mutation rate: 0,005 pop = 126 # MUST BE AN EVEN NUMBER tour = 13 cross = 1 mut = 0.01 # pop = 130 # tour = 26 # cross = 0.9 # mut = 0.01 best_solutions = [] with open(file_name, 'w') as f: best_population = None for i in range(0, 5): # randomize the initial population population = Population(helper, G.edges(keys=True, data=True), trucks, True, pop, tour, cross, mut) print 'initial population best fitness: ' + str( population.get_best_fitness().fitness) + ' with ' + str( len(population.get_best_fitness().trucks_used) ) + ' trucks and with paths number: ' + str( len(population.get_best_fitness().path)) line = '' last_best_iteration = None last_best_fitness = None # evolve the population for i in range(num_iterations): population = population.evolve() print 'iteration ' + str(i) + ' best fitness: ' + str( population.get_best_fitness().fitness) + ' with ' + str( len(population.get_best_fitness().trucks_used) ) + ' trucks and with paths number: ' + str( len(population.get_best_fitness().path)) line += str(population.get_best_fitness().fitness) + ';' # store the last best fitness and last best iteration if last_best_fitness == None or last_best_fitness > population.get_best_fitness( ).fitness: last_best_fitness = population.get_best_fitness().fitness last_best_iteration = i # check for stop condition, 8000 iterations without improvement if i - last_best_iteration > 800: break f.write(line) f.write('\n') best_solutions.append(population.get_best_fitness()) if best_population == None or best_population.get_best_fitness( ).fitness > population.get_best_fitness().fitness: best_population = population # save the best solutions to the json file for s in best_solutions: s.path = list(e[0:3] for e in s.path) with open("best_solutions_with_larger_truck_only.json", "w") as jfile: jfile.write('[') for s in best_solutions: d = s.toJSON() jfile.write(d) jfile.write(',') jfile.write(']') # Print the best fitness found print 'final best_population best fitness: ' + str(best_population.get_best_fitness().fitness) + ' with ' + str(len( best_population.get_best_fitness().trucks_used)) + ' trucks and with paths number: ' + str(len(best_population.get_best_fitness().path)) + \ ' - Distance from deposit: ' + str(best_population.get_best_fitness().deposit_distance) + ' - Difference: ' + \ str(best_population.get_best_fitness().fitness - best_population.get_best_fitness().deposit_distance)
def main(): #DATA #genes = ['Prague', 'Olomouc', 'Liberec', 'Opava', 'Brno', 'Pardubice', 'Znojmo', 'Jihlava', 'Opole', 'Bratislava', 'Berlin', 'Trnava', 'Most'] genes = [ 'Wien', 'Salzburg', 'Maribor', 'Villach', 'Klatovy', 'Prague', 'Olomouc', 'Liberec', 'Opava', 'Brno', 'Pardubice', 'Znojmo', 'Jihlava', 'Opole', 'Bratislava', 'Berlin', 'Trnava', 'Most' ] #INITIAL CONDITIONS populationSize = 100 eliteSize = 4 mutationRate = 0.4 #CONVERGENCE SETTINGS converged = False generationCount = 1 generationLimit = 200 #PLOTS distances = [] tours = [] removeLines = True fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 15)) #create a figure with 2 subplots ax2.set_xlim([1, generationLimit ]) #fixed length of x-axis (number of generations) ax2.locator_params(nbins=10, axis='x') # control density of axis desctiption #CREATE FIRST GENERATION routes = [random.sample(genes, len(genes)) for i in range(populationSize)] p = Population(routes) while not converged: distances.append(p[0][1]) tours.append(p[0][2]) converged = evolveByCount(generationCount, converged, generationLimit) if converged: removeLines = False ##plot the best tour in each generation lats = [] lons = [] for city in p[0][2]: lats.append(city.latitude) lons.append(city.longitude) plotTours(lons, lats, ax1, removeLines) ## plot the progress of the GA plotGA(distances, generationCount, ax2) #add the most successful individuals from the previous generation newRoutes = [] if eliteSize: newRoutes.extend(elitistSelection(p, eliteSize)) #create offspring vie crossover and mutation for i in range(eliteSize, populationSize - 10): parent1 = rouletteWheelSelection(p) parent2 = rouletteWheelSelection(p) child = ERO(parent1, parent2) newRoutes.append(invert(child, mutationRate)) #generate 10 new random individuales for j in range(10): routes = random.sample(genes, len(genes)) newRoutes.append(routes) p = Population(newRoutes) generationCount += 1 print('The shortest tour is ', tours[-1]) print('The distance is ', distances[-1]) plt.show()
from p5 import * from snake import Snake from food import Food from settings import Settings from inputs import Inputs from NeuralNetwork import NeuralNetwork from population import Population settings = Settings() #create Popluation object with settings P = Population(settings) def setup(): size(settings.windowSize, settings.windowSize) no_stroke() background(0) #setup population P.setup() def draw(): background(0) #run population P.run() text("Generation: {}".format(settings.generation), (0,100), wrap_at=None) text("Highest number of eats: {}".format(settings.globalBestTotal), (0, 120), wrap_at=None) text("Mutation rate: {}".format(settings.mutationRate), (0, 140), wrap_at=None)
def run(self): '''Run G.A.''' # store the generations historic chronology_fitness = [] # store the best fitness of each generation chronology_feasible = [ ] # store the number of feasible individuals of each generation noChange = 0 # control the number of rounds with no improvements # initialize a random population population = Population(self) population.initialize() # store the best fitness of the population best_pop = population chronology_feasible.append(population.getFeasiblePct()) best_fitness = population.getBestFitness() chronology_fitness.append(best_fitness) # print information population.print() #population.gantt() # run generations (previously set) or until cost = 0 for i in range(config.generations - 1): # generate a new population based on the ancestor population newPop = Population(self) newPop.generate(population) #newPop.autoAdjust() # identify if there is any improvement new_fitness = newPop.getBestFitness() best_fitness = best_pop.getBestFitness() if (new_fitness != -1 and best_fitness != -1): if new_fitness < best_fitness: if (config.autoAdjust): newPop.autoAdjust() best_pop = newPop best_fitness = newPop.getBestFitness( ) # after auto adjustment noChange = 0 self.bestTime = t.time() self.bestGen = self.generation_count self.bestFitness = best_fitness #best_pop.gantt() #pra ver o que muda cada vez que melhora o fitness else: noChange += 1 else: if (best_fitness == -1 and new_fitness >= 0): if (config.autoAdjust): newPop.autoAdjust() best_pop = newPop best_fitness = newPop.getBestFitness( ) # after auto adjustment noChange = 0 self.bestTime = t.time() self.bestGen = self.generation_count self.bestFitness = best_fitness #best_pop.gantt() #pra ver o que muda cada vez que melhora o fitness #else: # noChange += 1 self.no_change_generations = noChange newPop.print() # store the best fitness of the population chronology_feasible.append(newPop.getFeasiblePct()) chronology_fitness.append(best_fitness) # identify if the solution converged to global miminum if (best_fitness == 0): print("converged - round", self.generation_count) break elif (noChange > config.exitAfter): print("exit after", config.exitAfter, "generations without improvements - round", self.generation_count) break # set the population for the next generation population = newPop self.endTime = t.time() if (self.plot == True): # show the last solution generated best_pop.gantt() # plot the evolution graph plot.plot(chronology_fitness, 'cost') plot.plot(chronology_feasible, 'feasible')
while i <= 10: i+=1 self.env.reset() self.env.after(100, self.show_path_on_maze(best_specimen=best_specimen)) def show_path_on_maze(self, best_specimen): specimen = best_specimen start = 0 end = 2 for i in range(26): direction = specimen.genetic_code[start:end] start += 2 end += 2 action = maze_value[direction] self.move_view_truck(action) if __name__ == "__main__": algorithm = GeneticAlgorithm(0.7, 0.2, 1) population = Population(2000) population.create_random_population() generations_limit = 120 controler = Controller() #controler.main_show_maze(5000, 8.6, 0.5, 1000, 1)
def __main__(): try: opts, args = getopt.getopt(sys.argv[1:],"hT:l:N:G:u:x:g:t:X:s:r:K:S:b:c:e:z:v:") except getopt.GetoptError: usage() for opt, arg in opts: if opt == "-h": details() sys.exit(0) elif opt == "-T": global _T _T = int(arg) elif opt == "-l": global _l _l = map(float, arg.split(",")) elif opt == "-N": global _N _N = int(arg) elif opt == "-G": global _G _G = int(arg) elif opt == "-u": global _u _u = float(arg) elif opt == "-x": global _x _x = float(arg) elif opt == "-t": global _t _t = float(arg) elif opt == "-X": global _X _X = int(arg) elif opt == "-s": global _s _s = float(arg) elif opt == "-r": global _r _r = float(arg) elif opt == "-K": global _K _K = int(arg) elif opt == "-c": global _c _c = float(arg) elif opt == "-b": global _b _b = int(arg) elif opt == "-e": global _e _e = float(arg) elif opt == "-z": global _z _z = float(arg) elif opt == "-v": global _v _v = float(arg) elif opt == "-g": global _g if arg not in ['ALL', 'H**O', 'HET']: print("Invlaid selection calculation type: -g "+arg) sys.exit(0) _g = arg elif opt == "-S": global _S if arg not in ['R', 'T', 'O', 'P']: print("Invlaid selection type: -S "+arg) sys.exit(0) _S = arg else: sys.stderr.write("Unrecognized option: "+opt+"\n") usage() start = time.time() printParams() #initialize population pop = Population(size = _N, selfing = _r, silence = _l, numCrosses = _X, selection = (_x, _z), t = _t, mode = _g, transRate = _u, maxClasses = _b, classMutation = _c, silenceMutation = _s, epsilon = _e, excision = _v) pop.firstGen(loci = _G, numTEs = _T) #phase 1 initial population stabilization for gen in range(1, _K): pop.generation(_S, gen) sys.stderr.write("Run time (min): "+str((time.time()-start)/60.0)+"\n")
# ----------------------------------------------------------------------------------------------------- # Initialize checkpoints # ----------------------------------------------------------------------------------------------------- checkpoints = [] for i in range(0, len(dots), 4): checkpoints.append( Wall(dots[i] + canvas_width / 2, dots[i + 1] + canvas_height / 2, dots[i + 2] + canvas_width / 2, dots[i + 3] + canvas_height / 2)) # ----------------------------------------------------------------------------------------------------- generation = 0 step = 0 steps_for_gen = 50 pop_size = 100 population = Population(pop_size, steps_for_gen, route_walls, checkpoints) def create_circle(x, y, r, canvasName): #center coordinates, radius x0 = x - r y0 = y - r x1 = x + r y1 = y + r return canvasName.create_oval(x0, y0, x1, y1) car_forms = [0] * pop_size dots_sense = [0] * 8 def update():