def iteration(self): """ 算法迭代主程序 :return: 最后迭代完成的排序,去重的种群 """ node_count = len(self.read_json()) pop = population(self.pop_size, self.individuals_num, node_count) node_dirt = self.read_json() func_obj = MinMax2(pop, node_dirt) for i in range(self.iterations_num): copy_pop = pop.copy() selection(pop, func_obj) crossover(pop, self.cross_probability) mutation(pop, self.mutation_probability, node_count - 1) origin_pop = pop temp_pop = vstack((copy_pop, origin_pop)) func_obj = MinMax2(temp_pop, node_dirt) pop = dominanceMain(temp_pop, func_obj) print("第 %d 次迭代" % i) # estimate(pop, func_obj) pop_node = array(list(set([tuple(sorted(t)) for t in pop]))) # 个体按数值大小排序, 去重 return pop_node
def createnewactor(livingactors, actorsinview, parent1, parent2, t): position = [0, 0] birth = t death = -1 age = 0 for j in range(0, 2): position[j] = int((actorsinview[parent1].position[j] + actorsinview[parent2].position[j]) / 2) walkspeed = mutation.mutation(actorsinview[parent1].walkspeed, actorsinview[parent2].walkspeed) viewdistance = mutation.mutation(actorsinview[parent1].viewdistance, actorsinview[parent2].viewdistance) longevity = mutation.mutation(actorsinview[parent1].longevity, actorsinview[parent2].longevity) lifespan = mutation.mutation(actorsinview[parent1].lifespan, actorsinview[parent2].lifespan) role = actorsinview[parent1].role size = actorsinview[parent1].size fertility = 0 hunger = longevity / 2 vectors = [[position[0], position[1]]] index = len(livingactors) # Create actors and print out list Actor = Actors(role, size, position, walkspeed, viewdistance, hunger, longevity, birth, death, age, lifespan, actorsinview[parent1].id, actorsinview[parent2].id, fertility, vectors, index) livingactors.append(Actor) return (livingactors)
def gen(dico_cube): """ applique l'algo gen a un dico pour construire la figure entree en parametres """ liste_fig = [] liste_c = g.liste_cube(dico_cube) for i in range(len(liste_c)): liste_fig.append(gf.init_fig(o.hauteur, o.largeur, o.longueur)) g.placer_cube((0, 0, 0), liste_c[i], liste_fig[i], dico_cube) i = 0 while (not (Mut.est_pleine(liste_fig)) and i < nb_iter_max): liste_pond = t.ponderation(liste_fig) place_p = t.choix_pond(liste_pond) porteuse = liste_fig.pop(place_p) place_d = t.choix_alea(liste_fig) donneuse = liste_fig.pop(place_d) porteuse_temp = porteuse donneuse_temp = donneuse #matrice 3D croismnt = C.croisement(porteuse_temp, donneuse_temp, dico_cube) mut = R.randint(1, 10) if (mut == 1 and Me.nb_cube(donneuse_temp)): Mut.mutation(porteuse_temp, liste_fig, dico_cube) if (Me.nb_cube(porteuse_temp) >= Me.nb_cube(porteuse) and croismnt): liste_fig.append(porteuse_temp) else: liste_fig.append(porteuse) if (Me.nb_cube(donneuse_temp) != 0 ): #on rement la donneuse dans la liste que si elle est non vide liste_fig.append(donneuse_temp) else: liste_fig.append(donneuse) if not (i % 100000): print(i) if (len(liste_fig) <= 2): for k in range(len(liste_c)): liste_fig.append(gf.init_fig(o.hauteur, o.largeur, o.longueur)) g.placer_cube((0, 0, 0), liste_c[k], liste_fig[k], dico_cube) liste_fig = gf.en_double(liste_fig) liste_fig = gf.doublon(liste_fig) i += 1 print("nombre d'iteration : {0}".format(i)) return liste_fig
def solve(self): """ This method solves the problem using NTGA algorithm """ generation_limit = int(config.get('run_config', 'generation_limit')) self.generate_initial_population() new_population = [] print("Generation limit:", str(generation_limit)) for i in range(generation_limit): print("generation: ", i) self.population = self.evaluate(self.population) self.non_dominated_set.adds(self.population) while len(new_population) < self.population_size: parents = self.selection() children = mutation(crossover(parents)) for child in children: count = 0 while (child in new_population) or children[0] == children[1]: count += 1 mutate(child) if count > 100: break new_population += children self.population = new_population new_population = [] return self.non_dominated_set
def form_children(population, c_s = None, c_f = None, s = None): '''Select a pair of parents and form children Args: population: The population selection pool. Returns: The resulting children ''' if c_s == None: c_s = parameters.cs if c_f == None: c_f = parameters.cf if s == None: s = parameters.s # import pdb; pdb.set_trace(); # parent_a = random.choice(population) # crowding_selection_group = random.sample(population, c_s) parent_a = population.sample(1).iloc[0] crowding_selection_group = population.sample(c_s) # import pdb; pdb.set_trace() # pool_values['decoded'].apply(lambda x: euclidean(x, peak)).idxmin() parent_b = crowding_selection_group.ix[ crowding_selection_group['decoded'].apply(lambda x: similarity(parent_a.decoded, x)).idxmin() ] # parent_b.orient('index') # parent_b = max(crowding_selection_group, key= lambda x: similarity(parent_a, x)) # import pdb; pdb.set_trace() child_a, child_b = crossover(parent_a, parent_b) child_a = mutation(child_a) child_b = mutation(child_b) return child_a, child_b
def mutant_hero(population, crossover_fnc, cmp_fnc): hero = population[0] hero_o = hero['org'][0] hero_a = hero['amplitude'] child_o = [ele for ele in hero_o] child_o = [child_o, [0,0,0]] child = {'org' : child_o, 'amplitude' : hero_a} child = mut.mutation(child, True) population.append(child) population.sort(lambda x,y : mtr.comparator(x,y,cmp_fnc)) population = population[:-1] return population
def GA(): population = generatePopulation.generate(20) fitness_of_Population = calculateFitness.fitness(population) for i in range(10000): selcted_population = selection.selection(population, fitness_of_Population) crossover_population = crossover.crossover(selcted_population) population = mutation.mutation(crossover_population) fitness = calculateFitness.fitness(population) print(max(fitness)) if max(fitness) == 24: break
def non_greedy_selection(population, crossover_fnc, cmp_fnc): indices = four_indices(len(population)) parents = [population[indices[i]] for i in range(4)] parents.sort(lambda x,y : mtr.comparator(x,y,cmp_fnc)) winner1, winner2 = parents[0], parents[1] loser1, loser2 = parents[2], parents[3] # Fitness is measured before crossover is complete and automatically # cached in a tuple. child1 and child2 should be tuples now. child1, child2 = cvr.crossover(winner1, winner2, crossover_fnc) # Mutate (if the check passes) child1 = mut.mutation(child1) child2 = mut.mutation(child2) # Replace the losers from selection population[population.index(loser1)] = child1 population[population.index(loser2)] = child2 return population
def run(): # 初始化种群 population_init(encode, Num, Bit) for i in range(epoch): # 解码 decode = decode_function(encode, max, min, Bit) # 计算适应度(用直接以待求解的目标方程函数) y = val_function(decode) # 选择适应度提高的染色体进行复制 selection(encode, y) # 交叉基因 crossover(encode, CV, Bit) # 基因变异 mutation(encode, change, Bit) #解码 decode = decode_function(encode, max, min, Bit) # 新种群 y = val_function(decode) a = 100 - np.array(y).max() print(a) return a
def greedy_selection(population, crossover_fnc, cmp_fnc): indices = four_indices(len(population)) parents = [population[indices[i]] for i in range(4)] parents.sort(lambda x,y : mtr.comparator(x,y,cmp_fnc)) winner1, winner2 = parents[0], parents[1] # Fitness is measured before crossover is complete and automatically # cached in a tuple. child1 and child2 should be tuples now. child1, child2 = cvr.crossover(winner1, winner2, crossover_fnc) # Mutate (if the check passes) child1 = mut.mutation(child1) child2 = mut.mutation(child2) # Add the children, resort, remove the worst 2 population.append(child1) population.append(child2) population.sort(lambda x,y : mtr.comparator(x,y,cmp_fnc)) population = population[:-2] return population
def test_mutation(self, chrom1): import mutation chrom2 = mutation.mutation(chrom1) test_is_chromosome(self, chrom2, len(chrom1)) score = 0 for a, b in zip(chrom1, chrom2): if a != b: score += 1 self.assertFalse(score > 1, "Too many mutations") self.assertFalse(score == 0, "The chromosome has not been mutated")
def differential_evolution(population, f, km, kr, bounds, max_iter): history = np.array([population]) best = population[:, np.argmin(f(population))] i = 0 while i < max_iter: mutant = mut.mutation(population, best, km) offspring = recomb.recombination(population, mutant, kr) offspring[np.where(offspring >= bounds[1]) or np.where(offspring < bounds[0])] = np.random.uniform( bounds[0], bounds[1], 1) selected = sel.selection(population, offspring, f) history = np.append(history, selected) history = np.reshape(history, (i + 2, population.shape[0], population.shape[1])) population = np.array(selected) best = population[:, np.argmin(f(population))] i += 1 return history
def mainloop(generation, pop, bandwidth_matrix, neighbor_matrix, k_shortest, population_size): # main loop fit_v_gen = [] for g in range(generation): #display(pd.DataFrame(fitness.population_with_fitness(pop, bandwidth_matrix))) # crossover temp_pop = [] for idx in range(len(pop)-1): child1, child2 = crossover.random_crossover(pop[idx], pop[idx+1], bandwidth_matrix) temp_child1 = crossover.check_cycle(child1) temp_child2 = crossover.check_cycle(child2) if len(temp_child1) != len(set(temp_child1)): print(child1, temp_child1) input() if len(temp_child2) != len(set(temp_child2)): print(child2, temp_child2) input() temp_pop += [temp_child1, temp_child2] # mutation children_pop = [] for ind in pop: temp_child1 = mutation.mutation(neighbor_matrix, ind, 0.5) temp_child2 = crossover.check_cycle(temp_child1) if len(temp_child2) != len(set(temp_child2)): print(child2, temp_child2) input() children_pop.append(temp_child2) # diversity maintenance pop = np.unique(pop+temp_pop+children_pop).tolist() # fitness evaluation pop_with_fit = fitness.population_with_fitness(pop, bandwidth_matrix) # selection pop = pd.DataFrame(sorted(pop_with_fit, key=lambda ind: ind['fitness'], reverse=True)) fit_v_gen.append(np.mean(pop['fitness'][:k_shortest])) #display(pop) pop = pop['individual'][0:population_size].tolist() return pop, fit_v_gen
def run(self, generations): logging.debug('Running for %s generations', generations) # Compute initial payoffs (fitness evaluation) gen = 0 logging.debug('GEN %s', gen) (payoffs, mean_attendance) = population.simulate(gen, self._population, self._weeks) logging.info('Attendance for gen %s:\n%.4f\n', gen, (mean_attendance / self._lambda)) # Sort the initial population by payoffs (self._population, sorted_payoffs) = population.sorted_by_payoffs(self._population, payoffs) logging.info('Payoffs for gen %s:\n%s', gen, sorted_payoffs) for gen in range(1, generations): logging.debug('GEN %s', gen) new_pop = self._population.copy() # Select parents parents = selection.truncation(self._num_parents, new_pop) # Generate children by crossover children = crossover.crossover(parents) # Replace worst solutions in the population with children new_pop[-children.size:] = children # Mutation considering all individuals except the best new_pop[1:] = mutation.mutation(new_pop[1:], self._mutation_chance, self._mutation_rate) # Compute payoffs of new population (payoffs, mean_attendance) = population.simulate(gen, new_pop, self._weeks) logging.info('Attendance for gen %s:\n%.4f\n', gen, (mean_attendance / self._lambda)) # Sort the new population by payoffs (sorted_pop, sorted_payoffs) = population.sorted_by_payoffs(new_pop, payoffs) logging.info('Payoffs for gen %s:\n%s', gen, sorted_payoffs) self._population = sorted_pop assert self._population.size == self._lambda return mean_attendance / self._lambda
def solve(self): generation_limit = int(config.get('run_config', 'generation_limit')) self.generate_initial_population() new_population = [] for i in range(generation_limit): self.population = self.evaluate(self.population) self.non_dominated_set.adds(self.population) while len(new_population) < self.population_size: parents = self.selection() children = mutation(crossover(parents)) for child in children: while (child in new_population) or children[0] == children[1]: child.mutate() new_population += children self.population = new_population return self.non_dominated_set
def geneticAlgo(): func, ranges = fitnessfunction() strings = [] k = 10 for i in range(k): strings.append(generatestring(ranges)) m = 10000000000000000 state = strings[0] for _ in range(1000): strings = selection(strings, func) for i in range(0, k, 2): strings[i], strings[i + 1] = crossover(strings[i], strings[i + 1]) for i in range(k): strings[i] = mutation(strings[i], ranges) strings.sort(key=lambda x: getFitness(func, x)) x = getFitness(func, strings[0]) if x < m: m = x state = strings[0] x = int(state[:len(state) // 2], 2) y = int(state[len(state) // 2:], 2) print("Minimum of the function found: {} at x = {} and y = {}".format( m, x, y))
def genetic_algorithm(): gs = [] k = 30 for i in range(k): gs.append(random_generation()) for _ in range(1000): gs1 = selection(gs, k) for i in range(0, len(gs1), 2): gs1[i], gs1[i + 1] = crossover(gs1[i], gs1[i + 1]) for i in range(len(gs1)): gs1[i] = mutation(gs1[i]) gs = gs1[:] gs.sort(key=lambda x: -fitness_function(x)) m = fitness_function(gs[0]) s = gs[0] if m == 243: print( "5 Best solution along with their fitnesses of the final population" ) print("Sudoku 1: Solution Found!!") printSudoku(s) print("Fitness Value: {}".format(fitness_function(gs[0]))) for j in range(1, 5): print("Sudoku {}:".format(j + 1)) printSudoku(gs[i]) print("Fitness Value: {}".format(fitness_function(gs[i]))) break else: print( "5 Best solution along with their fitnesses of the final population" ) for i in range(5): print("Sudoku {}:".format(i + 1)) printSudoku(gs[i]) print("Fitness Value: {}".format(fitness_function(gs[i]))) print('')
tries.append(iteration) #stroring number of iterations decoded_values = decode(population, x_1, x_2, l1, l2) #decoding values of x1 and x2 fitness_values = [] #calculating fitness values [fitness_values.append(fitness(decoded_values[0][i],decoded_values[0][i])) for i in range(N)] P = [f/sum(fitness_values) for f in fitness_values] #probability of selection cum_prob = np.cumsum(P).tolist() #cumulative probability n = round((N/2)*2*Pc) #number of individuals participating in crossover Parents = Roulette(population,P,N,n) #parents selection using Roulette selection Children = crossover(Parents,n) #Children from crossover Children_new = mutation(Children,Pm,L) #new chidren after mutation f_values = [] #calculating value of original function and storing [f_values.append((1.0/fitness_values[i])-1.0) for i in range(N)] min_values.append(min(f_values)) #storing minimum value of function for iteration population = Children_new #redefining parameters N = len(Children_new) print('Minimum value of function is ',min(min_values)) #printing result, i.e., minimum value of the function plt.plot(tries,min_values,linewidth=1) #plotting iterations vs min value for each iteration plt.xlabel('Iterations') plt.ylabel('Function Value')
x = np.zeros((maxIter)) y = np.zeros((maxIter)) feature = np.zeros((maxIter, chromlength)) for iter in range(maxIter): #objvalue = objective(pop) #temp = objvalue = objective(pop, train_X, train_Y) fvalue = fitvalue(objvalue, 0.3) argmax = np.argmax(fvalue, axis=0)[0] y[iter] = fvalue[argmax, 0] feature[iter, :] = pop[argmax, :] newpop = selection(pop, fvalue) newpop = crossover(newpop, pc) newpop = mutation(newpop, pm) pop = newpop print y clf = svm.SVC() f = np.which(feature[maxIter - 1, :] == 1) X = test_X[:, np.array(f)[0]] #print X.shape clf.fit(X, test_Y) print clf.score(X, y, sample_weight=None)
probability = float(probability) for i in range(0, x): for org in population: org.selection_value = 0 for i in range(0, len(org.Protein)): if org.Protein[i:i + selected_protein_length] == adaptation: org.selection_value = org.selection_value + 1 #fittest = sorted(population, key=lambda org:org.selection_value, reverse=True) fittest = population for org in fittest: if org.selection_value == 1: org.survival_prob = 1.0 if org.selection_value == 0: org.survival_prob = random.random() if org.survival_prob < probability: fittest.remove(org) for org in fittest: print('Organism number: %d' % org.organism_number) print('Organism Protein: %s' % org.Protein) print('Organism selection value: %d' % org.selection_value) #organisms are now sorted in fittest list #need to only take certain amount #now for mutation for org in fittest: org.DNA = mutation.mutation(org.DNA, mutagen) new_org_file = open(org.location, 'w') new_org_file.write(org.DNA) new_org_file.close() org.RNA = central_dogma.transcription(org.DNA) org.Protein = central_dogma.translation(org.RNA)
def describe_mut(d_in, vcf_path, ref_fasta, ant_file, pop_vcf_file) -> dict: """ convert single mutation line (as list) to 'mut describer format' mut list should be in format [fname, chrom, pos, ref, alt, gt_bases, gt_quals, gt_depths, GQ1, GQ2] Parameters ---------- d_in : dict mut info from csv.DictReader vcf_path : str path to dir containing per-sample VCF files ref_fasta : str path to reference genome FASTA file ant_file : str path to annotation table file - should be tabixed pop_vcf_file : str path to VCF file to be used for pi calc [optional] Returns ------- mut_dict : dict dict with 'mut describer fields' to be written to outfile """ if not isinstance(d_in, dict): raise TypeError() ref_dict = SeqIO.to_dict(SeqIO.parse(ref_fasta, 'fasta')) d = {} # create mutation.mutation object fname = d_in['fname'] vcf_file = glob(vcf_path + fname.replace('samples', '') + '*.vcf.gz')[0] # ugh chrom = d_in['chrom'] pos = int(d_in['pos']) mut = mutation.mutation(chrom, pos) # populate dict mut.ref = d_in['ref'] mut.alt = eval(d_in['alt']) d['chromosome'] = chrom d['position'] = pos d['ref'] = d_in['ref'] d['alt'] = eval(d_in['alt']) d['qual'] = d_in['QUAL'] d['MQ'] = d_in['MQ'] # mut properties d['type'] = mut.mutation_type(vcf_file) vcf_reader = VCF(vcf_file) vcf_rec = [ rec for rec in vcf_reader(f'{chrom}:{pos}-{pos}') if rec.POS == pos ][0] # use fname to get sample and 0 and 5 names print(fname) if fname.startswith('DL41'): # have to hardcode this for now... sample = ['DL41_0', 'DL46_5'] elif fname.startswith('DL46'): sample = ['DL46_0', 'DL41_5'] elif fname.startswith('SL26'): sample = [None, 'SL26_5'] else: sample = [fname.replace('samples', '') + str(n) for n in [0, 5]] # assign mut and determine mut sample if sample[0]: idx_0 = vcf_reader.samples.index(sample[0]) base_0 = vcf_rec.gt_bases[idx_0][0] gt_0_count = list(vcf_rec.gt_bases).count(vcf_rec.gt_bases[idx_0]) else: # just for SL26 sample[0] = 'replace' # sigh idx_0 = None base_0 = d['ref'] gt_0_count = vcf_rec.num_hom_ref # we always have a '5' sample idx_5 = vcf_reader.samples.index(sample[1]) base_5 = vcf_rec.gt_bases[idx_5][0] gt_5_count = list(vcf_rec.gt_bases).count(vcf_rec.gt_bases[idx_5]) if gt_0_count < gt_5_count: # 0 sample is the mutant d['mutant_sample'] = sample[0] d['mutation'] = '>'.join([base_5, base_0]) elif gt_0_count > gt_5_count: # 5 sample is the mutant d['mutant_sample'] = sample[1] d['mutation'] = '>'.join([base_0, base_5]) elif gt_0_count == gt_5_count: tqdm.write( f"[saltMA] WARNING: this probably shouldn't happen - see {sample} {chrom} {pos}" ) tqdm.write('[saltMA] trying to resolve using ancestry...') # try to solve using ancestry if fname.startswith('D'): sub_samples = [ s for s in vcf_reader.samples if 'D' in s and s not in sample ] elif fname.startswith('S'): sub_samples = [ s for s in vcf_reader.samples if 'S' in s and s not in sample ] elif fname.startswith('C'): sub_samples = [ s for s in vcf_reader.samples if 'CC' in s and s not in sample ] sub_indices = [vcf_reader.samples.index(s) for s in sub_samples] sub_genotypes = [vcf_rec.gt_bases[idx] for idx in sub_indices] sub_0_count = sub_genotypes.count(vcf_rec.gt_bases[idx_0]) sub_5_count = sub_genotypes.count(vcf_rec.gt_bases[idx_5]) if sub_0_count < sub_5_count: d['mutant_sample'] = sample[0] d['mutation'] = '>'.join([base_5, base_0]) tqdm.write('resolved') elif sub_0_count > sub_5_count: d['mutant_sample'] = sample[1] d['mutation'] = '>'.join([base_0, base_5]) tqdm.write('[saltMA] resolved') elif sub_0_count == sub_5_count: raise Exception( f'could not solve mut with ancestry - {sample} {chrom} {pos}') # purity purity = mut.purity(vcf_file) if len(purity) > 0: d['min_purity'] = min(purity) else: d['min_purity'] = None # variant stats d['het_count'] = mut.het_count(vcf_file) d['number_mutants'] = mut.number_mutants(vcf_file) d['number_genotypes'] = mut.number_genotypes(vcf_file) # genomic stats d['gc20'] = mut.gc_content(ref_dict, 10) d['gc2000'] = mut.gc_content(ref_dict, 1000) if pop_vcf_file: d['pi1000'] = mut.pi(pop_vcf_file, 500, ref_dict) # nonsynonymous vs synonymous changes ant_pos = mut.annotation_position(ant_file) if ant_pos.CDS == '1' and d['type'] == 'SNP': d['nonsyn_v_syn'], d['anc_codon'], d['mut_codon'] = mut.nonsynonymous( ant_file, vcf_file) else: d['nonsyn_v_syn'], d['anc_codon'], d['mut_codon'] = None, None, None d['nonsense'] = mut.nonsense(vcf_file, ant_file) ant_cols = [ 'chromosome', 'position', 'reference_base', 'genic', 'exonic', 'intronic', 'intergenic', 'utr5', 'utr3', 'fold0', 'fold4', 'fold2', 'fold3', 'CDS', 'mRNA', 'rRNA', 'tRNA', 'feature_names', 'feature_types', 'feature_ID', 'cds_position', 'strand', 'frame', 'codon', 'aa', 'degen', 'FPKM', 'rho', 'FAIRE', 'recombination', 'mutability', 'all_quebec_alleles' ] ant_vals = ant_pos.output_line().split('\t') for i, _ in enumerate(ant_cols): d[ant_cols[i]] = ant_vals[i] return d
# 变异 for k in range(0, nm): n = np.random.randint(0, nPop) # 选取nm个个体,改变其[Pos, Time] p = pop[n] # AGA f_m = p[1].fitness if f_m >= f_avg: pm = np.abs(pm_1 - (((pm_1 - pm_2) * (f_max - f_m)) / (f_max - f_avg))) else: pm = pm_1 # 变异 pop_m['pop_m' + str(k)].chrom = mutation(p[1].chrom, nodesInd, NodeSize) # 计算适应度 pop_m['pop_m' + str(k)].fitness, pop_m['pop_m' + str(k)].total = fitnessFunction( pop_m['pop_m' + str(k)].chrom, extra, Velocities, ConductivityMatrix, pipes, nodesInd, con, demand) nm = np.round(pm * nPop).astype('int64') # 突变数量 print pm, nm # 合并种群 new_pop = dict(dict(pop_c).items() + pop_m.items() + dict(pop).items()) # 种群排序 、更新最优 最差个体 输出最优解决方案 sort_pop = sorted(new_pop.iteritems(), key=lambda x: x[1].fitness,
MI = settings["MI"] # initialize population and set time to 0 logger.debug(f"Creating init population with {MI} chromosomes") populations.append(createInitPopulation(MI)) # MI elements t = 0 # initialize count of generations not getting better to 0 stale_generations_count = 0 progress_bar = tqdm() while not stop_condition(t, stale_generations_count, lowest_fitness): temporary_population = [] for i in range(settings["LAMBDA"]): a = random.uniform(0, 1) if a < settings["CROSSOVER_PROB"]: logger.debug("Entering crossover section") logger.debug("Getting new chromosome") chromosome = mutation( crossover(select(populations[t], k=2))) logger.debug("Calculating fitness") fitness = calc_fitness(chromosome) logger.debug( "Appending chromosome with fitness to population") temporary_population.append((chromosome, fitness)) else: logger.debug("No crossover in this generation") logger.debug("Getting new chromosome") chromosome = mutation(select(populations[t], k=1)) logger.debug("Calculating fitness") fitness = calc_fitness(chromosome) logger.debug( "Appending chromosome with fitness to population") temporary_population.append((chromosome, fitness))
#initiate population population=[] for i in range(10): entity='' for j in range(17): entity=entity+str(np.random.randint(0,2)) population.append(entity) t=[] for i in range(1000): #selection value=utils.fitness(population,aimFunction) population_new=selection(population,value) #crossover offspring =crossover(population_new,0.7) #mutation population=mutation(offspring,0.02) result=[] for j in range(len(population)): result.append(aimFunction(utils.decode(population[j]))) t.append(max(result)) plt.plot(t) plt.axhline(max(y), linewidth=1, color='r')
def optimize(self, nbgen=10, nproc=1): """ Do the genetic algorithm with 'niter' generations :param 'nbgen': the maximal number of gnerations (i.e. iterations) :param 'nproc': the number of processes (parallel simulations) :return: a tuple of two elements: - the optimal individual of the last population (list) - the associated optimal cost function (float) """ if self.__ready == False: if self.verbose: print('--> genop Error!') print('\tThe problem is not correctly initialized.') print('\tRun the "initialize" method before optimizing.') return -1 # initialize arrays for the solutions self.popiter = np.array([]).reshape(0, self.nvar) self.foptiter = np.array([]).reshape(0, 1) # nsimul must be equal to 0 self.nsimul = 0 # Initialize a population from a uniform distribution self._pop = genpop(self.bounds, self.nvar, self.popsize) # Compute the initial score for the population self._fvalpop, ncalls = cost(self._pop, self.popsize, self.nvar, self.function, nproc) self.nsimul = self.nsimul + ncalls # Efficiency fmin = np.ndarray.min(self._fvalpop) fmax = np.ndarray.max(self._fvalpop) efficiency = (1. - self.pressure) * (fmax - self._fvalpop) / \ np.ndarray.max(np.array([fmax - fmin, np.spacing(1)])) + self.pressure # Evolution for i in range(0, nbgen): # Selection (ind1, f1, ind2, f2) = selection(self._pop, self._fvalpop, \ self.nbcouples, efficiency) # Crossover (ind1, ind2, tocompute1, tocompute2) = \ crossover(ind1, ind2, self.bounds, self.pbcross) # Mutation (ind1, ind2, tocompute1, tocompute2) = \ mutation(ind1, ind2, tocompute1, tocompute2, self.bounds, self.pbmut) # Update cost function values f1, f2, ncalls = updatecost(ind1, ind2, f1, f2, tocompute1, tocompute2, self.function, nproc) self.nsimul = self.nsimul + ncalls # Recombination (self._pop, self._fvalpop, efficiency) = elitist(self._pop, ind1, ind2, self._fvalpop, f1, f2, self.pressure) # Show info if self.verbose: printinfo(i, nbgen, self._fvalpop[0], self._pop[0, :], self.nsimul) # Save the best cost function and solution self.foptiter = np.vstack([self.foptiter, self._fvalpop[0]]) self.popiter = np.vstack([self.popiter, self._pop[0, :]]) # Check the stopping criteria over the last ten iterations if i > 9: cond = 0. for j in range(i-9, i+1): cond = cond + (self.foptiter[j-1] - self.foptiter[j]) / \ np.max([self.foptiter[j-1], self.foptiter[j], 1.]) if (cond <= self.epsconv): if self.verbose: printinfo(i, -1, self._fvalpop[0], self._pop[0, :], self.nsimul) break return self.foptiter, self.popiter
# print('完成挑选最好的耗时%s'%(end2-end1)) print('最好的', best_fit) print('最好的个体', geneDecode(best_individual, nodesIndexs)) results.append([best_fit, geneDecode(best_individual, nodesIndexs)]) ## 存储每一代的最优解,及其最终的节点ID,值越小越好 # 遗传算法核心,生成种群,为下一次计算适应度做准备 selection(pop, fit_value) #选择 # end3 = time.clock() # print('完成选择耗时%s'%(end3-end2)) crossover(pop, pc) #重组 # end4 = time.clock() # print('完成crossover耗时%s' % (end4 - end3)) mutation(pop, pm) #变异 # end5 = time.clock() # print('完成mutation耗时%s' % (end5 - end4)) print('第%s代已完成' % i) results = results[1:] # results.sort() #根据best_fit进行排序 print('最后一代最好的结果:', results[-1]) # 打印最好的结果 print('最后一代最好的个体', best_individual) # 打印最后一代最好的二进制个体 print('最后一代个体最好的适应度', best_fit) #打印最后一代最好的二进制个体成绩 #print('打印最后一代第一个个个体的分数fit_value',fit_value[1]) #打印最后一代每个个体的分数 print(pop) print(results)
max_results = [[]] # 世代ごとの最大解を保存 min_results = [[]] # 世代ごとの最小解を保存 fit_value = [] # 個体適応度 fit_mean = [] # 平均適応度 # pop = [[0, 1, 0, 1] for i in range(pop_size)] pop = geneEncoding(pop_size, chrom_length) for i in range(pop_size): obj_value = calobjValue(pop, chrom_length, max_value) # 個体評価 max_indi1, max_indi2, max_indi3, max_fit, min_indi1, min_indi2, min_indi3, min_fit = best(pop, obj_value) max_results.append([max_fit, b2d(max_indi1, max_value, chrom_length), b2d(max_indi2, max_value, chrom_length), b2d(max_indi3, max_value, chrom_length)]) min_results.append([min_fit, b2d(min_indi1, max_value, chrom_length), b2d(min_indi2, max_value, chrom_length), b2d(min_indi3, max_value, chrom_length)]) selection(pop, obj_value) # 新しい個体群をコピー crossover(pop, pc) # 交叉 mutation(pop, pm) # 突然変異 max_results = max_results[1:] max_results.sort() min_results = min_results[1:] min_results.sort(reverse=True) print(max_results[-1]) print(min_results[-1]) print("max : y = %f, x1 = %f, x2 = %f, x3 = %f" % (max_results[-1][0], max_results[-1][1], max_results[-1][2], max_results[-1][3])) print("min : y = %f, x1 = %f, x2 = %f, x3 = %f" % (min_results[-1][0], min_results[-1][1], min_results[-1][2], min_results[-1][3])) X = [] Y = [] for i in range(500): X.append(i)
#initiate population population=[] for i in range(10): entity='' for j in range(17): entity=entity+str(np.random.randint(0,2)) # По идее, лепится строчная хромосома из 0, 1 и 2 population.append(entity) # По идее, в список популяции добавляется слепленная хромосома t=[] for i in range(1000): #selection value=utils.fitness(population,aimFunction) # вызывается фитнес-функция из utils, иниц-ся популяцией и целевой функцией population_new=selection(population,value) # (импортируется отбор) инициализируется новая популяция #crossover offspring =crossover(population_new,0.7) #(импортируется скрещивание) происходит скрещивание #mutation population=mutation(offspring,0.02) #(импортируется мутация) происходит мутация result=[] for j in range(len(population)): # перебирает все элементы популяции result.append(aimFunction(utils.decode(population[j]))) # формируется список результата ф-ии от декод. эл-в поп-ии t.append(max(result)) # в список t записывается макс. результат plt.plot(t) # Вероятно, строит график plt.axhline(max(y), linewidth=1, color='r')
print("Original total fitness is: ", fitness.get_total_fitness(population)) print("Original highest fitness is: ", get_highest_fitness(population)) while (run_count < run_time) and not (solution_found(population)): print("----------", run_count + 1, "----------") # if run_count % 100 == 0: # mutation.mutation_rate -= 0.0001 population = selection.selection(population) fitness.evaluate_fitness(population) population = mutation.crossover(population) fitness.evaluate_fitness(population) population = mutation.mutation(population) fitness.evaluate_fitness(population) print("Total fitness is: ", fitness.get_total_fitness(population)) print("Mean fitness is: ", fitness.get_mean_fitness(population, individual.population_number)) print("Highest fitness is: ", get_highest_fitness(population)) if get_highest_fitness(population) > overall_highest: overall_highest = get_highest_fitness(population) print("Overall Highest is: ", overall_highest) run_count += 1
def genetic_algorithm(k): """ Function that actually runs the Genetic Algorithm. Keyword arguments: k -- number of iterations to run the algorithm for Returns: best_log and avg_log -- Both contain k lists, each containing x integers, where x is the number of generations on that iteration, that represent the best and average fitness score for each generation, respectively pop_size_log -- List containing k integers, each being the size of the population on that iteration """ best_log = [] avg_log = [] pop_size_log = [] for iteration in range(0, k): # Lists that will hold the gen_best_log = [] gen_avg_log = [] # Defining number of generations and probabilities gen_number = 25 crossover_probability = 0.75 mutation_probability = 0.0001 # Starting population population, pop_size = utils.create_population(iteration) pop_size_log.append(pop_size) # Defining the size of the tournament's bracket # the k in k-way tournament tournament_bracket_size = round(pop_size/20) # Iterating through all generations for j in range(0, gen_number): # Calculating the fitness score for every # subject in the population and saving the # best and the average score pop_fitness, best, average = utils.calculate_pop_fitness(population) gen_best_log.append(best) gen_avg_log.append(average) # Preventing the algorithm to go all the way through # during the last iteration, since that population # won't be recorded anyways if j == gen_number - 1: break # Calling the tournament, crossover and mutation function # and overwriting the current population with the new one population = tournament.k_way_tournament(population, pop_size, pop_fitness, tournament_bracket_size) population = crossover.uniform_crossover(population, pop_size, crossover_probability) population = mutation.mutation(population, mutation_probability) best_log.append(gen_best_log) avg_log.append(gen_avg_log) return best_log, avg_log, pop_size_log
Pop, Chrom_length, Timelist, Species_size) # calculate power supply time period(计算供电时间) # TODO: Redefine calcostValue function based on const_load Cost_list = calcostValue( Temp_pop_start, Temp_pop_end, u, const_load) #get the electricity price(得到电费价格表) Best_gene, Best_Results, Best_starttime, Best_endtime = best( Pop, Cost_list, Temp_pop_start, Temp_pop_end ) #get the best gene and the corresponding power start time and end time(找到最优基因及最优解以及最优解对应的供电起始时间) Results.append([ Best_Results, Best_starttime, Best_endtime ]) #save the best result of each generation(存储每一代的最优结果) selection(Pop, Cost_list) #copy the new species(新种群开始进行复制) crossover(Pop, Pc) #(对新种群进行交配) mutation(Pop, Pm) #对种群进行变异 Final_results = [] for i in range(len(Pop)): Final_results.append([]) for j in range(len(Results)): Final_results[i].append(Results[j][0][i]) for i in range(len(Final_results)): #Final_results[i] = Final_results[i][1:] Final_results[i].sort() # TODO const_load = [ Temp_pop_start, Temp_pop_end ] # based on Final_results of other users -> the results of the GA for applicance schedule
def genetic_algorithm_district_heating( DATA, iterations, population_size, hybrid_outside, proposed_config=PROPOSED_CONFIG, first_CO=CROSSOVER_RAND_TWO_POINT, second_CO=CROSSOVER_RAND_TWO_POINT, first_M=MUTATION_INVASIVE_ALLELE_FLIP_OPT, second_M=MUTATION_DISPLACED_INVERSION_OPT): start = time.time() best_solutions = [] best_solutions_opt_counter = 0 # POPULATION INITIALIZATION population = helpers.get_tree_based_population(population_size, DATA['number_of_nodes'], DATA['source']) sorted_population = [] probabilities = [] for it in range(iterations): """ arr1 = [1, 2, 3, 4, 5, 6] arr2 = [8, 9, 10, 11, 12, 13] arr_new = mutation.mutation(7, arr1, len(arr1)) arr_new2 = mutation.mutation(3, arr1, len(arr1)) print('arr1.: ') print(arr_new) print(arr_new2) children = crossover.crossover(5, arr1, arr2, len(arr1)) print(children) """ from_individual = 0 total_done = 0 new_population = [] # EVALUATION OF INDIVUDUALS IN POPULATION sum_evalutation = 0 for individual in population: if (individual['evaluation'] != 0): continue # already available flow, tree and evaluation values (elements form elitism) helpers.generate_tree_flow(individual, DATA['source']) individual['evaluation'] = evaluation.evaluate(individual, DATA) sum_evalutation += individual['evaluation'] # SELECTION sorted_population = sorted(population, key=itemgetter('evaluation')) len_sorted_pop = len(sorted_population) best_solutions.append(sorted_population[0]) probabilities = selection.get_probability_list(sum_evalutation, sorted_population) from_individual = math.floor(proposed_config['elitism'] * population_size / 100) new_population = sorted_population[:from_individual] total_done += from_individual # CROSSOVER total = math.floor(population_size * (proposed_config['crossover']) / 100) rand_single_limit = total / 2 crossover_index = 0 while (crossover_index < total): index_parent_1 = selection.select_roulette(len_sorted_pop, probabilities, 0, probabilities[0]) index_parent_2 = selection.select_roulette(len_sorted_pop, probabilities, 0, probabilities[0]) if (crossover_index < rand_single_limit): children = crossover.crossover( first_CO, sorted_population[index_parent_1]['prufer'], sorted_population[index_parent_2]['prufer'], DATA['number_of_nodes'] - 2) else: children = crossover.crossover( second_CO, sorted_population[index_parent_1]['prufer'], sorted_population[index_parent_2]['prufer'], DATA['number_of_nodes'] - 2) new_individual_1 = helpers.create_new_individual() new_individual_2 = helpers.create_new_individual() new_individual_1['prufer'] = children[0] new_individual_2['prufer'] = children[1] new_population.append(new_individual_1) new_population.append(new_individual_2) crossover_index += 2 total_done += 2 # MUTATION from_individual += math.floor(population_size * (proposed_config['crossover']) / 100) total = math.floor(population_size * (proposed_config['mutation']) / 100) invasive_mutation_limit = total / 2 mutation_index = 0 while mutation_index < total: new_individual = helpers.create_new_individual() index_chromosome = selection.select_roulette( len_sorted_pop, probabilities, 0, probabilities[0]) if (mutation_index < invasive_mutation_limit): new_individual['prufer'] = mutation.mutation( first_M, sorted_population[index_chromosome]['prufer'], DATA['number_of_nodes'] - 2) else: new_individual['prufer'] = mutation.mutation( second_M, sorted_population[index_chromosome]['prufer'], DATA['number_of_nodes'] - 2) new_population.append(new_individual) mutation_index += 1 total_done += 1 # HYBRID 2-opt if (not hybrid_outside): from_individual += math.floor(population_size * (proposed_config['mutation']) / 100) total = math.floor(population_size * (proposed_config['hybrid']) / 100) hybrid_index = 0 while hybrid_index < total: index_chromosome = selection.select_roulette( len_sorted_pop, probabilities, 0, probabilities[0]) session_best_individual = sorted_population[ index_chromosome].copy() session_best_individual = hybrid.opt_2( DATA, session_best_individual) new_population.append(session_best_individual) hybrid_index += 1 total_done += 1 # NEXT GENERATION population = new_population[:] # TERMINATION CRITERIA best_len = len(best_solutions) improvement_percentage = abs( best_solutions[best_len - 2]['evaluation'] - best_solutions[best_len - 1]['evaluation']) / best_solutions[ best_len - 1]['evaluation'] if (improvement_percentage < MINIMUM_IMPROVEMENT_TOLERANCE): best_solutions_opt_counter += 1 else: best_solutions_opt_counter = 0 if best_solutions_opt_counter == MAX_STATIC_IMPROVEMENT: break # equal results in several iterations # last evaluation sum_evalutation = 0 for individual in population: if (individual['evaluation'] != 0): continue # already available flow, tree and evaluation values (elements form elitism) helpers.generate_tree_flow(individual, DATA['source']) individual['evaluation'] = evaluation.evaluate(individual, DATA) sum_evalutation += individual['evaluation'] sorted_population = sorted(population, key=itemgetter('evaluation')) best_solutions.append(sorted_population[0]) if (hybrid_outside): probabilities = selection.get_probability_list(sum_evalutation, sorted_population) total = math.floor(population_size * (hybrid_outside) / 100) hybrid_index = 0 while hybrid_index < total: index_chromosome = selection.select_roulette( len(sorted_population), probabilities, 0, probabilities[0]) session_best_individual = sorted_population[index_chromosome] hybrid.opt_2(DATA, session_best_individual) hybrid_index += 1 sorted_population = sorted(population, key=itemgetter('evaluation')) best_solutions.append(sorted_population[0]) end = time.time() return {'best': best_solutions, 'duration': end - start} #for n in range(POPULATION_SIZE): # pp.pprint(sorted_population[n]['evaluation']) # print(probabilities) # print(len(probabilities)) # count = [0]*POPULATION_SIZE # for i in range(1000) : # rand = random.uniform(0,probabilities[0]) # for j in range (POPULATION_SIZE) : # if (j + 1 < POPULATION_SIZE and rand > probabilities[j + 1]) : # count[j] += 1 # break # if (j + 1 == POPULATION_SIZE): # count[j] += 1 # print(count) # print(sum(count)) #print(population[0]) #print(population) #population[0]['tree'] = helpers.prufer_to_tree(population[0]['prufer']) #print(population[0]['tree']) #arr1 = [1, 2, 3, 4, 5, 6] #arr2 = [8, 9, 10, 11, 12, 13] #let = cm.single_point_CO(arr1, arr2, len(arr1), math.floor((7)/2) ) #print(let) #let2 = cm.second_point_CO(arr1, arr2, len(arr1)) #print(let2) #let3 = cm.allele_flip_M(let2[0], len(arr1)) #print(let2[0]) #arr3 = [1, 2,3, 4, 5] #cm.insertion_M(arr3, len(arr3)) #print(arr3) #prufer_Of_individual = helpers.prufer_to_tree(population[0]['prufer'] #population[0]['tree'] = helpers.prufer_to_tree(population[0]['prufer']) #print(population[0]) #for n in helpers.prufer_to_tree(population[0]['tree']:
arr_new_pop_results = np.empty( (N, 3), float) #['num_vehicles', 'distance', 'fitness'] # RECOMBINATION arr_new_pop_results, new_pop_chromosome_routeList_array, new_pop_chromosome_distanceList_array = full_recombination( alpha, beta, num_cores, K_val, r, N, arr_pop_results, arr_new_pop_results, arr_customers, arr_distance_matrix, total_time, total_capacity, pop_chromosome_routeList_array, pop_chromosome_distanceList_array, new_pop_chromosome_routeList_array, new_pop_chromosome_distanceList_array) # #MUTATION pop_chromosome_routeList_array, pop_chromosome_distanceList_array, arr_pop_results = mutation( alpha, beta, N, num_cores, arr_new_pop_results, new_pop_chromosome_routeList_array, new_pop_chromosome_distanceList_array, arr_distance_matrix, arr_customers, total_time, total_capacity) # ELITISM elite_chromosome_routeList, elite_chromosome_distanceList, elite_result = elitism( pop_chromosome_routeList_array, pop_chromosome_distanceList_array, arr_pop_results) #Replace worst chromosome with elite worst_chromosome_number = np.nanargmax(arr_new_pop_results[:, 2]) new_pop_chromosome_routeList_array[ worst_chromosome_number] = elite_chromosome_routeList new_pop_chromosome_distanceList_array[ worst_chromosome_number] = elite_chromosome_distanceList arr_new_pop_results[worst_chromosome_number, :] = elite_result[:]