def calculate(self): outWorkbook = xlsxwriter.Workbook('D:\Program Files\Sztuczna inteligencja\Lista 1\wyniki\\' + self.fileName) outSheet = outWorkbook.add_worksheet() outSheet.write("A1", "najlepszy") outSheet.write("B1", "średni") outSheet.write("C1", "najgorszy") population = Population(self.pop_size, self.loader) m = Mutation(self.Pm) cross = OrderedCrossover(self.Px, self.loader) ts = Selection(self.Tour) generationNumber = 0 i = 2 while(generationNumber < self.gen): newPopSize = 0 newPopulation = Population(0, self.loader) while(newPopSize < self.pop_size): val1 = ts.selection_result(population) val2 = ts.selection_result(population) ox = cross.crossover(val1, val2) mut = m.mutate(ox) newPopulation.population_array.append(mut) newPopSize+=1 population = newPopulation outSheet.write("A"+str(i), str(selectBest(population.population_array))) outSheet.write("B" + str(i), str(avarage(population.population_array))) outSheet.write("C" + str(i), str(selectWorst(population.population_array))) generationNumber+=1 i+=1 outWorkbook.close()
def solve(self): ''' evolution process of differential evolution algorithm ''' self.t = 0 self.initialize() for i in range(0, self.sizepop): self.evaluate(self.population[i]) self.fitness[i] = self.population[i].fitness best = np.max(self.fitness) bestIndex = np.argmax(self.fitness) self.best = copy.deepcopy(self.population[bestIndex]) self.avefitness = np.mean(self.fitness) self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness print("当前代数 %d: 最优个体适应度值: %f; 当代平均适应度值 %f; " % (self.t, self.trace[self.t, 0], self.trace[self.t, 1])) # print("最有个体:") # print(self.best.chrom) while (self.t < self.MAXGEN - 1): self.t += 1 for i in range(0, self.sizepop): # 遗传操作 mutation = Mutation(self.vardim, self.bound, self.sizepop, self.population, self.params) vi = mutation.Random_3_different_current_mutation(i) crossover = Crossover(self.vardim, self.population, self.params) ui = crossover.Standard_Crossover(i, vi) selection = Selection(self.population) xi_next = selection.selectionOperation(i, ui) self.population[i] = xi_next for i in range(0, self.sizepop): self.evaluate(self.population[i]) self.fitness[i] = self.population[i].fitness best = np.max(self.fitness) bestIndex = np.argmax(self.fitness) if best > self.best.fitness: self.best = copy.deepcopy(self.population[bestIndex]) self.avefitness = np.mean(self.fitness) self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness print("当前代数 %d: 最优个体适应度值: %f; 当代平均适应度值 %f; " % (self.t, self.trace[self.t, 0], self.trace[self.t, 1])) # print("最优个体:") # print(self.best.chrom) print("Optimal function value is: %f; " % self.trace[self.t, 0]) print("Optimal solution is:") print(self.best.chrom) self.printResult()
def parse(LogFile): """ Returns a list of Mutation objects """ mutationRanges = MutationParser.findMutationRanges(LogFile.path) sortedMutationTuples = sorted(mutationRanges.items(), key=operator.itemgetter(1)) Mutations = [] for i, mutation in enumerate(sortedMutationTuples): mutation_data = [] mutationName = mutation[0].split("Scene")[0] mutationStart = mutation[1][0] mutationEnd = mutation[1][1] mutationOrder = i with open(LogFile.path) as f: reader = csv.reader(f, delimiter=';', quotechar='"') for row in islice(reader, mutationStart, mutationEnd): mutation_data.append(row) mutation = Mutation(mutationName, mutation_data, mutationOrder) mutation_data.append([ MutationParser.findLastTimeStampOfMutation(mutation), "LastRow" ]) Mutations.append(mutation) return Mutations
def do_mutation(self): mutation = Mutation(self.crossed_chromosomes) mutation.mutation() self.mutated_chromosomes = mutation.get_chromosomes() # Calculating the cost of each mutated chromosome self.mutated_chromosomes_cost = [] for c in range(0, len(self.mutated_chromosomes)): tokens = textwrap.wrap(self.mutated_chromosomes[c], 2) self.cost = 0 for i in range(0, len(Data.height)): if tokens[0] == Data.height[i][1]: self.cost += Data.height[i][2] for i in range(0, len(Data.width)): if tokens[1] == Data.width[i][1]: self.cost += Data.width[i][2] for i in range(0, len(Data.speed)): if tokens[2] == Data.speed[i][1]: self.cost += Data.speed[i][2] for i in range(0, len(Data.material)): if tokens[3] == Data.material[i][1]: self.cost += Data.material[i][2] for i in range(0, len(Data.weight)): if tokens[4] == Data.weight[i][1]: self.cost += Data.weight[i][2] if tokens[6] == "0": for i in range(0, len(Data.legnum)): if tokens[5] == Data.legnum[i][1]: self.cost += Data.legnum[i][2] elif tokens[6] == "1": for i in range(0, len(Data.wheelnum)): if tokens[5] == Data.wheelnum[i][1]: self.cost += Data.wheelnum[i][2] self.mutated_chromosomes_cost.append(self.cost) self.generation_cost = self.mutated_chromosomes_cost
def get_mutated_children(parents, x_values): first_child = Tree_Operations.get_copy(parents[0][0]) if len(parents) > 1: second_child = Tree_Operations.get_copy(parents[1][0]) Mutation.mutate_individual(first_child, len(x_values[0])) Mutation.mutate_individual(second_child, len(x_values[0])) return [first_child, second_child] elif len(parents) == 1: Mutation.mutate_individual(first_child, len(x_values[0])) return [first_child]
def __init__(self, **kwargs): for key, value in kwargs.items(): if key == 'genetarion': self.generation_size = value elif key == 'population': self.population_size = value elif key == 'limit_population': self.limit_population = value elif key == 'crossover_rate': self.crossover_rate = value elif key == 'mutation_rate': self.mutation_rate = value elif key == 'map_points': self.map_points = value elif key == 'max_coust': self.max_coust = np.array(value) elif key == 'coust_rate': self.coust_rate = value elif key == 'prizes_rate': self.prizes_rate = value elif key == 'start_point': self.start_point = value elif key == 'end_point': self.end_point = value elif key == 'prizes': prizes = np.loadtxt(value) self.prizes = prizes[:, 1] elif key == 'initial_cromossome': self.initial_cromossome = value self.best_route = value self.receive_route = True elif key == 'number_agents': self.number_agents = value self.mapa = np.loadtxt(self.map_points) self.distance_matrix_calculate() self.FunctionObject = FunctionObjective(self.mapa, self.prizes) self.function_objective = self.FunctionObject.FO self.med_custo = self.FunctionObject.med_custo self.function_insert_remove = self.FunctionObject.coust_insert # todos os pontos de um mapa self.all_elements = np.arange(self.mapa.shape[0]) if 'initial_cromossome' not in locals(): self.initial_cromossome = np.arange(self.mapa.shape[0]) self.receive_route = False if self.start_point != self.end_point: self.initial_cromossome = np.delete( self.initial_cromossome, [self.start_point, self.end_point]) else: self.initial_cromossome = np.delete(self.initial_cromossome, [self.start_point]) self.mutation_object = Mutation(self.med_custo, self.max_coust, self.prizes) self.mutation = self.mutation_object.scramble self.crossover_class = Crossover() self.crossover = self.crossover_class.cross_TOP self.Population = Population(self.start_point, self.end_point, self.med_custo, self.max_coust) self.Selection_object = Selection() self.selection = self.Selection_object.tournament
samplingSize = settings.getint('general', 'sampling_size') post_process = settings.get('general', 'post_process') selectionThreshold = settings.getint('selectionparams', 'scale') distanceMeasure = settings.get('selectionparams', 'distance') stringency = settings.getint('selectionparams', 'stringency') pcrCycleNum = settings.getint('amplificationparams', 'number_of_pcr') pcrYield = settings.getfloat('amplificationparams', 'pcr_efficiency') pcrerrorRate = settings.getfloat('amplificationparams', 'pcr_error_rate') # Instantiating the appropriate classes Apt = Aptamers() S = Selection() Amplify = Amplification() Mut = Mutation() if (distanceMeasure == "hamming"): # SELEX simulation based on random aptamer assignment, hamming-based stochastic selection, and # non-ideal stochastic amplfication with pyrimidine-based bias. for r in range(roundNum): if (r == 0): if (aptamerType == 'DNA'): alphabetSet = 'ACGT' aptamerSeqs, initialSeqNum = Apt.optimumAptamerGenerator( aptamerNum, alphabetSet, seqLength) elif (aptamerType == 'RNA'): alphabetSet = 'ACGU' aptamerSeqs, initialSeqNum = Apt.optimumAptamerGenerator( aptamerNum, alphabetSet, seqLength) else:
class GA_TSPKP: def distance_matrix_calculate(self): """ Method that calculate the distance matrix :param cidades: points or towns informed :return: numpy.matrix """ qtd = self.mapa.shape[0] distancias = np.zeros([qtd, qtd]) _temp_max = 0 for i in range(qtd): for j in range(i, qtd): if i != j: b = self.mapa[i, 0] - self.mapa[j, 0] c = self.mapa[i, 1] - self.mapa[j, 1] a = np.sqrt(np.square(b) + np.square(c)) distancias[i, j] = a distancias[j, i] = a if _temp_max < a: _temp_max = a self.distancias = distancias def med_custo(self, flux): dist_total = 0 rota = flux.astype(int) cidade_atual = -1 for cidade in rota: if cidade_atual >= 0: dist_total += self.distancias[cidade_atual, cidade] cidade_atual = cidade return dist_total def function_objective(self, cromossome): coust = self.med_custo(cromossome) prizes = self.prizes.take(cromossome).sum() return (self.coust_rate * coust) - (self.prizes_rate * prizes) '''funcao para remover valores repetidos da ordem da cidade''' @staticmethod def removed_citys_repeat(flux): citys_position = np.unique(flux, return_index=True)[1] citys_position.sort() new_citys = flux.take(citys_position) return new_citys # def correct_individual(self, flux): # flux_wrong = np.delete(flux, [0, -1]) # # flux_wrong = np.copy(self.removed_citys_repeat(flux_wrong)) # # city_s # # # while True: # coust_flux_wrong = self.mede_custo(np.concatenate([self.start_point, flux_wrong, self.end_point])) # # if coust_flux_wrong > self.max_coust: # city_remove = np.random.randint(flux_wrong.size, 1) def plota_rotas(self, cidades, rota, size=8, font_size=20): """ Method to create a chart with the best routes found :param cidades: all points of the route :param rota: the sequence with the best route :param size: size of the chart :param font_size: size of the label of the points """ pos_x = cidades[rota.astype(int), 0] pos_y = cidades[rota.astype(int), 1] all_x = self.mapa[rota.astype(int), 0] all_y = self.mapa[rota.astype(int), 1] cid_nome = range(len(all_x)) plt.figure(num=None, figsize=(size, size), dpi=40, facecolor='w', edgecolor='k') plt.plot(pos_x, pos_y, 'C3', lw=3) plt.scatter(self.mapa[:, 0], self.mapa[:, 1], s=120, marker="s") for i, txt in enumerate(cid_nome): plt.annotate(txt, (all_x[i], all_y[i]), fontsize=font_size) plt.title('Mapa GA') plt.show() # def __init__(self, # genetarion, # population, # limit_population, # crossover_rate, # mutation_rate, # map_points, # prizes, # max_coust, # start_point, # end_point): def __init__(self, **kwargs): for key, value in kwargs.items(): if key == 'genetarion': self.generation_size = value elif key == 'population': self.population_size = value elif key == 'limit_population': self.limit_population = value elif key == 'crossover_rate': self.crossover_rate = value elif key == 'mutation_rate': self.mutation_rate = value elif key == 'map_points': self.map_points = value elif key == 'max_coust': self.max_coust = value elif key == 'coust_rate': self.coust_rate = value elif key == 'prizes_rate': self.prizes_rate = value elif key == 'start_point': self.start_point = value elif key == 'end_point': self.end_point = value elif key == 'prizes': prizes = np.loadtxt(value) self.prizes = prizes[:, 1] elif key == 'initial_cromossome': self.initial_cromossome = value self.best_route = value self.receive_route = True # as variáveis # self.generation_size = genetarion # self.population_size = population # self.limit_population = limit_population # self.crossover_rate = crossover_rate # self.mutation_rate = mutation_rate # self.map_points = map_points # self.max_coust = max_coust # self.start_point = np.array([start_point]) # self.end_point = np.array([end_point]) # self.prizes = np.loadtxt(prizes) self.mapa = np.loadtxt(self.map_points) self.distance_matrix_calculate() if 'initial_cromossome' not in locals(): self.initial_cromossome = np.arange(self.mapa.shape[0]) self.receive_route = False if self.start_point != self.end_point: self.initial_cromossome = np.delete( self.initial_cromossome, [self.start_point, self.end_point]) else: self.initial_cromossome = np.delete(self.initial_cromossome, [self.start_point]) self.mutation_object = Mutation(self.max_coust, self.prizes) self.mutation = self.mutation_object.scramble self.crossover_class = Crossover() self.crossover = self.crossover_class.PMX self.Population = Population(self.start_point, self.end_point, self.med_custo, self.max_coust) self.Selection_object = Selection() self.selection = self.Selection_object.tournament def run(self): if not self.receive_route: population = self.Population.initialize(self.initial_cromossome, self.population_size) best_elements = population[0:4] best_elements_coust = np.array([ self.function_objective(element) for element in best_elements ]) best_count = 0 best_always = np.copy(best_elements[0]) best_coust = best_elements_coust[0] best_element_generation = list() for g in range(self.generation_size): print(g, best_coust, best_count) cousts_population = [ self.function_objective(value) for value in population ] cousts_population = np.array(cousts_population) selected_parents_index = self.selection( self.population_size, cousts_population, 5) parents_select = [ population[chromossome] for chromossome in selected_parents_index ] new_population = list() for i in range(selected_parents_index.size): select_2_parents = np.random.randint( selected_parents_index.size, size=2) offspring_1, offspring_2 = self.crossover( parents_select[select_2_parents[0]], parents_select[select_2_parents[1]]) new_population.append(offspring_1) new_population.append(offspring_2) rand = np.random.uniform(0, 1, len(new_population)) for i in range(rand.size): if rand[i] >= self.mutation_rate: if new_population[i].size > 3: list_mut = list() list_mut.append( self.mutation_object.swap(new_population[i])) list_mut.append( self.mutation_object.insertion( new_population[i])) list_mut.append( self.mutation_object.reverse( new_population[i])) list_mut.append( self.mutation_object.scramble( new_population[i])) list_mut.append( self.mutation_object.swap(new_population[i])) list_mut.append( self.mutation_object.WGWRGM( new_population[i], self.med_custo)) list_mut.append( self.mutation_object.WGWWGM( new_population[i], self.med_custo)) list_mut.append( self.mutation_object.WGWNNM( new_population[i], self.med_custo)) cousts_mut = np.zeros(8) cousts_mut[0] = self.function_objective( list_mut[0]) cousts_mut[1] = self.function_objective( list_mut[1]) cousts_mut[2] = self.function_objective( list_mut[2]) cousts_mut[3] = self.function_objective( list_mut[3]) cousts_mut[4] = self.function_objective( list_mut[4]) cousts_mut[5] = self.function_objective( list_mut[5]) cousts_mut[6] = self.function_objective( list_mut[6]) cousts_mut[7] = self.function_objective( list_mut[7]) min_mut = np.argmin(cousts_mut) new_population[i] = list_mut[min_mut] new_population = new_population + population fitness_values = np.zeros(len(new_population)) for i in range(fitness_values.size): fitness_values[i] = self.function_objective( new_population[i]) population_select = np.zeros(self.population_size) population = list() for i in range(self.population_size): min_index = np.argmin(fitness_values) population_select[i] = min_index exist_menor = [ best for best in range(4) if fitness_values[min_index] < best_elements_coust[best] ] crhomossome = new_population[min_index] if len(exist_menor) > 0: flag_possui = [ np.array_equal(element, crhomossome) for element in best_elements ] if True not in flag_possui: best_tmp = best_elements best_tmp.append(crhomossome) new_cousts = np.array([ self.function_objective(tmp) for tmp in best_tmp ]) indexes_tmp = np.argsort(new_cousts) best_elements_coust = new_cousts[indexes_tmp[0:4]] best_elements = [ best_tmp[best_index] for best_index in indexes_tmp ] population.append(new_population[min_index]) del new_population[min_index] fitness_values = np.delete(fitness_values, [min_index]) if best_elements_coust[0] < best_coust: best_coust = best_elements_coust[0] best_always = np.copy(best_elements[0]) best_count = 0 elif best_elements_coust[0] == best_coust: best_count += 1 best_element_generation.append(best_elements_coust[0]) if best_count >= self.limit_population: break self.best_route = best_elements[0] if self.max_coust > 0: new_population = list() for i in range(round(self.population_size / 4)): cousts_mut = np.zeros(2) list_mut = list() s = np.copy(self.best_route) list_mut.append( self.mutation_object.remove_random(s, self.med_custo)) list_mut.append( self.mutation_object.remove_pior_premio(s, self.med_custo)) cousts_mut[0] = self.function_objective(list_mut[0]) cousts_mut[1] = self.function_objective(list_mut[1]) index_min = np.argmin(cousts_mut) print(cousts_mut[index_min]) print(self.prizes.take(list_mut[index_min]).sum()) new_population.append(list_mut[index_min]) print(best_element_generation) return best_elements_coust, best_elements
def __init__(self, **kwargs): for key, value in kwargs.items(): if key == 'genetarion': self.generation_size = value elif key == 'population': self.population_size = value elif key == 'limit_population': self.limit_population = value elif key == 'crossover_rate': self.crossover_rate = value elif key == 'mutation_rate': self.mutation_rate = value elif key == 'map_points': self.map_points = value elif key == 'max_coust': self.max_coust = value elif key == 'coust_rate': self.coust_rate = value elif key == 'prizes_rate': self.prizes_rate = value elif key == 'start_point': self.start_point = value elif key == 'end_point': self.end_point = value elif key == 'prizes': prizes = np.loadtxt(value) self.prizes = prizes[:, 1] elif key == 'initial_cromossome': self.initial_cromossome = value self.best_route = value self.receive_route = True # as variáveis # self.generation_size = genetarion # self.population_size = population # self.limit_population = limit_population # self.crossover_rate = crossover_rate # self.mutation_rate = mutation_rate # self.map_points = map_points # self.max_coust = max_coust # self.start_point = np.array([start_point]) # self.end_point = np.array([end_point]) # self.prizes = np.loadtxt(prizes) self.mapa = np.loadtxt(self.map_points) self.distance_matrix_calculate() if 'initial_cromossome' not in locals(): self.initial_cromossome = np.arange(self.mapa.shape[0]) self.receive_route = False if self.start_point != self.end_point: self.initial_cromossome = np.delete( self.initial_cromossome, [self.start_point, self.end_point]) else: self.initial_cromossome = np.delete(self.initial_cromossome, [self.start_point]) self.mutation_object = Mutation(self.max_coust, self.prizes) self.mutation = self.mutation_object.scramble self.crossover_class = Crossover() self.crossover = self.crossover_class.PMX self.Population = Population(self.start_point, self.end_point, self.med_custo, self.max_coust) self.Selection_object = Selection() self.selection = self.Selection_object.tournament
def __init__(self, map_points, iterations, size_population, beta, alfa, cost_rate, prizes_rate, prizes, max_cost, start_point, end_point, depositos=[]): self.map_points = np.loadtxt(map_points) self.iterations = iterations self.size_population = size_population self.beta = beta self.alfa = alfa self.cost_rate = np.array(cost_rate) self.prizes_rate = prizes_rate self.prizes = np.loadtxt(prizes)[:, 1] self.max_cost = np.array(max_cost) self.start_point = start_point self.end_point = end_point self.depositos = depositos self.particles = [] self.number_agents = len(max_cost) self.distance = calculate_distances(self.map_points) self.functionObject = FunctionObjective(self.map_points, self.prizes) self.FO = self.functionObject.FO self.mensureCost = self.functionObject.med_custo self.methodInsertRemoveChromosome = self.functionObject.coust_insert self.allElementsMap = np.arange(self.map_points.shape[0]) self.allElementsMap = self.allElementsMap[self.number_agents:] # removendo depositos deposits = [x for x in self.start_point] deposits += [x for x in self.end_point if x not in deposits] deposits = np.unique(np.array(deposits)) self.initialChromossome = np.arange(self.map_points.shape[0]) self.initialChromossome = np.delete(self.initialChromossome, self.depositos) self.allElementsMap = np.copy(self.initialChromossome) self.mutationObject = Mutation(self.mensureCost, self.max_cost, self.prizes) self.mutation = self.mutationObject.scramble self.crossoverObject = Crossover() self.crossover = self.crossoverObject.cross_TOPMD self.PopulationObject = Population(self.start_point, self.end_point, self.mensureCost, self.max_cost, self.distance) self.SelectionObject = Selection() self.selection = self.SelectionObject.tournament solutions = self.PopulationObject.initializeTopMdGreed2( self.initialChromossome, self.size_population, self.number_agents, 1) for s in solutions: particle = Particle(route=s, mensure_function=self.mensureCost, fitness_function=self.FO) self.particles.append(particle)
from Crossover import Crossover from Mutation import Mutation crossover = Crossover(alpha=0.5) mutation = Mutation(mutation_rate=0.2) # SGA parent1 = [0, 0, 0, 0, 0] parent2 = [1, 1, 1, 1, 1] # print('ONE_POINT', crossover.one_point(parent1, parent2)) # print('N_POINT', crossover.n_point(parent1, parent2)) # print('UNIFORM', crossover.uniform(parent1, parent2)) # print('simple mutation', mutation.simple(parent1)) # RGA parent1 = [0, 0, 0, 0, 0] parent2 = [0.1, 0.2, 0.3, 0.4, 0.5] # print('SINGLE_ARITHMETIC', crossover.single_arithmetic(parent1, parent2)) # print('SIMPLE_ARITHMETIC', crossover.simple_arithmetic(parent1, parent2)) # print('WHOLE_ARITHMETIC', crossover.whole_arithmetic(parent1, parent2)) # print('uniform mutation', mutation.uniform(parent2)) # print('noise mutation', mutation.random_noise(parent2, 0.1)) # PGA parent1 = [1, 2, 3, 4, 5] parent2 = [5, 4, 1, 3, 2] # print('insert mutation', mutation.insert(parent1)) # print('swap mutation', mutation.swap(parent1)) print('PMX', crossover.pmx(parent1, parent2)) # print('ORDER1', crossover.order1(parent1, parent2))
def randomPCR_with_ErrorsAndBias_FASTv2(self, slctdSeqs, seqLength, pcrCycleNum, pcrYld, errorRate, aptamerSeqs, alphabetSet, distance): # initialize Mutation object from class mut = Mutation(seqLength=seqLength, errorRate=errorRate, pcrCycleNum=pcrCycleNum, pcrYld=pcrYld) totalseqs = 0 uniqSeqs = 0 #compute total seq num, unique seq num, and transfer info to x for i, seqIdx in enumerate(slctdSeqs): uniqSeqs += 1 totalseqs += int(slctdSeqs[seqIdx][0]) #initialize matrix to hold info for amplified pool x = np.zeros((uniqSeqs, pcrCycleNum+4)) for i, seqIdx in enumerate(slctdSeqs): x[i][0] = seqIdx x[i][1] = slctdSeqs[seqIdx][0] x[i][2] = slctdSeqs[seqIdx][1] x[i][3] = slctdSeqs[seqIdx][2] print("number of unique seqs in selected pool prior to amplification: "+str(uniqSeqs)) print("number of seqs in selected pool prior to amplification: "+str(totalseqs)) # calculate probabilities of different possible mutation numbers mutNumProbs = mut.get_mutation_probabilities_original() # compute a discrete distribution of mutation numbers mutDist = mut.get_mutation_distribution_original() print("Discrete Mutation Distribution has been computed") # PCR Amplification totalseqs = 0 # initialize dictionary to keep info on seqs to be mutated mutatedPool = {} #initialize matrix to hold info for mutation pool y = np.zeros((uniqSeqs, seqLength+1)) # keep track of sequence count after each pcr cycle (except last one) seqPop = np.zeros(pcrCycleNum) # compute cycle number probabilities for this seq cycleNumProbs = np.zeros(pcrCycleNum) print("Amplification has started...") # for each sequence in the selected pool for i, seqIdx in enumerate(list(slctdSeqs)): # random PCR with bias using brute force for n in xrange(pcrCycleNum): # sequence count after n cycles seqPop[n] = x[i][1] # amplify count using initial count, polymerase yield, and bias score x[i][1] += int(binom(x[i][1], pcrYld+x[i][3])) # compute cycle number probabilities for s, seqNum in enumerate(seqPop): cycleNumProbs[s] = seqNum/np.sum(seqPop) # transfer info to x for j, cycleNumProb in enumerate(cycleNumProbs): x[i][j+4] = cycleNumProb # update total num of seqs totalseqs += x[i][1] # transfer info from x to selection pool slctdSeqs[int(x[i][0])] = x[i][1:] #tranfer seq index to matrix y y[i][0] = x[i][0] #if accumulated seq count is greater than 10,000 if np.sum(seqPop) > 10000: # for each possible number of mutations in any seq copy (1-seqLength) for mutNum in xrange(seqLength): #approximate the proportion of copies that will be mutated using #corresponding probability p(M=mutNum) y[i][mutNum+1] = mutNumProbs[mutNum+1]*np.sum(seqPop) # if seq count is less than 10,000 else: # draw random mutNum from the mutation distribution for each seq copy muts = poisson(errorRate*seqLength, int(np.sum(seqPop))) #SLOW STEP # remove all drawn numbers equal to zero muts = muts[muts != 0] # for each non-zero mutation number for mutNum in muts: #increment copy number to be mutated y[i][mutNum+1] += 1 del(x) gc.collect() print("Amplification carried out") print("Sequence selection for mutation has started...") #remove all mutation numbers with zero copies to be mutated y = y[y[:, 1] != 0] #for each seq to be mutated for mutInfo in y: #add to mutation pool with it's corresponding mutation info mutatedPool[int(mutInfo[0])] = mutInfo[1:][mutInfo[1:] != 0] del(y) gc.collect() print("Mutation selection has been carried out") print("Mutant generation has started...") if(distance == "hamming"): # generate mutants and add to the amplfied sequence pool amplfdSeqs = mut.generate_mutants_1D(mutatedPool=mutatedPool, amplfdSeqs=slctdSeqs, aptamerSeqs=aptamerSeqs, alphabetSet=alphabetSet) del(slctdSeqs) del(mutatedPool) gc.collect() elif(distance == "basepair"): amplfdSeqs = mut.generate_mutants_2D(mutatedPool=mutatedPool, amplfdSeqs=slctdSeqs, aptamerSeqs=aptamerSeqs, alphabetSet=alphabetSet) del(slctdSeqs) del(mutatedPool) gc.collect() elif(distance == "loop"): amplfdSeqs = mut.generate_mutants_loop(mutatedPool=mutatedPool, amplfdSeqs=slctdSeqs, aptamerSeqs=aptamerSeqs, alphabetSet=alphabetSet) del(slctdSeqs) del(mutatedPool) gc.collect() else: print("argument given for distance is invalid") return print("Mutation has been carried out") return amplfdSeqs
pop = np.random.randint(min_x_value, (max_x_value - 1), size=[popsize, 1]) #种群初始化 #变量初始化 GN = 0 #记录进化过程遗传代数 less_df_cnt = 0 #几率两代最优个体适应度差值连续小于上限的次数 best_code = Evaluate(pop) #记录进化最优个体,初始化 history_best_code = np.zeros((max_GN_limitation, 1), dtype=np.int) #记录每一代最优个体 history_cut = 0 #记录最优个体数组‘history_best_code’的索引 #开始遗传迭代 while GN < max_GN_limitation: #执行遗传、交叉、变异操作,产生新种群 pop = Select(pop, ps) pop = Crossover(pop, pc) pop = Mutation(pop, pm) current_beat_code = Evaluate(pop) #记录当前种群最优个体 #判断是否结束遗传过程 if np.abs(fit_function(best_code) - fit_function(current_beat_code)) < df_max: less_df_cnt += 1 else: less_df_cnt = 0 if less_df_cnt >= less_df_n: break #模拟退火 #beat_code_move 最优个体轻微移动后的结果 for i in range(2):
class GaTopMd: generationSize: int populationSize: int limit_population: int crossover_rate: float mutation_rate: float cost_rate: int prizes_rate: int map_points: np.array prizes: np.array max_cost: np.array start_point: list end_point: list depositos: list distance = np.array def __init__(self, generation, population, limit_population, crossover_rate, mutation_rate, cost_rate, prizes_rate, map_points, prizes, max_cost, start_point, end_point, depositos=[]): self.generationSize = generation self.populationSize = population self.limit_population = limit_population self.crossover_rate = crossover_rate self.mutation_rate = mutation_rate self.cost_rate = np.array(cost_rate) self.prizes_rate = prizes_rate self.map_points = np.loadtxt(map_points) self.prizes = np.loadtxt(prizes)[:, 1] self.max_cost = np.array(max_cost) self.start_point = start_point self.end_point = end_point self.depositos = depositos self.number_agents = len(max_cost) self.distance = self.calculate_distances() self.functionObject = FunctionObjective(self.map_points, self.prizes) self.FO = self.functionObject.FO self.mensureCost = self.functionObject.med_custo self.methodInsertRemoveChromosome = self.functionObject.coust_insert self.allElementsMap = np.arange(self.map_points.shape[0]) self.allElementsMap = self.allElementsMap[self.number_agents:] # removendo depositos deposits = [x for x in self.start_point] deposits += [x for x in self.end_point if x not in deposits] deposits = np.unique(np.array(deposits)) self.initialChromossome = np.arange(self.map_points.shape[0]) self.initialChromossome = np.delete(self.initialChromossome, self.depositos) self.allElementsMap = np.copy(self.initialChromossome) self.mutationObject = Mutation(self.mensureCost, self.max_cost, self.prizes, self.distance) self.mutation = self.mutationObject.scramble self.crossoverObject = Crossover() self.crossover = self.crossoverObject.cross_TOPMD self.PopulationObject = Population(self.start_point, self.end_point, self.mensureCost, self.max_cost, self.distance) self.SelectionObject = Selection() self.selection = self.SelectionObject.tournament def plota_rotas_TOP(self, cidades, rota, size=12, font_size=20, file_plot=False, name_file_plot='plt'): """ Method to create a chart with the best routes found :param cidades: all points of the route300 :param rota: the sequence with the best route :param size: size of the chart :param font_size: size of the label of the points """ pos_x = [cidades[val.astype(int), 0] for val in rota] pos_y = [cidades[val.astype(int), 1] for val in rota] elements = self.map_points[:, 0] x = self.map_points[:, 0] y = self.map_points[:, 1] # cid_nome = range(elements.size) cid_nome = self.allElementsMap plt.figure(num=None, figsize=(size, size), dpi=80, facecolor='w', edgecolor='k') for i in range(len(rota)): plt.plot(pos_x[i], pos_y[i], 'C' + str(i), lw=5, label='agente ' + str(i + 1), zorder=1) plt.rc('font', size=font_size) plt.legend(loc='lower left', bbox_to_anchor=(0, 1.02, 1, 0.2), ncol=4, mode='expand') plt.scatter(self.map_points[cid_nome, 0], self.map_points[cid_nome, 1], s=120, marker="s", zorder=2) plt.scatter(self.map_points[self.depositos, 0], self.map_points[self.depositos, 1], s=150, marker='^', zorder=3, c='black') # for i, txt in enumerate(cid_nome): for i in cid_nome: plt.annotate(str(self.prizes[i]), (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # for i, txt in enumerate(cid_nome): # plt.annotate(txt, (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) for i in self.start_point: plt.annotate('dep.', (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) for i in self.depositos: plt.annotate('dep.', (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # plt.title('Mapa GA') # plt.margins(0.05) if file_plot: plt.savefig(name_file_plot + '.png') else: plt.show() def calculate_distances(self): size = self.map_points.shape[0] distances = np.zeros([size, size]) temp_max = 0 for i in range(size): for j in range(size): if i != j: b = self.map_points[i, 0] - self.map_points[j, 0] c = self.map_points[i, 1] - self.map_points[j, 1] a = np.sqrt(np.square(b) + np.square(c)) distances[i, j] = a distances[j, i] = a if temp_max < a: temp_max = a return distances @staticmethod def reply_method_top(method, chromossome): size = len(chromossome) result = np.zeros(size) for n in np.arange(size): result[n] = method(chromossome[n]) return result def reply_method_mutation_top(self, method, chromossome, sizeMut=0): size = len(chromossome) size_mut_genes = sizeMut if sizeMut == 0: size_mut_genes = size result = [0] * size rand = np.arange(size) np.random.shuffle(rand) rand = rand[:size_mut_genes] for n in np.arange(size): if n in rand: if chromossome[n].size > 3: result[n] = method(chromossome[n]) else: result[n] = chromossome[n] else: result[n] = chromossome[n] return result def run(self): population = self.PopulationObject.initializeTopMdGreed2( self.initialChromossome, int(self.populationSize * .5), self.number_agents, 1) population += self.PopulationObject.initializeTopMd2( self.initialChromossome, int(self.populationSize * .5), self.number_agents) populationCosts = np.array([ self.reply_method_top(self.FO, element).sum() for element in population ]) indicesMenorCusto = np.argpartition(populationCosts, 4) size_best_elements = 4 bestElements = list() for i in range(4): bestElements.append(population[indicesMenorCusto[i]]) bestElementsCosts = np.array([ self.reply_method_top(self.FO, element).sum() for element in bestElements ]) countGenaration = 0 bestCost = bestElementsCosts[0] bestElementAlways = bestElements[0] bestElementGenaration = list() bestElementGenarationCost = list() count_dis = 0 real_cost = self.max_cost # self.max_cost = [i*.7 for i in self.max_cost] flag = False mutation_rate_begin = self.mutation_rate crossover_rate_begin = self.crossover_rate pop_size = self.populationSize for g in range(self.generationSize): print(g, bestCost, countGenaration, len(population)) for chromossome in population: elements_chromossome = np.array([]) for i in chromossome: elements_chromossome = np.concatenate( [elements_chromossome, i[1:-1]]) if elements_chromossome.size > np.unique( elements_chromossome).size: print('aqui') # completando populacao depois que apenas os melhores individuos restaram population += self.PopulationObject.initializeTopMdGreed2( self.initialChromossome, self.populationSize - len(population), self.number_agents, 1) population += self.PopulationObject.initializeTopMd2( self.initialChromossome, self.populationSize - len(population), self.number_agents) cost_population = [ self.reply_method_top(self.FO, individual) for individual in population ] cost_population = [costs.sum() for costs in cost_population] cost_population = np.array( [costs.sum() for costs in cost_population]) for chromossome in population: elements_chromossome = np.array([]) for i in chromossome: elements_chromossome = np.concatenate( [elements_chromossome, i[1:-1]]) if elements_chromossome.size > np.unique( elements_chromossome).size: print('aqui') select_parents_index = self.selection( int(self.populationSize * self.crossover_rate), cost_population, 5) # Realizando o cruzamento entre os genees new_population = list() for cross in range(select_parents_index.size): ind = np.random.randint(select_parents_index.size, size=2) a, b = self.crossoverObject.cross_slice( population[ind[0]], population[ind[1]], self.mensureCost, self.start_point, self.end_point) new_population.append(a) new_population.append(b) population_mutation = list() for i in range(len(new_population)): population_mutation.append( self.mutationObject.insert_points_TOP_4( self.mensureCost, self.FO, self.allElementsMap, new_population[i], self.mutation_rate)) new_population += population_mutation fitness_values = np.zeros(len(new_population)) cousts_values = np.zeros(len(new_population)) for i in range(fitness_values.size): fitness_values[i] = self.reply_method_top( self.FO, new_population[i]).sum() cousts_values[i] = self.reply_method_top( self.mensureCost, new_population[i]).sum() for chromossome in new_population: elements_chromossome = np.array([]) for i in chromossome: elements_chromossome = np.concatenate( [elements_chromossome, i[1:-1]]) if elements_chromossome.size > np.unique( elements_chromossome).size: print('aqui') population_mutation = list() for i in range(len(new_population)): population_mutation.append( self.mutationObject.insert_points_TOP_4( self.mensureCost, self.FO, self.allElementsMap, new_population[i], self.mutation_rate)) new_population = population_mutation for chromossome in new_population: elements_chromossome = np.array([]) for i in chromossome: elements_chromossome = np.concatenate( [elements_chromossome, i[1:-1]]) if elements_chromossome.size > np.unique( elements_chromossome).size: print('aqui') # for i in range(len(new_population)): # new_population[i] = self.mutationObject.remove_points_TOP(self.mensureCost, # self.methodInsertRemoveChromosome, # self.allElementsMap, # new_population[i]) # new_population = new_population + population fitness_values = np.zeros(len(new_population)) cousts_values = np.zeros(len(new_population)) for i in range(fitness_values.size): fitness_values[i] = self.reply_method_top( self.FO, new_population[i]).sum() cousts_values[i] = self.reply_method_top( self.mensureCost, new_population[i]).sum() population_select = list() population = list() for i in range(int(self.populationSize)): if len(new_population) == 0: break min_index = np.argmin(fitness_values) # if cousts_values[i].sum() <= self.max_cost.sum(): passa = list() custo = self.reply_method_top(self.mensureCost, new_population[min_index]) s = False for j in range(self.number_agents): if custo[j] > self.max_cost[j]: s = True passa.append(False) break if not s: exist_menor = [ best for best in range(4) if fitness_values[min_index] < bestElementsCosts[best] ] crhomossome = new_population[min_index] if len(exist_menor) > 0: best_elements_temporary = bestElements best_elements_temporary.append(crhomossome) new_cousts = np.array([ self.reply_method_top(self.FO, tmp).sum() for tmp in best_elements_temporary ]) indexes_best_individual = np.argsort(new_cousts) indexes_best_individual = indexes_best_individual[ 0:size_best_elements] bestElementsCosts = new_cousts[indexes_best_individual] bestElements = [ best_elements_temporary[best_index] for best_index in indexes_best_individual ] else: population.append(new_population[min_index]) population_select.append(fitness_values[min_index]) del new_population[min_index] fitness_values = np.delete(fitness_values, [min_index]) if bestElementsCosts[0] < bestCost: bestCost = bestElementsCosts[0] bestElementAlways = np.copy(bestElements[0]) countGenaration = 0 self.populationSize = pop_size bestElementGenaration.append(bestCost) else: countGenaration += 1 # population.append(bestElementAlways) bestElementGenarationCost.append(bestElementsCosts[0]) if countGenaration == 5: self.max_cost = [ self.max_cost[i] + (real_cost[i] * .1) for i in range(len(real_cost)) ] c = False for i in range(self.number_agents): if self.max_cost[i] > real_cost[i]: c = True break if c: self.max_cost = real_cost if self.mutation_rate < .3: self.mutation_rate = self.mutation_rate + .05 # population = population+bestElements if countGenaration >= self.limit_population: break self.bestRoute = bestElements[0] return bestElementsCosts, bestElements, bestElementGenaration, bestElementAlways
class GA_TSPKP: def distance_matrix_calculate(self): """ Method that calculate the distance matrix :param cidades: points or towns informed :return: numpy.matrix """ qtd = self.mapa.shape[0] distancias = np.zeros([qtd, qtd]) _temp_max = 0 for i in range(qtd): for j in range(i, qtd): if i != j: b = self.mapa[i, 0] - self.mapa[j, 0] c = self.mapa[i, 1] - self.mapa[j, 1] a = np.sqrt(np.square(b) + np.square(c)) distancias[i, j] = a distancias[j, i] = a if _temp_max < a: _temp_max = a self.distancias = distancias '''funcao para remover valores repetidos da ordem da cidade''' @staticmethod def removed_citys_repeat(flux): citys_position = np.unique(flux, return_index=True)[1] citys_position.sort() new_citys = flux.take(citys_position) return new_citys def plota_rotas(self, cidades, rota, size=8, font_size=20): """ Method to create a chart with the best routes found :param cidades: all points of the route :param rota: the sequence with the best route :param size: size of the chart :param font_size: size of the label of the points """ pos_x = cidades[rota.astype(int), 0] pos_y = cidades[rota.astype(int), 1] # all_x = self.mapa[rota.astype(int), 0] # all_y = self.mapa[rota.astype(int), 1] elements = self.mapa[:, 0] x = self.mapa[:, 0] y = self.mapa[:, 1] cid_nome = range(elements.size) plt.figure(num=None, figsize=(size, size), dpi=40, facecolor='w', edgecolor='k') plt.plot(pos_x, pos_y, 'C3', lw=3) plt.scatter(self.mapa[:, 0], self.mapa[:, 1], s=120, marker="s") for i, txt in enumerate(cid_nome): plt.annotate(txt, (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) plt.title('Mapa GA') plt.show() def plota_rotas_TOP(self, cidades, rota, size=8, font_size=20): """ Method to create a chart with the best routes found :param cidades: all points of the route :param rota: the sequence with the best route :param size: size of the chart :param font_size: size of the label of the points """ pos_x = [cidades[val.astype(int), 0] for val in rota] pos_y = [cidades[val.astype(int), 1] for val in rota] elements = self.mapa[:, 0] x = self.mapa[:, 0] y = self.mapa[:, 1] cid_nome = range(elements.size) plt.figure(num=None, figsize=(size, size), dpi=40, facecolor='w', edgecolor='k') for i in range(len(rota)): plt.plot(pos_x[i], pos_y[i], 'C' + str(i), lw=5, label='agente ' + str(i + 1)) plt.rc('font', size=font_size) plt.legend(loc='lower left') plt.scatter(self.mapa[:, 0], self.mapa[:, 1], s=120, marker="s") for i, txt in enumerate(cid_nome): plt.annotate(str(self.prizes[i]), (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # plt.title('Mapa GA') plt.show() '''metodo auxilixar para replicar o metodo sempre que possivel''' def reply_method_TOP(self, method, chromossome): size = len(chromossome) result = np.zeros(size) for n in np.arange(size): result[n] = method(chromossome[n]) return result def reply_method_mutation_TOP(self, method, chromossome): size = len(chromossome) result = [0] * size for n in np.arange(size): if chromossome[n].size > 3: result[n] = method(chromossome[n]) else: result[n] = chromossome[n] return chromossome def reply_crossover_inner_agents(self, method, chromossome): size = len(chromossome) number_cross = int(size / 2) ids = np.random.choice(size, size, replace=False) new_chromossome = list() for i in range(0, size - 1, 2): x, y = method(chromossome[ids[i]], chromossome[ids[i + 1]]) new_chromossome.append(x) new_chromossome.append(y) new_chromossome.append(chromossome[ids[-1]]) return new_chromossome def __init__(self, **kwargs): for key, value in kwargs.items(): if key == 'genetarion': self.generation_size = value elif key == 'population': self.population_size = value elif key == 'limit_population': self.limit_population = value elif key == 'crossover_rate': self.crossover_rate = value elif key == 'mutation_rate': self.mutation_rate = value elif key == 'map_points': self.map_points = value elif key == 'max_coust': self.max_coust = np.array(value) elif key == 'coust_rate': self.coust_rate = value elif key == 'prizes_rate': self.prizes_rate = value elif key == 'start_point': self.start_point = value elif key == 'end_point': self.end_point = value elif key == 'prizes': prizes = np.loadtxt(value) self.prizes = prizes[:, 1] elif key == 'initial_cromossome': self.initial_cromossome = value self.best_route = value self.receive_route = True elif key == 'number_agents': self.number_agents = value self.mapa = np.loadtxt(self.map_points) self.distance_matrix_calculate() self.FunctionObject = FunctionObjective(self.mapa, self.prizes) self.function_objective = self.FunctionObject.FO self.med_custo = self.FunctionObject.med_custo self.function_insert_remove = self.FunctionObject.coust_insert # todos os pontos de um mapa self.all_elements = np.arange(self.mapa.shape[0]) if 'initial_cromossome' not in locals(): self.initial_cromossome = np.arange(self.mapa.shape[0]) self.receive_route = False if self.start_point != self.end_point: self.initial_cromossome = np.delete( self.initial_cromossome, [self.start_point, self.end_point]) else: self.initial_cromossome = np.delete(self.initial_cromossome, [self.start_point]) self.mutation_object = Mutation(self.med_custo, self.max_coust, self.prizes) self.mutation = self.mutation_object.scramble self.crossover_class = Crossover() self.crossover = self.crossover_class.cross_TOP self.Population = Population(self.start_point, self.end_point, self.med_custo, self.max_coust) self.Selection_object = Selection() self.selection = self.Selection_object.tournament def run(self): if not self.receive_route: # gerando uma população inicial population = self.Population.initialize_TOP( self.initial_cromossome, self.population_size, self.number_agents) # selecionando os 4melhores como os indivíduos iniciais best_elements = population[0:4] best_elements_coust = np.array([ self.reply_method_TOP(self.function_objective, element).sum() for element in best_elements ]) # best_elements_coust = self.reply_method_TOP(self.function_objective, best_elements) best_count = 0 best_always = np.copy(best_elements[0]) best_coust = best_elements_coust[0] best_element_generation = list() for g in range(self.generation_size): print(g, best_coust, best_count) population += self.Population.initialize_TOP( self.initial_cromossome, self.population_size - len(population), self.number_agents) # calculo do custo cousts_population = [ self.reply_method_TOP(self.function_objective, value) for value in population ] '''ealizando a soma do custo de todos os crhomossomos''' cousts_population = [ value.sum() for value in cousts_population ] cousts_population = np.array(cousts_population) # selecionano os pais para cruzamento selected_parents_index = self.selection( self.population_size, cousts_population, 5) parents_select = [ population[chromossome] for chromossome in selected_parents_index ] # lista que terá a nova população new_population = list() for i in range(selected_parents_index.size): # select_2_parents = np.random.randint( selected_parents_index.size, size=2) offspring_1, offspring_2 = self.crossover( parents_select[select_2_parents[0]], parents_select[select_2_parents[1]], function_objective=self.function_objective) new_population.append(offspring_1) new_population.append(offspring_2) #for i in range(len(new_population)): # new_population[i] = self.reply_crossover_inner_agents(self.crossover_class.PMX, new_population[i]) # gerando lista de probabilidades para os novos indivíduos sofrerem mutações rand = np.random.uniform(0, 1, len(new_population)) for i in range(len(new_population)): new_population[ i] = self.mutation_object.insert_remove_points_TOP( self.med_custo, self.function_insert_remove, self.all_elements, new_population[i]) # # custo = [self.reply_method_TOP(self.med_custo, val) for val in new_population] # for i in custo: # for j in range(self.number_agents): # if i[j] > self.max_coust[j]: # print(i[j] ,self.max_coust[j]) fitness_values = np.zeros(len(new_population)) cousts_values = np.zeros(len(new_population)) for i in range(fitness_values.size): fitness_values[i] = self.reply_method_TOP( self.function_objective, new_population[i]).sum() cousts_values[i] = self.reply_method_TOP( self.med_custo, new_population[i]).sum() test = fitness_values[np.argmin(fitness_values)] # aqui não foi atualizado#################### for i in range(rand.size): if rand[i] >= self.mutation_rate: list_mut = list() list_mut.append( self.reply_method_mutation_TOP( self.mutation_object.swap, new_population[i])) list_mut.append( self.reply_method_mutation_TOP( self.mutation_object.insertion, new_population[i])) list_mut.append( self.reply_method_mutation_TOP( self.mutation_object.reverse, new_population[i])) list_mut.append( self.reply_method_mutation_TOP( self.mutation_object.scramble, new_population[i])) list_mut.append( self.reply_method_mutation_TOP( self.mutation_object.swap, new_population[i])) list_mut.append( self.reply_method_mutation_TOP( self.mutation_object.WGWRGM, new_population[i])) list_mut.append( self.reply_method_mutation_TOP( self.mutation_object.WGWWGM, new_population[i])) list_mut.append( self.reply_method_mutation_TOP( self.mutation_object.WGWNNM, new_population[i])) cousts_mut = np.zeros(len(list_mut)) cousts_mut[0] = sum( self.reply_method_TOP(self.med_custo, list_mut[0])) cousts_mut[1] = sum( self.reply_method_TOP(self.med_custo, list_mut[1])) cousts_mut[2] = sum( self.reply_method_TOP(self.med_custo, list_mut[2])) cousts_mut[3] = sum( self.reply_method_TOP(self.med_custo, list_mut[3])) cousts_mut[4] = sum( self.reply_method_TOP(self.med_custo, list_mut[4])) cousts_mut[5] = sum( self.reply_method_TOP(self.med_custo, list_mut[5])) cousts_mut[6] = sum( self.reply_method_TOP(self.med_custo, list_mut[6])) cousts_mut[7] = sum( self.reply_method_TOP(self.med_custo, list_mut[7])) min_mut = np.argmin(cousts_mut) new_population[i] = list_mut[min_mut] new_population = new_population + population fitness_values = np.zeros(len(new_population)) cousts_values = np.zeros(len(new_population)) for i in range(fitness_values.size): fitness_values[i] = self.reply_method_TOP( self.function_objective, new_population[i]).sum() cousts_values[i] = self.reply_method_TOP( self.med_custo, new_population[i]).sum() population_select = list() population = list() for i in range(self.population_size): if len(new_population) == 0: break min_index = np.argmin(fitness_values) if cousts_values[i] <= self.max_coust.sum(): passa = True custo = self.reply_method_TOP( self.med_custo, new_population[min_index]) for j in range(self.number_agents): if custo[j] > self.max_coust[j]: passa = False break if passa: exist_menor = [ best for best in range(4) if fitness_values[min_index] < best_elements_coust[best] ] crhomossome = new_population[min_index] if len(exist_menor) > 0: flag_possui = [ np.array_equal(element, crhomossome) for element in best_elements ] if True not in flag_possui: best_tmp = best_elements best_tmp.append(crhomossome) new_cousts = np.array([ self.reply_method_TOP( self.function_objective, tmp).sum() for tmp in best_tmp ]) indexes_tmp = np.argsort(new_cousts) best_elements_coust = new_cousts[ indexes_tmp[0:4]] best_elements = [ best_tmp[best_index] for best_index in indexes_tmp ] else: if fitness_values[ min_index] not in population_select: population.append( new_population[min_index]) population_select.append( fitness_values[min_index]) del new_population[min_index] fitness_values = np.delete(fitness_values, [min_index]) # for i in range(len(population)): # if np.unique(population[i]).size < population[i].size - 1: # print('error') if best_elements_coust[0] < best_coust: best_coust = best_elements_coust[0] best_always = np.copy(best_elements[0]) best_count = 0 elif best_elements_coust[0] == best_coust: best_count += 1 best_element_generation.append(best_elements_coust[0]) if best_count >= self.limit_population: break self.best_route = best_elements[0] print(best_element_generation) return best_elements_coust, best_elements
class PSO: def __init__(self, map_points, iterations, size_population, beta, alfa, cost_rate, prizes_rate, prizes, max_cost, start_point, end_point, depositos=[]): self.map_points = np.loadtxt(map_points) self.iterations = iterations self.size_population = size_population self.beta = beta self.alfa = alfa self.cost_rate = np.array(cost_rate) self.prizes_rate = prizes_rate self.prizes = np.loadtxt(prizes)[:, 1] self.max_cost = np.array(max_cost) self.start_point = start_point self.end_point = end_point self.depositos = depositos self.particles = [] self.number_agents = len(max_cost) self.distance = calculate_distances(self.map_points) self.functionObject = FunctionObjective(self.map_points, self.prizes) self.FO = self.functionObject.FO self.mensureCost = self.functionObject.med_custo self.methodInsertRemoveChromosome = self.functionObject.coust_insert self.allElementsMap = np.arange(self.map_points.shape[0]) self.allElementsMap = self.allElementsMap[self.number_agents:] # removendo depositos deposits = [x for x in self.start_point] deposits += [x for x in self.end_point if x not in deposits] deposits = np.unique(np.array(deposits)) self.initialChromossome = np.arange(self.map_points.shape[0]) self.initialChromossome = np.delete(self.initialChromossome, self.depositos) self.allElementsMap = np.copy(self.initialChromossome) self.mutationObject = Mutation(self.mensureCost, self.max_cost, self.prizes) self.mutation = self.mutationObject.scramble self.crossoverObject = Crossover() self.crossover = self.crossoverObject.cross_TOPMD self.PopulationObject = Population(self.start_point, self.end_point, self.mensureCost, self.max_cost, self.distance) self.SelectionObject = Selection() self.selection = self.SelectionObject.tournament solutions = self.PopulationObject.initializeTopMdGreed2( self.initialChromossome, self.size_population, self.number_agents, 1) for s in solutions: particle = Particle(route=s, mensure_function=self.mensureCost, fitness_function=self.FO) self.particles.append(particle) def plota_rotas_TOP(self, cidades, rota, size=12, font_size=20, file_plot=False, name_file_plot='plt'): """ Method to create a chart with the best routes found :param cidades: all points of the route300 :param rota: the sequence with the best route :param size: size of the chart :param font_size: size of the label of the points """ pos_x = [cidades[val.astype(int), 0] for val in rota] pos_y = [cidades[val.astype(int), 1] for val in rota] elements = self.map_points[:, 0] x = self.map_points[:, 0] y = self.map_points[:, 1] # cid_nome = range(elements.size) cid_nome = self.allElementsMap plt.figure(num=None, figsize=(size, size), dpi=80, facecolor='w', edgecolor='k') for i in range(len(rota)): plt.plot(pos_x[i], pos_y[i], 'C' + str(i), lw=5, label='robot ' + str(i + 1), zorder=1) plt.rc('font', size=font_size) plt.xlabel("X") plt.ylabel("Y") plt.scatter(self.map_points[cid_nome, 0], self.map_points[cid_nome, 1], s=120, marker="s", zorder=2) plt.scatter(self.map_points[self.depositos, 0], self.map_points[self.depositos, 1], s=150, marker='^', zorder=3, c='black') plt.legend(loc='lower left', bbox_to_anchor=(0, 1.02, 1, 0.2), ncol=4, mode='expand') # for i, txt in enumerate(cid_nome): for i in cid_nome: plt.annotate(str(self.prizes[i]), (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # for i, txt in enumerate(cid_nome): # plt.annotate(txt, (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # for i in self.start_point: # plt.annotate('dep.', (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) for i in self.depositos: plt.annotate('base', (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # plt.title('Mapa GA') # plt.margins(0.05) if file_plot: plt.savefig(name_file_plot + '.png') else: plt.show() def setGBest(self, new_gbest): self.gbest = new_gbest # returns gbest (best particle of the population) def getGBest(self): return self.gbest def run(self): self.gbest = min(self.particles, key=attrgetter('finess_total')) self.primeiro = self.particles for t in range(self.iterations): if t % 10 == 0: print('interation : %d | gbest cost: %f | fitness: %f' % (t, self.gbest.cost.sum(), self.gbest.finess_total)) print(self.gbest.cost, self.gbest.fitness) self.pbest = min(self.particles, key=attrgetter('finess_total')) for ind_p in range(len(self.particles)): self.particles[ind_p].clearVelocity( ) # cleans the speed of the particle temp_velocity = [] solution_gbest = copy.copy( self.gbest.getPBest()) # gets solution of the gbest solution_pbest = self.particles[ind_p].getPBest( )[:] # copy of the pbest solution solution_particle = self.particles[ind_p].getCurrentSolution( )[:] # gets copy of the current solution of the particle particle_tmp = self.particles[ind_p] self.particles[ ind_p].setCurrentSolution = self.mutationObject.insert_points_TOP_2( self.mensureCost, self.methodInsertRemoveChromosome, self.allElementsMap, particle_tmp.getCurrentSolution(), 1) self.particles[ind_p].calcCostPath() # route1, route2 = self.crossoverObject.cross_slice(self.particles[ind_p].getCurrentSolution(), # self.gbest.getCurrentSolution(), # self.mensureCost, # self.start_point, # self.end_point) offspring1, offspring2 = list(), list() all_elements_1, all_elements_2 = np.array([]), np.array([]) for i in range(self.particles[ind_p].number_robots): x, y = self.crossoverObject.PMX_3( self.particles[ind_p].getCurrentSolution()[i], self.pbest.getCurrentSolution()[i], all_elements_1, all_elements_2) all_elements_1 = np.unique( np.concatenate([all_elements_1, x[1:-1]])) all_elements_2 = np.unique( np.concatenate([all_elements_2, y[1:-1]])) offspring1.append(x) offspring2.append(y) route1 = offspring1 route2 = offspring2 route1 = reply_method_mutation_top(self.mutationObject.swap, route1) route2 = reply_method_mutation_top(self.mutationObject.swap, route2) route1 = reply_method_mutation_top(self.mutationObject.SWGLM, route1) route2 = reply_method_mutation_top(self.mutationObject.SWGLM, route2) route1 = self.mutationObject.remove_points_TOP( self.mensureCost, self.FO, self.allElementsMap, route1) route2 = self.mutationObject.remove_points_TOP( self.mensureCost, self.FO, self.allElementsMap, route2) particle_tmp_1 = Particle(route=route1, mensure_function=self.mensureCost, fitness_function=self.FO) particle_tmp_2 = Particle(route=route2, mensure_function=self.mensureCost, fitness_function=self.FO) flag_1 = [ True for ind in range(particle_tmp_1.number_robots) if particle_tmp_1.cost[ind] > self.max_cost[ind] ] flag_2 = [ True for ind in range(particle_tmp_2.number_robots) if particle_tmp_2.cost[ind] > self.max_cost[ind] ] best_particle = None if True not in flag_1 and True not in flag_2: best_particle = particle_tmp_2 if particle_tmp_1.finess_total < particle_tmp_2.finess_total: best_particle = particle_tmp_1 elif True not in flag_1: best_particle = particle_tmp_1 elif True not in flag_2: best_particle = particle_tmp_2 if best_particle: flag = [ True for ind in range(best_particle.number_robots) if best_particle.cost[ind] > self.max_cost[ind] ] if self.gbest.finess_total > best_particle.finess_total: if True not in flag: self.gbest = best_particle if best_particle.finess_total < self.particles[ ind_p].finess_total: self.particles[ind_p] = best_particle self.ultimo = self.particles return self.gbest, self.primeiro, self.ultimo
def __init__(self, generation, population, limit_population, crossover_rate, mutation_rate, cost_rate, prizes_rate, map_points, prizes, max_cost, start_point, end_point, depositos=[]): self.generationSize = generation self.populationSize = population self.limit_population = limit_population self.crossover_rate = crossover_rate self.mutation_rate = mutation_rate self.cost_rate = np.array(cost_rate) self.prizes_rate = prizes_rate self.map_points = np.loadtxt(map_points) self.prizes = np.loadtxt(prizes)[:, 1] self.max_cost = np.array(max_cost) self.start_point = start_point self.end_point = end_point self.depositos = depositos self.number_agents = len(max_cost) self.distance = self.calculate_distances() self.functionObject = FunctionObjective(self.map_points, self.prizes) self.FO = self.functionObject.FO self.mensureCost = self.functionObject.med_custo self.methodInsertRemoveChromosome = self.functionObject.coust_insert self.allElementsMap = np.arange(self.map_points.shape[0]) self.allElementsMap = self.allElementsMap[self.number_agents:] # removendo depositos deposits = [x for x in self.start_point] deposits += [x for x in self.end_point if x not in deposits] deposits = np.unique(np.array(deposits)) self.initialChromossome = np.arange(self.map_points.shape[0]) self.initialChromossome = np.delete(self.initialChromossome, self.depositos) self.allElementsMap = np.copy(self.initialChromossome) self.mutationObject = Mutation(self.mensureCost, self.max_cost, self.prizes) self.mutation = self.mutationObject.scramble self.crossoverObject = Crossover() self.crossover = self.crossoverObject.cross_TOPMD self.PopulationObject = Population(self.start_point, self.end_point, self.mensureCost, self.max_cost, self.distance) self.SelectionObject = Selection() self.selection = self.SelectionObject.tournament
class GaTopMd: generationSize: int populationSize: int limit_population: int crossover_rate: float mutation_rate: float cost_rate: int prizes_rate: int map_points: np.array prizes: np.array max_cost: np.array start_point: list end_point: list depositos: list distance = np.array def __init__(self, generation, population, limit_population, crossover_rate, mutation_rate, cost_rate, prizes_rate, map_points, prizes, max_cost, start_point, end_point, depositos=[]): self.generationSize = generation self.populationSize = population self.limit_population = limit_population self.crossover_rate = crossover_rate self.mutation_rate = mutation_rate self.cost_rate = np.array(cost_rate) self.prizes_rate = prizes_rate self.map_points = np.loadtxt(map_points) self.prizes = np.loadtxt(prizes)[:, 1] self.max_cost = np.array(max_cost) self.start_point = start_point self.end_point = end_point self.depositos = depositos self.number_agents = len(max_cost) self.distance = self.calculate_distances() self.functionObject = FunctionObjective(self.map_points, self.prizes) self.FO = self.functionObject.FO self.mensureCost = self.functionObject.med_custo self.methodInsertRemoveChromosome = self.functionObject.coust_insert self.allElementsMap = np.arange(self.map_points.shape[0]) self.allElementsMap = self.allElementsMap[self.number_agents:] # removendo depositos deposits = [x for x in self.start_point] deposits += [x for x in self.end_point if x not in deposits] deposits = np.unique(np.array(deposits)) self.initialChromossome = np.arange(self.map_points.shape[0]) self.initialChromossome = np.delete(self.initialChromossome, self.depositos) self.allElementsMap = np.copy(self.initialChromossome) self.mutationObject = Mutation(self.mensureCost, self.max_cost, self.prizes) self.mutation = self.mutationObject.scramble self.crossoverObject = Crossover() self.crossover = self.crossoverObject.cross_TOPMD self.PopulationObject = Population(self.start_point, self.end_point, self.mensureCost, self.max_cost, self.distance) self.SelectionObject = Selection() self.selection = self.SelectionObject.tournament def plota_rotas_TOP(self, cidades, rota, size=12, font_size=20, file_plot=False, name_file_plot='plt'): """ Method to create a chart with the best routes found :param cidades: all points of the route300 :param rota: the sequence with the best route :param size: size of the chart :param font_size: size of the label of the points """ pos_x = [cidades[val.astype(int), 0] for val in rota] pos_y = [cidades[val.astype(int), 1] for val in rota] elements = self.map_points[:, 0] x = self.map_points[:, 0] y = self.map_points[:, 1] # cid_nome = range(elements.size) cid_nome = self.allElementsMap plt.figure(num=None, figsize=(size, size), dpi=80, facecolor='w', edgecolor='k') for i in range(len(rota)): plt.plot(pos_x[i], pos_y[i], 'C' + str(i), lw=5, label='agente ' + str(i + 1), zorder=1) plt.rc('font', size=font_size) plt.xlabel("X") plt.ylabel("Y") plt.scatter(self.map_points[cid_nome, 0], self.map_points[cid_nome, 1], s=120, marker="s", zorder=2, label='waypoint') plt.scatter(self.map_points[self.depositos, 0], self.map_points[self.depositos, 1], s=150, marker='^', zorder=3, c='black', label='depósito') plt.legend(loc='lower left', bbox_to_anchor=(0, 1.02, 1, 0.2), ncol=4, mode='expand') # for i, txt in enumerate(cid_nome): for i in cid_nome: plt.annotate(str(self.prizes[i]), (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # for i, txt in enumerate(cid_nome): # plt.annotate(txt, (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # for i in self.start_point: # plt.annotate('dep.', (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) for i in self.depositos: plt.annotate('dep.', (x[i] - 0.01, y[i] + 0.3), fontsize=font_size) # plt.title('Mapa GA') # plt.margins(0.05) if file_plot: plt.savefig(name_file_plot + '.png') else: plt.show() def calculate_distances(self): size = self.map_points.shape[0] distances = np.zeros([size, size]) temp_max = 0 for i in range(size): for j in range(size): if i != j: b = self.map_points[i, 0] - self.map_points[j, 0] c = self.map_points[i, 1] - self.map_points[j, 1] a = np.sqrt(np.square(b) + np.square(c)) distances[i, j] = a distances[j, i] = a if temp_max < a: temp_max = a return distances @staticmethod def reply_method_top(method, chromossome): size = len(chromossome) result = np.zeros(size) for n in np.arange(size): result[n] = method(chromossome[n]) return result def reply_method_mutation_top(self, method, chromossome, sizeMut=0): size = len(chromossome) size_mut_genes = sizeMut if sizeMut == 0: size_mut_genes = size result = [0] * size rand = np.arange(size) np.random.shuffle(rand) rand = rand[:size_mut_genes] for n in np.arange(size): if n in rand: if chromossome[n].size > 3: result[n] = method(chromossome[n]) else: result[n] = chromossome[n] else: result[n] = chromossome[n] return result def run(self): population = self.PopulationObject.initializeTopMdGreed( self.initialChromossome, int(self.populationSize * .5), self.number_agents, 1) population += self.PopulationObject.initializeTopMd( self.initialChromossome, int(self.populationSize * .5), self.number_agents) primeira_populacao = population size_best_elements = 4 bestElements = population[0:4] bestElementsCosts = np.array([ self.reply_method_top(self.FO, element).sum() for element in bestElements ]) countGenaration = 0 bestCost = bestElementsCosts[0] bestElementAlways = bestElements[0] bestElementGenaration = list() bestElementGenarationCost = list() count_dis = 0 real_cost = self.max_cost # self.max_cost = [i*.7 for i in self.max_cost] flag = False mutation_rate_begin = self.mutation_rate crossover_rate_begin = self.crossover_rate pop_size = self.populationSize for g in range(self.generationSize): print(g, bestCost, countGenaration, len(population)) # completando populacao depois que apenas os melhores individuos restaram population += self.PopulationObject.initializeTopMdGreed2( self.initialChromossome, self.populationSize - len(population), self.number_agents, 1) population += self.PopulationObject.initializeTopMd2( self.initialChromossome, self.populationSize - len(population), self.number_agents) # if countGenaration == 10: # bestElements = population[1:5] # bestElementsCosts = np.array([self.reply_method_top(self.FO, element).sum() for element in bestElements]) # teste_population = list() for i in range(len(population)): population[i] = self.mutationObject.insert_points_TOP_3( self.mensureCost, self.methodInsertRemoveChromosome, self.allElementsMap, population[i]) # teste_population = list() # for i in range(len(population)): # teste_population.append(self.mutationObject.insert_points_TOP_2(self.mensureCost, # self.methodInsertRemoveChromosome, # self.allElementsMap, # population[i])) # population += teste_population cost_population = [ self.reply_method_top(self.FO, individual).sum() for individual in population ] cost_population = [costs.sum() for costs in cost_population] cost_population = np.array( [costs.sum() for costs in cost_population]) select_parents_index = self.selection( int(self.populationSize * self.crossover_rate), cost_population, 5) parents_selected = [ population[idx] for idx in select_parents_index ] new_population = bestElements[:4] bestElementsTeste = bestElements # Realizando o cruzamento entre os genees for cross in range(select_parents_index.size): select_2_parents = np.random.randint(select_parents_index.size, size=2) # compar = [np.array_equal(parents_selected[select_2_parents[0]][i],parents_selected[select_2_parents[1]][i]) for i in range(self.number_agents)] # # for i in compar: # fg = True # if i == False: # fg = False # break # if fg: # par_1 = [np.copy(i) for i in parents_selected[select_2_parents[0]]] # par_2 = self.PopulationObject.initializeTopMdGreed(self.initialChromossome, 1,self.number_agents)[0] # # else: par_1 = [ np.copy(i) for i in parents_selected[select_2_parents[0]] ] par_2 = [ np.copy(i) for i in parents_selected[select_2_parents[1]] ] if flag: offspring1, offspring2 = list(), list() all_elements_1, all_elements_2 = np.array([]), np.array([]) for i in range(len(par_1)): x, y = self.crossoverObject.PMX_3( par_1[i], par_2[i], all_elements_1, all_elements_2) all_elements_1 = np.unique( np.concatenate([all_elements_1, x[1:-1]])) all_elements_2 = np.unique( np.concatenate([all_elements_2, y[1:-1]])) offspring1.append(x) offspring2.append(y) new_population.append(offspring1) new_population.append(offspring2) else: a, b = self.crossoverObject.cross_slice( par_1, par_2, self.mensureCost, self.start_point, self.end_point) new_population.append(a) new_population.append(b) # new_population = new_population + [population[i] for i in select_parents_index if i not in select_parents_index] # for i in range(len(new_population)): # new_population[i] = self.reply_crossover_inner_agents(self.crossover_class.PMX, new_population[i]) # gerando lista de probabilidades para os novos indivíduos sofrerem mutações # rand = np.random.uniform(0,1, len(new_population)) # # for i in range(len(new_population)): # new_population[i] = self.mutationObject.insert_points_TOP_3(self.mensureCost, # self.methodInsertRemoveChromosome, # self.allElementsMap, # new_population[i]) # fitness_values = np.zeros(len(new_population)) # cousts_values = np.zeros(len(new_population)) # # for i in range(fitness_values.size): # fitness_values[i] = self.reply_method_top(self.FO,new_population[i]).sum() # cousts_values[i] = self.reply_method_top(self.mensureCost,new_population[i]).sum() # # # aqui não foi atualizado#################### # new_population.append(bestElementAlways) # rand= np.insert(rand, rand.size, self.mutation_rate) # # for i in bestElements: # new_population.append(i) # rand= np.insert(rand, rand.size, self.mutation_rate) population_mutation = list() for a in range(5): for i in range(len(new_population)): r = np.random.uniform(0, 1, 1) if r <= self.mutation_rate: # for jj in range(3): # c = new_population[i] # c = self.reply_method_mutation_top(self.mutationObject.swap,c, 1) # c = self.reply_method_mutation_top(self.mutationObject.SWGLM,c, 1) list_mut = list() # list_mut.append(c) list_mut.append( self.reply_method_mutation_top( self.mutationObject.swap, new_population[i], 1)) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.insertion,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.reverse,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.scramble,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.swap,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.SWGLM,new_population[i])) list_mut.append( self.reply_method_mutation_top( self.mutationObject.SWGLM, new_population[i], 1)) # if not flag: # ind = np.random.choice(range(self.number_agents), 2, replace=False) # elemento = [np.copy(chromossome) for chromossome in new_population[i]] # elemento[ind[0]], elemento[ind[1]] = self.mutationObject.PMX_mutation( # elemento[ind[0]], elemento[ind[1]], np.array([]), np.array([])) # list_mut.append(elemento) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.insertion,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.reverse,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.scramble,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.WGWRGM,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.WGWWGM,new_population[i])) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.WGWNNM,new_population[i])) # # if self.number_agents > 1: # try: # ind = np.random.choice(range(self.number_agents), 2, replace=False) # elemento = [np.copy(chromossome) for chromossome in new_population[i]] # elemento[ind[0]], elemento[ind[1]] = self.mutationObject.PMX_mutation( # elemento[ind[0]], elemento[ind[1]], np.array([]), np.array([])) # list_mut.append(elemento) # except: # print(ind, elemento) # print(ind, elemento) # raise # else: # list_mut.append(self.reply_method_mutation_top(self.mutationObject.scramble, new_population[i])) # if(countGenaration > self.limit_population * 0.2): # list_mut.append(self.reply_method_mutation_top(self.mutationObject.reverse,new_population[i], 2)) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.swap,new_population[i], 2)) # # list_mut.append(self.reply_method_mutation_top(self.mutationObject.insertion,new_population[i], 2)) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.WGWRGM,new_population[i],1)) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.WGWWGM,new_population[i],1)) # list_mut.append(self.reply_method_mutation_top(self.mutationObject.SWGLM,new_population[i])) # cousts_mut = np.zeros(len(list_mut)) # # cousts_mut[0] = sum(self.reply_method_top(self.mensureCost,list_mut[0])) # cousts_mut[1] = sum(self.reply_method_top(self.mensureCost,list_mut[1])) # cousts_mut[2] = sum(self.reply_method_top(self.mensureCost,list_mut[2])) # cousts_mut[3] = sum(self.reply_method_top(self.mensureCost,list_mut[3])) # cousts_mut[4] = sum(self.reply_method_top(self.mensureCost,list_mut[4])) # cousts_mut[5] = sum(self.reply_method_top(self.mensureCost,list_mut[5])) # # # if(countGenaration > self.limit_population * 0.6): # cousts_mut[6] = sum(self.reply_method_top(self.mensureCost,list_mut[6])) # cousts_mut[7] = sum(self.reply_method_top(self.mensureCost,list_mut[7])) # cousts_mut[8] = sum(self.reply_method_top(self.mensureCost,list_mut[8])) # # min_mut = np.argmin(cousts_mut) # new_population[i] = list_mut[min_mut] population_mutation = population_mutation + list_mut # if self.number_agents > 1: # ind = np.random.choice(range(self.number_agents), 2) # elemento = [np.copy(chromossome) for chromossome in new_population[i]] # elemento[ind[0]], elemento[ind[1]] = self.crossoverObject.PMX_2( # elemento[ind[0]], elemento[ind[1]],np.array([]), np.array([])) else: population_mutation.append(new_population[i]) new_population = population_mutation for i in range(len(new_population)): new_population[i] = self.mutationObject.remove_points_TOP( self.mensureCost, self.FO, self.allElementsMap, new_population[i]) # new_population = new_population + population fitness_values = np.zeros(len(new_population)) cousts_values = np.zeros(len(new_population)) for i in range(fitness_values.size): fitness_values[i] = self.reply_method_top( self.FO, new_population[i]).sum() cousts_values[i] = self.reply_method_top( self.mensureCost, new_population[i]).sum() population_select = list() population = list() for i in range(int(self.populationSize)): if len(new_population) == 0: break min_index = np.argmin(fitness_values) # if cousts_values[i].sum() <= self.max_cost.sum(): passa = list() custo = self.reply_method_top(self.mensureCost, new_population[min_index]) s = False for j in range(self.number_agents): if custo[j] > self.max_cost[j]: s = True passa.append(False) break if not s: exist_menor = [ best for best in range(4) if fitness_values[min_index] < bestElementsCosts[best] ] crhomossome = new_population[min_index] if len(exist_menor) > 0: best_elements_temporary = bestElements best_elements_temporary.append(crhomossome) new_cousts = np.array([ self.reply_method_top(self.FO, tmp).sum() for tmp in best_elements_temporary ]) indexes_best_individual = np.argsort(new_cousts) indexes_best_individual = indexes_best_individual[ 0:size_best_elements] bestElementsCosts = new_cousts[indexes_best_individual] bestElements = [ best_elements_temporary[best_index] for best_index in indexes_best_individual ] else: # if fitness_values[min_index] not in population_select: population.append(new_population[min_index]) population_select.append(fitness_values[min_index]) del new_population[min_index] fitness_values = np.delete(fitness_values, [min_index]) # for i in range(len(population)): # if np.unique(population[i]).size < population[i].size - 1: # print('error') if bestElementsCosts[0] < bestCost: bestCost = bestElementsCosts[0] bestElementAlways = np.copy(bestElements[0]) countGenaration = 0 self.populationSize = pop_size # print(self.reply_method_top(self.mensureCost, bestElementAlways)) # print(self.max_cost) # print(real_cost) # flag = True # self.mutation_rate = mutation_rate_begin # self.crossover_rate = crossover_rate_begin # bestElementTesteando = [np.copy(element) for element in bestElements] # bestElementTesteandoCost = np.copy(bestElementsCosts) bestElementGenaration.append(bestCost) # print(self.reply_method_top(self.mensureCost, bestElements[0])) # elif bestElementsCosts[0] == bestCost: # countGenaration += 1 else: countGenaration += 1 population.append(bestElementAlways) # custos = self.reply_method_top(self.mensureCost, bestElements[0]) # print(custos) # print([True for item in range(self.number_agents) if custos[item] > self.max_cost[item]]) # bestElements = [np.copy(element) for element in bestElementTesteando] # bestElementsCosts = np.copy(bestElementTesteandoCost) bestElementGenarationCost.append(bestElementsCosts[0]) if countGenaration == 5: # bestElementAlways = np.copy(bestElements[0]) # bestCost = bestElementsCosts[0] self.max_cost = [ self.max_cost[i] + (real_cost[i] * .1) for i in range(len(real_cost)) ] c = False for i in range(self.number_agents): if self.max_cost[i] > real_cost[i]: c = True break if c: self.max_cost = real_cost if self.mutation_rate < .3: self.mutation_rate = self.mutation_rate + .05 # self.crossover_rate = self.crossover_rate + .1 # else: # self.mutation_rate = mutation_rate_begin # self.crossover_rate = crossover_rate_begin if countGenaration == 10: flag = False # if countGenaration % 10 == 0: # self.populationSize = self.populationSize + 10 # self.crossover_rate = crossover_rate_begin # if len(population) > self.populationSize * .7: # x = np.arange(len(population)) # # np.random.shuffle(x) # population = [population[i] for i in x[:int(self.populationSize*.5)]] # population.append(bestElementAlways) population = population + bestElements if countGenaration >= self.limit_population: break self.bestRoute = bestElements[0] population += bestElements population.append(bestElementAlways) return bestElementsCosts, bestElements, bestElementGenaration, bestElementAlways, primeira_populacao, population
def start(self): # temp advance_dirs = { 'Merged_vcf': '{analydir}/Advance/{newjob}/Merged_vcf', 'ACMG': '{analydir}/Advance/{newjob}/ACMG', 'FilterSV': '{analydir}/Advance/{newjob}/FilterSV', 'FilterCNV': '{analydir}/Advance/{newjob}/FilterCNV', 'Noncoding': '{analydir}/Advance/{newjob}/Noncoding', 'ModelF': '{analydir}/Advance/{newjob}/ModelF', 'Share': '{analydir}/Advance/{newjob}/Share', 'Denovo': '{analydir}/Advance/{newjob}/Denovo', 'Linkage': '{analydir}/Advance/{newjob}/Linkage', 'ROH': '{analydir}/Advance/{newjob}/ROH', 'Network': '{analydir}/Advance/{newjob}/Network', 'Pathway': '{analydir}/Advance/{newjob}/Pathway', 'PPI': '{analydir}/Advance/{newjob}/PPI', 'HLA': '{analydir}/Advance/{newjob}/HLA', 'SiteAS': '{analydir}/Advance/{newjob}/SiteAS', 'GeneAS': '{analydir}/Advance/{newjob}/GeneAS', 'IntegrateResult': '{analydir}/Advance/{newjob}/IntegrateResult', 'Disease': '{analydir}/Advance/{newjob}/Disease', 'BriefResults': '{analydir}/Advance/{newjob}/BriefResults', } for k, v in advance_dirs.iteritems(): self.args.update({k: v.format(**self.args)}) # print self.args['SiteAS'] # exit() # print self.analy_array print 'hello, {}'.format(self.username) # Require rawdata or not qc_status = utils.get_status('qc', self.startpoint, config.ANALYSIS_POINTS) mapping_status = utils.get_status('bwa_mem', self.startpoint, config.ANALYSIS_POINTS) print 'qc status:', qc_status print 'mapping status:', mapping_status ANALY_DICT = utils.get_analysis_dict(self.analy_array, config.ANALYSIS_CODE) self.args.update({'ANALY_DICT': ANALY_DICT}) # print ANALY_DICT.keys();exit() softwares = utils.get_softwares(self.analy_array, self.args['ANALY_DICT'], self.args, self.seqstrag) # pprint(softwares);exit() self.args.update({'softwares': softwares}) # check inputs self.queues = utils.check_queues(self.queues, self.username) self.args.update({'queues': self.queues}) # use sentieon specific queues if needed if 'sentieon' in softwares.values(): print 'add sentieon_queues' sentieon_queues = self.queues if config.CONFIG.has_option('resource', 'sentieon_queues'): sentieon_queues = config.CONFIG.get( 'resource', 'sentieon_queues').split(',') sentieon_queues = utils.check_queues(sentieon_queues, self.username) if not sentieon_queues: sentieon_queues = self.queues self.args.update({'sentieon_queues': sentieon_queues}) # print self.args['sentieon_queues'];exit() # print sentieon_queues;exit() utils.check_analy_array(self.seqstrag, self.analy_array, config.ANALYSIS_CODE) utils.check_files(self.pn, self.samp_info, self.samp_list) newTR = utils.check_target_region(config.CONFIG, self.seqstrag, self.refgenome, self.rawTR) self.args.update({'TR': newTR}) print 'analysis items:' for analysis_code in self.analy_array: print utils.color_text( '{:4} {}'.format(analysis_code, config.ANALYSIS_CODE[analysis_code][0]), 'yellow') # Analysis start point if self.startpoint: if self.startpoint in config.ANALYSIS_POINTS: print 'start point: {}'.format( utils.color_text(self.startpoint)) else: print '[error] invalid startpoint: {}'.format( utils.color_text(self.startpoint)) print 'maybe you want to choose: {}'.format( utils.color_text( process.extractOne(self.startpoint, config.ANALYSIS_POINTS.keys())[0], 'cyan')) print 'available startpoints are as follows:\n {}'.format( ' '.join(config.ANALYSIS_POINTS.keys())) exit(1) is_advance = max(self.analy_array) > 6.1 project = utils.Project(self.analydir, self.samp_info, self.samp_info_done, self.samp_list, self.qc_list, qc_status, mapping_status, is_advance) # Extract sample_info print 'extract sample informations...' fenqi, tissue, disease_name, sample_infos, sample_infos_all, sample_done = project.get_sample_infos( self.samp_list, self.samp_info, self.samp_info_done, is_advance) database = '{}/project/DisGeNet.json'.format( config.CONFIG.get('software', 'soft_dir')) disease_ids = utils.get_disease_id(disease_name, database) self.args.update({ 'disease_name': disease_name, 'disease_ids': disease_ids, }) sample_infos_waiting = { sampleid: infos for sampleid, infos in sample_infos.iteritems() if sampleid not in sample_done } self.args.update({'sample_infos_waiting': sample_infos_waiting}) # print sample_infos_waiting # exit() # print 'fenqi:', fenqi # print 'tissue:', tissue # exit() sample_lists = project.get_sample_lists # print sample_lists # print sample_infos.keys() # print sample_infos_all.keys() # for sample in sample_infos: # print sample, sample_infos[sample]['familyid'] # exit() if mapping_status == 'waiting': sample_lists = project.update_qc_list() print ' report number: {}'.format(utils.color_text(fenqi)) if disease_name: print ' disease name: {}'.format(utils.color_text(disease_name)) print ' disease id: {}'.format(utils.color_text(disease_ids)) if tissue: print ' tissue: {}'.format(utils.color_text(tissue)) print ' samples ({}): {}'.format( len(sample_infos), utils.color_text(sample_infos.keys())) if sample_done: print ' samples done({}): {}'.format( len(sample_done), utils.color_text(sample_done)) # Update qc_list and extract sample_list # print 'update qc_list...' # print json.dumps(sample_lists, indent=2) # set memory according seqstrag print 'set analysis memory...' if self.seqstrag == 'WGS': print 'upate memory for WGS...' for analysis, memory in config.ANALYSIS_MEM_WGS.items(): if analysis in config.ANALYSIS_POINTS: config.ANALYSIS_POINTS[analysis][0] = memory # exit() # =========================================================== # =========================================================== print '>>> pipeline start...' mutation_soft, sv_soft, cnv_soft, denovo_soft = [ softwares[each] for each in ('mutation', 'sv', 'cnv', 'denovo') ] print ' mutation_soft:{}, sv_soft:{}, cnv_soft:{}, denovo_soft:{}'.format( mutation_soft, sv_soft, cnv_soft, denovo_soft) # QC if ANALY_DICT['quality_control'] and qc_status == 'waiting': utils.print_color('> QC', 'white') QC(self.args, self.jobs, self.orders, sample_lists, config).start() # Mapping if ANALY_DICT['mapping']: utils.print_color('> Mapping', 'white') Mapping(self.args, self.jobs, self.orders, sample_lists, sample_infos, config, qc_status, mapping_status).start() # Mutation if ANALY_DICT['snpindel_call']: utils.print_color('> Mutation', 'white') Mutation(self.args, self.jobs, self.orders, sample_lists, sample_infos, config).start() # SV if ANALY_DICT['sv_call']: utils.print_color('> SV', 'white') SV(self.args, self.jobs, self.orders, sample_infos, config).start() # CNV if ANALY_DICT['cnv_call']: utils.print_color('> CNV', 'white') CNV(self.args, self.jobs, self.orders, sample_infos, config).start() # FilterDB if ANALY_DICT['filter']: utils.print_color('> FilterDB', 'white') FilterDB(self.args, self.jobs, self.orders, mutation_soft, sv_soft, cnv_soft, sample_infos, config, disease_name, tissue, ANALY_DICT).start() # ModelF if ANALY_DICT['filter_model']: utils.print_color('> Model', 'white') FilterModel(self.args, self.jobs, self.orders, mutation_soft, sv_soft, cnv_soft, sample_infos, config).start() # Denovo if ANALY_DICT['denovo']: utils.print_color('> Denovo', 'white') Denovo(self.args, self.jobs, self.orders, mutation_soft, sv_soft, cnv_soft, denovo_soft, sample_infos, config, ANALY_DICT).start() # Linkage if ANALY_DICT['linkage']: utils.print_color('> Linkage', 'white') Linkage(self.args, self.jobs, self.orders, mutation_soft, sv_soft, cnv_soft, denovo_soft, sample_infos_all, config, ANALY_DICT).start() # IntegrateResult if any(ANALY_DICT[analysis] for analysis in ['filter', 'filter_model', 'denovo', 'phenolyzer']): utils.print_color('> IntegrateResult', 'white') IntegrateResult(self.args, self.jobs, self.orders, config).start() # ROH if ANALY_DICT['roh']: utils.print_color('> ROH', 'white') ROH(self.args, self.jobs, self.orders, sample_infos, mutation_soft, config).start() # OTHER other = Other(self.args, self.jobs, self.orders, config, disease_name) # IBD if any(ANALY_DICT[each] for each in ['filter_model', 'linkage', 'denovo' ]) and len(sample_infos_waiting) > 1: utils.print_color('> IBD', 'white') other.ibd() # Network if ANALY_DICT['phenolyzer']: utils.print_color('> Phenolyzer', 'white') other.phenolyzer() # Pathway if ANALY_DICT['pathway']: utils.print_color('> Pathway', 'white') other.pathway() # PPI if ANALY_DICT['ppi']: utils.print_color('> PPI', 'white') other.ppi() # SiteAS if ANALY_DICT['site_association']: utils.print_color('> SiteAS', 'white') Association(self.args, self.jobs, self.orders, config).site_association() # GeneAS if ANALY_DICT['gene_association']: utils.print_color('> GeneAS', 'white') Association(self.args, self.jobs, self.orders, config).gene_association() # HLA if ANALY_DICT['hla']: utils.print_color('> HLA', 'white') HLA(self.args, self.jobs, self.orders, sample_lists, sample_infos, config, qc_status).start() # result and report utils.print_color('> Result', 'white') Result(self.args, self.jobs, self.orders, config).start() utils.print_color('> Report', 'white') Report(self.args, self.jobs, self.orders, config).start() # job summary print 'lenght of jobs waiting/total: {}/{}'.format( len([job for job in self.jobs if job.get('status') == 'waiting']), len(self.jobs)) utils.write_job(self.analydir, self.newjob, self.jobs, self.orders) print '{:-^80}'.format(' all done ')