def run(N, M, S): nseed(S) rseed(S) edges = set() start = randrange(0, N) cur = start # Inizia con un super-ciclo, tanto per farlo connesso for i in xrange(0, N): edges.add((i, (i+1)%N)) # E poi tutto il resto, a caso for i in xrange(N, M): next = randrange(0, N) while next == cur or is_in(edges, (cur, next)) or (i == M-1 and next == start): next = randrange(0, N) edges.add((cur, next)) cur = next stop = cur # Genera una permutazione dei vertici perm = range(N) shuffle(perm) print N, M, perm[start]+1, perm[stop]+1 for edge in edges: if randint(0, 1) == 0: print perm[edge[0]]+1, perm[edge[1]]+1 else: print perm[edge[1]]+1, perm[edge[0]]+1
def run(N, M, P, Q, C, S): global randseq nseed(S) rseed(S) last = 0 randseq = [] for i in xrange(M+1): last += randint(1,10) randseq += [last] shuffle( randseq ) #print N, M, P, Q, C, S origM = M M -= 2*(P+Q) e = eso(M, P, Q) seqs = [flatten(e)] for i in xrange( N-1 ): if randint(0,N) < C: e = eso(M, P, Q) else: e = gen_equiv( e ) seqs += [flatten(e)] seqs = list(set(seqs)) shuffle( seqs ) print len(seqs), origM for i in xrange(len(seqs)): print "%s" % seqs[i]
def run_hard(N, M, X0, X1, P, Seed): """ N: numero segmenti M: numero fontane X0: inizio linea X1: fine linea P: lunghezza percentuale del salto """ nseed(Seed) rseed(Seed) fountains, segments = line_test(N/3, M/3, X0, X1, randbetween(X0, X1), randbetween(1, MAXX), P) fountains2, segments2 = line_test(N/3-1, M/3, X0, X1, segments[-1][0], randbetween(1, MAXX), P) fountains3, segments3 = line_test(N/3-1, M/3, X0, X1, segments2[-1][0], randbetween(1, MAXX), P) F = fountains|fountains2|fountains3 S = segments+segments2+segments3 print len(S)-1, len(F) for s in S: print s[0], s[1] for f in F: print f[0], f[1]
def run(N, M, S): nseed(S) rseed(S) edges = [] start = 0 cur = start # Inizia con un super-ciclo, tanto per farlo connesso for i in xrange(0, N): edges.append((i, (i + 1) % N)) # E poi tutto il resto, a caso for i in xrange(N, M - 1): next = randrange(0, N) while next == cur: next = randrange(0, N) edges.append((cur, next)) cur = next edges.append((cur, 0)) # Genera una permutazione dei vertici perm = range(N) shuffle(perm) print N, M for edge in edges: print perm[edge[0]], perm[edge[1]]
def setup_env() -> argparse.Namespace: setup_logger() args = parse_arguments() # setup seed value if args.rand_seed == -1: myseed = 1 myseed = int(timer() * 1e9 % 2**32) else: myseed = args.rand_seed rseed(myseed) np.random.seed(myseed) # build command string to repeat this run # FIXME if an option is a flag this does not work, sorry recap = f"python3 mypy_ex.py" for a, v in args._get_kwargs(): if a == "rand_seed": recap += f" --rand_seed {myseed}" else: recap += f" --{a} {v}" logmain = logging.getLogger(f"c.{__name__}.setup_env") logmain.info(recap) return args
def run(N, S, M, A): nseed(S) rseed(S) print N # Decide quali elementi saranno presi, in modo che 1 sia nel # periodo o nell'antiperiodo numeri = range(1, N) shuffle(numeri) numeri = [0] + numeri # Crea un array di base che contiene numeri brutti per le # posizioni che non saranno mai raggiunte salti = [] for i in xrange(N): salti.append(randint(0, N-1)) # Modifica alcuni numeri in modo che venga compiuto il percorso # voluto for i in xrange(A+M-1): dist = numeri[i+1] - numeri[i] if dist < 0: dist += N salti[numeri[i]] = dist dist = numeri[A] - numeri[A+M-1] if dist < 0: dist += N salti[numeri[A+M-1]] = dist print " ".join(map(str, salti))
def run(N, S, M, A): nseed(S) rseed(S) print N # Decide quali elementi saranno presi, in modo che 1 sia nel # periodo o nell'antiperiodo numeri = range(1, N) shuffle(numeri) numeri = [0] + numeri # Crea un array di base che contiene numeri brutti per le # posizioni che non saranno mai raggiunte salti = [] for i in xrange(N): salti.append(randint(0, N - 1)) # Modifica alcuni numeri in modo che venga compiuto il percorso # voluto for i in xrange(A + M - 1): dist = numeri[i + 1] - numeri[i] if dist < 0: dist += N salti[numeri[i]] = dist dist = numeri[A] - numeri[A + M - 1] if dist < 0: dist += N salti[numeri[A + M - 1]] = dist print " ".join(map(str, salti))
def run(N, M, S): nseed(S) rseed(S) edges = set() start = randrange(0, N) cur = start # Inizia con un super-ciclo, tanto per farlo connesso for i in xrange(0, N): edges.add((i, (i + 1) % N)) # E poi tutto il resto, a caso for i in xrange(N, M): next = randrange(0, N) while next == cur or is_in(edges, (cur, next)) or (i == M - 1 and next == start): next = randrange(0, N) edges.add((cur, next)) cur = next stop = cur # Genera una permutazione dei vertici perm = range(N) shuffle(perm) print N, M, perm[start] + 1, perm[stop] + 1 for edge in edges: if randint(0, 1) == 0: print perm[edge[0]] + 1, perm[edge[1]] + 1 else: print perm[edge[1]] + 1, perm[edge[0]] + 1
def run(N, A, Mg, Mb, Mt, S): nseed(S) rseed(S) grafo = ugraph(N) grafo.addedges(Mg+Mb+Mt) grafo.shuffle() #Attrazioni attr = sample(xrange(1, N), A) #Lista degli archi archi = list(grafo) shuffle(archi) #Collegamenti gratuiti gratis = archi[:Mg] archi = archi[Mg:] shuffle(archi) #Collegamenti bus bus = archi[:Mb] archi = archi[Mb:] shuffle(archi) #Collegamenti traghetto trag = archi del archi print N, A, Mg, Mb, Mt for x in attr: print x for x in gratis: print x[0], x[1] for x in bus: print x[0], x[1] for x in trag: print x[0], x[1]
def shuffle(data, seed=None): if seed is not None: rseed(seed) questions = data[QUESTIONS] list(map(rshuffle, questions)) align(questions) list(map(rshuffle, questions))
def run(N, M, P, Q, C, S): global randseq nseed(S) rseed(S) last = 0 randseq = [] for i in xrange(M + 1): last += randint(1, 10) randseq += [last] shuffle(randseq) #print N, M, P, Q, C, S origM = M M -= 2 * (P + Q) e = eso(M, P, Q) seqs = [flatten(e)] for i in xrange(N - 1): if randint(0, N) < C: e = eso(M, P, Q) else: e = gen_equiv(e) seqs += [flatten(e)] seqs = list(set(seqs)) shuffle(seqs) print len(seqs), origM for i in xrange(len(seqs)): print "%s" % seqs[i]
def run_line(N, M, X0, X1, P, Seed): """ N: numero segmenti M: numero fontane X0: inizio linea X1: fine linea P: lunghezza percentuale del salto """ nseed(Seed) rseed(Seed) y = randbetween(1, MAXX) fountains = set((randbetween(X0, X1), y) for i in xrange(M)) while len(fountains) < M: fountains |= set( (randbetween(X0, X1), y) for i in xrange(M - len(fountains))) print N, M xi = randbetween(X0, X1) print xi, y for i in xrange(N): l1 = xi - X0 l2 = X1 - xi f = random() if f < float(l1) / (l1 + l2): d = int(float(l1 * P) / 100) xi = randbetween(X0, xi - d - 1) else: d = int(float(l2 * P) / 100) xi = randbetween(X1, xi + d + 1) print xi, y for f in fountains: print f[0], f[1]
def run_hard(N, M, X0, X1, P, Seed): """ N: numero segmenti M: numero fontane X0: inizio linea X1: fine linea P: lunghezza percentuale del salto """ nseed(Seed) rseed(Seed) fountains, segments = line_test(N / 3, M / 3, X0, X1, randbetween(X0, X1), randbetween(1, MAXX), P) fountains2, segments2 = line_test(N / 3 - 1, M / 3, X0, X1, segments[-1][0], randbetween(1, MAXX), P) fountains3, segments3 = line_test(N / 3 - 1, M / 3, X0, X1, segments2[-1][0], randbetween(1, MAXX), P) F = fountains | fountains2 | fountains3 S = segments + segments2 + segments3 print len(S) - 1, len(F) for s in S: print s[0], s[1] for f in F: print f[0], f[1]
def run(N, ST, D, S): nseed(S) rseed(S) transitions = [] characters = [0 for _ in xrange(N)] for i in xrange(N - 1, 0, -1): min_delta = -i max_delta = N - 1 - i good_characters = [] for j, t in enumerate(transitions): if min(t) >= min_delta and max(t) <= max_delta: good_characters += [j] choice = randint(0, len(good_characters) + 1) if choice == len(good_characters): # Aggiungi un nuovo elemento. transitions.append([ randint(max(min_delta, -D), min(max_delta, D)) for _ in xrange(ST) ]) characters[i] = len(transitions) - 1 else: characters[i] = good_characters[choice] characters[0] = randint(0, len(transitions)) C = len(transitions) print N, ST, C for cur_st in range(0, ST): for cur_c in range(0, C): print cur_st, cur_c, randint(0, ST), transitions[cur_c][cur_st] for i in xrange(0, N): print characters[i]
def haikunate(delimiter='-', tokenlength=4, tokenhex=False, tokenchars='0123456789', seed=None): """ Generate Heroku-like random names to use in your applications. :param delimiter: Delimiter :type delimiter: str :param tokenlength: TokenLength :type tokenlength: int :param tokenhex: TokenHex :type tokenhex: bool :param tokenchars: TokenChars :type tokenchars: str :param seed: Seed for random :type seed: str or int :returns: haikunated string :rtype: str """ if seed: rseed(seed) if tokenhex: tokenchars = '0123456789abcdef' adjective = choice(ADJECTIVES) noun = choice(NOUNS) token = ''.join(choice(tokenchars) for _ in range(tokenlength)) sections = [adjective, noun, token] return delimiter.join(filter(None, sections))
def run(N, A, Mg, Mb, Mt, S): nseed(S) rseed(S) grafo = ugraph(N) grafo.addedges(Mg + Mb + Mt) grafo.shuffle() #Attrazioni attr = sample(xrange(1, N), A) #Lista degli archi archi = list(grafo) shuffle(archi) #Collegamenti gratuiti gratis = archi[:Mg] archi = archi[Mg:] shuffle(archi) #Collegamenti bus bus = archi[:Mb] archi = archi[Mb:] shuffle(archi) #Collegamenti traghetto trag = archi del archi print N, A, Mg, Mb, Mt for x in attr: print x for x in gratis: print x[0], x[1] for x in bus: print x[0], x[1] for x in trag: print x[0], x[1]
def run_line(N, M, X0, X1, P, Seed): """ N: numero segmenti M: numero fontane X0: inizio linea X1: fine linea P: lunghezza percentuale del salto """ nseed(Seed) rseed(Seed) y = randbetween(1, MAXX) fountains = set((randbetween(X0, X1), y) for i in xrange(M)) while len(fountains)<M: fountains |= set((randbetween(X0, X1), y) for i in xrange(M-len(fountains))) print N, M xi = randbetween(X0, X1) print xi, y for i in xrange(N): l1 = xi - X0 l2 = X1 - xi f = random() if f < float(l1)/(l1+l2): d = int(float(l1 * P)/100) xi = randbetween(X0, xi - d - 1) else: d = int(float(l2 * P)/100) xi = randbetween(X1, xi + d + 1) print xi, y for f in fountains: print f[0], f[1]
def run_random(N, S): """Genera una stringa completamente casuale. """ nseed(S) rseed(S) print N print "".join(map(lambda x: choice("qwertyuiopasdfghjklzxcvbnm"), range(N)))
def getPoint(self, seed=None, **kwargs): #seed if requested if seed: rseed(seed) #Create total config return NetworkConfig(segmentconfigs=[ seg.config(**kwargs) for seg in self.__segments(**kwargs)])
def explore(self, count=1, seed=None, **kwargs): #seed once if required if seed: rseed(seed) #collect "count" number of points self.points=NetworkConfigs([self.getPoint(**kwargs) for _ in xrange(count)]) #return return self.points
def run(N, M, T, S): nseed(S) rseed(S) if T == 0: g = graph.dgraph(N) else: g = graph.dag(N) g.addedges(M) g.shuffle() print g
def run(N, S, P, T, X, Y, K, Seed): nseed(Seed) rseed(Seed) piano_stazioni = [randint(N) for i in xrange(P)] piano_stazioni.sort() piano_stazioni.reverse() stazioni=set() rect=[randint(X)+1,randint(X-1)+1,randint(Y)+1,randint(Y-1)+1] if rect[1] >= rect[0]: rect[1] += 1 if rect[3] >= rect[2]: rect[3] += 1 new=[randint(X)+1,randint(Y)+1] if K == 2: new=[rect[0],rect[2]] direction = randint(2) vertices = [[new[0], new[1]]] for i in xrange(N): old=list(new) direction = direction if (randint(4) == 0 and K != 2) else 1-direction if direction == 0: new[0]=randcentered(1,X,old[0] + (2*randint(2)-1)*(T*P/N)) if old[0] <= new[0]: new[0] += 1 if K == 2: new[0] = rect[0] if old[0] == rect[1] else rect[1] else: new[1]=randcentered(1,Y,old[1] + (2*randint(2)-1)*(T*P/N)) if old[1] <= new[1]: new[1] += 1 if K == 2: new[1] = rect[2] if old[1] == rect[3] else rect[3] vertices += [[ new[0], new[1]]] while (len(piano_stazioni) > 0 and piano_stazioni[-1] == i): piano_stazioni.pop() stazione_corrente = (randbetween(old[0],new[0]),randbetween(old[1],new[1])) for r in xrange(TRIES): if stazione_corrente in stazioni: stazione_corrente = (randbetween(old[0],new[0]),randbetween(old[1],new[1])) stazioni.add(stazione_corrente) for i in xrange(S-len(stazioni)): stazione_corrente = (randint(X)+1,randint(Y)+1) for r in xrange(TRIES): if stazione_corrente in stazioni: stazione_corrente = (randint(X)+1,randint(Y)+1) stazioni.add(stazione_corrente) stazioni = list(stazioni) print N, len(stazioni) for v in vertices: print v[0], v[1] shuffle(stazioni) for s in stazioni: print s[0], s[1]
def SMOTE(data=None, atleast=50, atmost=100, k=5, resample=False): "Synthetic Minority Oversampling Technique" def knn(a, b): return sorted(b, key=lambda F: euclidean(a[:-1], F[:-1])) def extrapolate(one, two): new = len(one) * [None] new[:-1] = [ min(a, b) + rand(0, 1) * (abs(a - b)) for a, b in zip(one[:-1], two[:-1]) ] new[-1] = int(one[-1]) return new def populate(data): newData = [] for _ in xrange(atleast): for one in data: neigh = knn(one, data)[1:k + 1] try: two = choice(neigh) except IndexError: two = one newData.append(extrapolate(one, two)) return [choice(newData) for _ in xrange(atleast)] def depopulate(data): if resample: newer = [] for _ in xrange(atmost): orig = choice(data) newer.append(extrapolate(orig, knn(orig, data)[1])) return newer else: return [choice(data).tolist() for _ in xrange(atmost)] newCells = [] rseed(1) klass = lambda df: df[df.columns[-1]] count = Counter(klass(data)) for u in count.keys(): if count[u] <= atleast: newCells.extend( populate([r for r in data.as_matrix() if r[-1] == u])) if count[u] >= atmost: newCells.extend( depopulate([r for r in data.as_matrix() if r[-1] == u])) else: newCells.extend( [r.tolist() for r in data.as_matrix() if r[-1] == u]) return pd.DataFrame(newCells, columns=data.columns)
def lista_pacientes(self): #return [Paciente(3) for i in range(1)] rseed(self.seed) cantidad_pacientes = [int(uniform(0, 4.75)), int(uniform(0, 4)), int(uniform(0.45, 4.34)), int(uniform(0, 2.89)), int(uniform(0, 5)), int(uniform(0.35, 4.44)), int(uniform(0, 5.28)), int(uniform(0, 4.24)), int(uniform(0, 4.12))] lista_pacientes = [] for i, cant in enumerate(cantidad_pacientes): for _ in range(cant): lista_pacientes.append(Paciente(i+1)) return lista_pacientes
def go(self): rseed(1) for planner in ['xtrees', 'cart', 'HOW', 'baseln0', 'baseln1']: out = [planner] predRows = [] train_DF = createTbl(self.train[self._n], isBin=True) test_df = createTbl(self.test[self._n], isBin=True) actual = np.array(Bugs(test_df)) before = self.pred(train_DF, test_df, tunings=self.tunedParams, smoteit=True) base = lambda X: sorted(X)[-1] - sorted(X)[0] newRows = lambda newTab: map(lambda Rows: Rows.cells[:-1], newTab._rows) after = lambda newTab: self.pred(train_DF, newTab, tunings=self.tunedParams , smoteit=True) frac = lambda aft: sum([0 if a < 1 else 1 for a in aft] ) / sum([0 if b < 1 else 1 for b in before]) predRows = [row.cells for predicted, row in zip(before , createTbl(self.test[self._n] , isBin=False)._rows) if predicted > 0] predTest = genTable(test_df, rows=predRows) for _ in xrange(self.reps): "Apply Different Planners" if planner == 'xtrees': newTab = xtrees(train=self.train[-1], test_DF=predTest, bin=False, majority=True).main() elif planner == 'cart' or planner == 'CART': newTab = xtrees(train=self.train[-1], test_DF=predTest, bin=False, majority=False).main() elif planner == 'HOW': newTab = HOW(train=self.train[-1], test=self.test[-1], test_df=predTest).main() elif planner == 'baseln0': newTab = strawman(train=self.train[-1], test=self.test[-1]).main() elif planner == 'baseln1': newTab = strawman(train=self.train[-1] , test=self.test[-1], prune=True).main() out.append(frac(after(newTab))) self.logResults(out) yield out
def run(K, N, C, S): nseed(S) rseed(S) print K print N l = genera(0, K, C) l1 = [] for i in xrange(N-C): j = randint(0, len(l)) l1 += [genint(l[j][0], l[j][1]+1)] l = l+l1 shuffle(l) for x, y in l: print x, y
def run(N, M, S): nseed(S) rseed(S) print N, M partenza=range(1,N+1) shuffle(partenza) for i in partenza: print i for i in range(M): idx=randint(1,N) print partenza[idx], partenza[idx-1] t=partenza[idx]; partenza[idx]=partenza[idx-1] partenza[idx-1]=t
def run(K, N, C, S): nseed(S) rseed(S) print K print N l = genera(0, K, C) l1 = [] for i in xrange(N - C): j = randint(0, len(l)) l1 += [genint(l[j][0], l[j][1] + 1)] l = l + l1 shuffle(l) for x, y in l: print x, y
def run(N, M, S): nseed(S) rseed(S) print N, M partenza = range(1, N + 1) shuffle(partenza) for i in partenza: print i for i in range(M): idx = randint(1, N) print partenza[idx], partenza[idx - 1] t = partenza[idx] partenza[idx] = partenza[idx - 1] partenza[idx - 1] = t
def run(T, K, N, C, S): nseed(S); rseed(S) print(T) print(K) print(N) if C > 0: l = covering_family(0,K,N,C) else: l = covering_family(0, K//2-randint(K//5), N//2, max(1,N//4)) l += covering_family(K//2+randint(K//5),K,(N+1)//2,max(1,N//4)) shuffle(l) assert len(l) == N for x, y in l: assert 0 <= x <= y < K print(x, y)
def __init__(self): """ Initializes the class. """ self.Helpers = Helpers("Data", False) self.dim = self.Helpers.confs["qnn"]["data"]["dim"] self.dir_train = self.Helpers.confs["qnn"]["data"]["dir_train"] self.seed = self.Helpers.confs["qnn"]["data"]["seed"] nseed(self.seed) rseed(self.seed) self.data = [] self.labels = [] self.paths = [] self.Helpers.logger.info("Data Helper Class initialization complete.")
def where(data): """ Recursive FASTMAP clustering. """ rseed(0) if isinstance(data, pd.core.frame.DataFrame): data = data.as_matrix() if not isinstance(data, np.ndarray): raise TypeError( 'Incorrect data format. Must be a pandas Data Frame, or a numpy nd-array.' ) N = np.shape(data)[0] clusters = [] norm = np.max(data, axis=0)[:-1] - np.min(data, axis=0)[:-1] def aDist(one, two): return np.sqrt( np.sum((np.array(one[:-1]) / norm - np.array(two[:-1]) / norm)**2)) def farthest(one, rest): return sorted(rest, key=lambda F: aDist(F, one))[-1] def recurse(dataset): R, C = np.shape(dataset) # No. of Rows and Col # Find the two most distance points. one = dataset[randi(0, R - 1)] mid = farthest(one, dataset) two = farthest(mid, dataset) # Project each case on def proj(test): a = aDist(one, test) b = aDist(two, test) c = aDist(one, two) return (a**2 - b**2 + c**2) / (2 * c) if R < np.sqrt(N): clusters.append(dataset) else: _ = recurse(sorted(dataset, key=lambda F: proj(F))[:int(R / 2)]) _ = recurse(sorted(dataset, key=lambda F: proj(F))[int(R / 2):]) recurse(data) return clusters
def run(N, R, S): nseed(S) rseed(S) print N out = []; new = randint(0,7) for i in range(R): old = new new = randint(0,7) if random() < 0.5: out.append([old,new]) else: out.append([new,old]) for i in range(R+1,N+1): out.append([randint(0,7),randint(0,7)]) shuffle(out) for i in out: print i[0], i[1]
def where(data): """ Recursive FASTMAP clustering. """ rseed(0) if isinstance(data, pd.core.frame.DataFrame): data = data.as_matrix() if not isinstance(data, np.ndarray): raise TypeError('Incorrect data format. Must be a pandas Raw_Data Frame, or a numpy nd-array.') N = np.shape(data)[0] clusters = [] norm = np.max(data, axis=0)[:-1] -np.min(data, axis=0)[:-1] def aDist(one, two): return np.sqrt(np.sum((np.array(one[:-1])/norm-np.array(two[:-1])/norm)**2)) def farthest(one,rest): return sorted(rest, key=lambda F: aDist(F,one))[-1] def recurse(dataset, step=0): R, C = np.shape(dataset) # No. of Rows and Col # Find the two most distance points. one=dataset[randi(0,R-1)] mid=farthest(one, dataset) two=farthest(mid, dataset) # Project each case on def proj(test): a = aDist(mid, test) b = aDist(two, test) c = aDist(mid, two) return (a**2-b**2+c**2)/(2*c) # if R < np.sqrt(N/2): # since we need 64 cells if R < 16: # since we need 64 cells clusters.append(dataset) else: _ = recurse(sorted(dataset,key=lambda F:proj(F))[:int(R/2)], step+1) _ = recurse(sorted(dataset,key=lambda F:proj(F))[int(R/2):], step+1) recurse(data) return clusters
def run(N, M, K, W, S): nseed(S) rseed(S) def wh(): return randint(1,W+1) out = ugraph(N,type='tree',w=wh) out.addedges(M-N+1) out.shuffle() air = sample( range(1,N+1), K ) C = randint(1, K) if len(air) > 1: while air[C] == 1: C = randint(1, K) print N, M, air[C], K for i in range(K): print air[i], print s = split(str(out), '\n') print join(s[1:-1], '\n')
def run_con_fibstr_cattiva(N, S): """Genera una stringa di Fibonacci e ci mette attorno roba casuale usando gli stessi due simboli. """ nseed(S) rseed(S) print N alphabet = set("qwertyuiopasdfghjklzxcvbnm") x = choice(list(alphabet)) alphabet.remove(x) y = choice(list(alphabet)) fibs = fibonacci(N) fiblen = randrange(3, len(fibs)) before = randint(0, N - fibs[fiblen]) after = N - fibs[fiblen] - before print "".join(map(lambda z: choice([x, y]), range(before))) + \ genera_fibstr(x, y, fiblen) + \ "".join(map(lambda z: choice([x, y]), range(after)))
def run_con_fibstr_cattiva(N, S): """Genera una stringa di Fibonacci e ci mette attorno roba casuale usando gli stessi due simboli. """ nseed(S) rseed(S) print N alphabet = set("qwertyuiopasdfghjklzxcvbnm") x = choice(list(alphabet)) alphabet.remove(x) y = choice(list(alphabet)) fibs = fibonacci(N) fiblen = randrange(3, len(fibs)) before = randint(0, N-fibs[fiblen]) after = N-fibs[fiblen]-before print "".join(map(lambda z: choice([x, y]), range(before))) + \ genera_fibstr(x, y, fiblen) + \ "".join(map(lambda z: choice([x, y]), range(after)))
def MLEPoissonPosteriorRatio (sample_number, burn, count1, count2): """MLE method to calculate ratio distribution of two Posterior Poisson distributions. MLE of Posterior Poisson is Gamma(k+1,1) if there is only one observation k. sample_number: number of sampling. It must be greater than burn, however there is no check. burn: number of samples being burned. count1: observed counts of condition 1 count2: observed counts of condition 2 return: list of log2-ratios """ rseed(1) ratios = pyarray('f',[]) ra = ratios.append for i in xrange(sample_number): x1 = rgamma(count1+1,1) x2 = rgamma(count2+1,1) ra( log(x1,2) - log(x2,2) ) return ratios[int(burn):]
def MLEPoissonPosteriorRatio(sample_number, burn, count1, count2): """MLE method to calculate ratio distribution of two Posterior Poisson distributions. MLE of Posterior Poisson is Gamma(k+1,1) if there is only one observation k. sample_number: number of sampling. It must be greater than burn, however there is no check. burn: number of samples being burned. count1: observed counts of condition 1 count2: observed counts of condition 2 return: list of log2-ratios """ rseed(1) ratios = pyarray('f', []) ra = ratios.append for i in xrange(sample_number): x1 = rgamma(count1 + 1, 1) x2 = rgamma(count2 + 1, 1) ra(log(x1, 2) - log(x2, 2)) return ratios[int(burn):]
def run(N, M, K, W, S): nseed(S) rseed(S) def wh(): return randint(1, W + 1) out = ugraph(N, type='tree', w=wh) out.addedges(M - N + 1) out.shuffle() air = sample(range(1, N + 1), K) C = randint(1, K) if len(air) > 1: while air[C] == 1: C = randint(1, K) print N, M, air[C], K for i in range(K): print air[i], print s = split(str(out), '\n') print join(s[1:-1], '\n')
def run(N, L, S): nseed(S) rseed(S) out = [] #Numero di indicazioni per ogni nazione indic_nazioni = list(multinomial(L - N, [1. / N] * N)) for i in range(len(indic_nazioni)): indic_nazioni[i] += 1 #Numero di ragazzi in ogni nazione ragazzi = [0] * N for i in range(N): for j in range(indic_nazioni[i]): temp = randint(1, MAXR / indic_nazioni[i]) ragazzi[i] += temp out.append("{0} {1}".format(i, temp if random() > .93 else temp - 1)) shuffle(out) print N, L for x in ragazzi: print x for x in out: print x
def main(argc, argv): t0 = time() seed = (argc > 1 and int(argv[1])) or 123456789 xmin = 1000000000000 ymin = 1000000000000 xmax = 5000000000000 ymax = 5000000000000 rseed(seed) for b in xrange(2, 64 + 1): for i in xrange(5000): x = rint(xmin, xmax) y = rint(ymin, ymax) assert(karatsuba_multiply(x, y, b) == (x * y)) t1 = time() dt = t1 - t0 print("[main] dt=%fs" % dt) return 0
def run(N, L, S): nseed(S) rseed(S) risolvi = random() > 0.5 print "risolvi:", risolvi print N, L out = [] #Numero di indicazioni per ogni nazione indic_nazioni = list(multinomial(L-N, [1./N]*N)) for i in range(len(indic_nazioni)): indic_nazioni[i] += 1 #Numero di ragazzi in ogni nazione ragazzi = [0]*N for i in range(N): for j in range(indic_nazioni[i]): temp = randint(1, MAXR/indic_nazioni[i]); ragazzi[i] += temp out.append("{0} {1}".format(i, temp if (risolvi or (random() > .85)) else temp-1)) shuffle(out) for x in ragazzi: print x for x in out: print x
def main(argc, argv): t0 = time() seed = (argc > 1 and int(argv[1])) or 123456789 xmin = 1000000000000 ymin = 1000000000000 xmax = 5000000000000 ymax = 5000000000000 rseed(seed) for b in xrange(2, 64 + 1): for i in xrange(5000): x = rint(xmin, xmax) y = rint(ymin, ymax) assert (karatsuba_multiply(x, y, b) == (x * y)) t1 = time() dt = t1 - t0 print("[main] dt=%fs" % dt) return 0
def run(R, C, S): nseed(S) rseed(S) #True se la parola e' presente risolvi = random() > 0.5 #La matrice con le lettere matr = [] for i in xrange(R): temp = [] for j in xrange(C): temp.append(choice(uppercase)) matr.append(temp) lung_parola = randint((R+C)/10+2, R+C if risolvi else (R+C)/2) parola = "" posr, posc = 0, 0 for i in xrange(lung_parola): parola += matr[posr][posc] if random() > 0.5: if posr < R-1: posr += 1 else: posc += 1 else: if posc < C-1: posc += 1 else: posr += 1 if not risolvi: for i in xrange(randint(0, (R+C)/2-2)): parola += choice(uppercase) print parola for i in xrange(R): temp = "" for j in xrange(C): temp += matr[i][j] print temp
def run(R, C, T, S): nseed(S) rseed(S) #La matrice con le lettere matr = [] for i in xrange(R): temp = [] for j in xrange(C): temp.append(choice(uppercase)) matr.append(temp) lung_parola = randint((R + C) / 10 + 2, R + C if T == 1 else (R + C) / 2) parola = "" posr, posc = 0, 0 for i in xrange(lung_parola): parola += matr[posr][posc] if random() > 0.5: if posr < R - 1: posr += 1 else: posc += 1 else: if posc < C - 1: posc += 1 else: posr += 1 if T == 0: for i in xrange(randint(0, (R + C) / 2 - 2)): parola += choice(uppercase) print R, C print parola for i in xrange(R): temp = "" for j in xrange(C): temp += matr[i][j] print temp
import os os.environ['PYTHONHASHSEED'] = '42' from numpy.random import seed, shuffle from random import seed as rseed from tensorflow.random import set_seed seed(42) rseed(42) set_seed(42) import random import pickle import shutil import models from utils import * from dataGenerator import * from datasetProcess import * from tensorflow.keras.models import load_model from tensorflow.keras.utils import plot_model from tensorflow.python.keras import backend as K import pandas as pd import argparse def evaluateEfficiency(args): #-------------------------------------------------- flops, params = get_flops(args) print("============================") print('fusion:', args.fusionType) print('lstm type:', args.lstmType) print('input mode:', args.mode) print('----------------------------')
def SMOTE(data=None, k=5, atleast=100, atmost=100, bugIndx=2, resample=False): def Bugs(tbl): cells = [i.cells[-bugIndx] for i in tbl._rows] return cells def minority(data): unique = list(set(sorted(Bugs(data)))) counts = len(unique) * [0] # set_trace() for n in xrange(len(unique)): for d in Bugs(data): if unique[n] == d: counts[n] += 1 return unique, counts def knn(one, two): pdistVect = [] # set_trace() for ind, n in enumerate(two): pdistVect.append([ind, euclidean(one.cells[:-1], n.cells[:-1])]) indices = sorted(pdistVect, key=lambda F: F[1]) return [two[n[0]] for n in indices] def extrapolate(one, two): new = one # set_trace() if bugIndx == 2: new.cells[3:-1] = [ max(min(a, b), min(min(a, b) + rand() * (abs(a - b)), max(a, b))) for a, b in zip(one.cells[3:-1], two.cells[3:-1]) ] new.cells[-2] = int(new.cells[-2]) else: new.cells[3:] = [ min(a, b) + rand() * (abs(a - b)) for a, b in zip(one.cells[3:], two.cells[3:]) ] new.cells[-1] = int(new.cells[-1]) return new def populate(data): newData = [] # reps = (len(data) - atleast) for _ in xrange(atleast): for one in data: neigh = knn(one, data)[1:k + 1] # If you're thinking the following try/catch statement is bad coding # etiquette i i . try: two = choice(neigh) except IndexError: two = one newData.append(extrapolate(one, two)) # data.extend(newData) return newData def depopulate(data): if resample: newer = [] for _ in xrange(atmost): orig = choice(data) newer.append(extrapolate(orig, knn(orig, data)[1])) return newer else: return [choice(data) for _ in xrange(atmost)] newCells = [] rseed(1) unique, counts = minority(data) rows = data._rows for u, n in zip(unique, counts): if n < atleast: newCells.extend(populate([r for r in rows if r.cells[-2] == u])) if n > atmost: newCells.extend(depopulate([r for r in rows if r.cells[-2] == u])) else: newCells.extend([r for r in rows if r.cells[-2] == u]) return clone(data, rows=[k.cells for k in newCells])
Parametri: * N (numero) * S (seed) Constraint: * 1 <= N <= %d """ % (MAXN) def run(N): print N print " ".join(map(str, [randint(-70, 99) for i in xrange(0, N * N)])) if __name__ == "__main__": if len(argv) != 3: print usage exit(1) N, S = map(int, argv[1:]) assert (1 <= N <= MAXN) # su seed non positivo copia un input di esempio dal .tex if S <= 0: print extract_input()[-S], exit(0) nseed(S) rseed(S) run(N)
division_points.sort() partition = [ division_points[i+1] - division_points[i] for i in xrange(M) ] for L in partition: interval = fill_interval(L,H) for a in interval: print a, if __name__ == "__main__": if len(argv) != 5: print usage exit(1) N, H, M, S = map(int, argv[1:]) # Finche' generiamo elementi distinti, non ha senso avere L<3. assert (1 <= N <= MAXN) assert (1 <= H <= MAXH) # su seed non positivo copia un input di esempio dal .tex if S <= 0: print extract_input(cmsbooklet=True)[-S], exit(0) nseed(S) rseed(S) run(N,H,M)
def seed(z): rseed(z) random.seed(z)
def _test(file='ant'): rseed(1) for file in ['ivy', 'lucene', 'jedit', 'poi', 'ant']: print('## %s\n' % (file)) R = [r for r in run(dataName=file, reps=10, _tuneit=False).go()] rdivDemo(R, isLatex=False)
def SMOTE(data=None, k=5, atleast=10, atmost=51, bugIndx=2, resample=False): def Bugs(tbl): cells = [i.cells[-bugIndx] for i in tbl._rows] return cells def minority(data): unique = list(set(sorted(Bugs(data)))) counts = len(unique) * [0] # set_trace() for n in xrange(len(unique)): for d in Bugs(data): if unique[n] == d: counts[n] += 1 return unique, counts def knn(one, two): pdistVect = [] # set_trace() for ind, n in enumerate(two): pdistVect.append([ind, euclidean(one.cells[:-1], n.cells[:-1])]) indices = sorted(pdistVect, key=lambda F: F[1]) return [two[n[0]] for n in indices] def extrapolate(one, two): new = one # set_trace() if bugIndx == 2: new.cells[3:-1] = [max(min(a, b), min(min(a, b) + rand() * (abs(a - b)), max(a, b))) for a, b in zip(one.cells[3:-1], two.cells[3:-1])] new.cells[-2] = int(new.cells[-2]) else: new.cells[3:] = [min(a, b) + rand() * (abs(a - b)) for a, b in zip(one.cells[3:], two.cells[3:])] new.cells[-1] = int(new.cells[-1]) return new def populate(data): newData = [] # reps = (len(data) - atleast) for _ in xrange(atleast): for one in data: neigh = knn(one, data)[1:k + 1] # If you're thinking the following try/catch statement is bad coding # etiquette i i . try: two = choice(neigh) except IndexError: two = one newData.append(extrapolate(one, two)) # data.extend(newData) return newData def depopulate(data): if resample: newer = [] for _ in xrange(atmost): orig = choice(data) newer.append(extrapolate(orig, knn(orig, data)[1])) return newer else: return [choice(data) for _ in xrange(atmost)] newCells = [] rseed(1) unique, counts = minority(data) rows = data._rows for u, n in zip(unique, counts): if n < atleast: newCells.extend(populate([r for r in rows if r.cells[-2] == u])) if n > atmost: newCells.extend(depopulate([r for r in rows if r.cells[-2] == u])) else: newCells.extend([r for r in rows if r.cells[-2] == u]) return clone(data, rows=[k.cells for k in newCells])