def get_move(self, board): legal_moves = board.get_legal_moves(self.mark) do_explore = random_choice([True,False], 1, p=[self.epsilon, 1 - self.epsilon]) if do_explore: # epsilon greedy method. Choosing random exploratory move choice = random_choice(legal_moves) choice = self.get_value(choice) if self.last_move: self.last_move.set_exploratory(True) else: # get list of all states with the highest value best_states = self.get_best_states(legal_moves) # randomly choose a state if there are ties in value choice = random_choice(best_states) # set up link to previous move choice.set_previous_move(self.last_move) self.last_move = choice # returns chosen move return choice
def add_noise_to_string(self,a_string): """Adds aritificial random noise to a string, returns a list of strings with noise added""" CHARS = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .") incorrectVersions=[] origString=a_string for i in range(random.randrange(1,4)): a_string=origString onehop=random.randrange(1,3) for _ in range(onehop): j=random.randrange(1,5) if j==1 and len(a_string)>0: # Replace a character with a random character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + random_choice(CHARS[:-1]) + a_string[random_char_position + 1:] elif j==2 and len(a_string)>0: # Delete a character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + a_string[random_char_position + 1:] elif j==3: # Add a random character if len(a_string)>0: random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + random_choice(CHARS[:-1]) + a_string[random_char_position:] else: a_string=random_choice(CHARS[:-1]) elif len(a_string)>1: # Transpose 2 characters random_char_position = random_randint(len(a_string) - 1) a_string = (a_string[:random_char_position] + a_string[random_char_position + 1] + a_string[random_char_position] + a_string[random_char_position + 2:]) incorrectVersions.append(a_string) return incorrectVersions
def add_noise_to_string( a_string, amount_of_noise): # Add artificial spelling mistakes to string from numpy.random import choice as random_choice, randint as random_randint, seed as random_seed, rand CHARS = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .") if rand() < amount_of_noise * len(a_string): # Replace a character with a random character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + random_choice( CHARS[:-1]) + a_string[random_char_position + 1:] if rand() < amount_of_noise * len(a_string): # Delete a character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + a_string[ random_char_position + 1:] if len(a_string) < MAX_INPUT_LEN and rand( ) < amount_of_noise * len(a_string): # Add a random character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + random_choice( CHARS[:-1]) + a_string[random_char_position:] if rand() < amount_of_noise * len(a_string): # Transpose 2 characters random_char_position = random_randint(len(a_string) - 1) a_string = (a_string[:random_char_position] + a_string[random_char_position + 1] + a_string[random_char_position] + a_string[random_char_position + 2:]) return a_string
def add_noise_to_string(a_string, amount_of_noise): """Add some artificial spelling mistakes to the string""" if rand() < amount_of_noise * len(a_string): # Replace a character with a random character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + random_choice( CHARS[:-1]) + a_string[random_char_position + 1:] if rand() < amount_of_noise * len(a_string): # Delete a character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + a_string[ random_char_position + 1:] if len(a_string) < MAX_INPUT_LEN and rand( ) < amount_of_noise * len(a_string): # Add a random character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + random_choice( CHARS[:-1]) + a_string[random_char_position:] if rand() < amount_of_noise * len(a_string): # Transpose 2 characters random_char_position = random_randint(len(a_string) - 1) a_string = (a_string[:random_char_position] + a_string[random_char_position + 1] + a_string[random_char_position] + a_string[random_char_position + 2:]) return a_string
def add_noise_to_sentence(sentence, amount_of_noise): """ Add artificial spelling mistakes to string :param sentence: list of words :param amount_of_noise: constant from 0 to 1 which show amount of mistakes :return: list of words with mistakes """ CHARS = list("abcdefghijklmnopqrstuvwxyz") if rand() < amount_of_noise * len(sentence): # Replace a character with a random character random_word_position = random_randint(len(sentence)) if len(sentence[random_word_position]): random_char_position = random_randint( len(sentence[random_word_position])) sentence[random_word_position] = sentence[ random_word_position][:random_char_position] + random_choice( CHARS[:-1] ) + sentence[random_word_position][random_char_position + 1:] if rand() < amount_of_noise * len(sentence): # Delete a character random_word_position = random_randint(len(sentence)) if len(sentence[random_word_position]) > 1: random_char_position = random_randint( len(sentence[random_word_position])) sentence[random_word_position] = sentence[random_word_position][:random_char_position] + \ sentence[random_word_position][random_char_position + 1:] if rand() < amount_of_noise * len(sentence): # Add a random character random_word_position = random_randint(len(sentence)) if len(sentence[random_word_position]): random_char_position = random_randint( len(sentence[random_word_position])) sentence[random_word_position] = sentence[ random_word_position][:random_char_position] + random_choice( CHARS[:-1] ) + sentence[random_word_position][random_char_position:] if rand() < amount_of_noise * len(sentence): # Transpose 2 characters random_word_position = random_randint(len(sentence)) if len(sentence[random_word_position]) > 1: random_char_position = random_randint( len(sentence[random_word_position]) - 1) sentence[random_word_position] = sentence[random_word_position][:random_char_position] + \ sentence[random_word_position][random_char_position + 1] + \ sentence[random_word_position][random_char_position] + \ sentence[random_word_position][random_char_position + 2:] return sentence
def revise_cluster_membership(self, robot_cluster_index): mean_belief_state = [ np.sum(self.clusters_belief_state[cluster_index] ["cells_belief_state"]) for cluster_index in self.clusters_belief_state.keys() ] variance_belief_state = [ np.sum( self.clusters_belief_state[cluster_index]["cells_belief_state"] * (1 - self.clusters_belief_state[cluster_index] ["cells_belief_state"])) for cluster_index in self.clusters_belief_state.keys() ] payoff = np.array( mean_belief_state ) + self.regularization_weight * np.array(variance_belief_state) smith_protocol = [ np.maximum(p - payoff[robot_cluster_index], 0) / self.revision_rate for p in payoff ] for cluster_index in self.clusters_belief_state.keys(): if (self.clusters_belief_state[cluster_index] ["robot_population_state"] >= self.robot_population_state_limit[cluster_index]): smith_protocol[cluster_index] = 0 smith_protocol[robot_cluster_index] = 1 - np.sum(smith_protocol) new_robot_cluster_index = random_choice(a=range( self.number_of_clusters), p=smith_protocol) return new_robot_cluster_index
def swap_misclassified_chars(self, a_string, nIterations=5): """Given a string (word), returns a list of strings of length nIterations, where characters in each string is replaced by another character with probability according to confusion matrix distribution """ mistakesDist = pd.read_csv(self.cmDist, index_col=0).T population = list(mistakesDist.columns.values) new_string = '' incorrectVersions = [] for _ in range(nIterations): for char in a_string: if char not in population: # char could be ' ' or other special characters like "!" new_string += char continue # Randomly replace existing character with new character according to distribution new_char = random_choice(population, size=None, replace=True, p=list(mistakesDist[char])) #if new_char != char: # print('[INFO] Replacing {} with {}'.format(char, new_char)) new_string += new_char incorrectVersions.append(new_string) new_string = '' return incorrectVersions
def add_noise_to_string(a_string, amount_of_noise): """Add some artificial spelling mistakes to the string""" if rand() < amount_of_noise * len(a_string): # Replace a character with a random character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + random_choice(CHARS[:-1]) + a_string[random_char_position + 1:] if rand() < amount_of_noise * len(a_string): # Delete a character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + a_string[random_char_position + 1:] if len(a_string) < MAX_INPUT_LEN and rand() < amount_of_noise * len(a_string): # Add a random character random_char_position = random_randint(len(a_string)) a_string = a_string[:random_char_position] + random_choice(CHARS[:-1]) + a_string[random_char_position:] if rand() < amount_of_noise * len(a_string): # Transpose 2 characters random_char_position = random_randint(len(a_string) - 1) a_string = (a_string[:random_char_position] + a_string[random_char_position + 1] + a_string[random_char_position] + a_string[random_char_position + 2:]) return a_string
def getMaxAction_test(self, state): """ Return the action with the maximal value for the given state. """ values = self.getActionValues_test(state) maxvalue = values[argmax(values)] nb_max_states = (maxvalue == values).sum() if nb_max_states > 1: # if several actions give the same Q value max_indices = [ i for i in range(len(values)) if values[i] == maxvalue ] chosen_index = random_choice(max_indices) return chosen_index
def getMaxAction(self, state): """ Return the action with the maximal value for the given state. """ values = self.getActionValues(state) chosen_index = argmax(values) maxvalue = values[chosen_index] nb_max_states = (maxvalue == values).sum() if nb_max_states > 1: # if several actions give the same Q value max_indices = [ i for i in range(len(values)) if values[i] == maxvalue ] chosen_index = random_choice(max_indices) print("Number of max states: ", nb_max_states, "chosen index: ", chosen_index) return chosen_index
def assign_destination(self, robot_cluster_index): cells_belief_state = np.array( self.clusters_belief_state[robot_cluster_index] ["cells_belief_state"]) number_of_cells = cells_belief_state.shape[0] payoff = cells_belief_state + self.regularization_weight * cells_belief_state * ( 1 - cells_belief_state) payoff_softmax = softmax(payoff / self.noise_level) cell_index = random_choice(a=range(number_of_cells), p=payoff_softmax) cell_center = self.clusters_belief_state[robot_cluster_index][ "cells_center"][cell_index] return cell_center
def revise_cluster_membership_model_based(self, robot_cluster_index): mean_belief_state = [ np.sum(self.clusters_belief_state[cluster_index] ["cells_belief_state"]) for cluster_index in self.clusters_belief_state.keys() ] variance_belief_state = [ np.sum( self.clusters_belief_state[cluster_index]["cells_belief_state"] * (1 - self.clusters_belief_state[cluster_index] ["cells_belief_state"])) for cluster_index in self.clusters_belief_state.keys() ] payoff = np.array( mean_belief_state ) + self.regularization_weight * np.array(variance_belief_state) for cluster_index in range(self.number_of_clusters): a = self.model_parameters[cluster_index]["a"] b = self.model_parameters[cluster_index]["b"] object_relocation_rate = self.model_parameters[cluster_index][ "object_relocation_rate"] payoff[cluster_index] += self.model_weight * ( a * self.clusters_belief_state[cluster_index] ["robot_population_state"]**b + object_relocation_rate ) # robot_population_state is the number of robots smith_protocol = [ np.maximum(p - payoff[robot_cluster_index], 0) / self.revision_rate for p in payoff ] for cluster_index in self.clusters_belief_state.keys(): if (self.clusters_belief_state[cluster_index] ["robot_population_state"] >= self.robot_population_state_limit[cluster_index]): smith_protocol[cluster_index] = 0 smith_protocol[robot_cluster_index] = 1 - np.sum(smith_protocol) new_robot_cluster_index = random_choice(a=range( self.number_of_clusters), p=smith_protocol) return new_robot_cluster_index
def create_pairs(self,dataset): inputLines=[] outputLines=[] for line in dataset: incorrectSentences=[] minWords=min(len(line.split(" ")),3) wordList=random_choice(line.split(" "),minWords,False) for word in line.split(" "): if word in wordList: #incorrectWords=self.add_noise_to_string(word) incorrectWords=self.swap_misclassified_chars(word) else: incorrectWords=[word] if len(incorrectSentences)==0: incorrectSentences=incorrectWords else: newSentences=[] for x in itertools.product(incorrectSentences,incorrectWords): newSentences.append(' '.join(x)) incorrectSentences=list(set(newSentences)) #incorrectSentences = [self.add_mistakes(text=sent, tokenized=True) for sent in incorrectSentences] random.shuffle(incorrectSentences) # Cap max no. of incorrect sentences generated per sentence if len(incorrectSentences) > self.maxGenerations: incorrectSentences=incorrectSentences[:self.maxGenerations] # Cap each sentence till max length input_lines = [sent[:self.MAX_LENGTH] for sent in incorrectSentences] output_lines = [line[:self.MAX_LENGTH]]*len(incorrectSentences) inputLines+=input_lines outputLines+=output_lines # zip and sort according to length for buckecting lines = zip(inputLines,outputLines) # lines = sorted(lines, key=lambda x: len(x[1]), reverse=True) lines=list(lines) random.shuffle(lines) return lines
def __init__(self, pb_client, robot_id): self.pb_client = pb_client self.handlers = { "LOWER": self.lower, "GRAB": self.grab, "PLACE": self.place, "RELEASE": self.release, "ORIGIN": self.origin } self.handler = self.origin self.current_state = "NONE" self.robot_id = robot_id self.object_id = None self.constraint_id = None self.trash_volume_collected = random_choice(range(MAX_TRASH_VOLUME)) self.pb_robot_control = RobotControl(pb_client) self.pb_sensor_models = SensorModels(pb_client) self.arm_target_state = [0] * 4
def _vis_igraph(self, g, options, limit): nodes = [] node_type = options.get('node_type', '') vis_labels = options.get('vis_labels', {}) edge_type = options.get('edge_type', '') if limit: _g = g.subgraph(random_choice(g.vs.indices, limit, replace=False)) else: _g = g for v in _g.vs: node_label = v.attributes().get(node_type, '') vis_label = v.attributes().get(vis_labels.get(node_label, ''), '') nodes.append({ 'id': v.index, 'label': vis_label, 'group': node_label, 'title': repr(v) }) edges = [{ 'from': e.source, 'to': e.target, 'label': e.attributes().get(edge_type, '') } for e in _g.es] return nodes, edges
def start_shuffle(library): track = random_choice(library) set_track(track, p_url) play_playlist(p_id)
def get_referer(self): return random_choice(self._referers)
def add_noise_to_sentence(sentence, amount_of_noise): """ Add artificial spelling mistakes to string :param sentence: list of words :param amount_of_noise: constant from 0 to 1 which show amount of mistakes :return: list of words with mistakes """ CHARS = list("abcdefghijklmnopqrstuvwxyz") substitutions = { "a": ["a"], "b": ["b"], "c": ["c"], "d": ["d"], "e": ["e"], "f": ["f"], "g": ["g"], "h": ["h"], "i": ["i"], "j": ["j"], "k": ["k"], "l": ["l"], "m": ["m"], "n": ["n"], "o": ["o"], "p": ["p"], "q": ["q"], "r": ["r"], "s": ["s"], "t": ["t"], "u": ["u"], "v": ["v"], "w": ["w"], "x": ["x"], "y": ["y"], "z": ["z"], "A": ["A"], "B": ["B"], "C": ["C"], "D": ["D"], "E": ["E"], "F": ["F"], "G": ["G"], "H": ["H"], "I": ["I"], "J": ["J"], "K": ["K"], "L": ["L"], "M": ["M"], "N": ["N"], "O": ["O"], "P": ["P"], "Q": ["Q"], "R": ["R"], "S": ["S"], "T": ["T"], "U": ["U"], "V": ["V"], "W": ["W"], "X": ["X"], "Y": ["Y"], "Z": ["Z"], " ": [" "], ".": ["."] } if rand() < amount_of_noise * len(sentence): # Replace a character with a random character random_char_position = random_randint(len(sentence)) sentence = sentence[:random_char_position] + random_choice( substitutions[sentence[random_char_position]] ) + sentence[random_char_position + 1:] if rand() < amount_of_noise * len(sentence): # Delete a character random_char_position = random_randint(len(sentence)) sentence = sentence[:random_char_position] + sentence[ random_char_position + 1:] if rand() < amount_of_noise * len(sentence) and len(sentence) < 197: # Add a random character random_char_position = random_randint(len(sentence)) sentence = sentence[:random_char_position] + random_choice( CHARS[:-1]) + sentence[random_char_position:] if rand() < amount_of_noise * len(sentence): # Transpose 2 characters random_char_position = random_randint(len(sentence) - 1) sentence = sentence[:random_char_position] + sentence[random_char_position + 1] + \ sentence[random_char_position] + sentence[random_char_position + 2:] return sentence