def crossover_inplace(self, gene1, gene2): keys = list(gene1.param_dict.keys()) # References to the variable tensors W1 = gene1.param_dict W2 = gene2.param_dict num_variables = len(W1) if num_variables != len(W2): print 'Warning: Genes for crossover might be incompatible' # Crossover opertation [Indexed by column, not rows] num_cross_overs = fastrand.pcg32bounded( num_variables * 2) # Lower bounded on full swaps for i in range(num_cross_overs): tensor_choice = fastrand.pcg32bounded( num_variables) # Choose which tensor to perturb receiver_choice = random.random( ) # Choose which gene to receive the perturbation if receiver_choice < 0.5: ind_cr = fastrand.pcg32bounded( W1[keys[tensor_choice]].shape[-1]) # W1[keys[tensor_choice]][:, ind_cr] = W2[ keys[tensor_choice]][:, ind_cr] #W1[keys[tensor_choice]][ind_cr, :] = W2[keys[tensor_choice]][ind_cr, :] else: ind_cr = fastrand.pcg32bounded( W2[keys[tensor_choice]].shape[-1]) # W2[keys[tensor_choice]][:, ind_cr] = W1[ keys[tensor_choice]][:, ind_cr]
def mutate(self, actor): num_mutation_frac = 0.1 super_mut_prob = 0.05 reset_prob = super_mut_prob + 0.05 super_mut_strength = 10 mut_strength = 0.1 ori_weights = actor.get_parameters_weights() for weight in ori_weights: if len(weight.shape) == 2: num_weights = weight.shape[0] * weight.shape[1] num_mutations = fastrand.pcg32bounded(int(math.ceil(num_mutation_frac * num_weights))) for _ in range(num_weights): ind_dim1 = fastrand.pcg32bounded(weight.shape[0]) ind_dim2 = fastrand.pcg32bounded(weight.shape[1]) random_num = random.random() if random_num < super_mut_prob: # Super Mutation probability weight[ind_dim1, ind_dim2] += random.gauss(0, super_mut_strength * weight[ind_dim1, ind_dim2]) elif random_num < reset_prob: weight[ind_dim1, ind_dim2] = random.gauss(0, 1) else: # mutauion even normal weight[ind_dim1, ind_dim2] += random.gauss(0, mut_strength * weight[ind_dim1, ind_dim2]) weight[ind_dim1, ind_dim2] = self.regularize_weight(weight[ind_dim1, ind_dim2], 100000) actor.set_parameters_weights(ori_weights)
def mutate_inplace(self, gene): mut_strength = 0.1 num_mutation_frac = 0.1 super_mut_strength = 10 super_mut_prob = 0.05 reset_prob = super_mut_prob + 0.05 # References to the variable keys keys = list(gene.param_dict.keys()) W = gene.param_dict num_structures = len(keys) ssne_probabilities = np.random.uniform(0,1,num_structures)*2 for ssne_prob, key in zip(ssne_probabilities, keys): #For each structure apart from poly if random.random()<ssne_prob: num_mutations = fastrand.pcg32bounded(int(math.ceil(num_mutation_frac * W[key].size))) # Number of mutation instances for _ in range(num_mutations): ind_dim1 = fastrand.pcg32bounded(W[key].shape[0]) ind_dim2 = fastrand.pcg32bounded(W[key].shape[-1]) random_num = random.random() if random_num < super_mut_prob: # Super Mutation probability W[key][ind_dim1, ind_dim2] += random.gauss(0, super_mut_strength * W[key][ind_dim1, ind_dim2]) elif random_num < reset_prob: # Reset probability W[key][ind_dim1, ind_dim2] = random.gauss(0, 1) else: # mutauion even normal W[key][ind_dim1, ind_dim2] += random.gauss(0, mut_strength *W[key][ind_dim1, ind_dim2]) # Regularization hard limit W[key][ind_dim1, ind_dim2] = self.regularize_weight(W[key][ind_dim1, ind_dim2], self.parameters.weight_magnitude_limit)
def getTrainInstances(self): totalData = [] for s in range(self.numUsers * self.num_negatives): while True: u = fastrand.pcg32bounded(self.numUsers) cu = self.userCache[u] if len(cu) == 0: continue t = fastrand.pcg32bounded(len(cu)) #i = list(cu)[t] i = cu[t] j = fastrand.pcg32bounded(self.numItems) while j in cu: j = fastrand.pcg32bounded(self.numItems) break totalData.append([u, i, j]) totalData = np.array(totalData) return totalData
def updateWithVibe (img, sampleArray, distanceThreshold, sampleCount): segmentationMap =np.copy (segTemplate) # print (img.shape) # print (img.shape) start = time.time() for i in range (0,(img.shape[0]-1)): start = time.time() for j in range (0,(img.shape[1]-1)): count=0 index=0 distance=0 start6=time.time () while ( ( count < minMatches ) and ( index < sampleCount ) ): # print (img[i][j]) distance=0 # if len (img[i][j])==3: # distance = math.sqrt ((img[i][j][0]-sampleArray[i][j][index][0])**2+(img[i][j][1]-sampleArray[i][j][index][1])**2+(img[i][j][2]-sampleArray[i][j][index][2])**2) # else: start2 = time.time() distance = ((img[i][j] - sampleArray[i][j][index]))** 2 end2= time.time () # print ("\t\tTime for each distane calc " + str (end2 - start2)) # print (distance) if (distance < threshold_squared ): count+=1 index+=1 end6=time.time () # print ("Time for each while loop " + str (end6-start6)) if count < minMatches: segmentationMap [i][j]= 255 # segmentationMap[i]=BACKGROUND_PIXEL else: start3 = time.time() randNum= fastrand.pcg32bounded(subSamplingFactor) end3= time.time () # print ("\t\tTime for each random generation " + str (end3 - start3)) if randNum==0: randNum=fastrand.pcg32bounded(sampleCount) sampleArray[i][j][randNum]=img[i][j] randNum= fastrand.pcg32bounded(subSamplingFactor) if randNum==0: randNum=random.choice ([-1,1]) sampleArray[(i+randNum)][(j+randNum)][randNum]=img[i][j] end= time.time () # print ("\tTime for each height loop " + str (end - start)) return segmentationMap, sampleArray
def crossover_inplace(self, gene1, gene2): """Conduct one point crossover in place Parameters: gene1 (object): A pytorch model gene2 (object): A pytorch model Returns: None """ keys1 = list(gene1.state_dict()) keys2 = list(gene2.state_dict()) for key in keys1: if key not in keys2: continue # References to the variable tensors W1 = gene1.state_dict()[key] W2 = gene2.state_dict()[key] if len(W1.shape) == 2: #Weights no bias num_variables = W1.shape[0] # Crossover opertation [Indexed by row] try: num_cross_overs = fastrand.pcg32bounded( int(num_variables * 0.3)) # Number of Cross overs except: num_cross_overs = 1 for i in range(num_cross_overs): receiver_choice = random.random( ) # Choose which gene to receive the perturbation if receiver_choice < 0.5: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W1[ind_cr, :] = W2[ind_cr, :] else: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W2[ind_cr, :] = W1[ind_cr, :] elif len(W1.shape) == 1: #Bias or LayerNorm if random.random() < 0.8: continue #Crossover here with low frequency num_variables = W1.shape[0] # Crossover opertation [Indexed by row] #num_cross_overs = fastrand.pcg32bounded(int(num_variables * 0.05)) # Crossover number for i in range(1): receiver_choice = random.random( ) # Choose which gene to receive the perturbation if receiver_choice < 0.5: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W1[ind_cr] = W2[ind_cr] else: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W2[ind_cr] = W1[ind_cr]
def reset(self): self.initCount = fastrand.pcg32bounded(self.count_n) + 1 self.count = self.initCount seq_len = fastrand.pcg32bounded(self.seq_n) + 1 self.input = [] for i in range(seq_len): # https://stackoverflow.com/a/46820635 self.input += [1 if random.random() < 0.5 else -1] self.input_len = len(self.input) self.num_steps = self.seq_n * 2 + self.count self.curr_step = 1 return np.array([self.input[0], 0])
def mutate_inplace(gene): ''' Mutates a neural network policy (in place) todo: what are the parameters ''' mut_strength = 0.1 num_mutation_frac = 0.1 super_mut_strength = 10 super_mut_prob = 0.05 reset_prob = super_mut_prob + 0.05 num_params = len(list(gene.parameters())) # ssne_probabilities = np.random.uniform(0, 1, num_params) * 2 model_params = gene.state_dict() for i, key in enumerate(model_params): # Mutate each param if key == 'lnorm1.gamma' or key == 'lnorm1.beta' or key == 'lnorm2.gamma' or key == 'lnorm2.beta' or key == 'lnorm3.gamma' or key == 'lnorm3.beta': continue # References to the variable keys W = model_params[key] if len(W.shape) == 2: # Weights, no bias num_weights = W.shape[0] * W.shape[1] ssne_prob = 1 # ssne_probabilities[i] if random.random() < ssne_prob: num_mutations = fastrand.pcg32bounded( int(math.ceil( num_mutation_frac * num_weights))) # Number of mutation instances for _ in range(num_mutations): ind_dim1 = fastrand.pcg32bounded(W.shape[0]) ind_dim2 = fastrand.pcg32bounded(W.shape[-1]) random_num = random.random() if random_num < super_mut_prob: # Super Mutation probability W[ind_dim1, ind_dim2] += random.gauss( 0, super_mut_strength * W[ind_dim1, ind_dim2]) elif random_num < reset_prob: # Reset probability W[ind_dim1, ind_dim2] = random.gauss(0, 1) else: # mutauion even normal W[ind_dim1, ind_dim2] += random.gauss( 0, mut_strength * W[ind_dim1, ind_dim2]) # Regularization hard limit W[ind_dim1, ind_dim2] = regularize_weight( W[ind_dim1, ind_dim2], 1000000)
def pcgchoice(data, size=1): if size == 1: if isinstance(data, int): return pcg32bounded(data) return data[pcg32bounded(len(data))] else: d = list(range(len(data))) if not isinstance(data, int) else list(range(data)) idx = [ d.pop(pcg32bounded(len(d))) for i in range(size) ] if isinstance(data, (np.ndarray, np.matrix)): return data[idx] elif isinstance(data, int): return idx else: return [ data[i] for i in idx ]
def mutate_inplace(self, gene): # gene.setflags(write=1) mut_strength = 0.1 num_mutation_frac = 0.1 super_mut_strength = 10 super_mut_prob = 0.05 reset_prob = super_mut_prob + 0.05 num_params = len(list(gene)) ssne_probabilities = np.random.uniform(0, 1, num_params) * 2 # model_params = copy.deepcopy(gene)# gene model_params = gene for i, key in enumerate(model_params): #Mutate each param if key == 'lnorm1.gamma' or key == 'lnorm1.beta' or key == 'lnorm2.gamma' or key == 'lnorm2.beta' or key == 'lnorm3.gamma' or key == 'lnorm3.beta': continue # References to the variable keys W = model_params[key] # W.setflags(write=1) if len(W.shape) == 2: #Weights, no bias num_weights = W.shape[0] * W.shape[1] ssne_prob = ssne_probabilities[i] if random.random() < ssne_prob: num_mutations = fastrand.pcg32bounded( int(math.ceil( num_mutation_frac * num_weights))) # Number of mutation instances for _ in range(num_mutations): ind_dim1 = fastrand.pcg32bounded(W.shape[0]) ind_dim2 = fastrand.pcg32bounded(W.shape[-1]) random_num = random.random() if random_num < super_mut_prob: # Super Mutation probability W[ind_dim1, ind_dim2] += random.gauss( 0, super_mut_strength * W[ind_dim1, ind_dim2]) elif random_num < reset_prob: # Reset probability W[ind_dim1, ind_dim2] = random.gauss(0, 1) else: # mutauion even normal W[ind_dim1, ind_dim2] += random.gauss( 0, mut_strength * W[ind_dim1, ind_dim2]) # Regularization hard limit W[ind_dim1, ind_dim2] = self.regularize_weight( W[ind_dim1, ind_dim2], 1000000)
def selection_tournament(self, index_rank, num_offsprings, tournament_size): """Conduct tournament selection Parameters: index_rank (list): Ranking encoded as net_indexes num_offsprings (int): Number of offsprings to generate tournament_size (int): Size of tournament Returns: offsprings (list): List of offsprings returned as a list of net indices """ total_choices = len(index_rank) offsprings = [] for i in range(num_offsprings): winner = np.min( np.random.randint(total_choices, size=tournament_size)) offsprings.append(index_rank[winner]) offsprings = list(set(offsprings)) # Find unique offsprings if len(offsprings) % 2 != 0: # Number of offsprings should be even offsprings.append(offsprings[fastrand.pcg32bounded( len(offsprings))]) return offsprings
def int(limit): try: return fastrand.pcg32bounded(limit) except: if limit == 0: return 0 raise
def reset(self): self.input = [] self.curr_step = 1 # Ability to set constant number of ones num_of_ones = self.N if (self.N == 0): num_of_ones = fastrand.pcg32bounded(11)+4 for i in range(num_of_ones): # https://stackoverflow.com/a/46820635 self.input += [1 if random.random() < 0.5 else -1] if (self.gap_size): self.input += [0]*self.gap_size else: self.input += [0]*(fastrand.pcg32bounded(10)+10) self.input += [1 if random.random() < 0.5 else -1] self.input_len = len(self.input) return np.array([self.input[0]])
def randint(start, end): """ Faster version of random.randint """ v = fastrand.pcg32bounded(end) if v < start: v += start if v > end: return (v + start) / 2 return v
def classic_parameter_mutation(self, network): mut_strength = self.cfg.mutation.mutation_sd num_mutation_frac = 0.1 super_mut_strength = 10 super_mut_prob = 0.05 reset_prob = super_mut_prob + 0.05 model_params = network.state_dict() potential_keys = [] for i, key in enumerate(model_params): # Mutate each param if not 'norm' in key: W = model_params[key] if len(W.shape) == 2: # Weights, no bias potential_keys.append(key) how_many = np.random.randint(1, len(potential_keys) + 1, 1)[0] chosen_keys = np.random.choice(potential_keys, how_many, replace=False) for key in chosen_keys: # References to the variable keys W = model_params[key] num_weights = W.shape[0] * W.shape[1] # Number of mutation instances num_mutations = fastrand.pcg32bounded( int(np.ceil(num_mutation_frac * num_weights))) for _ in range(num_mutations): ind_dim1 = fastrand.pcg32bounded(W.shape[0]) ind_dim2 = fastrand.pcg32bounded(W.shape[-1]) random_num = self.rng.uniform(0, 1) if random_num < super_mut_prob: # Super Mutation probability W[ind_dim1, ind_dim2] += self.rng.normal( 0, np.abs(super_mut_strength * W[ind_dim1, ind_dim2])) elif random_num < reset_prob: # Reset probability W[ind_dim1, ind_dim2] = self.rng.normal(0, 1) else: # mutauion even normal W[ind_dim1, ind_dim2] += self.rng.normal( 0, np.abs(mut_strength * W[ind_dim1, ind_dim2])) # Regularization hard limit W[ind_dim1, ind_dim2] = self.regularize_weight(W[ind_dim1, ind_dim2], 1000000) return network
def mutate_inplace(self, gene): mut_strength = 0.1 num_mutation_frac = 0.1 super_mut_strength = 10 super_mut_prob = 0.05 reset_prob = super_mut_prob + 0.05 model_params = np.array(gene.get_weights()) num_params = model_params.shape[0] * model_params.shape[ 1] * model_params[0].shape[0] #len(list(gene.parameters())) ssne_probabilities = np.random.uniform(0, 1, num_params) * 2 #the shape of the matrix of the model (n_layers, n_matrix_each_layer) --> i.e (4,2)4 layers with 2matrices each layers #need to check whether its weight matrix or bias matrix for layer in model_params: for W in layer: if len( W.shape ) == 2: #Weights, no bias, the original implementation doesn't mutate the bias matrix num_weights = W.shape[0] * W.shape[1] ssne_prob = ssne_probabilities[i] if random.random() < ssne_prob: num_mutations = fastrand.pcg32bounded( int(math.ceil( num_mutation_frac * num_weights))) # Number of mutation instances for _ in range(num_mutations): ind_dim1 = fastrand.pcg32bounded(W.shape[0]) ind_dim2 = fastrand.pcg32bounded(W.shape[-1]) random_num = random.random() if random_num < super_mut_prob: # Super Mutation probability W[ind_dim1, ind_dim2] += random.gauss( 0, super_mut_strength * W[ind_dim1, ind_dim2]) elif random_num < reset_prob: # Reset probability W[ind_dim1, ind_dim2] = random.gauss(0, 1) else: # mutauion even normal W[ind_dim1, ind_dim2] += random.gauss( 0, mut_strength * W[ind_dim1, ind_dim2]) # Regularization hard limit W[ind_dim1, ind_dim2] = self.regularize_weight( W[ind_dim1, ind_dim2], 1000000)
def epoch(self, pop, fitness_evals): # Entire epoch is handled with indices; Index rank nets by fitness evaluation (0 is the best after reversing) index_rank = self.list_argsort(fitness_evals); index_rank.reverse() elitist_index = index_rank[:self.num_elitists] # Elitist indexes safeguard # Selection step offsprings = self.selection_tournament(index_rank, num_offsprings=len(index_rank) - self.num_elitists, tournament_size=3) # Figure out unselected candidates unselects = []; new_elitists = [] for i in range(self.population_size): if i in offsprings or i in elitist_index: continue else: unselects.append(i) random.shuffle(unselects) #COMPUTE RL_SELECTION RATE if self.rl_policy != None: #RL Transfer happened self.selection_stats['total'] += 1.0 if self.rl_policy in elitist_index: self.selection_stats['elite'] += 1.0 elif self.rl_policy in offsprings: self.selection_stats['selected'] += 1.0 elif self.rl_policy in unselects: self.selection_stats['discarded'] += 1.0 self.rl_policy = None # Elitism step, assigning elite candidates to some unselects for i in elitist_index: try: replacee = unselects.pop(0) except: replacee = offsprings.pop(0) new_elitists.append(replacee) self.clone(master=pop[i], replacee=pop[replacee]) # Crossover for unselected genes with 100 percent probability if len(unselects) % 2 != 0: # Number of unselects left should be even unselects.append(unselects[fastrand.pcg32bounded(len(unselects))]) for i, j in zip(unselects[0::2], unselects[1::2]): off_i = random.choice(new_elitists); off_j = random.choice(offsprings) self.clone(master=pop[off_i], replacee=pop[i]) self.clone(master=pop[off_j], replacee=pop[j]) self.crossover_inplace(pop[i], pop[j]) # Crossover for selected offsprings for i, j in zip(offsprings[0::2], offsprings[1::2]): if random.random() < self.args.crossover_prob: self.crossover_inplace(pop[i], pop[j]) # Mutate all genes in the population except the new elitists for i in range(self.population_size): if i not in new_elitists: # Spare the new elitists if random.random() < self.args.mutation_prob: self.mutate_inplace(pop[i]) return new_elitists[0]
def next_g(self, actors, all_fitness): index_rank = self.list_argsort(all_fitness) index_rank.reverse() elitist_index = index_rank[:self.num_elitists] offsprings = self.selection_tournament(index_rank, num_offsprings=len(index_rank) - self.num_elitists, tournament_size=self.tournament_size) losers = [] for i in range(self.population_size): if i in offsprings or i in elitist_index: continue else: losers.append(i) random.shuffle(losers) if self.rl_policy_index != None: self.selection_stats['total'] += 1.0 if self.rl_policy_index in elitist_index: self.selection_stats['elite'] += 1.0 elif self.rl_policy_index in offsprings: self.selection_stats['selected'] += 1.0 elif self.rl_policy_index in losers: self.selection_stats['discarded'] += 1.0 self.rl_policy_index = None new_elitists = [] for i_elitist in elitist_index: try: replace = losers.pop(0) except: replace = offsprings.pop(0) new_elitists.append(replace) self.hard_clone(master_actor=actors[i_elitist], replace_actor=actors[replace]) if len(losers) % 2 != 0: losers.append(losers[fastrand.pcg32bounded(len(losers))]) for i, j in zip(losers[0::2], losers[1::2]): off_i = random.choice(new_elitists) off_j = random.choice(offsprings) self.hard_clone(master_actor=actors[off_i], replace_actor=actors[i]) self.hard_clone(master_actor=actors[off_j], replace_actor=actors[j]) self.crossover(actors[i], actors[j]) for i, j in zip(offsprings[0::2], offsprings[1::2]): if random.random() < self.crossover_prob: self.crossover(actors[i], actors[j]) for i in range(self.population_size): if i not in new_elitists: if random.random() < self.mutation_prob: self.mutate(actors[i]) return new_elitists[0]
def _create_new_sick_nodes(G, sick_nodes, showing, prob): for node in sick_nodes: # Go through each contact for edge in G[node]: if edge not in sick_nodes and edge not in showing: if fastrand.pcg32bounded(1000)/1000 < prob: G.node[edge]['COVID-19'] = 1 sick_nodes.append(edge) return(G, sick_nodes)
def selection_tournament(self, index_rank, num_offsprings, tournament_size): total_choices = len(index_rank) offsprings = [] for i in range(num_offsprings): winner = np.min(np.random.randint(total_choices, size=tournament_size)) offsprings.append(index_rank[winner]) offsprings = list(set(offsprings)) # Find unique offsprings if len(offsprings) % 2 != 0: # Number of offsprings should be even offsprings.append(offsprings[fastrand.pcg32bounded(len(offsprings))]) return offsprings
def randInt(start, stop): """returns a random int in the range [start, stop) Args: start (int): start of interval, inclusive stop (int): end of interval, exclusive Returns: int: random int """ return fastrand.pcg32bounded(stop - start) + start
def crossover(self, actor1, actor2): ori_parameters1, ori_parameters2 = actor1.get_parameters(), actor2.get_parameters() for param1, param2 in zip(ori_parameters1, ori_parameters2): if len(param1.shape) == 2: # weight num_variables = param1.shape[0] num_cross_overs = fastrand.pcg32bounded(num_variables * 2) for i in range(num_cross_overs): if random.random() < 0.5: ind_cr = fastrand.pcg32bounded(param1.shape[0]) param1[ind_cr, :] = param2[ind_cr, :] else: ind_cr = fastrand.pcg32bounded(param1.shape[0]) param2[ind_cr, :] = param1[ind_cr, :] elif len(param1.shape) == 1: # bias num_variables = param1.shape[0] num_cross_overs = fastrand.pcg32bounded(num_variables) for i in range(num_cross_overs): if random.random() < 0.5: ind_cr = fastrand.pcg32bounded(param1.shape[0]) param1[ind_cr] = param2[ind_cr] else: ind_cr = fastrand.pcg32bounded(param1.shape[0]) param2[ind_cr] = param1[ind_cr] actor1.set_parameters(ori_parameters1) actor2.set_parameters(ori_parameters2)
def crossover_inplace(self, gene1, gene2): for (k1, v1), (k2, v2) in zip(gene1.items(), gene2.items()): W1 = v1 W2 = v2 if len(W1.shape) == 2: #Weights no bias num_variables = W1.shape[0] # Crossover opertation [Indexed by row] num_cross_overs = fastrand.pcg32bounded( num_variables * 2) # Lower bounded on full swaps for i in range(num_cross_overs): receiver_choice = random.random( ) # Choose which gene to receive the perturbation if receiver_choice < 0.5: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W1[ind_cr, :] = W2[ind_cr, :] else: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W2[ind_cr, :] = W1[ind_cr, :] elif len(W1.shape) == 1: #Bias num_variables = W1.shape[0] # Crossover opertation [Indexed by row] num_cross_overs = fastrand.pcg32bounded( num_variables) # Lower bounded on full swaps for i in range(num_cross_overs): receiver_choice = random.random( ) # Choose which gene to receive the perturbation if receiver_choice < 0.5: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # # print("ind_cr,",ind_cr) W1[ind_cr] = W2[ind_cr] else: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # # print("ind_cr,", ind_cr) W2[ind_cr] = W1[ind_cr]
def crossover_inplace(self, gene1, gene2): for param1, param2 in zip(gene1.parameters(), gene2.parameters()): # References to the variable tensors W1 = param1.data W2 = param2.data if len(W1.shape) == 2: #Weights no bias num_variables = W1.shape[0] # Crossover opertation [Indexed by row] num_cross_overs = fastrand.pcg32bounded(num_variables * 2) # Lower bounded on full swaps for i in range(num_cross_overs): receiver_choice = random.random() # Choose which gene to receive the perturbation if receiver_choice < 0.5: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W1[ind_cr, :] = W2[ind_cr, :] else: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W2[ind_cr, :] = W1[ind_cr, :] elif len(W1.shape) == 1: #Bias num_variables = W1.shape[0] # Crossover opertation [Indexed by row] num_cross_overs = fastrand.pcg32bounded(num_variables) # Lower bounded on full swaps for i in range(num_cross_overs): receiver_choice = random.random() # Choose which gene to receive the perturbation if receiver_choice < 0.5: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W1[ind_cr] = W2[ind_cr] else: ind_cr = fastrand.pcg32bounded(W1.shape[0]) # W2[ind_cr] = W1[ind_cr]
def epoch(self, all_hives, fitness_evals): # Entire epoch is handled with indices; Index rank nets by fitness evaluation (0 is the best after reversing) index_rank = self.list_argsort(fitness_evals); index_rank.reverse() elitist_index = index_rank[:self.num_elitists] # Elitist indexes safeguard # Selection step offsprings = self.selection_tournament(index_rank, num_offsprings=len(index_rank) - self.num_elitists, tournament_size=3) #Extinction step (Resets all the offsprings genes; preserves the elitists) if random.random() < self.parameters.extinction_prob: #An extinction event print print "######################Extinction Event Triggered#######################" print for i in offsprings: if random.random() < self.parameters.extinction_magnituide and not (i in elitist_index): # Extinction probabilities self.reset_genome(all_hives[i]) # Figure out unselected candidates unselects = []; new_elitists = [] for i in range(self.population_size): if i in offsprings or i in elitist_index: continue else: unselects.append(i) random.shuffle(unselects) # Elitism step, assigning elite candidates to some unselects for i in elitist_index: replacee = unselects.pop(0) new_elitists.append(replacee) self.copy_individual(master=all_hives[i], replacee=all_hives[replacee]) # Crossover for unselected genes with 100 percent probability if len(unselects) % 2 != 0: # Number of unselects left should be even unselects.append(unselects[fastrand.pcg32bounded(len(unselects))]) for i, j in zip(unselects[0::2], unselects[1::2]): off_i = random.choice(new_elitists); off_j = random.choice(offsprings) self.copy_individual(master=all_hives[off_i], replacee=all_hives[i]) self.copy_individual(master=all_hives[off_j], replacee=all_hives[j]) self.crossover_inplace(all_hives[i], all_hives[j]) # Crossover for selected offsprings for i, j in zip(offsprings[0::2], offsprings[1::2]): if random.random() < self.parameters.crossover_prob: self.crossover_inplace(all_hives[i], all_hives[j]) # Mutate all genes in the population except the new elitists plus homozenize for i in range(self.population_size): if i not in new_elitists: # Spare the new elitists if random.random() < self.parameters.mutation_prob: self.mutate_inplace(all_hives[i])
def generate_list(): """ Get the length from user and generate a random array of that size """ upper_bound = int(input("-> Please choose the Upper Bound of the array: ")) size = int(input("-> Please choose the size of the array: ")) # return upperBound, size, np.random.randint(0, upperBound, size) rand_arr = [] for _ in range(size): rand_arr.append(pcg32bounded(upper_bound)) return upper_bound, size, rand_arr
def simulateBedpe(input_df, n, chromosome): sim_dfs = [] end = int(ranges[str(chromosome)]) for i in range(n): sim_df = input_df.copy() r=-1 for row in input_df.itertuples(): r = r+1 offset1 = int(row.end1) - int(row.start1) #this could be 0 or greater than 0 depending on the resolution of the caller offset2 = int(row.end2) - int(row.start2) #this could be 0 or greater than 0 depending on the resolution of the caller #new_coord = random.randint(end) #pick random start coordinate within chromosome #once you have the new start coordinate, where to put the end coordinate so that resolution and length is preserved? new_coord = end+1 while new_coord + row["length"] >= end or new_coord + offset1 + row["length"] >= end: new_coord = fastrand.pcg32bounded(end) #Fast random number generation in Python using PCG sim_df.iat[r, 1] = new_coord sim_df.iat[r, 2] = new_coord + offset1 sim_df.iat[r, 4] = new_coord + row["length"] sim_df.iat[r, 5] = new_coord + offset2 # if (row["svclass"] != "trans"): # if new_coord + row["length"] <= end: #if you don't go off end of chromosome # sim_df.iat[i, 1] = new_coord # sim_df.iat[i, 2] = new_coord + offset1 # sim_df.iat[i, 4] = new_coord + row["length"] # sim_df.iat[i, 5] = new_coord + offset2 # else: #you go off end of chromosome # sim_df.iat[i, 4] = new_coord # sim_df.iat[i, 5] = new_coord + offset2 # sim_df.iat[i, 1] = new_coord - row["length"] # sim_df.iat[i, 2] = new_coord + offset1 # else: #we are dealing with a translocation and the breakpoints are on another chromosome # if sim_df[sim_df.columns[0]].nunique() == 1 and row["chrom1"] != row["chrom2"]: # sim_df.iat[i, 1] = new_coord # sim_df.iat[i, 2] = new_coord + offset1 # else: # sim_df.iat[i, 4] = new_coord # sim_df.iat[i, 5] = new_coord + offset1 #recompute simulated lengths and make sure they are the same as actual lengths = [] for row in sim_df.itertuples(): lengths.append(abs(row.start1 - row.start2)) sim_df["length"] == lengths assert(list(input_df["length"] == sim_df["length"])) #sizes of events are the same assert(all(i > 0 for i in list(sim_df['start1']))) assert(all(i > 0 for i in list(sim_df['start2']))) sim_dfs.append(sim_df) return sim_dfs
def metropolis_step_removal(X, move_index=None): '''For a given state vector X, apply MC Metropolis move and update bookkeeping associated with counting grid''' global accepted, rejected, counting_grid # Original point if move_index == None: index = fastrand.pcg32bounded(N) else: index = move_index point_old = X[index].copy() counting_index_old = ( int(point_old[1] * grid_marks_y / Ly), int(point_old[0] * grid_marks_x / Lx) ) # the index that the particle appears in in the counting grid # Propose new move point_new = [0, 0] point_new[0] = (point_old[0] + step * (fastrand.pcg32() / 2147483647)) % Lx point_new[1] = (point_old[1] + step * (fastrand.pcg32() / 2147483647)) % Ly counting_index_new = (int(point_new[1] * grid_marks_y / Ly), int(point_new[0] * grid_marks_x / Lx)) # Use counting grid to check for nearest neighbours neighbour_indicies = [ neighbour for neighbours in extract_3X5(counting_grid, counting_index_new).flatten() for neighbour in neighbours ] # Determine whether overlap occurs # Here is the difference from old step. Move accepted if it's further away for point_index in neighbour_indicies: if torus_dist(point_new, X[point_index]) < 2 * r and point_index != index: rejected += 1 return X # Else accept move accepted += 1 X[index] = point_new # Determine whether grid counts need to be changed if counting_index_old != counting_index_new: counting_grid[counting_index_old].remove(index) counting_grid[counting_index_new].append(index) return X
def her_augmentation(self, buffer, k): her_buffer = [] buffer_dim = len(buffer) - 1 for _ in range(k): for i in range(len(buffer)): # Chooses an index in the future if buffer_dim == i: her_goal_index = i #Edge case of last experience else: her_goal_index = i + fastrand.pcg32bounded(buffer_dim - i) her_goal = buffer[her_goal_index][3][0:self.args.goal_dim] her_buffer.append(buffer[i][:]) her_buffer[-1][1] = her_goal return buffer + her_buffer
def run(self): drive = open(self.device, 'rb') timer = time() while self.bytes_read < self.bytes_needed: rand = fastrand.pcg32bounded(self.size) drive.seek(rand, 0) data = drive.read(512) if data != pattern * 512: print('Non-zero data has been failed. Erasure has failed.') self.success = False break self.CURRENT_DATA.emit((self.bytes_read / self.bytes_needed) * 100) self.CURRENT_TIME.emit(str(timedelta(seconds=(time() - timer)))) self.bytes_read += 512 drive.close()