def random_lobster(n, p1, p2, seed=None): """Returns a random lobster graph. A lobster is a tree that reduces to a caterpillar when pruning all leaf nodes. A caterpillar is a tree that reduces to a path graph when pruning all leaf nodes; setting ``p2`` to zero produces a caterillar. Parameters ---------- n : int The expected number of nodes in the backbone p1 : float Probability of adding an edge to the backbone p2 : float Probability of adding an edge one level beyond backbone seed : int, optional Seed for random number generator (default=None). """ # a necessary ingredient in any self-respecting graph library if seed is not None: random.seed(seed) llen=int(2*random.random()*n + 0.5) L=path_graph(llen) L.name="random_lobster(%d,%s,%s)"%(n,p1,p2) # build caterpillar: add edges to path graph with probability p1 current_node=llen-1 for n in range(llen): if random.random()<p1: # add fuzzy caterpillar parts current_node+=1 L.add_edge(n,current_node) if random.random()<p2: # add crunchy lobster bits current_node+=1 L.add_edge(current_node-1,current_node) return L # voila, un lobster!
def resample(self): #print "Before resampling:" #ifor x in range(len(self.particles)): print self.particles[x].state," ", #iprint N=self.numParticles weights=[] new_particles=[] index = int(random.random() * N) maxW=0 for i in range(N): weights.append(self.particles[i].weight) beta = 0.0 mw = max(weights) for i in range(N): beta += random.random() * 2.0 * mw #print "beta =", beta while beta > weights[index]: beta -= weights[index] index = (index + 1) % N #print "\tbeta= %f, index = %d, weight = %f" % (beta, index, weights[index]) new_particles.append(self.particles[index])
def test_translational_alignment(self): """ Test the translational alignment in 2D routine """ random.seed() name=self.get_input_file_name("1z5s-projection-2.spi") srw = IMP.em2d.SpiderImageReaderWriter() image=IMP.em2d.Image() image.read(name,srw) translated=IMP.em2d.Image() # random translation trans=IMP.algebra.Vector2D(random.random()*10,random.random()*10) transformation = IMP.algebra.Transformation2D(trans) IMP.em2d.get_transformed(image.get_data(),translated.get_data(), transformation) fn_translated = self.get_input_file_name("translated.spi") # translated.write(fn_translated,srw) result=IMP.em2d.get_translational_alignment( image.get_data(),translated.get_data(),True) fn_aligned = self.get_input_file_name("trans_aligned.spi") # translated.write(fn_aligned,srw) # -1 to get the translation applied to reference. # Result contains the translation required for align the second matrix determined_trans= (-1)*result[0].get_translation() # Tolerate 1 pixel error self.assertAlmostEqual(abs(determined_trans[0]-trans[0]),0, delta=1, msg="1st coordinate is incorrect: Applied %f Determined %f" \ % (trans[0], determined_trans[0])) self.assertAlmostEqual(abs(determined_trans[1]-trans[1]),0, delta=1, msg="2nd coordinate is incorrect: Applied %f Determined %f" \ % (trans[1], determined_trans[1]))
def one_schedule_var(P, mu, K, q, N, ucb_avg, l, R): # one scheduling step # P : permutation matrix # mu : actual transmission rate # K : number of queues # q : current queuelengths # N : number of samplings to be updates # avg : number of samplings # R : maximum alphabet size rates = np.diag(P * np.transpose(mu)) # print P # print "rates", rates for i in range(K): bit = random.random() if bit <= l[i]: q[i] = q[i] + 1 nodes = P * np.transpose(np.matrix(range(K))) # print nodes for i in range(K): bit = random.random() if bit <= float(rates[i] / R): # print "testing" if q[i] - R >= 0: q[i] = q[i] - R else: q[i] = 0 ucb_avg[i, int(nodes[i])] = (N[i, int(nodes[i])] * ucb_avg[i, int(nodes[i])] + 1) / ( N[i, int(nodes[i])] + 1 ) N[i, int(nodes[i])] = N[i, int(nodes[i])] + 1 else: ucb_avg[i, int(nodes[i])] = (N[i, int(nodes[i])] * ucb_avg[i, int(nodes[i])]) / (N[i, int(nodes[i])] + 1) N[i, int(nodes[i])] = N[i, int(nodes[i])] + 1
def test_complete_alignment(self): """ Test the complete alignment in 2D routine (new) """ random.seed() name=self.get_input_file_name("1z5s-projection-2.spi") srw = IMP.em2d.SpiderImageReaderWriter() image=IMP.em2d.Image() image.read(name,srw) transformed=IMP.em2d.Image() rot=IMP.algebra.Rotation2D(random.random()*2*pi) trans=IMP.algebra.Vector2D(random.random()*10,random.random()*10) T=IMP.algebra.Transformation2D(rot,trans) IMP.em2d.get_transformed(image.get_data(),transformed.get_data(),T) fn_transformed = self.get_input_file_name("transformed.spi") # transformed.write(fn_transformed,srw) result=IMP.em2d.get_complete_alignment(image.get_data(), transformed.get_data(),True) fn_aligned = self.get_input_file_name("aligned_complete.spi") # transformed.write(fn_aligned,srw) cross_correlation_coefficient = result.second # Tolerate 1 pixel error self.assertAlmostEqual(cross_correlation_coefficient,1, delta=0.03, msg="Error in the complete aligment routine," "ccc %f less than 0.97" % (cross_correlation_coefficient))
def __init__(self): self.x = random.random() * world_size self.y = random.random() * world_size self.orientation = random.random() * 2.0 * pi self.forward_noise = 0.0; self.turn_noise = 0.0; self.sense_noise = 0.0;
def __init__(self, game, name="", description="", connections=[], explored=False, itemchances=[0.5,0.25,0.1], monsterchances=[0.3,0.2,0.1,0.05], bosschances=[0.0], hint="" ): """need game instance""" self.number = Room.number # void room has number 0 game.rooms[self.number] = self # add room into game dict Room.number += 1 self.explored = explored # True or False self.name = name self.hint = hint # description of room if still unexplored self.description = description self.connections = connections self.itemchances = itemchances self.monsterchances = monsterchances self.bosschances = bosschances # create items for chance in self.itemchances: if random.random()< chance: newItem = Item(game, self.number) # create item in this room # create monsters for chance in self.monsterchances: if random.random() < chance: newMonster = Monster(game, self.number) # create monster in this room # create boss(es) for chance in self.bosschances: if random.random() < chance: newMonster = Monster(game, self.number, boss=True)
def __init__(self): id_length = random.randint(config.min_id_length, config.max_id_length) self.id = utils.random_string(id_length) sex = random.choice(['male', 'female']) if sex == 'male': self.sex = 'M' else: self.sex = 'F' self.first_name = names.get_first_name(sex) self.last_name = names.get_last_name() self.middle_name = '' if config.gen_mid_name: if random.random() < config.gen_mid_name_chance: if random.randint(0, 1): self.middle_name = names.get_first_name(sex) else: self.middle_name = names.get_first_name(sex)[0] start = datetime.datetime(1900, 1, 1) end = datetime.datetime.now() self.birth_date = utils.random_date_between(start, end) self.aliases = [] if config.gen_alias: for i in xrange(config.gen_alias_max): if random.random() < config.gen_alias_chance: self.aliases.append(self.generate_alias()) self.studies = self.generate_studies(self.birth_date)
def on_run(self): """Run when button is pressed.""" inside = 0 for i in xrange(self.samples): sleep(0.001) r1, r2 = (random(), random()) if r1*r1 + r2*r2 < 1.0: inside += 1 if (i+1) % 100 == 0: draws = i+1 self.emit('log', { 'draws': draws, 'inside': inside, 'r1': r1, 'r2': r2, }) p = float(inside)/draws uncertainty = 4.0*math.sqrt(draws*p*(1.0 - p)) / draws self.emit('status', { 'pi-estimate': 4.0*inside/draws, 'pi-uncertainty': uncertainty }) self.emit('log', {'action': 'done'})
def mutate(self,global_innovations): mut_conn_c = float(self.settings["mutation"]) mut_link_c = float(self.settings["new_link"]) mut_node_c = float(self.settings["new_node"]) perturb_c = float(self.settings["perturbing"]) mut_step = float(self.settings["step"]) # mutate existing genes self.geneMutation(mut_conn_c,perturb_c,mut_step) # new link while True: r = random.random() if r <= mut_link_c: mut_link_c-=r self.linkMutation(global_innovations) else: break # new node while True: r = random.random() if r <= mut_node_c: mut_node_c-=r self.nodeMutation(global_innovations) else: break
def generateArr(duration, dataType): if dataType == "int": return [int(1000*random.random()) for i in xrange(duration * 64)] elif dataType == "float": return [1000*random.random() for i in xrange(duration * 64)] else: return []
def GaussianSampling(): u1 = rnd.random() u2 = rnd.random() r = np.sqrt(-2.0 * np.log(u1)) theta = 2.0 * np.pi * u2 nx = r * np.cos(theta) return nx
def scroll_down(driver): at_bottom = False while random.random() > .20 and not at_bottom: k = str(10 + int(200*random.random())) driver.execute_script("window.scrollBy(0,"+k+")") at_bottom = driver.execute_script("return (((window.scrollY + window.innerHeight ) +100 > document.body.clientHeight ))") time.sleep(0.5 + random.random())
def randomList(self, jobsList, tot): """ Handy method to create a random job number set with a random behaviour. There are 4 predefined behaviour: * producing a list of all the jobs from 1 to tot * producing the input jobsList * producing a subset of jobsList * producing a subset of all the jobs if the set produced is empty it returns the jobsList itself """ r = random.random() if r < 0.25: # All the jobs ret = range(1, tot+1) elif r < 0.75: # Exactly the requested jobs ret = jobsList elif r < 0.90: # A subset of the requested jobs ret = [x for x in jobsList if random.random() > .25] else: # A subset of all the jobs ret = [x for x in range(1, tot+1) if random.random() > .25] ret = set(ret) jobsList = set(jobsList) if ret & jobsList: # return ret else: return jobsList
def func(generation, parents_with_fit): parents_with_fit.sort(key = lambda (_, f): f) parents = map(lambda (p,_): p, parents_with_fit) elite = parents[0:elite_count] xover = parents[elite_count:xover_last_index] xover_children = crossover_func(options, xover) mutated_elite = mutate_func(generation, options, elite) if cm_preserve: xover_2_mutate = filter(lambda _: random.random() < cm_ch, xover_children) mutated_xover = mutate_func(generation, options, xover_2_mutate) # case cm_preserve mutate_count = default_mutate_count - len(mutated_xover) resulting_xover = xover_children + mutated_xover else: mutate_count = default_mutate_count resulting_xover = [ mutate_func(generation, options, [ch])[0] if random.random() < cm_ch else ch for ch in xover_children ] rest = parents[xover_last_index:xover_last_index+mutate_count] mutated_rest = mutate_func(generation, options, rest) mutated = mutated_elite + mutated_rest return elite + resulting_xover + mutated
def handle_event(self, message, details): if self.state is 'idle': if message == 'timer': # go to a random point, wake up sometime in the next 10 seconds world = self.body.world x, y = random.random()*world.width, random.random()*world.height self.body.go_to((x,y)) self.body.set_alarm(random.random()*10) elif message == 'collide' and details['what'] == 'Slug': # a slug bumped into us; get curious self.state = 'curious' self.body.set_alarm(1) # think about this for a sec self.body.stop() self.target = details['who'] elif self.state == 'curious': if message == 'timer': # chase down that slug who bumped into us if self.target: if random.random() < 0.5: self.body.stop() self.state = 'idle' else: self.body.follow(self.target) self.body.set_alarm(1) elif message == 'collide' and details['what'] == 'Slug': # we meet again! slug = details['who'] slug.amount -= 0.01 # take a tiny little bite
def load_random_chromosome(chr_name): """Generate a chromosome with random information about it. """ cur_chromosome = BasicChromosome.Chromosome(chr_name) num_segments = random.randrange(num_possible_segments) for seg in range(num_segments): # make the top and bottom telomeres if seg == 0: cur_segment = BasicChromosome.TelomereSegment() elif seg == num_segments - 1: cur_segment = BasicChromosome.TelomereSegment(1) # otherwise, they are just regular segments else: cur_segment = BasicChromosome.ChromosomeSegment() color_chance = random.random() if color_chance <= color_prob: fill_color = random.choice(color_choices) cur_segment.fill_color = fill_color id_chance = random.random() if id_chance <= id_prob: id = get_random_id() cur_segment.label = id cur_chromosome.add(cur_segment) return cur_chromosome, num_segments
def add(self, child, width, height, x=None, y=None, locked=False): if x is not None and y is not None: rect = gtk.gdk.Rectangle(x, y, width, height) weight = self.compute_weight(rect) else: trials = _PLACE_TRIALS weight = _MAX_WEIGHT while trials > 0 and weight: x = int(random.random() * (self.width - width)) y = int(random.random() * (self.height - height)) rect = gtk.gdk.Rectangle(x, y, width, height) new_weight = self.compute_weight(rect) if weight > new_weight: weight = new_weight trials -= 1 self._child_rects[child] = rect self._children.append(child) self.add_weight(self._child_rects[child]) if locked: self._locked_children.add(child) if weight > 0: self._detect_collisions(child)
def create_boost_particle(self): s = rabbyt.Sprite(self.texture_id, self.shape) lifetime = .5 s.xy = self.xy s.rot = self.rot s.scale = rabbyt.lerp(1, 2, dt=lifetime) s.alpha = rabbyt.lerp(.8, 0, dt=lifetime) Car.boost_particles.add(s) rabbyt.scheduler.add(rabbyt.get_time()+lifetime, lambda:Car.boost_particles.remove(s)) lt = .8 star = rabbyt.Sprite("star2.png") x = random.random()*80-40 y = random.random()*80-40 star.x = rabbyt.lerp(self.x+x, self.convert_offset((-20,0))[0]+x, dt=lt) star.y = rabbyt.lerp(self.y+y, self.convert_offset((-20,0))[1]+y, dt=lt) star.rot = rabbyt.lerp(0, 190*random.choice([-2,-1,1,2]), dt=5, extend="extrapolate") star.scale = rabbyt.lerp(random.random()+.2,0, rabbyt.get_time()+lt/2, dt=lt/2) star.rgb = 0, .5, .9 Car.boost_particles.add(star) rabbyt.scheduler.add(rabbyt.get_time()+lt, lambda:Car.boost_particles.remove(star))
def sample(a, diversity=0.75): if random.random() > diversity: return np.argmax(a) while 1: i = random.randint(0, len(a)-1) if a[i] > random.random(): return i
def optimize_hyperparameters(self, samples=5, step=3.0): old_hyper_parameters = [math.log(self._alpha_alpha), math.log(self._alpha_beta)] for ii in xrange(samples): log_likelihood_old = self.compute_likelihood(self._alpha_alpha, self._alpha_beta) log_likelihood_new = math.log(random.random()) + log_likelihood_old #print("OLD: %f\tNEW: %f at (%f, %f)" % (log_likelihood_old, log_likelihood_new, self._alpha_alpha, self._alpha_beta)) l = [x - random.random() * step for x in old_hyper_parameters] r = [x + step for x in old_hyper_parameters] for jj in xrange(self._alpha_maximum_iteration): new_hyper_parameters = [l[x] + random.random() * (r[x] - l[x]) for x in xrange(len(old_hyper_parameters))] trial_alpha, trial_beta = [math.exp(x) for x in new_hyper_parameters] lp_test = self.compute_likelihood(trial_alpha, trial_beta) if lp_test > log_likelihood_new: self._alpha_alpha = math.exp(new_hyper_parameters[0]) self._alpha_beta = math.exp(new_hyper_parameters[1]) #self._alpha_sum = self._alpha_alpha * self._K #self._beta_sum = self._alpha_beta * self._number_of_language_types old_hyper_parameters = [math.log(self._alpha_alpha), math.log(self._alpha_beta)] break else: for dd in xrange(len(new_hyper_parameters)): if new_hyper_parameters[dd] < old_hyper_parameters[dd]: l[dd] = new_hyper_parameters[dd] else: r[dd] = new_hyper_parameters[dd] assert l[dd] <= old_hyper_parameters[dd] assert r[dd] >= old_hyper_parameters[dd] print("\nNew hyperparameters (%i): %f %f" % (jj, self._alpha_alpha, self._alpha_beta))
def __init__(self, num_states, num_symbols): """ generated source for method __init___0 """ self.num_states = num_states self.num_symbols = num_symbols self.transition = numpy.zeros(shape=(int(num_states), int(num_states))) self.output = numpy.zeros(shape=(int(num_states), int(num_symbols))) self.pi = numpy.zeros(shape=(int(num_states),)) self.pi[0] = 1 i = 1 while i < num_states: self.pi[i] = 0 i += 1 i = 0 while i < self.num_states: j=0 while j < self.num_states: if j < i or j > i + self.delta: self.transition[i][j] = 0 else: self.transition[i][j] = random.random() j += 1 j=0 while j < self.num_symbols: self.output[i][j] = random.random() j += 1 i += 1
def step(self): """ Perform a step. This is called repeatedly by the runner and causes the client to issue commands to the server. This holds all "intelligence" of the dummy client. """ if self.istep == 0 and random.random() > CHANCE_OF_LOGIN: return elif random.random() > CHANCE_OF_ACTION: return global NLOGGED_IN if not self._cmdlist: # no cmdlist in store, get a new one if self.istep == 0: NLOGGED_IN += 1 cfunc = self._actions[0] else: # random selection using cumulative probabilities rand = random.random() cfunc = [func for cprob, func in self._actions[2] if cprob >= rand][0] # assign to internal cmdlist cmd, self._report = cfunc(self) self._cmdlist = list(makeiter(cmd)) self._ncmds = len(self._cmdlist) # output if self.istep == 0 and not (self._echo_brief or self._echo_all): # only print login print "client %i %s (%i/%i)" % (self.cid, self._report, NLOGGED_IN, NCLIENTS) elif self.istep == 0 or self._echo_brief or self._echo_all: print "client %i %s (%i/%i)" % (self.cid, self._report, self._ncmds-(len(self._cmdlist)-1), self._ncmds) # launch the action by popping the first element from cmdlist (don't hide tracebacks) self.sendLine(str(self._cmdlist.pop(0))) self.istep += 1 # only steps up if an action is taken
def build_xc(c, eid): line_max = random.randint(1, 50) line_min = random.randint(0, line_max) turn_max = random.randint(-360, 360) turn_min = random.randint(-360, turn_max) rt_max = random.randint(7000,10000) rt_min = random.randint(4000,rt_max) sense = random.randint(100,250) L0 = random.random() L1 = random.random() L2 = random.random() L3 = random.random() T0 = 1 - L0 T1 = 1 - L1 T2 = 1 - L2 T3 = 1 - L3 xc_str = {'light':{'dMax':line_max,'dMin':line_min, 'phiMax':turn_max,'phiMin':turn_min, 'rtMax':rt_max, 'rtMin':rt_min, 'lSense':sense,'weights':{'line':{'L':L0,'T':T0},'turn':{'L':L1,'T':T1}}},'dark':{'dMax':line_max,'dMin':line_min, 'phiMax':turn_max,'phiMin':turn_min, 'rtMax':rt_max, 'rtMin':rt_min, 'lSense':sense,'weights':{'line':{'L':L2,'T':T2},'turn':{'L':L3,'T':T3}}}} if eid > 1: old_eid = eid - 1 xc_str = splicer.get_mutated_xc(c, old_eid) add_xc(c, eid, 1, pickle.dumps(xc_str), 1, 1, 100)
def random_divide(self): ta = int(random.random()**2 * (self.instance.solution_size() - 1)) tb = ta while tb == ta: tb = int(random.random() * ( self.instance.solution_size() - 1)) ta, tb = min(ta, tb), max(ta, tb) return ta, tb
def bootstrapRealization(genTable, pathOutput, realization): #Input is table to give Kriging import random lines = [] for jj in genTable: lines.append(jj) # #Shuffling # newList = [] for jj in numpy.arange(len(lines)): random.seed() select = choice(lines) # To avoid duplicates, if the line already exists, the positions RA and Dec are # offset by a random value in the range -0.5<D<0.5 arcsec. if select in numpy.array(newList): select[0] += random.random()-0.5 select[1] += random.random()-0.5 # if len(select) == 4: newList.append([select[0],select[1],select[2],select[3]]) else: newList.append([select[0],select[1],select[2]]) # newList = numpy.array(newList) # Save in dir if not(os.path.exists(pathOutput+'/MC'+str(realization))): os.mkdir(pathOutput+'/MC'+str(realization)) # Savetxt file listTmp = [] for jj in newList: listTmp.append('\t'.join(map(str, jj))) #Join elements of the same line fileTMP = open(pathOutput+'/MC'+str(realization)+'/realization_'+str(int(realization))+'_Points.txt', 'wb') fileTMP.write("\n".join(listTmp)) fileTMP.close() return True
def f(image_config): array = np.array(image_config.image) prob = misc.uniform_sample_from_interval(func_config.min_prob, func_config.max_prob) rnd = np.random.rand(*array.shape[0:2]) array[rnd < prob] = 255 * random.random() array[rnd > 1 - prob] = 255 * random.random() image_config.image = Image.fromarray(array)
def add_particles(): particle = batch.add(1, GL_POINTS, None, ('v2f/stream', [win.width / 2, 0])) particle.dx = (random.random() - .5) * win.width / 4 particle.dy = win.height * (.5 + random.random() * .2) particle.dead = False particles.append(particle)
def run( self ): self.heightmap = numpy.zeros( ( self.width, self.height ) ) # reset on run c1 = random.random() # top c3 = random.random() # bottom c2 = random.random() # right c4 = random.random() # left self.divideRect( 0, 0, self.width, self.height, c1, c2, c3, c4 )
def generate_doc(db, prefix, suffix_length=12, id_string="%s%s", max_retry=100, data={}): """Generate doc with unique ID, based on provided prefix and random suffix. Retries on duplicate. **Deprecated since couchdbcurl.client.Document.create() method.** """ assert not "_id" in data assert not "_rev" in data assert max_retry > 0 assert type(max_retry) == int # doc = data.copy() rand = hashlib.sha1(str(random.random())).hexdigest()[:suffix_length] doc_id = id_string % (prefix, "") while True: try: db[doc_id] = data break except ResourceConflict: pass max_retry -= 1 if max_retry < 0: raise Exception("Retry-limit reached during document generation") rand = hashlib.sha1(str(random.random())).hexdigest()[:suffix_length] doc_id = id_string % (prefix, "_%s" % rand)
cubiculo_vacio.acquire() auxiliarPreguntas = dicAlumnos[str(unichr(num + 48))] auxiliarPreguntas += 1 dicAlumnos[str(unichr(num + 48))] = auxiliarPreguntas pregunta(num, auxiliarPreguntas) mutex.release() mano.release() time.sleep(0.3) mutex.acquire() turno += 1 if dicAlumnos[str(unichr(num + 48))] == 2: alumnos -= 2 if alumnos == 0: cubiculo_vacio.release() asesor() mutex.release() time.sleep(0.2) #Iniciar hilos alumno a 1 (Pues no existen alumnos 0) hilos_alum = 1 #Mientras True siempre lanzara hilos while True: #Control del maximo numero permitido de alumnos while (hilos_alum < 4): time.sleep(0.05) if random.random() < 0.05: threading.Thread(target=alumno, args=[hilos_alum]).start() hilos_alum += 1
def __init__(self): self.gene = [] for i in range(LENGTH): self.gene.append(int(random.random() * ALLELE)) self.fitness = 0
#!/usr/bin/env python import socket import time from datetime import datetime import msgpack import random import os TCP_IP = '127.0.0.1' TCP_PORT = 8000 BUFFER_SIZE = 1024 # MESSAGE = "Hello, World!" MESSAGE=[random.random()*1000,random.random()*1000,random.random()*1000,random.random()*1000,random.random()*1000,random.random()*1000] s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((TCP_IP,TCP_PORT)) seq=1 sleep=1 filename = "TCP_delay " + datetime.now().strftime("%m-%d-%H-%M-%S") save_path=os.path.realpath(os.path.join(os.getcwd() , os.path.dirname(filename))).replace("\\" , "/") + "/%s.csv" appendfile = open(save_path % filename , "a") info_config= str("IP , PORT") appendfile.write(info_config+',') info_config= str("average code length") appendfile.write(info_config+',') info_config= str('sleep time') appendfile.write(str(info_config)+'\n') info_config= str([TCP_IP , TCP_PORT]) appendfile.write(info_config+',') info_config=str(len(str(MESSAGE))) appendfile.write(info_config+',') info_config=str(sleep) appendfile.write(str(info_config)+'\n')
def random_horizontal_flip(imgs): if random.random() < 0.5: for i in range(len(imgs)): imgs[i] = np.flip(imgs[i], axis=1).copy() return imgs
def main(winstyle = 0): # Initialize pygame pygame.init() if pygame.mixer and not pygame.mixer.get_init(): print ('Warning, no sound') pygame.mixer = None # Set the display mode winstyle = 0 # |FULLSCREEN bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32) screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth) #Load images, assign to sprite classes #(do this before the classes are used, after screen setup) img = load_image('player1.gif') Player.images = [img, pygame.transform.flip(img, 1, 0)] img = load_image('explosion1.gif') Explosion.images = [img, pygame.transform.flip(img, 1, 1)] Alien.images = load_images('alien1.gif', 'alien2.gif', 'alien3.gif') Bomb.images = [load_image('bomb.gif')] Shot.images = [load_image('shot.gif')] #decorate the game window icon = pygame.transform.scale(Alien.images[0], (32, 32)) pygame.display.set_icon(icon) pygame.display.set_caption("Jacob's aliens") pygame.mouse.set_visible(0) #create the background, tile the bgd image bgdtile = load_image('background.gif') background = pygame.Surface(SCREENRECT.size) for x in range(0, SCREENRECT.width, bgdtile.get_width()): background.blit(bgdtile, (x, 0)) screen.blit(background, (0,0)) pygame.display.flip() #load the sound effects boom_sound = load_sound('boom.wav') shoot_sound = load_sound('car_door.wav') if pygame.mixer: music = os.path.join(main_dir, 'data', 'house_lo.wav') pygame.mixer.music.load(music) pygame.mixer.music.play(-1) # Initialize Game Groups aliens = pygame.sprite.Group() shots = pygame.sprite.Group() bombs = pygame.sprite.Group() all = pygame.sprite.RenderUpdates() lastalien = pygame.sprite.GroupSingle() #assign default groups to each sprite class Player.containers = all Alien.containers = aliens, all, lastalien Shot.containers = shots, all Bomb.containers = bombs, all Explosion.containers = all Score.containers = all #Create Some Starting Values global score alienreload = ALIEN_RELOAD kills = 0 clock = pygame.time.Clock() #initialize our starting sprites global SCORE player = Player() Alien() #note, this 'lives' because it goes into a sprite group if pygame.font: all.add(Score()) while player.alive(): #get input for event in pygame.event.get(): if event.type == QUIT or \ (event.type == KEYDOWN and event.key == K_ESCAPE): return keystate = pygame.key.get_pressed() # clear/erase the last drawn sprites all.clear(screen, background) #update all the sprites all.update() #handle player input direction = keystate[K_RIGHT] - keystate[K_LEFT] player.move(direction) firing = keystate[K_SPACE] if not player.reloading and firing and len(shots) < MAX_SHOTS: Shot(player.gunpos()) shoot_sound.play() player.reloading = firing # Create new alien if alienreload: alienreload = alienreload - 1 elif not int(random.random() * ALIEN_ODDS): Alien() alienreload = ALIEN_RELOAD # Drop bombs if lastalien and not int(random.random() * BOMB_ODDS): Bomb(lastalien.sprite) # Detect collisions for alien in pygame.sprite.spritecollide(player, aliens, 1): boom_sound.play() Explosion(alien) Explosion(player) SCORE = SCORE + 1 player.kill() for alien in pygame.sprite.groupcollide(shots, aliens, 1, 1).keys(): boom_sound.play() Explosion(alien) SCORE = SCORE + 1 for bomb in pygame.sprite.spritecollide(player, bombs, 1): boom_sound.play() Explosion(player) Explosion(bomb) player.kill() for alien in pygame.sprite.groupcollide(aliens, aliens, 1, 1).keys(): boom_sound.play() Explosion(alien) SCORE = SCORE + 1 #draw the scene dirty = all.draw(screen) pygame.display.update(dirty) #cap the framerate clock.tick(40) if pygame.mixer: pygame.mixer.music.fadeout(1000) pygame.time.wait(1000) pygame.quit()
def test(learn_proportion=0.6): price_data = pd.read_csv('spy_historical.csv') price_data['gain'] = (price_data['Close'] - price_data['Open']) / price_data['Open'] price_data = price_data[['Date', 'gain']] price_data['date_copy'] = price_data['Date'] price_data_dates = list(price_data['Date']) price_data.set_index('date_copy', inplace=True) learn_data = [] test_data = [] for i, date in enumerate(price_data_dates): if random.random() <= learn_proportion: learn_data.append(i) else: test_data.append(i) positive_vec = Vector() negative_vec = Vector() positive_vec.initialize('McDonaldWords/positive.txt','McDonaldWords/negative.txt') negative_vec.initialize('McDonaldWords/positive.txt','McDonaldWords/negative.txt') counter = 0 num_learn = len(learn_data) for i in learn_data: if i <= 1: continue date = price_data.iloc[i - 1]['Date'] counter += 1 if counter % 100 == 0: print(round (100 * (counter/ num_learn)),'percent done learning') year, month, day = date.split('-') filename = 'newspaperdata/{}/{}_{}/nyt_business.txt'.format(year, month, day) days_text = read_file(filename) if price_data.iloc[i]['gain'] >= 0: positive = True else: positive = False for word in days_text.split(): if positive: positive_vec.add_word(word) else: negative_vec.add_word(word) correct_days = 0 total_days = 0 total_matches = 0 for i in test_data: if i <= 1: continue date = price_data.iloc[i - 1]['Date'] total_days += 1 year, month, day = date.split('-') filename = 'newspaperdata/{}/{}_{}/nyt_business.txt'.format(year, month, day) days_text = read_file(filename) day_vec = Vector() day_vec.initialize('McDonaldWords/positive.txt','McDonaldWords/negative.txt') for word in days_text.split(): day_vec.add_word(word) neg_angle = day_vec.get_angle(negative_vec) pos_angle = day_vec.get_angle(positive_vec) if pos_angle < neg_angle: predicted_gain = True else: predicted_gain = False if price_data.iloc[i]['gain'] >= 0: actual_gain = True else: actual_gain = False if predicted_gain == actual_gain: correct_days += 1 total_matches += day_vec.matches print('avg word hits per day: ',total_matches/total_days) print('correctly predicted direction on {} percent of days'.format(100 * (correct_days / total_days))) return correct_days / total_days
def mutation(self, instance): if random.random() < self.mutation_rate: i1, i2 = random.sample(range(len(self.cities_list)), 2) instance[i1], instance[i2] = instance[i2], instance[i1]
''' The Autoregressive Moving Average (ARMA) method models the next step in the sequence as a linear function of the observations and resiudal errors at prior time steps. It combines both Autoregression (AR) and Moving Average (MA) models. The notation for the model involves specifying the order for the AR(p) and MA(q) models as parameters to an ARMA function, e.g. ARMA(p, q). An ARIMA model can be used to develop AR or MA models. The method is suitable for univariate time series without trend and seasonal components. ''' from random import random # ARMA example from statsmodels.tsa.arima_model import ARMA # contrived dataset data = [random() for x in range(1, 100)] # fit model model = ARMA(data, order=(2, 1)) model_fit = model.fit(disp=False) # make prediction yhat = model_fit.predict(len(data), len(data)) print(yhat)
db = mysql.connector.connect( host="127.0.0.1", user="******", password="******", database="ebookmarket" ) cur = db.cursor() cur.execute("SELECT id, title, author, filehandle FROM books") res = cur.fetchall() for row in res: print(row) title = row[1].replace("'", r"\'") author = row[2] os.system(r'pdflatex -output-directory ../assets/ebooks/ "\def\coverpath{../assets/covers/} \def\booktitle{' + title + r'} \def\bookauthor{' + author + r'} \def\covername{' + row[3] + r'} \input{ebook}" &> /dev/null') if os.path.isfile("../assets/ebooks/ebook.pdf"): os.system("rm -f ../assets/ebooks/*{.aux,.log}") n = random.random() os.rename("../assets/ebooks/ebook.pdf", "../assets/ebooks/" + row[3] + ".pdf") if n <= epubprob: os.system("ebook-convert ../assets/ebooks/" + row[3] + "{.pdf,.epub} &> /dev/null") if n <= mobiprob: os.system("ebook-convert ../assets/ebooks/" + row[3] + "{.pdf,.mobi} &> /dev/null") if n > pdfprob: os.remove("../assets/ebooks/" + row[3] + ".pdf") else: print("PDFLATEX ERROR !! STOP") break
def main(): """ Main program cycle """ print console.colored("""\nThis script will migrate your Indico DB to a new version. We recommend that this operation be executed while the web server is down, in order to avoid concurrency problems and DB conflicts.\n\n""", 'yellow') parser = argparse.ArgumentParser(description='Execute migration') parser.add_argument('--dry-run', '-n', dest='dry_run', action='store_true', help='Only show which migration tasks would be executed') parser.add_argument('--run-only', dest='specified', default='', help='Specify which step(s) to run (comma-separated)') parser.add_argument('--run-from', dest='run_from', default='', help='Specify FROM which step to run (inclusive)') parser.add_argument('--prev-version', dest='prevVersion', help='Previous version of Indico (used by DB)', default=__version__) parser.add_argument('--profile', dest='profile', help='Use profiling during the migration', action='store_true') args = parser.parse_args() if args.dry_run or console.yesno("Are you sure you want to execute the migration now?"): try: if args.profile: import profile, random, os proffilename = os.path.join(Config.getInstance().getTempDir(), "migration%s.prof" % str(random.random())) result = None profile.runctx("""result=runMigration( prevVersion=parse_version(args.prevVersion), specified=filter(lambda x: x, map(lambda x: x.strip(), args.specified.split(','))), run_from=args.run_from, dry_run=args.dry_run)""", globals(), locals(), proffilename) return result else: with make_app().app_context(): return runMigration(prevVersion=parse_version(args.prevVersion), specified=filter(None, map(str.strip, args.specified.split(','))), run_from=args.run_from, dry_run=args.dry_run) except ControlledExit: return 1 except (Exception, SystemExit, KeyboardInterrupt): print console.colored("\nMigration failed! DB may be in an inconsistent state:", 'red', attrs=['bold']) print console.colored(traceback.format_exc(), 'red') return -1 else: return 1
def GA_binary(citys): n = len(citys) population = 50 gen_t = 100*n p = 0.01 # citys = [City() for i in range(n)] peoples = [] _list = [i for i in range(n)] min_dis = 99999999 answer = '' for _ in range(population): code = np.array([[0]*n]*n) random.shuffle(_list) for i in range(len(code)): code[i][_list[i]] = 1 # initial peoples.append(code) for _ in range(gen_t): p_list = [] min_list = [] peoples = sorted(peoples, key=lambda x: all_dis_binary(x, citys)) for i in peoples[0]: min_list.append(np.argmax(i)) new_dis = all_dis(min_list, citys) if min_dis > new_dis: min_dis = new_dis answer = min_list.__str__() if _ % 100 == 0: print('distance:{}, GA answer:{}'.format(min_dis, answer)) _sum = 0 for i in peoples: _dis = all_dis_binary(i, citys) p_list.append(_dis) _sum += _dis for i in range(len(p_list)): p_list[i] /= _sum index_list = np.random.choice([i for i in range(population)], size=2, p=p_list) father = peoples[index_list[0]] mom = peoples[index_list[1]] son = [] mating_index = random.randint(0, n-1) for i in father[:mating_index]: son.append(i) for i in range(0, len(mom)): flag = True for j in son: _code = np.argmax(j) if np.argmax(mom[i]) == _code: flag = False break if flag: son.append(mom[i]) i_idx = -1 # genic mutation j_idx = -1 if random.random() < p: while i_idx == j_idx: i_idx = random.randint(0, n-1) j_idx = random.randint(0, n-1) son[i_idx], son[j_idx] = son[j_idx], son[i_idx] # swap son = np.array(son) peoples.pop(-1) peoples.append(son) return min_dis
def chance(p): return random.random() < p
def make_move(self, board, doors, roll, loc, other_players, sol): moves = self.get_valid_moves(board, doors, roll, loc, other_players) if len(moves) == 0: return None actions = self.get_valid_actions(board, moves) state = self.get_state() #select action max_r = -10000 a = [] for i in actions: ia = (i[1], i[2]) if self.qtable.table[state][ia] > max_r: max_r = self.qtable.table[state][ia] a = [i] elif self.qtable.table[state][ia] == max_r: a.append(i) #if tie select random move a = a[random.randint(0, len(a) - 1)] #explore vs exploit if random.random() > .98: a = actions[random.randint(0, len(actions) - 1)] #make move and get reward reward = 0 ret_val = None if a[1] == 'n': #in hall self.location = a[0] reward = -5 elif a[1] == 's': #guess solution self.location = a[0] if sol == a[2]: reward = 1000 else: reward = -1000 ret_val = a[2] else: #accusation self.location = a[0] acc = a[2] #move accused player to room for i in other_players: if i.character == acc[2]: i.location = self.location break learn = False #ask other players for a card for i in other_players: response = i.acc_respond(acc) if response != None: learn = True self.record_cards([response]) reward = 20 break if not learn: reward = -20 #get new state new_state = self.get_state() #get all possible next moves new_moves = [] for i in range(1, 7): new_moves += self.get_valid_moves(board, doors, i, loc, other_players) new_actions = self.get_valid_actions(board, new_moves) #select best possible next action new_max_r = -10000 new_a = [] for i in new_actions: ia = (i[1], i[2]) if self.qtable.table[state][ia] > new_max_r: new_max_r = self.qtable.table[state][ia] new_a = [i] elif self.qtable.table[state][ia] == new_max_r: new_a.append(i) new_action = new_a[random.randint(0, len(new_a) - 1)] #generalize actions a = (a[1], a[2]) new_action = (new_action[1], new_action[2]) learning_rate = .8 discount_factor = .95 #update q value self.qtable.table[state][a] = ( 1 - learning_rate) * self.qtable.table[state][a] + learning_rate * ( reward + discount_factor * self.qtable.table[new_state][new_action]) return ret_val
from random import random N = 20 arr = [0] * N for i in range(N): arr[i] = int(random() * 100) print(arr[i], end=' ') print() mn = 0 mx = 0 for i in range(N): if arr[i] < arr[mn]: mn = i elif arr[i] > arr[mx]: mx = i print( f'Минимальный элемент под №{mn + 1} равен {arr[mn]}, максимальный элемент под №{mx + 1} равен {arr[mx]}!' ) b = arr[mn] arr[mn] = arr[mx] arr[mx] = b for i in range(N): print(arr[i], end=' ')
def roll_dice(num_dice, num_rolls): double_list = [[0 for i in range(num_dice)] for j in range(num_rolls)] for roll in range(0, len(double_list)): for die in range(0, len(double_list[roll])): double_list[roll][die] = (int)(random.random()*6 + 1) return double_list
def message_is_spam(): return random.random() > 0.5
def setUp(self): MongoDBTestCase.setUp(self) # WORKLOAD timestamp = time.time() sess = self.metadata_db.Session() sess['session_id'] = 0 sess['ip_client'] = "client:%d" % (1234 + 0) sess['ip_server'] = "server:5678" sess['start_time'] = timestamp # generate query 0 querying field00 _id = str(random.random()) queryId = long((0 << 16) + 0) queryContent = {} queryPredicates = {} projectionField = {} responseContent = {"_id": _id} responseId = (queryId << 8) responseContent['field00'] = random.randint(0, 100) queryContent['field00'] = responseContent['field00'] queryPredicates['field00'] = constants.PRED_TYPE_EQUALITY projectionField['field02'] = random.randint(0, 100) queryContent = { constants.REPLACE_KEY_DOLLAR_PREFIX + "query": queryContent } op = Session.operationFactory() op['collection'] = CostModelTestCase.COLLECTION_NAME op['type'] = constants.OP_TYPE_QUERY op['query_id'] = queryId op['query_content'] = [queryContent] op['resp_content'] = [responseContent] op['resp_id'] = responseId op['predicates'] = queryPredicates op['query_time'] = timestamp op['query_fields'] = projectionField timestamp += 1 op['resp_time'] = timestamp sess['operations'].append(op) # generate query 1 querying field01 _id = str(random.random()) queryId = long((1 << 16) + 1) queryContent = {} queryPredicates = {} responseContent = {"_id": _id} responseId = (queryId << 8) projectionField = {} responseContent['field01'] = random.randint(0, 100) queryContent['field01'] = responseContent['field01'] queryPredicates['field01'] = constants.PRED_TYPE_EQUALITY projectionField['field02'] = random.randint(0, 100) queryContent = { constants.REPLACE_KEY_DOLLAR_PREFIX + "query": queryContent } op = Session.operationFactory() op['collection'] = CostModelTestCase.COLLECTION_NAME op['type'] = constants.OP_TYPE_QUERY op['query_id'] = queryId op['query_content'] = [queryContent] op['resp_content'] = [responseContent] op['resp_id'] = responseId op['predicates'] = queryPredicates op['query_time'] = timestamp op['query_fields'] = projectionField timestamp += 1 op['resp_time'] = timestamp sess['operations'].append(op) # generate query 2 querying field00, field01 _id = str(random.random()) queryId = long((2 << 16) + 2) queryContent = {} queryPredicates = {} projectionField = {} responseContent = {"_id": _id} responseId = (queryId << 8) responseContent['field01'] = random.randint(0, 100) queryContent['field01'] = responseContent['field01'] queryPredicates['field01'] = constants.PRED_TYPE_EQUALITY responseContent['field00'] = random.randint(0, 100) queryContent['field00'] = responseContent['field00'] queryPredicates['field00'] = constants.PRED_TYPE_EQUALITY projectionField['field02'] = random.randint(0, 100) queryContent = { constants.REPLACE_KEY_DOLLAR_PREFIX + "query": queryContent } op = Session.operationFactory() op['collection'] = CostModelTestCase.COLLECTION_NAME op['type'] = constants.OP_TYPE_QUERY op['query_id'] = queryId op['query_content'] = [queryContent] op['resp_content'] = [responseContent] op['resp_id'] = responseId op['predicates'] = queryPredicates op['query_time'] = timestamp op['query_fields'] = projectionField timestamp += 1 op['resp_time'] = timestamp sess['operations'].append(op) sess['end_time'] = timestamp timestamp += 1 # generate query 3 querying field00, field01 but without projection field _id = str(random.random()) queryId = long((2 << 16) + 3) queryContent = {} queryPredicates = {} projectionField = {} responseContent = {"_id": _id} responseId = (queryId << 8) responseContent['field01'] = random.randint(0, 100) queryContent['field01'] = responseContent['field01'] queryPredicates['field01'] = constants.PRED_TYPE_EQUALITY responseContent['field00'] = random.randint(0, 100) queryContent['field00'] = responseContent['field00'] queryPredicates['field00'] = constants.PRED_TYPE_EQUALITY queryContent = { constants.REPLACE_KEY_DOLLAR_PREFIX + "query": queryContent } op = Session.operationFactory() op['collection'] = CostModelTestCase.COLLECTION_NAME op['type'] = constants.OP_TYPE_QUERY op['query_id'] = queryId op['query_content'] = [queryContent] op['resp_content'] = [responseContent] op['resp_id'] = responseId op['predicates'] = queryPredicates op['query_time'] = timestamp op['query_fields'] = projectionField timestamp += 1 op['resp_time'] = timestamp sess['operations'].append(op) sess['end_time'] = timestamp timestamp += 1 # generate query 4 querying field00, field01 but it goes to collection 2 _id = str(random.random()) queryId = long((2 << 16) + 4) queryContent = {} queryPredicates = {} projectionField = {} responseContent = {"_id": _id} responseId = (queryId << 8) responseContent['field00'] = random.randint(0, 100) queryContent['field00'] = responseContent['field00'] queryPredicates['field00'] = constants.PRED_TYPE_EQUALITY queryContent = { constants.REPLACE_KEY_DOLLAR_PREFIX + "query": queryContent } op = Session.operationFactory() op['collection'] = CostModelTestCase.COLLECTION_NAME_2 op['type'] = constants.OP_TYPE_QUERY op['query_id'] = queryId op['query_content'] = [queryContent] op['resp_content'] = [responseContent] op['resp_id'] = responseId op['predicates'] = queryPredicates op['query_time'] = timestamp op['query_fields'] = projectionField timestamp += 1 op['resp_time'] = timestamp sess['operations'].append(op) sess['end_time'] = timestamp timestamp += 1 # generate query 5 querying field00 but it goes to collection 3 _id = str(random.random()) queryId = long((2 << 16) + 5) queryContent = {} queryPredicates = {} projectionField = {} responseContent = {"_id": _id} responseId = (queryId << 8) responseContent['field00'] = random.randint(0, 100) queryContent['field00'] = responseContent['field00'] queryPredicates['field00'] = constants.PRED_TYPE_EQUALITY queryContent = { constants.REPLACE_KEY_DOLLAR_PREFIX + "query": queryContent } op = Session.operationFactory() op['collection'] = CostModelTestCase.COLLECTION_NAME_3 op['type'] = constants.OP_TYPE_QUERY op['query_id'] = queryId op['query_content'] = [queryContent] op['resp_content'] = [responseContent] op['resp_id'] = responseId op['predicates'] = queryPredicates op['query_time'] = timestamp op['query_fields'] = projectionField timestamp += 1 op['resp_time'] = timestamp sess['operations'].append(op) sess['end_time'] = timestamp timestamp += 1 sess.save() # Use the MongoSniffConverter to populate our metadata converter = MongoSniffConverter(self.metadata_db, self.dataset_db) converter.no_mongo_parse = True converter.no_mongo_sessionizer = True converter.process() self.assertEqual(CostModelTestCase.NUM_SESSIONS, self.metadata_db.Session.find().count()) self.collections = dict([(c['name'], c) for c in self.metadata_db.Collection.fetch()]) populated_workload = list(c for c in self.metadata_db.Session.fetch()) self.workload = populated_workload # Increase the database size beyond what the converter derived from the workload for col_name, col_info in self.collections.iteritems(): col_info['doc_count'] = CostModelTestCase.NUM_DOCUMENTS col_info['avg_doc_size'] = 1024 # bytes col_info['max_pages'] = col_info['doc_count'] * col_info[ 'avg_doc_size'] / (4 * 1024) col_info.save() # print pformat(col_info) self.costModelConfig = { 'max_memory': 1024, # MB 'skew_intervals': CostModelTestCase.NUM_INTERVALS, 'address_size': 64, 'nodes': CostModelTestCase.NUM_NODES, 'window_size': 10 } self.state = State(self.collections, populated_workload, self.costModelConfig)
def learning(self, batch_size): ''' Learning process ''' explore_rate = self.get_explore_rate(0) step_index =0 loss_list = list() reward_list = list() for epi in range(self.episode): state_old = self.environment.reset() temp_loss_list = list() total_reward = 0 for t in range(self.max_t): #self.environment.render() if random.random() < explore_rate: action = self.environment.action_space.sample() else: temp = self.mlp.predict(state[np.newaxis, :]) action = np.argmax(temp[0]) state, reward, done, _ = self.environment.step(action) total_reward += reward if self.reward_func != None: reward = self.reward_func(self.environment, state, state_old, done) self.memory.append((state_old, action, reward, state, done)) state_old = state if len(self.memory) > batch_size: if step_index % self.C_replace == 0 or epi == self.episode - 1: self.mlp.update_paramters() step_index = step_index + 1 sample_data = random.sample(self.memory, batch_size) totrain = list() next_states = [data[3] for data in sample_data] pred_reward = self.mlp.get_target(next_states) for b_index in range(batch_size): temp_state, temp_action, temp_reward, temp_next_state, temp_done = sample_data[b_index] predict_action = max(pred_reward[b_index]) #print(predict_action) if temp_done: yj = temp_reward else: yj = temp_reward + self.discount_factor * predict_action totrain.append([temp_state, temp_action, yj]) ##update states = [k[0] for k in totrain] actions = [k[1] for k in totrain] rewards = [k[2] for k in totrain] loss = self.mlp.update(states, actions, rewards) temp_loss_list.append(loss) if len(self.memory) > self.max_memory: self.memory = self.memory[1:] if done or t >= self.max_t - 1: reward_list.append(total_reward) if len(temp_loss_list) > 0: loss_list.append(sum(temp_loss_list)/len(temp_loss_list)) print("Episode %d finished %d" % (epi, t)) break explore_rate = self.get_explore_rate(epi) if self.model_name != None: self.mlp.saveModel(self.model_name) #plot the figure fig = plt.figure() plt.plot(range(len(loss_list)), loss_list) plt.xlabel("Episode") plt.ylabel("Average Loss") plt.savefig("loss_Idqn.png") plt.close('all') fig = plt.figure() plt.plot(range(self.episode), reward_list) plt.xlabel("Episode") plt.ylabel("Total reward") plt.savefig("reward_Idqn.png") plt.close('all')
def truncated_svd(X, num_val=2, max_iter=1000): """Compute the first component of SVD.""" def normalize_vec(X, n=2): """Normalize two parts (:m and m:) of the vector.""" X_m = X[:m] X_n = X[m:] norm_X_m = norm(X_m, n) Y_m = [x / norm_X_m for x in X_m] norm_X_n = norm(X_n, n) Y_n = [x / norm_X_n for x in X_n] return Y_m + Y_n def remove_component(X): """Remove components of already obtained eigen vectors from X.""" X_m = X[:m] X_n = X[m:] for eivec in eivec_m: coeff = dotproduct(X_m, eivec) X_m = [x1 - coeff * x2 for x1, x2 in zip(X_m, eivec)] for eivec in eivec_n: coeff = dotproduct(X_n, eivec) X_n = [x1 - coeff * x2 for x1, x2 in zip(X_n, eivec)] return X_m + X_n m, n = len(X), len(X[0]) A = [[0] * (n + m) for _ in range(n + m)] for i in range(m): for j in range(n): A[i][m + j] = A[m + j][i] = X[i][j] eivec_m = [] eivec_n = [] eivals = [] for _ in range(num_val): X = [random.random() for _ in range(m + n)] X = remove_component(X) X = normalize_vec(X) for i in range(max_iter): old_X = X X = matrix_multiplication(A, [[x] for x in X]) X = [x[0] for x in X] X = remove_component(X) X = normalize_vec(X) # check for convergence if norm([x1 - x2 for x1, x2 in zip(old_X, X)]) <= 1e-10: break projected_X = matrix_multiplication(A, [[x] for x in X]) projected_X = [x[0] for x in projected_X] new_eigenvalue = norm(projected_X, 1) / norm(X, 1) ev_m = X[:m] ev_n = X[m:] if new_eigenvalue < 0: new_eigenvalue = -new_eigenvalue ev_m = [-ev_m_i for ev_m_i in ev_m] eivals.append(new_eigenvalue) eivec_m.append(ev_m) eivec_n.append(ev_n) return (eivec_m, eivec_n, eivals)
def pickIndex(self) -> int: return bisect.bisect_left(self.sum, self.sum[-1] * random.random())
def randInt(): import random print(int(random.random() * 100)) print(int(random.random() * 50)) print(random.randrange(50, 100, 1)) print(random.randrange(50, 500, 1))
video_data = list_video_metadata(yt_crawler, vid) except Exception, e: logging.error( '>>> Metadata crawler: Error occurred when crawl {0}:\n{1}' .format(vid, str(e))) # get video insights data from request csvstring = None # fail over 5 times, pass for i in range(5): exit_code, csvstring = request(opener, vid, cookie, postdata) # success flag if exit_code == 0 or exit_code == 3: break else: time.sleep(2**i + random.random()) if csvstring is not None: insights_data = csvstring.split() video_data['insights'] = {} video_data['insights']['startDate'] = insights_data[0] video_data['insights']['days'] = insights_data[1] video_data['insights']['dailyView'] = insights_data[2] video_data['insights']['totalView'] = insights_data[3] video_data['insights']['dailyShare'] = insights_data[4] video_data['insights']['totalShare'] = insights_data[5] video_data['insights']['dailyWatch'] = insights_data[6] video_data['insights']['avgWatch'] = insights_data[7] video_data['insights']['dailySubscriber'] = insights_data[8] video_data['insights']['totalSubscriber'] = insights_data[9] else:
def parse_url(self, url): '''发送请求''' time.sleep(random.random()) # 每次发送请求停顿0-1s避免被封 # print(url) resp = requests.get(url=url, headers=self.headers) return resp.content.decode('utf-8','ignore')
def createRandomInstance(n, max=100, dem = 100): return [(rd.random()*max,rd.random()*max,rd.randint(1, dem)) for i in range(n)]
def __next__(self): return random.random()
def __init__(self, image_directory): print(image_directory) img = cv2.imread(os.path.join(image_directory, "raw.png")) self.height, self.width = img.shape[0:2] self.horizontal_lines = [] self.vertical_lines = [] low_threshold = 100 high_threshold = 150 #edges = cv2.Canny(img, low_threshold, high_threshold) edges = auto_canny(img) cv2.imwrite("/tmp/edges.png", edges) rho = 1 # distance resolution in pixels of the Hough grid theta = np.pi / 180 # angular resolution in radians of the Hough grid threshold = 15 # minimum number of votes (intersections in Hough grid cell) min_line_length = 150 # minimum number of pixels making up a line max_line_gap = 30 # maximum gap in pixels between connectable line segments line_image = np.copy(img) * 0 # creating a blank to draw lines on # Run Hough on edge detected image # Output "lines" is an array containing endpoints of detected line segments lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap) try: vertical_lines = [x for x in lines if x[0][0] == x[0][2]] except TypeError: return # debounce vertical_lines lines vertical_lines.sort(key=lambda a: a[0][0]) clean = vertical_lines[:1] for i in range(len(vertical_lines) - 1): line = vertical_lines[i + 1] last = clean[-1] if abs(last[0][0] - line[0][0]) < 20: # two very close line, just keep the longest last_len = last[0][1] - last[0][3] line_len = line[0][1] - line[0][3] if line_len > last_len: clean = clean[:-1] clean.append(line) else: clean.append(line) vertical_lines = clean # sort by size, biggest to smallest vertical_lines.sort(key=lambda a: a[0][1] - a[0][3], reverse=True) try: main_line = vertical_lines[0] except IndexError: return self.vertical_lines = [ VerticalLine(a[0][0], a[0][3], a[0][1]) for a in vertical_lines[:2] ] # filter: y1 == y2 and x1 < main_line.x1 and y2 > main_line.y1 - and y1 > main_line.y1 and y1 < main_line.y2 horizontal_lines = [ a for a in lines if a[0][1] == a[0][3] and (a[0][0] < main_line[0][0] < a[0][2]) and (main_line[0][3] <= a[0][3] <= main_line[0][1]) ] # debounce horizontal_lines horizontal_lines.sort(key=lambda a: a[0][1]) clean = horizontal_lines[:1] for i in range(len(horizontal_lines) - 1): line = horizontal_lines[i + 1] last = clean[-1] if abs(last[0][1] - line[0][1]) < 20: # two very close line, just keep the longest last_len = last[0][2] - last[0][0] line_len = line[0][2] - line[0][0] if line_len > last_len: clean = clean[:-1] clean.append(line) else: clean.append(line) horizontal_lines = clean # now filter the horizontal lines, keeping the ones at the clostest to top and bottom try: top_line = horizontal_lines[0] bottom_line = horizontal_lines[-1] self.horizontal_lines = [ HorizontalLine(x[0][1], x[0][0], x[0][2]) for x in [top_line, bottom_line] ] except IndexError: self.horizontal_lines = [] # # horizontal_lines.sort(key=lambda a: a[0][2] - a[0][0], reverse=True) # horizontal_lines = horizontal_lines[:2] # horizontal_lines.sort(key=lambda a: a[0][1]) # Find the longest vertical line_image - ideally just one for vline in self.vertical_lines: _ = cv2.line(line_image, (vline.x, vline.y1), (vline.x, vline.y2), (128 + int(random.random() * 128), 128, 128), 1) for hline in self.horizontal_lines: _ = cv2.line(line_image, (hline.x1, hline.y), (hline.x2, hline.y), (128, 128 + int(random.random() * 128), 128), 1) # # # lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0) cv2.imwrite(os.path.join(image_directory, "lines.png"), line_image)
def set_image(entity, probability): if probability < random.random(): return imagename = random.choice(os.listdir(IMAGES_FOLDER)) entity.set_image(os.path.join(IMAGES_FOLDER, imagename), imagename)
def face_detection(DONE, saved_frames_counter, max_saved_frames): cap = cv2.VideoCapture(2) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") x_ratio = 0.3 y_ratio = 0.5 frame_id = 0 save_frame_path = "./face_frames/" # overwrite previus images frame_container = [None] * max_saved_frames frame_container_coords = [None] * max_saved_frames while True: _, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = detector(gray) center = np.zeros(2, dtype=int) x_Crop = 0 y_Crop = 0 save = False for face in faces: x1 = face.left() y1 = face.top() x2 = face.right() y2 = face.bottom() # cv2.rectangle(frame, (x1,y1),(x2,y2),(0,255,0),3) y_Crop = int((x2 - x1) * (1.0 + x_ratio)) // 2 x_Crop = int((y2 - y1) * (1.0 + y_ratio)) // 2 landmarks = predictor(gray, face) center = np.zeros(2, dtype=int) for n in range(0, 68): x = landmarks.part(n).x y = landmarks.part(n).y # cv2.circle(frame,(x,y),4,(255,0,0),-1) center += np.array([x, y]) center = center // 68 save = random.random() > 0.5 # print(center) # cv2.circle(frame, (center[0],center[1]), 4, (0,0,255), -1) frame_coords = [ center[1] - x_Crop, center[1] + x_Crop, center[0] - y_Crop, center[0] + y_Crop ] cv2.imshow("LandMark Detection", frame) if save and saved_frames_counter < max_saved_frames: frame_container[saved_frames_counter] = copy.copy(frame) frame_container_coords[saved_frames_counter] = copy.copy( frame_coords) saved_frames_counter += 1 if saved_frames_counter == max_saved_frames and not DONE: print("The Coords") shape = np.matrix(frame_container_coords).sum( axis=0) // max_saved_frames shape = shape.tolist()[0] print(shape) for ii in range(max_saved_frames): alt_frame = frame_container[ii][shape[0]:shape[1], shape[2]:shape[3], :] cv2.imwrite(save_frame_path + "frame{:d}.jpg".format(ii), alt_frame) print("Working" + "".join(["."] * (ii % 4))) print("DONE") DONE = True key = cv2.waitKey(1) if key == 27: break frame_id += 1 cv2.destroyAllWindows() cap.release()
def norme(n, m): for i in range(m): x, y = 1 - 2 * random.random(), 1 - 2 * random.random() if ((abs(x)**n) + (abs(y)**n))**(1 / n) < 1: plt.plot([x], [y], 'k.')
def generate_number(self): rand = random() index = bisect.bisect_left(self.cum_probs, rand) return self.numbers[index]