def _compute_distribution(self, nearby_res): atoms = [] for r in nearby_res: atoms.extend(r.child_list) coordinates = np.zeros((len(atoms), 3)) counter = 0 for a in atoms: coordinates[counter] = a.get_coord() counter += 1 n = len(atoms) if self.number_of_samples != -1: first = coordinates[np.ix_(choice(n, self.number_of_samples))] second = coordinates[np.ix_(choice(n, self.number_of_samples))] dist = norm(first - second, axis=1) else: first = coordinates second = coordinates dist_pair = cdist(first, second) dist = dist_pair.reshape((dist_pair.size,)) dist = dist/np.max(dist) bins = np.linspace(0, 1, self.number_of_bins) indices = np.digitize(dist, bins) distribution = np.bincount(indices) distribution = self._normalize(distribution) return distribution[1:]
def sample_sentences(section, n=1000): pair_list = array(list(alignment_pairs(section=section))) pair_len = len(pair_list) plagiarized_sample = [] non_plagiarized_sample = [] # draw plagiarized sentences for pair in pair_list[randint(0, high=pair_len, size=n)]: gold = pair.gold_alignments() if len(gold) > 0: susp_draw, src_draw = gold[randint(0, high=len(gold))] plagiarized_sample.append((pair.susp_fn, pair.src_fn, susp_draw, src_draw)) else: logging.warn("Empty gold alignment for %s - %s" % (pair.susp_fn, pair.src_fn)) # draw random non-plagiarized sentence pairs for pair in pair_list[randint(0, high=pair_len, size=n)]: gold = pair.gold_alignments() gold_susp_indexes = [x[0] for x in gold] gold_src_indexes = [x[1] for x in gold] susp_draw = choice([x for x in xrange(len(pair.susp_doc)) if x not in gold_susp_indexes]) src_draw = choice([x for x in xrange(len(pair.src_doc)) if x not in gold_src_indexes]) non_plagiarized_sample.append((pair.susp_fn, pair.src_fn, susp_draw, src_draw)) return plagiarized_sample, non_plagiarized_sample
def stratified_sample(df, size=None, target_col=None): if not size or not target_col: raise ValueError target_counts = df[target_col].value_counts() total = sum(target_counts.values) stratified_counts = zip(target_counts.index.values, np.round(target_counts.values * size / total).astype(np.int)) sampled_dfs = [df[df[target_col] == target].iloc[choice(target_counts[target], c, replace=False)] for target, c in stratified_counts] return concat(sampled_dfs)
def _compute_distribution_(self, coordinates, center): n = (coordinates.shape[0]) if self.number_of_samples != -1: first = coordinates[np.ix_(choice(n, self.number_of_samples))] second = np.ones((self.number_of_samples, 3)) * center else: first = coordinates second = np.ones(coordinates.shape) * center dist = norm(first - second, axis=1) dist = dist / np.max(dist) bins = np.linspace(0, 1, self.number_of_bins) indices = np.digitize(dist, bins) distribution = np.bincount(indices) return self._normalize(distribution)[1:]
def generate_solution(self): # self.solution = [randint(0,1) for i in range(self.instance.num_variables)] #generates only zeros (wrong) self.solution = [0 for i in range(self.instance.num_variables)] #same as above # self.solution = random.random_integers(0,1,self.instance.num_variables) #generate [0,1] (right but slower) # self.solution = zeros(self.instance.num_variables, dtype="int") #generate zeros a = where(self.instance.hard_clauses) #indexes of hard clauses for i in nditer(a[0]): satisfied, possiblevars = self.check_satisfied_unsat_clause(self.instance.instance_matrix[i]) if not satisfied: self.solution[choice(possiblevars)] = 1 return
def mutate(self, pos, prob): if pos <= 0: assert False if pos > self.size: assert False if self.left and pos <= self.left.size: self.left.mutate(pos, prob) else: leftSize = 0 if self.left: leftSize = self.left.size if leftSize + 1 == pos: if random() < prob: if self.val in T: self.val = choice(T) else: if self.val == 'sin': self.val = 'cos' elif self.val == 'cos': self.val = 'sin' else: self.val = choice(F[:3]) else: self.right.mutate(pos - leftSize - 1, prob)
def decorate_room(game_map, rect): elements = [RoomDecoration.WATER_SHALLOW_CENTER, RoomDecoration.WATER_DEEP_CENTER] weights = [0.5, 0.5] decoration = choice(elements, p=weights) if decoration == RoomDecoration.WATER_SHALLOW_CENTER \ or decoration == RoomDecoration.WATER_DEEP_CENTER: tile_type = TileType.WATER_SHALLOW if decoration == RoomDecoration.WATER_DEEP_CENTER: tile_type = TileType.WATER_DEEP x1 = randint(rect.x1 + 2, rect.x2 - 1) x2 = randint(x1, rect.x2 - 1) y1 = randint(rect.y1 + 2, rect.y2 - 1) y2 = randint(y1, rect.y2 - 1) for x in range(x1, x2): for y in range(y1, y2): game_map.field[x][y].__init__(x, y, tile_type)
def get_random_possible_trait(monster, trait_list=None): if trait_list is None: trait_list = all_traits trait_list = list( filter( lambda trait: trait.meet_prerequisites(monster) and trait not in monster.traits , trait_list ) ) if not trait_list: return None return choice(trait_list)
def stratified_sample(df, size=None, target_col=None): if not size or not target_col: raise ValueError target_counts = df[target_col].value_counts() total = sum(target_counts.values) stratified_counts = zip( target_counts.index.values, np.round(target_counts.values * size / total).astype(np.int)) sampled_dfs = [ df[df[target_col] == target].iloc[choice(target_counts[target], c, replace=False)] for target, c in stratified_counts ] return concat(sampled_dfs)
def move_ant(self): available_edges = [ce for ce in self.current_vertex.connected_edges if ce is not None] if self.prev_edge: available_edges.remove(self.prev_edge) [available_edges.remove(e) for e in available_edges if e in self.edges_travelled] if len(available_edges) == 0 or len(self.edges_travelled) > 10000: self.is_stuck = True return weights = normalize(np.array([e.pheromone_strength for e in available_edges])) edge = choice(available_edges, p=weights) self.current_vertex = edge.a if self.current_vertex != edge.a else edge.b self.edges_travelled.append(edge) self.prev_edge = edge if self.current_vertex == self.start_vertex: self.at_target = True
def gibbs_sampling(self, n_topics, alpha, n_iterations): seed(0) # randomly assign topics to words self.word_topic_map = {w: randint(0, n_topics-1) for w in self.vocab} n_dt = [{t: 0 for t in range(n_topics)} for _ in range(len(self.corpus))] n_tw = [{w: 0 for w in self.vocab} for _ in range(n_topics)] for d_index in range(len(self.corpus)): d = self.corpus[d_index] for w in d: t = self.word_topic_map[w] n_dt[d_index][t] += 1 n_tw[t][w] += 1 for i in range(n_iterations): print("Iteration %d/%d (%f%%)..." % (i, n_iterations, 100 * i / float(n_iterations))) for d_index in range(len(self.corpus)): print("Document %d/%d (%f%%)..." % (d_index, len(self.corpus), 100 * d_index / float(len(self.corpus)))) d = self.corpus[d_index] for w in d: # i. remove current word from counts old_topic = self.word_topic_map[w] # TODO if n_dt[d_index][old_topic] == 0: print("oops dt", d_index, old_topic) else: n_dt[d_index][old_topic] -= 1 n_tw[old_topic][w] -= 1 # ii. estimate probabilities using 5.6, 5.7 word_topic_probs = [] for t in range(n_topics): p_t_d = float(n_dt[d_index][t] + alpha) / (sum(n_dt[d_index].values()) + n_topics * alpha) p_w_t = float(n_tw[t][w] + alpha) / (sum(n_tw[t].values()) + len(self.vocab) * alpha) word_topic_probs.append(p_w_t * p_t_d) # iii. assign w to a topic randomly word_topic_probs = [float(p) / sum(word_topic_probs) for p in word_topic_probs] self.word_topic_map[w] = choice(range(n_topics), p=word_topic_probs) # iv. increment counts accordingly topic = self.word_topic_map[w] n_tw[topic][w] += 1 n_dt[d_index][topic] += 1
def random_Butter(n=(5, 10), Wc=(0.1, 0.8), W1=(0.1, 0.5), W2=(0.5, 0.8), form=None, onlyEven=True, seed=None): """ Generate a n-th order Butterworh filters Parameters ---------- - n: (int) The order of the filter - Wc: used if btype is 'lowpass' or 'highpass' Wc is a tuple (min,max) for the cut frequency - W1 and W2: used if btype is ‘bandpass’, ‘bandstop’ W1 and W2 are tuple (min,max) for the two start/stop frequencies - form: (string) {None, ‘lowpass’, ‘highpass’, ‘bandpass’, ‘bandstop’}. Gives the type of filter. If None, the type is randomized - onlyEven: if True, only even order filter are generated - seed: if not None, indicates the seed toi use for the random part (in order to be reproductible, the seed is stored in the name of the filter) """ # change the seed if asked if seed: numpy_seed(seed) # choose a form if asked if form is None: form = choice(("lowpass", "highpass", "bandpass", "bandstop")) # choose Wn if form in ("bandpass", "bandstop"): # choose 2 frequencies if W2[1] <= W1[0]: raise ValueError("iter_random_Butter: W1 should be lower than W2") Wn1 = (W1[1] - W1[0]) * random_sample() + W1[0] Wn2 = (W2[1] - W2[0]) * random_sample() + W2[0] while Wn2 <= Wn1: Wn2 = (W2[1] - W2[0]) * random_sample() + W2[0] W = [Wn1, Wn2] else: # choose 1 frequency W = (Wc[1] - Wc[0]) * random_sample() + Wc[0] # choose order order = randint(*n) if onlyEven and order % 2 == 0: order += 1 return Butter(order, W, form, name='Butterworth-random-%d' % seed)
def apply_random_stats(self, monster): stats = ['strength', 'dexterity', 'constitution', 'constitution'] weights = [ self.weight_strength, self.weight_dexterity, self.weight_constitution, self.weight_intelligence ] sum_weights = sum(weights) weights = list(map(lambda weight: weight / sum_weights, weights)) for _ in range(self.stat_per_level): chosen_stat = choice(stats, p=weights) if chosen_stat == 'strength': monster.strength += 1 elif chosen_stat == 'dexterity': monster.dexterity += 1 elif chosen_stat == 'constitution': monster.constitution += 1 elif chosen_stat == 'intelligence': monster.intelligence += 1
def weighted_choice(self, islands, visited, probs=None): """ :type islands: numpy.ndarray :type probs: numpy.ndarray :type visited: set[int] :rtype: int """ full = {x for x in islands if x in self.islands and self.islands[x].r.capacity == self.islands[x].r.count} | visited if self.current_island.id == START_ISLAND: full |= self.ignore_start_islands mask = in1d(islands, list(full), invert=True) m = islands[mask] p = probs[mask] / probs[mask].sum() if probs is not None else None if len(m) > 0: c = choice(m, size=1, p=p) if len(c) > 0: return c[0]
def random_TF(n=(5, 10), Wc=(0.1, 0.8), W1=(0.1, 0.5), W2=(0.5, 0.8)): """Generate one n-th order stable butterworth filter ((num, den) of the transfer function)""" # choose a form form = choice(['lowpass', 'highpass', 'bandpass', 'bandstop']) # choose Wn if form in ("bandpass", "bandstop"): # choose 2 frequencies such that Wn2<=Wn1 Wn1 = (W1[1] - W1[0]) * random_sample() + W1[0] Wn2 = (W2[1] - W2[0]) * random_sample() + W2[0] while Wn2 <= Wn1: Wn2 = (W2[1] - W2[0]) * random_sample() + W2[0] W = [Wn1, Wn2] else: # choose 1 frequency W = (Wc[1] - Wc[0]) * random_sample() + Wc[0] # choose order order = randint(*n) num, den = butter(order, W, form) return num, den
def take_turn(self): abilities = list( filter(lambda ability: ability.meet_prerequisites(self.monster), self.monster.get_abilities())) ability_choices = [] ability_weights = [] for ability in abilities: ability_choices.append(ability) ability_weights.append(ability.get_weight(self.monster)) sum_weight = sum(ability_weights) if not ability_choices or sum_weight == 0: self.monster.rest_turn() return ability_weights = list( map(lambda weight: weight / sum_weight, ability_weights)) chosen_ability = choice(ability_choices, p=ability_weights) chosen_ability.use_ability(self.monster)
def test_equivalence(self, test_sul: SUL) -> Tuple[bool, Iterable]: A = list(test_sul.get_alphabet()) # Generate random walk paths over alphabet A paths = choice(A, size=(self.num_samples, self.max_depth)) counterexample = None for path in paths: self.sul.reset() test_sul.reset() self_output = self.sul.process_input(path) test_output = test_sul.process_input(path) if self_output != test_output: counterexample = tuple(path) break equivalent = counterexample is None return equivalent, counterexample
def mutate(self, pos, d): if pos <= 0: return self if pos > self.size: return self if self.left and pos <= self.left.size: self.left.mutate(pos, d - 1) else: leftSize = 0 if self.left: leftSize = self.left.size if leftSize + 1 == pos: if self.val in T: #if we are in a terminal node we have a value here so we change it #with another value/constant self.val = choice(T) else: #if it is not a terminal node we change the node creating a new one new_node = Node() new_node.init(d - 1) self.left = new_node else: self.right.mutate(pos - leftSize - 1, d - 1)
def _compute_distribution(self, nearby_res, center): atoms = [] for r in nearby_res: atoms.extend(r.child_list) coordinates = np.zeros((len(atoms), 3)) counter = 0 for a in atoms: coordinates[counter] = a.get_coord() counter += 1 n = len(atoms) if self.number_of_samples != -1: first = coordinates[np.ix_(choice(n, self.number_of_samples))] second = np.ones((self.number_of_samples, 3)) * center else: first = coordinates second = np.ones(coordinates.shape) * center dist = norm(first - second, axis=1) dist = dist/np.max(dist) bins = np.linspace(0, 1, self.number_of_bins) indices = np.digitize(dist, bins) distribution = np.bincount(indices) distribution = self._normalize(distribution) return distribution
def test_equivalence(self, test_sul: SUL) -> Tuple[bool, Iterable]: A = list(test_sul.get_alphabet()) # Generate random walk paths over alphabet A paths = choice(A, size=(self.num_samples, self.max_depth)) counterexample = None for path in paths: for i in range(1, len(path)): self.sul.reset() test_sul.reset() self_output = self.sul.process_input(path[0:i]) test_output = test_sul.process_input(path[0:i]) if not self.eq(self_output, test_output): counterexample = tuple(path[0:i]) break if counterexample is not None: break equivalent = counterexample is None return equivalent, (self_output, counterexample)
def __iter__(self): idx = choice(self.n, size=self.n, replace=False) slices = ((self.size*i, min(self.size + self.size*i, self.n)) for i in xrange(self.n_batch)) return ((self.X[idx[apply(range, s)]], self.y[idx[apply(range, s)]]) for s in slices)
def gibbs_sampling(self, alpha, n_iterations): n_topics = self.n_topics # initialize all counts to 0 n_d_t = [[0 for _ in range(n_topics)] for _ in range(len(self.corpus))] n_t_w = {w: [0 for _ in range(n_topics)] for w in self.vocab} # store intermediate values for estimating parameters doc_sums = [float(len(d) + n_topics * alpha) for d in self.corpus] topic_sums = [len(self.vocab) * alpha for _ in range(n_topics)] word_topic_map = [[None for _ in d] for d in self.corpus] # randomly assign topics to words, and set up initial counts for di in range(len(self.corpus)): d = self.corpus[di] for wi in range(len(d)): w = d[wi] t = randint(0, n_topics) word_topic_map[di][wi] = t n_t_w[w][t] += 1 n_d_t[di][t] += 1 topic_sums[t] += 1 for i in range(n_iterations): for di in range(len(self.corpus)): pct_done = 100 * (i * len(self.corpus) + di) / float(len(self.corpus) * n_iterations) print("Iteration %d/%d, Document %d/%d.... (%2.2f%%)" % \ (i, n_iterations, di, len(self.corpus), pct_done)) d = self.corpus[di] for wi in range(len(d)): w = d[wi] # i. remove current word from counts old_topic = word_topic_map[di][wi] n_d_t[di][old_topic] -= 1 n_t_w[w][old_topic] -= 1 topic_sums[old_topic] -= 1 # ii. estimate probabilities using 5.6, 5.7 word_topic_probs = [] for t1 in range(n_topics): p_t_d = (n_d_t[di][t1] + alpha) / doc_sums[di] p_w_t = (n_t_w[w][t1] + alpha) / topic_sums[t1] word_topic_probs.append(p_t_d * p_w_t) s = float(sum(word_topic_probs)) word_topic_probs = [p/s for p in word_topic_probs] # iii. assign w to a topic randomly new_topic = choice(list(range(n_topics)), p=word_topic_probs) word_topic_map[di][wi] = new_topic # iv. increment counts accordingly n_d_t[di][new_topic] += 1 n_t_w[w][new_topic] += 1 topic_sums[new_topic] += 1 # finalize parameters for di in range(len(self.corpus)): self.p_t_d.append([(n_d_t[di][t1] + alpha) / doc_sums[di] for t1 in range(n_topics)]) for w in self.vocab: self.p_w_t[w] = [(n_t_w[w][t1] + alpha) / topic_sums[t1] for t1 in range(n_topics)] # also store counts self.n_t_w = n_t_w
def predict(self, examples: np.ndarray) -> List[int]: return choice(range(self.n_classes), size=len(examples), replace=True, p=self.weights_)
import pickle import pickle problem = "Problem12" cache = 'cache' path = f'../rers/TrainingSeqReachRers2019/{problem}/{problem}' n = 100000 w = 100 trie_cached_sul = TrieCache(RERSSOConnector(f'{path}.so'), storagepath=cache) dict_cached_sul = DictCache(RERSSOConnector(f'{path}.so'), storagepath=cache) # Generate a bunch of random keys alphabet = trie_cached_sul.get_alphabet() quers = choice(alphabet, (n, w)) queries = [] for i in range(n): queries.append(tuple(quers[i])) print("done generating") queries = iter(queries) trie_mem = [] dict_mem = [] count = 0 ns = [10, 100, 1000, 10000] ns = range(0, 100000, 10000) for run in ns: while count < run: query = next(queries)
def make_map(game_map, player=None): rooms = [] num_rooms = 0 player_placed = False for r in range(max_rooms): w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) x = randint(0, game_map.width - w - 1) y = randint(0, game_map.height - h - 1) new_room = Rect(x, y, w, h) for other_room in rooms: if new_room.intersect(other_room): break else: create_room(game_map, new_room) number_monsters_to_generate = int( (randint(0, int((w - 1) * (h - 1) * monsters_per_room_max_ratio)) + randint(0, int((w - 1) * (h - 1) * monsters_per_room_max_ratio))) / 2) number_items_to_generate = int( (randint(0, int((w - 1) * (h - 1) * items_per_room_max_ratio)) + randint(0, int((w - 1) * (h - 1) * items_per_room_max_ratio))) / 2) for _ in range(number_monsters_to_generate): monster_x = randint(new_room.x1 + 1, new_room.x2 - 1) monster_y = randint(new_room.y1 + 1, new_room.y2 - 1) if game_map.can_walk_at(monster_x, monster_y): if player_placed is False and player is not None: game_map.add_monster(monster_x, monster_y, player.entity) player_placed = True else: game_map.add_monster(monster_x, monster_y) for _ in range(number_items_to_generate): item_x = randint(new_room.x1 + 1, new_room.x2 - 1) item_y = randint(new_room.y1 + 1, new_room.y2 - 1) if game_map.can_walk_at(item_x, item_y, False): game_map.add_random_item(item_x, item_y) if num_rooms != 0: prev = rooms[num_rooms - 1] possible_walls_begin = [] possible_walls_end = [] if prev.x2 <= new_room.x1 + 1: possible_walls_begin.append('left') possible_walls_end.append('right') elif prev.x1 + 1 >= new_room.x2: possible_walls_begin.append('right') possible_walls_end.append('left') if prev.y2 <= new_room.y1 + 1: possible_walls_begin.append('top') possible_walls_end.append('bottom') elif prev.y1 + 1 >= new_room.y2: possible_walls_begin.append('bottom') possible_walls_end.append('top') begin_wall = choice(possible_walls_begin) end_wall = choice(possible_walls_end) if begin_wall == 'left': begin_x = new_room.x1 begin_y = randint(new_room.y1 + 1, new_room.y2 - 1) elif begin_wall == 'right': begin_x = new_room.x2 begin_y = randint(new_room.y1 + 1, new_room.y2 - 1) elif begin_wall == 'top': begin_y = new_room.y1 begin_x = randint(new_room.x1 + 1, new_room.x2 - 1) else: begin_y = new_room.y2 begin_x = randint(new_room.x1 + 1, new_room.x2 - 1) if end_wall == 'left': end_x = prev.x1 end_y = randint(prev.y1 + 1, prev.y2 - 1) elif end_wall == 'right': end_x = prev.x2 end_y = randint(prev.y1 + 1, prev.y2 - 1) elif end_wall == 'top': end_y = prev.y1 end_x = randint(prev.x1 + 1, prev.x2 - 1) else: end_y = prev.y2 end_x = randint(prev.x1 + 1, prev.x2 - 1) if randint(0, 1) == 1: continue_carving = create_h_tunnel(game_map, begin_x, end_x, begin_y) if continue_carving: create_v_tunnel(game_map, begin_y, end_y, end_x) else: continue_carving = create_v_tunnel(game_map, begin_y, end_y, begin_x) if continue_carving: create_h_tunnel(game_map, begin_x, end_x, end_y) rooms.append(new_room) num_rooms += 1
def get_random_class_for_monster(monster): possible_classes = [] for monster_class in all_classes: possible_classes.append(monster_class) return choice(possible_classes)
def get_random_item_type(level): types = [ItemType.POTION, ItemType.WEAPON, ItemType.SHIELD, ItemType.BODY] weights = [0.55, 0.20, 0.10, 0.15] return choice(types, p=weights)
def notify_of_added_delegating_node(self, potential_delegations): self.delegations.append(choice(potential_delegations))