def schedule_arrival(self, transducer, params, pos): """ :param transducer: :param params: :param pos: """ distance_to = distance(pos, params["pos"]()) if distance_to > 0.01: # I should not receive my own transmissions receive_power = params["power"] - \ attenuation_db(params["frequency"], distance_to) # Speed of sound in water = 1482.0 m/s travel_time = distance_to / transducer.physical_layer.medium_speed packet = params['packet'] if DEBUG: transducer.logger.debug("{type} from {source} to {dest} will take {t} to get to me {d}m away".format( type=packet['type'], source=packet['source'], dest=packet['dest'], t=travel_time, d=int(distance_to)) ) yield Sim.hold, self, travel_time new_incoming_packet = IncomingPacket( db2linear(receive_power), params["packet"], transducer.physical_layer) Sim.activate( new_incoming_packet, new_incoming_packet.receive(params["duration"]))
def visit_greeter(self): name = 'greeter' if self.debug: print self.name, "at ",name, simpy.now() arrive = simpy.now() yield simpy.request, self, self.resources[name] wait = simpy.now() - arrive self.monitors[name].observe(wait) GREETER_ENABLED = True if GREETER_ENABLED: time = random.triangular(low=5/60.0, high=92/60.0, mode=23/60.0) #time = random.triangular(low=1.77/60.0, high=2.66/60.0, mode=2.38/60.0) tib = self.numberOfForms * time else: tib = 0 yield simpy.hold,self,tib yield simpy.release, self, self.resources[name] p = random.random() if self.preScreened: for i in self.visit_dispenser(): yield i else: for i in self.visit_screener(): yield i
def __init__(self, lang_x, lang_y, speak_rate, learn_rate, innovation): '''Initializes the agent. The variables used are: lang_x: the x parameter for this agent's language lang_y: the y parameter for this agent's language speak_rate: the rate at which this agent initiates conversation. time between conversation events is random.expovariate(speak_rate) speak_rate is floored at 0.001. learn_rate: the amount the speaker changes his language when he hears another speaker. Changes his language by learn_rate*(self.lang - other.lang) innovation: The rate at which this speaker randomly changes his language. time between innovation events is random.expovaraiate(innovation). next_conversation: the simulation time for the next conversation initiated by this agent. next_innovation: the simulation time for the next innovation introduced by this agent.''' Sim.Process.__init__(self) self.lang_x = lang_x self.lang_y = lang_y if speak_rate < 0.001: speak_rate = 0.001 self.speak_rate = speak_rate self.learn_rate = learn_rate self.innovation = innovation self.next_conversation = Sim.now() + expovariate(speak_rate) self.next_innovation = Sim.now() + expovariate(innovation) Agent.all_agents.append(self);
def test_schedule_input_presentation(): schedule_input_presentation(Tns.p1, Tns.s1, None, 10) assert sim.peek() == sim.now() schedule_input_presentation(Tns.p1, Tns.s1, start_t=20, duration=10) assert sim.Globals.allEventTimes() == [0, 20] from scheduling.pynn_scheduling import SIMULATION_END_T as end_t assert end_t == 30
def visit_medic(self): """ Lognormal with : logarithmic mean: 1.024 logarithmic std dev: 0.788 """ name = 'medic' if self.debug: print self.name, "at ",name, simpy.now() arrive = simpy.now() yield simpy.request, self, self.resources[name] wait = simpy.now() - arrive self.monitors[name].observe(wait) time = random.lognormvariate(mu=1.024, sigma=0.788) tib = self.numberOfForms * time yield simpy.hold,self,tib yield simpy.release, self, self.resources[name] p = random.random() if p < 0.99: for i in self.visit_dispenser(): yield i else: for i in self.visit_exit(): yield i
def configure_scheduling(): global SIMULATION_END_T sim.initialize() pynnn.setup() SIMULATION_END_T = 0 RATE_ENC_RESPAWN_DICT.clear() POP_ADAPT_DICT.clear()
def generate(self, number, interval, resources, monitors, times, exitResource, exitMonitor, entryResource, entryMonitor, preScreenedPercentage): """ @param number: number of entitites to generate (integer) @param interval: mean (in minutes) of times inter arrival @param resources: array of resources (servers) @param monitors: array of monitors to collect statistics @param times: avg. service time depending on the resource """ for i in range(number): customerName = "customer%d"%i c = Customer.Customer(name=customerName, resources=resources, monitors=monitors, times=times, exitResource=exitResource, exitMonitor=exitMonitor, entryResource=entryResource, entryMonitor=entryMonitor, preScreenedPercentage=preScreenedPercentage ) simpy.activate(c,c.visit()) t = random.expovariate(1.0/interval) yield simpy.hold, self, t
def update(self, now): #print(self.membrane_potential) if self.type == 'current': self.value_record.append(self.value) self.value *= self.output_current_decay if self.refact == 'yes': self.spikes_record.append(self.membrane_potential) return self.membrane_potential -= (self.membrane_potential - self.reset_potential)*0.1 #leak input = 0.0 for source in self.dendrites.keys(): if source.type == 'current': input += source.value elif source.type == 'voltage': pass # Voltage type input, add code here self.membrane_potential += input * 0.1 if self.membrane_potential < self.reset_potential: self.membrane_potential = self.reset_potential record = self.membrane_potential if self.membrane_potential >= self.threshold: if simpy.now() > self.last_firing_time: self.last_firing_time = simpy.now() event = Event(name = str(self) + " fire") simpy.activate(event, event.fire(self), delay = 0.0) record = self.spike_potential self.spikes_record.append(record)
def __init__(self, physical_layer, ambient_noise, channel_event, position_query, sir_thresh, on_success, name="a_transducer"): # A resource with large capacity, because we don't want incoming messages to queue, # We want them to interfere. Sim.Resource.__init__(self, name=name, capacity=1000) self.physical_layer = physical_layer self.logger = physical_layer.logger.getChild( "{0}".format(self.__class__.__name__)) # Interference is initialized as ambient noise self.interference = ambient_noise # Setup our event listener self.channel_event = channel_event self.listener = AcousticEventListener(self) Sim.activate(self.listener, self.listener.listen( self.channel_event, position_query)) # Function to call when we've received a packet self.on_success = on_success # SIR threshold self.SIR_thresh = sir_thresh # Controls the half-duplex behavior self.transmitting = False # Takes statistics about the collisions self.collision = False self.collisions = []
def operate(self, repairduration, triplength): """Travel until the trip is finished. The bus may break down down several times during the trip. Parameters: repairduration -- time to recover from a breakdown and continue triplength -- duration of the trip """ tripleft = triplength # "tripleft"is the driving time to finish trip # if there are no further breakdowns while tripleft > 0: yield sim.hold, self, tripleft # try to finish the trip # if a breakdown intervenes if self.interrupted(): print self.interruptCause.name, 'at %s' % sim.now() tripleft = self.interruptLeft # update driving time to finish # the trip if no more breakdowns self.interruptReset() # end self interrupted state #update next breakdown time sim.reactivate(br, delay=repairduration) #impose delay for repairs on self yield sim.hold, self, repairduration print 'Bus repaired at %s' % sim.now() else: # no breakdowns intervened, so bus finished trip break print 'Bus has arrived at %s' % sim.now()
def transmit_packet(self, packet): if not self.is_idle(): # The MAC protocol is the one that should check this before # transmitting self.logger.warn( "I should not do this... the channel is not idle!" "Trying to send {typ} to {dest}" "Currently have {q}".format( typ=packet['type'], dest=packet['dest'], q=self.transducer.activeQ )) if self.variable_power: tx_range = self.level2distance[str(packet["level"])] power = distance2intensity( self.bandwidth, self.freq, tx_range, self.SNR_threshold) else: power = self.transmit_power if power > self.max_output_power_used: self.max_output_power_used = power if power > self.max_output_power: power = self.max_output_power new_transmission = OutgoingPacket(self) Sim.activate( new_transmission, new_transmission.transmit(packet, power))
def test__schedule_output_rate_encoder(): setup_clean_simpy() pynnn.reset() assert sim.now() + 0. == pynnn.get_current_time() + 0. assert sim.allEventTimes() == [] _schedule_output_rate_encoder(Tns.rore1, 14, 89042) _schedule_output_rate_encoder(Tns.rore2, 100, None) assert sim.allEventTimes() == [14, 100]
def test_run_simulation_no_event(): run_simulation() assert sim.now() == 0 assert pynnn.get_current_time() == 0.0 assert sim.Globals.allEventTimes() == [] run_simulation(end_time=50) assert sim.now() == 50 assert pynnn.get_current_time() == 50.0 assert sim.Globals.allEventTimes() == []
def maybeSendShadowReads(self, replicaToServe, replicaSet): if (random.uniform(0, 1.0) < self.shadowReadRatio): for replica in replicaSet: if (replica is not replicaToServe): shadowReadTask = task.Task("ShadowRead", None) self.taskArrivalTimeTracker[shadowReadTask] =\ Simulation.now() self.taskSentTimeTracker[shadowReadTask] = Simulation.now() self.sendRequest(shadowReadTask, replica) self.rateLimiters[replica].forceUpdates()
def visit_exit(self): name = 'exit' if self.debug: print self.name, "at ",name, simpy.now() arrive = simpy.now() yield simpy.request, self, self.exitResource wait = simpy.now() - arrive self.exitMonitor.observe(wait) tib = 0#random.expovariate(1.0/self.times[name]) yield simpy.hold,self,tib yield simpy.release, self, self.exitResource
def test_activate(): """Test of "activate" call""" s = Simulation() s.initialize() r = Activatetest(sim=s) try: activate(r,r.run()) ## missing s. except FatalSimerror: pass else: assert False, "expected FatalSimerror" s.simulate(until=1)
def visit_entry(self): name = 'entry' if self.debug: print self.name, "at ",name, simpy.now() arrive = simpy.now() yield simpy.request, self, self.entryResource wait = simpy.now() - arrive self.entryMonitor.observe(wait) tib = 0 yield simpy.hold,self,tib yield simpy.release, self, self.entryResource
def listen(self, channel_event, position_query): """ :param channel_event: :param position_query: """ while True: yield Sim.waitevent, self, channel_event params = channel_event.signalparam sched = ArrivalScheduler(name="ArrivalScheduler" + self.name[-1]) Sim.activate(sched, sched.schedule_arrival( self.transducer, params, position_query()))
def new_agent(self): x = gauss(self.mean_x, self.sd_x) y = gauss(self.mean_y, self.sd_y) speak = expovariate(1.0/self.mean_speak) learn = expovariate(1.0/self.mean_learn) innovation = expovariate(1.0/self.mean_innovation) agent = Agent(x, y, speak, learn, innovate) self.agents.append(agent) Sim.activate(agent, agent.go()) return agent
def updateRates(self, replica, metricMap, task): # Cubic Parameters go here # beta = 0.2 # C = 0.000004 # Smax = 10 beta = self.cubicBeta C = self.cubicC Smax = self.cubicSmax hysterisisFactor = self.hysterisisFactor currentSendingRate = self.rateLimiters[replica].rate currentReceiveRate = self.receiveRate[replica].getRate() if (currentSendingRate < currentReceiveRate): # This means that we need to bump up our own rate. # For this, increase the rate according to a cubic # window. Rmax is the sending-rate at which we last # observed a congestion event. We grow aggressively # towards this point, and then slow down, stabilise, # and then advance further up. Every rate # increase is capped by Smax. T = Simulation.now() - self.lastRateDecrease[replica] self.lastRateIncrease[replica] = Simulation.now() Rmax = self.valueOfLastDecrease[replica] newSendingRate = C * (T - (Rmax * beta/C)**(1.0/3.0))**3 + Rmax if (newSendingRate - currentSendingRate > Smax): self.rateLimiters[replica].rate += Smax else: self.rateLimiters[replica].rate = newSendingRate elif (currentSendingRate > currentReceiveRate and Simulation.now() - self.lastRateIncrease[replica] > self.rateInterval * hysterisisFactor): # The hysterisis factor in the condition is to ensure # that the receive-rate measurements have enough time # to adapt to the updated rate. # So we're in here now, which means we need to back down. # Multiplicatively decrease the rate by a factor of beta. self.valueOfLastDecrease[replica] = currentSendingRate self.rateLimiters[replica].rate *= beta self.rateLimiters[replica].rate = \ max(self.rateLimiters[replica].rate, 0.0001) self.lastRateDecrease[replica] = Simulation.now() assert (self.rateLimiters[replica].rate > 0) alphaObservation = (replica.id, self.rateLimiters[replica].rate) receiveRateObs = (replica.id, self.receiveRate[replica].getRate()) self.rateMonitor.observe("%s %s" % alphaObservation) self.receiveRateMonitor.observe("%s %s" % receiveRateObs)
def __init__(self): Simulation.__init__(self) self.initialize() self.config = ConfigParser.ConfigParser() self.config.read(pyu.getlong('simulnet.ini','ini')) self.sim_opt = dict(self.config.items('Simulation')) self.lay_opt = dict(self.config.items('Layout')) self.meca_opt = dict(self.config.items('Mechanics')) self.net_opt = dict(self.config.items('Network')) self.loc_opt = dict(self.config.items('Localization')) self.save_opt = dict(self.config.items('Save')) self.sql_opt = dict(self.config.items('Mysql')) self.roomlist=[]
def visit(self, res): arrive = Sim.now() #arrival time print "%.3f: %s Arrived" % (Sim.now(), self.name) yield Sim.request, self, res wait = Sim.now()-arrive #waiting time print "%.3f: %s Waited for %6.3f" % (Sim.now(), self.name, wait) tib = random.expovariate(1.0/timeInBank) yield Sim.hold, self, tib yield Sim.release, self, res print "%.3f: %s took %.3f minutes to complete. Done." \ % (Sim.now(), self.name, tib)
def run(self, client, task, replicaThatServed): yield Simulation.hold, self, yield Simulation.waitevent, self, task.completionEvent delay = constants.NW_LATENCY_BASE + \ random.normalvariate(constants.NW_LATENCY_MU, constants.NW_LATENCY_SIGMA) yield Simulation.hold, self, delay # OMG request completed. Time for some book-keeping client.pendingRequestsMap[replicaThatServed] -= 1 client.pendingXserviceMap[replicaThatServed] = \ (1 + client.pendingRequestsMap[replicaThatServed]) \ * replicaThatServed.serviceTime client.pendingRequestsMonitor.observe( "%s %s" % (replicaThatServed.id, client.pendingRequestsMap[replicaThatServed])) client.responseTimesMap[replicaThatServed] = \ Simulation.now() - client.taskSentTimeTracker[task] client.latencyTrackerMonitor\ .observe("%s %s" % (replicaThatServed.id, Simulation.now() - client.taskSentTimeTracker[task])) metricMap = task.completionEvent.signalparam metricMap["responseTime"] = client.responseTimesMap[replicaThatServed] metricMap["nw"] = metricMap["responseTime"] - metricMap["serviceTime"] client.updateEma(replicaThatServed, metricMap) client.receiveRate[replicaThatServed].add(1) # Backpressure related book-keeping if (client.backpressure): client.updateRates(replicaThatServed, metricMap, task) client.lastSeen[replicaThatServed] = Simulation.now() if (client.REPLICA_SELECTION_STRATEGY == "ds"): client.latencyEdma[replicaThatServed]\ .update(metricMap["responseTime"]) del client.taskSentTimeTracker[task] del client.taskArrivalTimeTracker[task] # Does not make sense to record shadow read latencies # as a latency measurement if (task.latencyMonitor is not None): task.latencyMonitor.observe("%s %s" % (Simulation.now() - task.start, client.id))
def new_agent(self): x = gauss(self.mean_x, self.sd_x) y = gauss(self.mean_y, self.sd_y) speak = expovariate(1.0/self.mean_speak) learn = expovariate(1.0/self.mean_learn) innovation = expovariate(1.0/self.mean_innovation) location = gauss(self.mean_loc, self.loc_sd) radius = expovariate(1.0/self.mean_talk_radius) agent = LangRelativeAgent(x, y, speak, learn, innovation, location, radius) self.agents.append(agent) Sim.activate(agent, agent.go()) return agent
def run(self): start = Simulation.now() queueSizeBefore = len(self.server.queueResource.waitQ) yield Simulation.hold, self yield Simulation.request, self, self.server.queueResource waitTime = Simulation.now() - start # W_i serviceTime = self.server.getServiceTime() # Mu_i yield Simulation.hold, self, serviceTime yield Simulation.release, self, self.server.queueResource queueSizeAfter = len(self.server.queueResource.waitQ) self.task.sigTaskComplete({"waitingTime": waitTime, "serviceTime": serviceTime, "queueSizeBefore": queueSizeBefore, "queueSizeAfter": queueSizeAfter})
def adjust_weight(self, source, time_difference): #time_difference = t_pre - t_post if time_difference < 0 and self.dendrites[source] < self.weight_ceiling and (self.STDP == "on" or self.STDP == "left_only"): delta = self.dendrites[source] self.dendrites[source] += self.weight_ceiling * self.left_learning_rate * math.exp( time_difference / self.left_window_constant) if self.dendrites[source] > self.weight_ceiling: self.dendrites[source] = self.weight_ceiling delta = self.dendrites[source] - delta self.weights_record.append([simpy.now(), source, delta]) #print(simpy.now(), ":", "increse", str(source), "->", str(self), "to", self.dendrites[source]) elif time_difference >= 0 and self.dendrites[source] > 0 and (self.STDP == "on" or self.STDP == "right_only") and self.dendrites[source] < 256: delta = self.dendrites[source] self.dendrites[source] -= self.weight_ceiling * self.right_learning_rate * math.exp( -time_difference / self.right_window_constant) if self.dendrites[source] < 0: self.dendrites[source] = 0 delta = self.dendrites[source] - delta self.weights_record.append([simpy.now(), source, delta])
def schedule(self, task, replicaSet=None): replicaToServe = None firstReplicaIndex = None # Pick a random node and it's next RF - 1 number of neighbours if (self.accessPattern == "uniform"): firstReplicaIndex = random.randint(0, len(self.serverList) - 1) elif(self.accessPattern == "zipfian"): firstReplicaIndex = numpy.random.zipf(1.5) % len(self.serverList) if (replicaSet is None): replicaSet = [self.serverList[i % len(self.serverList)] for i in range(firstReplicaIndex, firstReplicaIndex + self.replicationFactor)] startTime = Simulation.now() self.taskArrivalTimeTracker[task] = startTime if(self.backpressure is False): sortedReplicaSet = self.sort(replicaSet) replicaToServe = sortedReplicaSet[0] self.sendRequest(task, replicaToServe) self.maybeSendShadowReads(replicaToServe, replicaSet) else: self.backpressureSchedulers[replicaSet[0]].enqueue(task, replicaSet)
def clock(self): ''' Convert to seconds because that's what the ExponentiallyDecayingSample assumes. Else, the internal Math.exp overflows. ''' return Simulation.now()/1000.0
def initialize(): print 'Initializing...' Sim.initialize() print 'Random seed: ' + repr(params['random seed']) random.seed(params['random seed']) pop0 = LinePopulation( 500.0, 500.0, params['initial x std dev'], params['initial y std dev'], params['mean speak'], params['mean learn'], params['mean innovation'], 500.0, params['pop0 loc sd'], params['mean talk radius'] ) for i in range(params['num agents']): pop0.new_agent() segregator = Segregator() Sim.activate( segregator, segregator.go() )
def go(self): # add ourselves to our initial language yield Sim.put, self, self.language.population, 1 while 1: yield Sim.hold, self, Parameters.timeStep # For each other available language for otherLang in Parameters.languages: if otherLang == self.language: continue if self.shouldSwitch(otherLang): if Parameters.verbose: print "Time %s: %s: switching from %s to %s (with probability %s)" % ( Sim.now(), self, self.language, otherLang, probChange, ) yield Sim.hold, self, (Parameters.timeStep / 2.0) # make sure we wait until other agents make their decision before switching yield Sim.get, self, self.language.population, 1 yield Sim.put, self, otherLang.population, 1 self.language = otherLang break
def runModel(self): # Initialize Simulation instance self.initialize() self.parking = simulation.Resource(name="Parking lot", unitName="spaces", capacity=self.spaces, sim=self) for i in range(self.nrCars): auto = Car(name="Car%s" % i, sim=self) self.activate(auto, auto.park()) self.simulate(until=100)
def test_observe_no_time(): """Observe with time being picked up from now()""" s = Simulation() s.initialize() m = Monitor(name='No time', sim=s) t = Thing(m, sim=s) s.activate(t, t.execute(), 0.0) s.simulate(until=20.0) assert m.yseries() == (0, 10, 5), 'yseries wrong:%s' % (m.yseries(), ) assert m.tseries() == (0, 10, 20), 'tseries wrong:%s' % (m.tseries(), ) assert m.total() == 15, 'total wrong:%s' % m.total() assert m.timeAverage( 10.0) == 5.0, 'time average is wrong: %s' % m.timeAverage(10.0)
def run(self): while (1): print("disk") if len(G.master_node_attr.master_disk_queue) == 0: yield Sim.passivate, self print("reactivated master disk") #print("disk alloted for req no",G.master_node_attr.master_disk_queue[0][0]) #try: #yield Sim.hold,self,abs(constant.RND.expovariate(G.master_node_attr.master_disk_queue[0][2] - G.master_node_attr.master_disk_curr_head)) #wait for time proportional to the distance from the current head yield Sim.hold, self, 6 + constant.CLOUD_DISK_RATE #except: # print("an exception has occured in master") #G.master_node_attr.master_disk_curr_head = G.master_node_attr.master_disk_queue[0][2]#reactivate query object Sim.reactivate(G.master_node_attr.master_disk_queue[0][1]) G.master_node_attr.master_disk_queue.pop(0)
def run(self, node_no) : while(1) : print("hello i am slave node ", node_no) if len(G.slave_nodes_attr[node_no].node_queries_queue) == 0 : #if queue is empty, sleep yield Sim.passivate, self print("reactivated", node_no) yield Sim.hold,self,1#0.5*abs(constant.RND.expovariate(constant.QUERY_RATE)) print("query_created for req no",G.slave_nodes_attr[node_no].node_queries_queue[0][0]) time_of_use = self.sim.now() query_obj = query() #create query object to process the query Sim.activate(query_obj,query_obj.run(G.slave_nodes_attr[node_no].node_queries_queue[0], node_no)) if self.interrupted() : yield Sim.hold , self ,1#abs(self.interruptLeft - time_of_use) #check for break down G.slave_nodes_attr[node_no].node_queries_queue.pop(0) #remove request after processed
def run_simulation(end_time=None): """Runs the simulation while keeping SimPy and PyNN synchronized at event times. Runs until no event is scheduled unless end_time is provided. if end_time is given, runs until end_time.""" global SIMULATION_END_T def run_pynn(end_t): pynn_now = pynnn.get_current_time() pynn_now_round = round(pynn_now, PYNN_TIME_ROUNDING) delta_t = round(end_t - pynn_now_round, PYNN_TIME_ROUNDING) if pynn_now <= pynn_now_round and delta_t > PYNN_TIME_STEP: delta_t = round(delta_t - PYNN_TIME_STEP, PYNN_TIME_ROUNDING) if delta_t > 0: # necessary because run(0) may run PyNN by timestep pynnn.run(delta_t) # neuralensemble.org/trac/PyNN/ticket/200 is_not_end = None if end_time == None: # Would testing len(sim.Globals.allEventTimes()) be faster? is_not_end = lambda t: not isinstance(t, sim.Infinity) else: DummyProcess().start(at=end_time) e_t = end_time is_not_end = lambda t: t <= e_t for e in RATE_ENC_RESPAWN_DICT.iteritems(): if e[1] == None: continue renc, start_time, end_time = e[0], e[1][0], e[1][1] _schedule_output_rate_encoder(renc, start_t=start_time, end_t=end_time) if SIMULATION_END_T < end_time: SIMULATION_END_T = end_time RATE_ENC_RESPAWN_DICT.clear( ) # unnecessary as _schedule_output_rate_encoder performs cleanup t_event_start = sim.peek() while is_not_end(t_event_start): LOGGER.debug("Progressing to SimPy event at time %s", t_event_start) run_pynn(t_event_start) # run until event start sim.step() # process the event run_pynn(get_current_time()) # run PyNN until event end t_event_start = sim.peek() if SIMULATION_END_T < get_current_time(): SIMULATION_END_T = get_current_time()
def run(self): seed(seed) req = 0 curr_node_index = 0 print("hi i am the master node") while (self.sim.now() < constant.MAXTIME): req = req + 1 operation_no = randint(0, 1) #chooses operation no_of_slaves = constant.NO_SLAVE_NODES #distributing computation proportionally between master and slaves choice = randint(0, no_of_slaves + 1) #master computation if (choice % 3) == 0: print("Computing in the master node") G.master_node_attr.master_node_queries_queue.append( (req, operation_no)) #adds the request to master queue print("sent request no:", req, "to master node") if len(G.master_node_attr.master_node_queries_queue ) == 1: #if the node did not have any queries Sim.reactivate(G.master_node_attr.master_node_id ) #if the node is idle, reactivate yield Sim.hold, self, abs( constant.RND.expovariate(constant.QUERY_RATE)) #slave computation else: print("Computing in the slave nodes") node_no = curr_node_index curr_node_index = (curr_node_index + 1) % ( constant.NO_SLAVE_NODES) #choose slave node using RR G.slave_nodes_attr[node_no].node_queries_queue.append( (req, operation_no)) #adds the request to queue print("sent request no:", req, "to node", node_no) if len(G.slave_nodes_attr[node_no].node_queries_queue ) == 1: #if the node did not have any queries Sim.reactivate(G.slave_nodes_attr[node_no].slave_node_id ) #if the node is idle, reactivate yield Sim.hold, self, 1 #abs(constant.RND.expovariate(constant.QUERY_RATE))
def runModel(self): self.initialize() self.parking = simulation.Resource(name="Parking lot", capacity=self.spaces, sim=self) for i in range(self.nrCars): auto = CarModel.Car(name="Car%s" % i, sim=self) self.activate(auto, auto.park()) for i in range(self.nrTrucks): truck = Van(name="Van%s" % i, sim=self) self.activate(truck, truck.park()) self.simulate(until=100)
def tryAcquire(self): tokens = min( self.maxTokens, self.tokens + self.rate / float(self.rateInterval) * (Simulation.now() - self.lastSent)) if (tokens >= 1): self.tokens = tokens return 0 else: assert self.tokens < 1 timetowait = (1 - tokens) * self.rateInterval / self.rate return timetowait
def test_rate_calculation_process_corrected_time(): setup_clean_simpy() assert sim.now() + 0. == pynnn.get_current_time() + 0. scheduling.pynn_scheduling.SIMULATION_END_T = 100 rc1 = RateCalculation(Tns.rore1, end_t=None, correct_event_t=2) rc2 = RateCalculation(Tns.rore2, end_t=None, correct_event_t=None) assert rc1.corrected_time == 2 assert rc2.corrected_time == 0 run_simulation(1) assert rc1.corrected_time == 2 run_simulation(2) assert rc1.corrected_time == 2
def run(self): seed(seed) req = 0 curr_node_index = 0 print("hi i am the master node") while (self.sim.now() < constant.MAXTIME): req = req + 1 operation_no = randint(0, 1) #chooses operation node_no = curr_node_index curr_node_index = (curr_node_index + 1) % ( constant.NO_SLAVE_NODES) #choose slave node using RR G.slave_nodes_attr[node_no].node_queries_queue.append( (req, operation_no)) #adds the request to queue print("sent request no:", req, "to node", node_no) if len(G.slave_nodes_attr[node_no].node_queries_queue ) == 1: #if the node did not have any queries Sim.reactivate(G.slave_nodes_attr[node_no].slave_node_id ) #if the node is idle, reactivate yield Sim.hold, self, abs( constant.RND.expovariate(constant.QUERY_RATE))
def run(self, data, txRate): print "%7.4f Starting transmitter: %s" % (simpy.now(), self.name) lastTx = -1000 for sample in data: time = sample[0] date = sample[1] if time >= lastTx + txRate: lastTx = time self.transmitPacket(sample) yield simpy.hold, self, txRate
def testBackPressureLoopTwoServers(self): Simulation.initialize() s1 = server.Server(1, resourceCapacity=1, serviceTime=4, serviceTimeModel="constant") s2 = server.Server(2, resourceCapacity=1, serviceTime=4, serviceTimeModel="constant") c1 = client.Client(id_="Client1", serverList=[s1, s2], replicaSelectionStrategy="primary", accessPattern="uniform", replicationFactor=2, backpressure=True, shadowReadRatio=0.0, rateInterval=20, cubicC=0.000004, cubicSmax=10, cubicBeta=0.2, hysterisisFactor=2, demandWeight=1.0) observer = Observer([s1, s2], c1) Simulation.activate(observer, observer.testBackPressureLoopTwoServers(), at=0.1) Simulation.simulate(until=100)
def test_updating_trajectory(self): """Creates a vehicle, starts the sim with it travelling at a fixed velocity, then changes the trajectory 11 seconds after the start. When the trajectory is changed, the vehicle is straddling two track segments.""" v0 = vehicle.BaseVehicle(0, self.ts0, 50, 5) # fixed velocity, 5 m/s # stop at 50 meters on ts2 spline = CubicSpline( [2, 6.2324427610773778, 20.693227678868798, 46.258768272424305, 46.67126664464751, 49.999999999978492], # pos [5, 5.8065992674127145, 9.581117951456692, 5.3923182554918929, 4.9953989633679301, -9.5825569701446511e-12], # vel [0, 2.0082321422244926, 2.0082321422244926, -4.9976989522066617, -4.9976989522066617, -1.8332002582610585e-12], # accel [2.5, 0, -2.5, 0, 2.5], # jerk [11, 11.803292856889797, 13.682815948210798, 16.48518838598326, 16.564608794439177, 18.56368837532111]) # time spline_msg = api.Spline() spline.fill_spline_msg(spline_msg) controller = MockController() controller.add_cmd(11, v0.process_spline_msg, spline_msg) Sim.activate(controller, controller.run()) Sim.activate(v0, v0.ctrl_loop()) Sim.simulate(until=20) self.assertAlmostEqual(v0.pos, 50, self.PLACES) self.assertTrue(v0.loc is self.ts2) self.assertAlmostEqual(v0.vel, 0, self.PLACES) self.assertAlmostEqual(v0.accel, 0, self.PLACES) self.assertAlmostEqual(v0.tail_pos, 45, self.PLACES) self.assertTrue(v0.tail_loc is self.ts2) self.assertTrue(len(self.ts0.vehicles) == 0) self.assertTrue(len(self.ts1.vehicles) == 0) self.assertTrue(len(self.ts2.vehicles) == 1)
def run(self): while (1): proc_time = 0 global query_choice_master if query_choice_master < constant.QUERY1_RATE: proc_time = constant.master_cloud_proc_time_1 elif query_choice_master < constant.QUERY2_RATE: proc_time = constant.master_cloud_proc_time_2 elif query_choice_master < constant.QUERY3_RATE: proc_time = constant.master_cloud_proc_time_3 elif query_choice_master < constant.QUERY4_RATE: proc_time = constant.master_cloud_proc_time_4 elif query_choice_master < constant.QUERY5_RATE: proc_time = constant.master_cloud_proc_time_5 elif query_choice_master < constant.QUERY6_RATE: proc_time = constant.master_cloud_proc_time_6 else: proc_time = constant.master_cloud_proc_time_7 if (query_choice_master < 9): query_choice_master = query_choice_master + 1 else: query_choice_master = 0 print("processor") if len(G.master_node_attr.master_processor_queue ) == 0: #sleep if there are no requests yield Sim.passivate, self print("reactivated master processor") #print("processor alloted for req no",G.master_node_attr.master_processor_queue[0][0]) yield Sim.hold, self, 6 + proc_time #yield Sim.hold,self, 4*abs(constant.RND.expovariate(constant.QUERY_RATE)) Sim.reactivate(G.master_node_attr.master_processor_queue[0] [1]) #reactivate the query object G.master_node_attr.master_processor_queue.pop( 0) #remove request from queue
def run(self, request_query): while (1): #yield Sim.hold,self,6000*abs(constant.RND.expovariate(constant.QUERY_RATE)) G.master_node_attr.master_processor_queue.append( (request_query, self)) if len(G.master_node_attr.master_processor_queue) >= 1: Sim.reactivate(G.master_node_attr.master_processor_id) yield Sim.passivate, self yield Sim.hold, self, 82 + constant.RND.expovariate( constant.QUERY_RATE) disk_addr = randint(1, 360) #generate disk address #yield Sim.hold,self,4000*constant.RND.expovariate(constant.QUERY_RATE) G.master_node_attr.master_disk_queue.append( (request_query, self, disk_addr)) if len(G.master_node_attr.master_disk_queue) >= 1: Sim.reactivate(G.master_node_attr.master_disk_id) yield Sim.passivate, self print("query done - master")
def run(self, request_query, node_no): while (1): #print("query done") #yield Sim.hold,self,2000*abs(constant.RND.expovariate(constant.QUERY_RATE)) G.slave_nodes_attr[node_no].processors_queue.append( (request_query, node_no, self)) if len(G.slave_nodes_attr[node_no].processors_queue) >= 1: Sim.reactivate(G.slave_nodes_attr[node_no].processor_id) yield Sim.passivate, self yield Sim.hold, self, 82 + constant.RND.expovariate( constant.QUERY_RATE) disk_addr = randint(1, 360) #generate disk address #yield Sim.hold,self,2000*abs(constant.RND.expovariate(constant.QUERY_RATE)) G.slave_nodes_attr[node_no].disk_queue.append( (request_query, node_no, self, disk_addr)) if len(G.slave_nodes_attr[node_no].disk_queue) >= 1: Sim.reactivate(G.slave_nodes_attr[node_no].disk_id) yield Sim.passivate, self print("query done")
def run_one_test(args, log, ezsegway, flow_file): ezsegway.load_flows_for_test(flow_file) # Setup signal handlers signal.signal(signal.SIGTERM, sigterm_handler) signal.signal(signal.SIGINT, sigint_handler) start = time.clock() global_vars.start_time = start # Begin simulation Simulation.simulate(until=args.simulationDuration) finish = time.clock() delta = datetime.timedelta(seconds=(finish - start)) computation_time = datetime.timedelta( seconds=(global_vars.finish_computation_time - start)) prioritizing_time = datetime.timedelta( seconds=(global_vars.finish_prioritizing_time - global_vars.finish_computation_time)) log.info("Simulation duration: %s" % Simulation.now()) log.info("Simulation finished in %s" % str(delta)) log.info("Computation finished in %s" % str(computation_time)) log.info("Prioritizing finished in %s" % str(prioritizing_time)) return Simulation.now()
def test_rate_calculation_actions(): setup_clean_simpy() pynnn.reset() assert sim.now() + 0. == pynnn.get_current_time() + 0. scheduling.pynn_scheduling.SIMULATION_END_T = 100 rc1 = RateCalculation(Tns.rore1) Tns.rore1.update_rates = Mock() rc2 = RateCalculation(Tns.rore2, end_t=11) Tns.rore2.update_rates = Mock() rc1.start(at=0) rc2.start(at=99) run_simulation() assert Tns.rore1.update_rates.call_count == 100 / Tns.rore1_update_p + 2 assert Tns.rore2.update_rates.call_count == 1
def test_schedule_output_rate_calculation(): setup_clean_simpy() pynnn.reset() setup_registered_rectinilinear_ouput_rate_encoders() Tns.p1.record(to_file=False) Tns.p2.record(to_file=False) assert sim.now() + 0. == pynnn.get_current_time() + 0. scheduling.pynn_scheduling.SIMULATION_END_T = 200 schedule_output_rate_calculation(Tns.p1) schedule_output_rate_calculation(Tns.p2, start_t=8, duration=100) assert sim.Globals.allEventTimes() == [PYNN_TIME_STEP, 8] run_simulation(11) assert sim.Globals.allEventTimes() == \ [Tns.rore1_update_p, 8 + Tns.rore2_update_p]
def __init__(self): self.numberOfcores = 1 #self.scale_par = scalePar #self.contentType = 'All' self.workload = list([0.0]) self.rand = random.mtrand.RandomState() self.process = Simulation() self.queueLengthMon = Monitor(sim=self.process, name = 'Queue length') self.workLoadMon = Monitor(sim=self.process, name = 'Workload') self.waitTimeMon = Monitor(sim=self.process, name = 'Wait Time in System') self.processorCores = Resource(self.numberOfcores,sim=self.process,monitored=True, monitorType=Monitor) #number of cores self.queueLength = list([0]) self.serviceTimesMon = list([]) self.interArrivalTime = Tally(sim=self.process)
def add(self, requests): now = int(Simulation.now() / self.interval) if (now - self.last < self.interval): self.count += requests if (now > self.last): # alpha = (now - self.last)/float(self.interval) alpha = 0.9 self.rate = alpha * self.count + (1 - alpha) * self.rate self.last = now self.count = 0 else: self.rate = self.count self.last = now self.count = 0
def getServiceTime(self): serviceTime = 0.0 if (self.serviceTimeModel == "random.expovariate"): serviceTime = random.expovariate(1.0 / (self.serviceTime)) elif (self.serviceTimeModel == "constant"): serviceTime = self.serviceTime elif (self.serviceTimeModel == "math.sin"): serviceTime = self.serviceTime \ + self.serviceTime \ * math.sin(1 + Simulation.now()/100) else: print "Unknown service time model" sys.exit(-1) return serviceTime
def computeExpectedDelayRefresh(self, replica): total = 0 if (len(self.expectedDelayMap[replica]) != 0): metricMap = self.expectedDelayMap[replica] if (Simulation.now() - metricMap["receiveTime"] < 100): twiceNetworkLatency = metricMap["responseTime"] else: twiceNetworkLatency = 0.5 total += twiceNetworkLatency self.edScoreMonitor.observe( "%s %s %s %s" % (replica.id, metricMap["queueSizeAfter"], metricMap["serviceTime"], total)) else: return 0 return total
def test_collision(self): # TODO: Test post collision state, once decided what to do upon collisions... """Simple collision test. Two vehicles on the same track segment, the rear one with an initial velocity, the lead one stopped. Simulation halts before the 'rear' vehicle leaves the segment.""" v0 = vehicle.BaseVehicle(0, self.ts0, 10, 5) # fixed speed, 5 m/s v1 = vehicle.BaseVehicle(1, self.ts0, 50, 0) Sim.activate(v0, v0.ctrl_loop()) Sim.activate(v1, v1.ctrl_loop()) Sim.simulate(until=16) self.assertAlmostEqual(v0.pos, 90, self.PLACES)
def test_activate(): """Test of "activate" call""" s = Simulation() s.initialize() r = Activatetest(sim=s) try: activate(r, r.run()) ## missing s. except FatalSimerror: pass else: assert False, "expected FatalSimerror" s.simulate(until=1)
def run(self): # Start each process with a minor delay while (1): yield Simulation.hold, self, self.SNITCHING_INTERVAL # Adaptation of DynamicEndpointSnitch algorithm maxLatency = 1.0 maxPenalty = 1.0 latencies = [ entry.get_snapshot().get_median() for entry in self.client.latencyEdma.values() ] latenciesGtOne = [ latency for latency in latencies if latency > 1.0 ] if (len(latencies) == 0): # nothing to see here continue maxLatency = max(latenciesGtOne) if len(latenciesGtOne) > 0 \ else 1.0 penalties = {} for peer in self.client.serverList: penalties[peer] = self.client.lastSeen[peer] penalties[peer] = Simulation.now() - penalties[peer] if (penalties[peer] > self.SNITCHING_INTERVAL): penalties[peer] = self.SNITCHING_INTERVAL penaltiesGtOne = [ penalty for penalty in penalties.values() if penalty > 1.0 ] maxPenalty = max(penalties.values()) \ if len(penaltiesGtOne) > 0 else 1.0 for peer in self.client.latencyEdma: score = self.client.latencyEdma[peer] \ .get_snapshot() \ .get_median() / float(maxLatency) if (peer in penalties): score += penalties[peer] / float(maxPenalty) else: score += 1 assert score >= 0 and score <= 2.0 self.client.dsScores[peer] = score
def sendRequest(self, request, toServer): delay = constants.NW_LATENCY_BASE + \ random.normalvariate(constants.NW_LATENCY_MU, constants.NW_LATENCY_SIGMA) # Send out the request requestDeliveryProcess = DeliverRequest() Simulation.activate(requestDeliveryProcess, requestDeliveryProcess.run(request, toServer, delay), at=Simulation.now()) responseHandler = ResponseHandler() Simulation.activate(responseHandler, responseHandler.run(request), at=Simulation.now())
def f_rate(self, np_a, t=None, update_history=None): """Returns the weighted average of the rates recorded in the differences of the array np_a. The t parameter can be used to silence rate information older than t units of time, which is necessary to select the firing rate pertaining to one event only. If now-t does not fall on a recording boundary, the more recent boundary is used, otherwise the rate recording may be contaminated by spikes older than t. If that leaves no record available (i.e. t < age of previous record), an error is raised. The update_history parameter overrides the rate encoder's update history, it should only be used for testing.""" if update_history == None: update_history = self.update_history update_hist = numpy.append(update_history[self.idx+1:], update_history[:self.idx+1]) cut_i = 0 if t != None: cut_t = sim.now() - t cut_i = numpy.searchsorted(update_hist, cut_t, side='left') # t must not be in the last interval: if cut_i >= len(update_hist) - 1: raise SimulationError("The rate encoder resolution is " "insufficient to get any rate " "data on the requested period.") update_hist = update_hist[cut_i:] update_dt = numpy.diff(update_hist) * 1. np_a = numpy.append(np_a[self.idx+1:], np_a[:self.idx+1]) np_a = np_a[cut_i:] rates = numpy.diff(np_a) window_width = min(sum(update_dt), self.window_width) if t!= None \ else self.window_width return self.make_hist_weights_vec(update_history=update_hist, window_width=window_width, idx=len(update_hist) ).dot(rates / update_dt)
def test2(self): yield Simulation.hold, self, rl = client.RateLimiter("RL-1", self, 10, 20) rl.rate = 5 # Five tokens per 20ms rl.tokens = 10 assert rl.getTokens() == 10 # Let's now force consume tokens to simulate # repair activity rl.tryAcquire() rl.tokens = 10 rl.lastSent = Simulation.now() self.forceConsumeNTokens(rl, 30) assert rl.getTokens() == -20, rl.getTokens() # No juice. self.consumeNTokens(rl, 100) assert rl.getTokens() == -20 yield Simulation.hold, self, 20.0 # Tokens update despite negative waiting times assert rl.getTokens() == -15 # Test waiting time accuracy. # The number of tryAcquires() should not affect # the waiting time. timeToWait = rl.tryAcquire() yield Simulation.hold, self, timeToWait - 1.1 assert rl.getTokens() > 0 and rl.getTokens() < 1 # ... test for side-effects self.consumeNTokens(rl, 100) assert rl.getTokens() > 0 and rl.getTokens() < 1.0 timeToWait = rl.tryAcquire() assert timeToWait > 0.09 and timeToWait < 1.1, timeToWait yield Simulation.hold, self, timeToWait
def run(self, aseed): random.seed(aseed) agents = Sim.Resource(capacity=F.numAgents, name="Service Agents", unitName="Agent", monitored=True, sim=self) specialagents = Sim.Resource(capacity=F.numSpecialists, name="Specialist Agents", unitName="Specialist", monitored=True, sim=self) agentspm = Sim.Resource(capacity=F.numAgentsPM, name="Service AgentsPM", unitName="Agent", monitored=True, sim=self) specialagentspm = Sim.Resource(capacity=F.numSpecialistsPM, name="Specialist AgentsPM", unitName="Specialist", monitored=True, sim=self) self.meanTBA = 0.0 self.initialize() s = Source('Source', sim=self) a = Arrival('Arrival Rate', sim=self) tchange = SecondShift('PM Shift', sim=self) self.Regular10 = Sim.Monitor(name="Regular time", ylab='hours', sim=self) self.Special10 = Sim.Monitor(name="Special time", ylab='hours', sim=self) self.activate(a, a.generate(F.aRateperhour)) self.activate(s, s.generate(resourcenormal=agents, resourcespecial=specialagents, resourcenormalPM=agentspm, resourcespecialPM=specialagentspm), at=0.0) self.activate(tchange, tchange.generate(F.tPMshiftchange)) self.simulate(until=F.maxTime)