class TicketCounterSimulation: # Create a simulation object. def __init__( self, numAgents, numMinutes, betweenTime, serviceTime ): # Parameters supplied by the user. self._arriveProb = 1.0 / betweenTime self._serviceTime = serviceTime self._numMinutes = numMinutes self._psgArrTimeList = random.sample( range(numMinutes), int(self._arriveProb * numMinutes) ) # Simulation components. self._passengerQ = Queue() self._theAgents = myArray( numAgents ) for i in range( numAgents ): self._theAgents[i] = TicketAgent(i+1) # Computed during the simulation. self._totalWaitTime = 0 self._numPassengers = 0 # Run the simulation using the parameters supplied earlier. def run( self ): for curTime in range(self._numMinutes): self._handleArrive( curTime ) self._handleBeginService( curTime ) self._handleEndService( curTime ) # Print the simulation results. def printResults( self ): numServed = self._numPassengers - len(self._passengerQ) avgWait = float( self._totalWaitTime ) / numServed print '' print 'Number of passengers served = %d ' % numServed print 'Number of passengers remaining in line = %d ' % len(self._passengerQ) print 'The average wait time was %4.2f minutes. ' % avgWait # The help function used in run. def _handleArrive( self, curTime ): # Handles simulation rule 1 if curTime in self._psgArrTimeList: cur_psg = Passenger( self._numPassengers + 1, curTime ) #print 'Time %4d : Passenger %d arrived.' % (curTime, cur_psg.idNum()) self._passengerQ.enqueue( cur_psg ) self._numPassengers += 1 def _handleBeginService( self, curTime ): # Handles simulation rule 2 if len(self._passengerQ): for i in range(len(self._theAgents)): if self._theAgents[i].isFree(): cur_psg = self._passengerQ.dequeue() #print 'Time %4d : Agent %d Started serving passenger %d.' % (curTime, self._theAgents[i].idNum(), cur_psg.idNum()) self._theAgents[i].startService(cur_psg, curTime + self._serviceTime) self._totalWaitTime += ( curTime - cur_psg.timeArrived() ) break def _handleEndService( self, curTime ): # Handles simulation rule 3 for i in range(len(self._theAgents)): if self._theAgents[i].isFinished( curTime ): cur_psg = self._theAgents[i].stopService()
def _buildTree(self, expStr): expQ = Queue() for token in expStr: expQ.enqueue(token) # Create an empty root node self._expTree = _ExpTreeNode(None) # Call the recursive function to build the expression tree self._recBuildTree(self._expTree, expQ)
class TicketCounterSimulation: def __init__(self, numAgents, numMins, arrivalTime, serviceTime): self._arrivalProb = 1.0 / arrivalTime self._serviceTime = serviceTime self._numMins = numMins self._passengerQ = Queue() self._theAgents = Array(numAgents) for i in range(numAgents): self._theAgents[i] = TicketAgent(i + 1) self._totalWaitTime = 0 self._numPassengers = 0 def run(self): for curTime in range(0, self._numMins + 1): self._handleArrival(curTime) self._handleBeginService(curTime) self._handleEndService(curTime) def printResults(self): numServed = self._numPassengers - len(self._passengerQ) avgWaitTime = float(self._totalWaitTime) / numServed print "" print "Number of passengers served = ", numServed print "Number of passengers remaining in line = %2d" % \ len(self._passengerQ) print "The average wait time was %4.2f minutes." % avgWaitTime def _handleArrival(self, curTime): prob = random.random() / 4.0 if prob >= self._arrivalProb: self._numPassengers += 1 passenger = Passenger(self._numPassengers, curTime) self._passengerQ.enqueue(passenger) print "Time %2d: Passenger %2d arrived." % (curTime, \ self._numPassengers) def _handleBeginService(self, curTime): while not self._passengerQ.isEmpty(): freeFlag = False for i in range(len(self._theAgents)): if self._theAgents[i].isFree(): freeFlag = True passenger = self._passengerQ.dequeue() self._theAgents[i].startService(passenger, \ curTime + self._serviceTime) print "Time %2d: Agent %2d started serving passenger %2d." % \ (curTime, self._theAgents[i]._idNum, passenger._idNum) self._totalWaitTime += curTime - passenger.timeArrived() break if freeFlag == False: return def _handleEndService(self, curTime): for i in range(len(self._theAgents)): if self._theAgents[i].isFinished(curTime): passenger = self._theAgents[i].stopService() print "Time %2d: Agent %2d stopped serving passenger %2d." % \ (curTime, self._theAgents[i]._idNum, passenger._idNum)
class TicketCounterSimulation: def __init__(self, numAgents, numMins, arrivalTime, serviceTime): self._arrivalProb = 1.0/arrivalTime self._serviceTime = serviceTime self._numMins = numMins self._passengerQ = Queue() self._theAgents = Array(numAgents) for i in range(numAgents): self._theAgents[i] = TicketAgent(i + 1) self._totalWaitTime = 0 self._numPassengers = 0 def run(self): for curTime in range(0, self._numMins+1): self._handleArrival(curTime) self._handleBeginService(curTime) self._handleEndService(curTime) def printResults(self): numServed = self._numPassengers - len(self._passengerQ) avgWaitTime = float(self._totalWaitTime)/numServed print "" print "Number of passengers served = ", numServed print "Number of passengers remaining in line = %2d" % \ len(self._passengerQ) print "The average wait time was %4.2f minutes." % avgWaitTime def _handleArrival(self, curTime): prob = random.random()/4.0 if prob >= self._arrivalProb: self._numPassengers += 1 passenger = Passenger(self._numPassengers, curTime) self._passengerQ.enqueue(passenger) print "Time %2d: Passenger %2d arrived." % (curTime, \ self._numPassengers) def _handleBeginService(self, curTime): while not self._passengerQ.isEmpty(): freeFlag = False for i in range(len(self._theAgents)): if self._theAgents[i].isFree(): freeFlag = True passenger = self._passengerQ.dequeue() self._theAgents[i].startService(passenger, \ curTime + self._serviceTime) print "Time %2d: Agent %2d started serving passenger %2d." % \ (curTime, self._theAgents[i]._idNum, passenger._idNum) self._totalWaitTime += curTime - passenger.timeArrived() break if freeFlag == False: return def _handleEndService(self, curTime): for i in range(len(self._theAgents)): if self._theAgents[i].isFinished(curTime): passenger = self._theAgents[i].stopService() print "Time %2d: Agent %2d stopped serving passenger %2d." % \ (curTime, self._theAgents[i]._idNum, passenger._idNum)
def __init__(self, numAgents, numMins, arrivalTime, serviceTime): self._arrivalProb = 1.0 / arrivalTime self._serviceTime = serviceTime self._numMins = numMins self._passengerQ = Queue() self._theAgents = Array(numAgents) for i in range(numAgents): self._theAgents[i] = TicketAgent(i + 1) self._totalWaitTime = 0 self._numPassengers = 0
def breathFirstTrav(self): """Breath-first traversal of an AVL tree.""" from llistqueue import Queue trQ = Queue() if self._root is None: print "<empty AVL tree>" return trQ.enqueue(self._root) while not trQ.isEmpty(): node = trQ.dequeue() print node.key, if node.bfactor == LEFT_HIGH: print " >", elif node.bfactor == EQUAL_HIGH: print " =", else: print " <", if node.left is not None: print "(L: ", node.left.key, ")", trQ.enqueue(node.left) if node.right is not None: print "(R: ", node.right.key, ")", trQ.enqueue(node.right) print ""
def _buildTree(self, expStr): # Build a queue containing the tokens in the expression string. expQ = Queue() for token in expStr: expQ.enqueue(token) # Create an empty root node. self._expTree = _ExpTreeNode(None) # Call the recursive function to build the expression tree. self._recBuildTree(self._expTree, expQ)
def __init__(self, numAgents, numMinutes, betweenTime, serviceTime): # Parameters supplied by the user. self._arriveProb = 1.0 / betweenTime self._serviceTime = serviceTime self._numMinutes = numMinutes # Simulation components. self._passengerQ = Queue() self._theAgents = Array(numAgents) for i in range(numAgents): self._theAgents[i] = TicketAgent(i + 1) # Computed during the simulation. self._totalWaitTime = 0 self._numPassengers = 0
def radixSort(intList, numDigits): # Create an array of queues to represent the bins. binArray = Array(10) for k in range(10): binArray[k] = Queue() # The value of the current column. column = 1 # Iterate over the number of digits. for d in range(numDigits): # Distribute the keys across the 10 bins. for key in intList: digit = (key // column) % 10 binArray[digit].enqueue(key) # Gather the keys from the bins and place them back in intList i = 0 for bin in binArray: while not bin.isEmpty(): intList[i] = bin.dequeue() i += 1 # Advance to the next column value. column *= 10
def __init__( self, numAgents, numMinutes, betweenTime, serviceTime ): # Parameters supplied by the user. self._arriveProb = 1.0 / betweenTime self._serviceTime = serviceTime self._numMinutes = numMinutes # Simulation components. self._passengerQ = Queue() self._theAgents = Array( numAgents ) for i in range( numAgents ) : self._theAgents[i] = TicketAgent(i+1) # Computed during the simulation. self._totalWaitTime = 0 self._numPassengers = 0 # Run the simulation using the parameters supplied earlier. def run( self ): for curTime in range(self._numMinutes + 1) : self._handleArrival( curTime ) self._handleBeginService( curTime ) self._handleEndService( curTime ) # Print the simulation results. def printResults( self ): numServed = self._numPassengers - len(self._passengerQ) avgWait = float( self._totalWaitTime ) / numServed print( "" ) print( "Number of passengers served = ", numServed ) print( "Number of passengers remaining in line = %d" % len(self._passengerQ) ) print( "The average wait time was %4.2f minutes." % avgWait )
def __init__(self, numAgents, numMins, arrivalTime, serviceTime): self._arrivalProb = 1.0/arrivalTime self._serviceTime = serviceTime self._numMins = numMins self._passengerQ = Queue() self._theAgents = Array(numAgents) for i in range(numAgents): self._theAgents[i] = TicketAgent(i + 1) self._totalWaitTime = 0 self._numPassengers = 0
def __init__( self, numAgents, numMinutes, betweenTime, serviceTime ): # Parameters supplied by the user. self._arriveProb = 1.0 / betweenTime self._serviceTime = serviceTime self._numMinutes = numMinutes # Simulation components. self._passengerQ = Queue() self._theAgents = Array( numAgents ) for i in range( numAgents ) : self._theAgents[i] = TicketAgent(i+1) random.seed(4500) # Computed during the simulation. self._totalWaitTime = 0 self._numPassengers = 0
from llistqueue import Queue values = Queue() for i in range(16): if i % 3 == 0: values.enqueue(i) elif i % 4 == 0: values.dequeue() while not values.isEmpty(): print(values.dequeue())
def __init__(self, numLevels): self._qSize = 0 self._qLevels = Array(numLevels) for i in range(numLevels): self._qLevels[i] = Queue()
def __init__(self, arg, maxSize): super(BPriorityQueue, self).__init__() self._qsize = 0 self._qArray = Array(maxSize) for i in range(maxSize): self._qArray[i] = Queue()
class TicketCounterSimulation : # Create a simulation object. def __init__( self, numAgents, numMinutes, betweenTime, serviceTime ): # Parameters supplied by the user. self._arriveProb = 1.0 / betweenTime self._serviceTime = serviceTime self._numMinutes = numMinutes # Simulation components. self._passengerQ = Queue() self._theAgents = Array( numAgents ) for i in range( numAgents ) : self._theAgents[i] = TicketAgent(i+1) random.seed(4500) # Computed during the simulation. self._totalWaitTime = 0 self._numPassengers = 0 # Run the simulation using the parameters supplied earlier. def run( self ): for curTime in range(self._numMinutes + 1) : if (random.random() <= self._arriveProb): self._handleArrival( curTime ) self._handleBeginService( curTime ) self._handleEndService( curTime ) self._passengerQ.show() # Print the simulation results. def printResults( self ): numServed = self._numPassengers - len(self._passengerQ) avgWait = float( self._totalWaitTime ) / numServed print( "" ) print( "Number of passengers served = ", numServed ) print( "Number of passengers remaining in line = %d" % len(self._passengerQ) ) print( "The average wait time was %4.2f minutes." % avgWait ) # The remaining methods that have yet to be implemented. def _handleArrival(self, curTime ): # Handles simulation rule #1. self._numPassengers += 1 passanger = Passenger(self._numPassengers, curTime) self._passengerQ.enqueue(passanger) print "Time %2d: Passeger %2d arrived." % \ (curTime, self._numPassengers) return def _handleBeginService(self, curTime ): # Handles simulation rule #2. while not self._passengerQ.isEmpty(): free = 0 for i in range(len(self._theAgents)): if self._theAgents[i].isFree(): passanger = self._passengerQ.dequeue() self._theAgents[i].startService(passanger, \ curTime + self._serviceTime) free = 1 print "Time %2d: Agent %2d started serving passenger %2d." % \ (curTime, self._theAgents[i]._idNum, passanger._idNum) self._totalWaitTime += curTime - passanger._arrivalTime break; # no free if free == 0: return def _handleEndService(self, curTime ): # Handles simulation rule #3. for i in range(len(self._theAgents)): if self._theAgents[i].isFinished(curTime): passanger = self._theAgents[i].stopService() print "Time %2d: Agent %2d stopped serving passenger %2d." % \ (curTime, self._theAgents[i]._idNum, passanger._idNum)
class TicketCounterSimulation: # Create a simulation object. def __init__(self, numAgents, numMinutes, betweenTime, serviceTime): # Parameters supplied by the user. self._arriveProb = 1.0 / betweenTime self._serviceTime = serviceTime self._numMinutes = numMinutes # Simulation components. self._passengerQ = Queue() self._theAgents = Array(numAgents) for i in range(numAgents): self._theAgents[i] = TicketAgent(i + 1) # Computed during the simulation. self._totalWaitTime = 0 self._numPassengers = 0 # Run the simulation using the parameters supplied earlier. def run(self): for curTime in range(self._numMinutes + 1): self._handleArrival(curTime) self._handleBeginService(curTime) self._handleEndService(curTime) # Print the simulation results. def printResults(self): numServed = self._numPassengers - len(self._passengerQ) avgWait = float(self._totalWaitTime) / numServed print("") print("Number of passengers served = ", numServed) print("Number of passengers remaining in line = %d" % len(self._passengerQ)) print("The average wait time was %4.2f minutes." % avgWait) def _handleArrival(self, curTime): prob = random.random() if prob <= self._arriveProb: self._numPassengers += 1 passenger = Passenger(self._numPassengers, curTime) self._passengerQ.enqueue(passenger) print(("Time %6d Passenger %d arrived.") % (curTime, passenger.idNum())) def _handleBeginService(self, curTime): for i in range(len(self._theAgents)): if not self._passengerQ.isEmpty() and self._theAgents[i].isFree(): passenger = self._passengerQ.dequeue() print(passenger) endTime = curTime + self._serviceTime self._theAgents[i].startService(passenger, endTime) self._totalWaitTime += (curTime - passenger.timeArrived()) print(("Time %6d Agent %d started serving passenger %d") % (curTime, self._theAgents[i].idNum(), passenger.idNum())) break def _handleEndService(self, curTime): for i in range(len(self._theAgents)): if self._theAgents[i].isFinished(curTime): passenger = self._theAgents[i].stopService() print(("Time %6d Agent %d stopped serving passenger %d") % (curTime, self._theAgents[i].idNum(), passenger.idNum())) break
class TicketCounterSimulation: """ Create a simulation object. """ def __init__(self, numAgents, numMinutes, betweenTime, serviceTime): """ Parameters supplied by the user. """ self._arriveProb = 1.0 / betweenTime self._serviceTime = serviceTime self._numMinutes = numMinutes # Simulation components. self._passengerQ = Queue() self._theAgents = Array(numAgents) for i in range(numAgents): self._theAgents[i] = TicketAgent(i + 1) # Computed during the simulation. self._totalWaitTime = 0 self._numPassengers = 0 def run(self): """ Run the simulation using the parameters supplied earlier. """ for curTime in range(self._numMinutes + 1): self._handleArrival(curTime) self._handleBeginService(curTime) self._handleEndService(curTime) def _handleArrival(self, curTime): """ Makes changes according to 1st rule. """ prob_number = random.random() if prob_number <= self._arriveProb: self._numPassengers += 1 passenger = Passenger(self._numPassengers, curTime) self._passengerQ.enqueue(passenger) print(f"Time {curTime}: Passenger {self._numPassengers} arrived.") def _handleBeginService(self, curTime): """ Makes changes according to 2nd rule. """ for agent in self._theAgents: if (not self._passengerQ.isEmpty() and agent.isFree()): passenger = self._passengerQ.dequeue() self._totalWaitTime += (curTime - passenger._arrivalTime) agent.startService(passenger, curTime + self._serviceTime) print(f"Time {curTime}: Agent {agent._idNum} started serving p\ assenger {passenger._idNum}.") def _handleEndService(self, curTime): """ Makes changes according to 3rd rule. """ for agent in self._theAgents: if agent.isFinished(curTime): passenger = agent.stopService() print(f"Time {curTime}: Agent {agent._idNum} stopped serving p\ assenger {passenger._idNum}.") def printResults(self): """ Print the simulation results. """ numServed = self._numPassengers - len(self._passengerQ) avgWait = float(self._totalWaitTime) / numServed print("") print("Number of passengers served = ", numServed) print("Number of passengers remaining in line = %d" % len(self._passengerQ)) print("The average wait time was %4.2f minutes." % avgWait)