def selection(self, population): selection = [] out = [] for p in population: touple = [] touple.append(p) touple.append(self.get_word_value(p)) selection.append(touple) if self.check_if_in_dict(p): if self.get_word_value( p) >= self.best and self.can_create_from_letters(p): if p not in out: out.append(p) out.append(''.join(random_permutation(p))) self.best = self.get_word_value(p) self.best_word = p Sort(selection) i = 0 while len(out) < 10: if selection[len(selection) - i - 1][0] not in out: out.append(selection[len(selection) - i - 1][0]) i += 1 return out
def random_split_windows( window, start: int = 3, end: int = 10, select_prob: float = 0.7 ): set_range = random_window((start, end)) yield from random_permutation( random_sample(select_prob, set_range(window)) )
def test_full_permutation(self): """ensure every item from the iterable is returned in a new ordering 15 elements have a 1 in 1.3 * 10e12 of appearing in sorted order, so we fix a seed value just to be sure. """ i = range(15) r = mi.random_permutation(i) self.assertEqual(set(i), set(r)) if i == r: raise AssertionError("Values were not permuted")
def take_unchanged(mating_group: t.List[t.Tuple[GraphIndividual, Record]], brood_size: int) -> t.List[GraphIndividual]: """ Randomly takes `brood_size` number of individuals from the mating groups. :param mating_group: A group of individuals selected to give progeny. :param brood_size: A number of offsprings. :return: List of offsprings -- copies of the parents. """ individuals, _ = unzip(mating_group) return list( take(brood_size, (ind.copy() for ind in random_permutation(individuals))))
def recombine_into(mating_group: t.List[t.Tuple[GraphIndividual, Record]], brood_size: int) -> t.List[GraphIndividual]: """ Take all genes and distribute them into progeny. For two individuals with N genes and `brood_size=1`, the single offspring will have 2N genes. :param mating_group: A group of individuals selected to give progeny. :param brood_size: A number of offsprings. :return: List of offsprings. """ pool = random_permutation( chain.from_iterable(ind.genes() for ind, _ in mating_group)) chunks = distribute(brood_size, pool) return _init_chunks(mating_group, chunks)
def recombine_genes_uniformly(mating_group: t.List[t.Tuple[Individual, Record]], brood_size: int) -> t.List[Individual]: """ Combines genes of individuals in the `mating_group` in a single pool, and uniformly divides these genes into `brood_size` number of individuals. :param mating_group: A group of individuals selected to give progeny. :param brood_size: A number of offsprings. :return: List of offsprings. """ pool = random_permutation( chain.from_iterable(ind.genes() for ind, _ in mating_group)) chunks = take(brood_size, distribute(len(mating_group), pool)) return _init_chunks(mating_group, chunks)
def get_proxies(): url = 'https://free-proxy-list.net/' response = requests.get(url) parser = fromstring(response.text) proxies = set() for i in parser.xpath('//tbody/tr')[:10]: if i.xpath('.//td[7][contains(text(),"yes")]'): # Grabbing IP and corresponding PORT proxy = ":".join( [i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]]) proxies.add(proxy) return random_permutation(list(proxies))
def create_population(self): population = [] for e in self.origin_setup: population.append(e) while len(population) < self.population_size: r = random.uniform(0, 1) if r > 0.5: population.append( self.create_word(random.randint(0, self.max_word_length))) else: population.append(''.join( random_permutation(random.choice(self.origin_setup)))) self.best = self.get_word_value(population[0]) return population
def exchange_fraction(mating_group: t.List[t.Tuple[GraphIndividual, Record]], brood_size: int, fraction: float = 0.1) -> t.List[GraphIndividual]: """ Takes `fraction` of genes from each Individual. Aggregates all taken fractions into a single pool. Samples from the pool the same number of genes an Individual has donated. :param mating_group: A group of individuals selected to give progeny. :param brood_size: A number of offsprings. :param fraction: A fraction of genes to take from an Individual. :return: List of offsprings. """ staged_comb, staged_pool = tee( ((ind, floor(len(ind) * fraction)) for ind, _ in mating_group), 2) pool_samples, samples = tee( (take(n, ind.genes()) for ind, n in staged_pool), 2) pool = random_permutation(chain.from_iterable(pool_samples)) recombined = (ind.copy().remove_genes(s).add_genes(take(n, pool)) for (ind, n), s in zip(staged_comb, samples)) return list(take(brood_size, recombined))
def test_partial_permutation(self): """ensure all returned items are from the iterable, that the returned permutation is of the desired length, and that all items eventually get returned. Sampling 100 permutations of length 5 from a set of 15 leaves a (2/3)^100 chance that an item will not be chosen. Multiplied by 15 items, there is a 1 in 2.6e16 chance that at least 1 item will not show up in the resulting output. Using a random seed will fix that. """ items = range(15) item_set = set(items) all_items = set() for _ in range(100): permutation = mi.random_permutation(items, 5) self.assertEqual(len(permutation), 5) permutation_set = set(permutation) self.assertLessEqual(permutation_set, item_set) all_items |= permutation_set self.assertEqual(all_items, item_set)
def generate_buffer_from_lbl(self, X, cls_label, ses_label, random=False, truncate=False): buff_fill = 0 buff_lbl = [] buff_ind = [] if random: iter = more_itertools.random_permutation(\ more_itertools.unique_everseen(itertools.izip(cls_label, ses_label))) else: iter = self.iters['cls'] if self.buff_size > X.shape[0]: buff_ind = [] for i in iter: bloc_length = X[(cls_label == i[0]) & (ses_label == i[1]), :].shape[0] ind = np.where((cls_label == i[0]) & (ses_label == i[1]))[0] buff_ind.append([i[0], i[1], buff_fill, buff_fill+bloc_length, ind[0], ind[-1]+1]) else: for i in iter: bloc_length = X[(cls_label == i[0]) & (ses_label == i[1]), :].shape[0] ind = np.where((cls_label == i[0]) & (ses_label == i[1]))[0] if bloc_length > self.buff_size: # process the current buffer first if any if buff_fill > 0: buff_ind.append(buff_lbl) if truncate: # fill a new buffer the truncated segment buff_lbl = [] buff_lbl.append([i[0], i[1], 0, self.buff_size, ind[0], ind[0]+self.buff_size]) buff_ind.append(buff_lbl) # empty buffer and continue processing buff_fill = 0 buff_lbl = [] else: bloc_left = bloc_length while bloc_left > self.buff_size: buff_lbl = [] buff_lbl.append([i[0], i[1], 0, self.buff_size, ind[-1]+1 - bloc_left, ind[-1]+1 - bloc_left+self.buff_size]) print bloc_left, buff_lbl buff_ind.append(buff_lbl) bloc_left -= self.buff_size buff_lbl = [] buff_lbl.append([i[0], i[1], 0, bloc_left, ind[-1]+1 - bloc_left, ind[-1]+1]) buff_fill = bloc_left print bloc_left, buff_lbl else: if buff_fill + bloc_length <= self.buff_size: buff_lbl.append([i[0], i[1], buff_fill, buff_fill+bloc_length, ind[0], ind[-1]+1]) buff_fill = buff_fill+bloc_length else: buff_ind.append(buff_lbl) buff_lbl = [] buff_lbl.append([i[0], i[1], 0, bloc_length, ind[0], ind[-1]+1]) buff_fill = bloc_length if buff_fill > 0: buff_ind.append(buff_lbl) return buff_ind
def random_permute_generator(iterable, n): for _ in range(n): yield mit.random_permutation(iterable)
def random_permute_generator(iterable, n=10): """Yield a random permuation of an iterable n times.""" for _ in range(n): yield mit.random_permutation(iterable)
from more_itertools import random_permutation UppercaseLetter1 = chr(randint(65, 90)) #print(UppercaseLetter1) UppercaseLetter2 = chr(randint(65, 90)) #print(UppercaseLetter2) LowercaseLetter1 = chr(randint(65, 90)).lower() #print(LowercaseLetter1) LowercaseLetter2 = chr(randint(65, 90)).lower() #print(LowercaseLetter2) digit1 = str(randint(0, 9)) #print(digit1) digit2 = str(randint(0, 9)) #print(digit2) punctuationSign1 = chr(randint(33, 39)) #print(punctuationSign1) punctuationSign2 = chr(randint(33, 39)) #print(punctuationSign2) password = str(UppercaseLetter1 + UppercaseLetter2 + LowercaseLetter1 + LowercaseLetter2 + digit1 + digit2 + punctuationSign1 + punctuationSign2) print(''.join(random_permutation(password)))
def sys_schedule(self, algo=lambda l: list(mit.random_permutation(l))[-1]): # In modern Linux kernels, schedule mustn't be called from any user processes, so it is no schedule function in the API # default algo: random scheduling proc = algo(self.processes) self.self_proc = proc return proc
def parse(self, df: pd.DataFrame) -> Iterable[Journey]: groups = (group for _, group in df.groupby(self.user_column, sort=False)) groups = more_itertools.random_permutation( groups) # randomly shuffle users return map(self.extract, groups)