Question: The aim of this second assignment is to a) implement a queue class using a different internal data structure, and b) to create a new

The aim of this second assignment is to a) implement a queue class using a different internal data structure, and b) to create a new TrafficSimulator class that inherits from your new queue class. These are two parts are broken up as follows.

Part 1: Implement a QueueLL queue class using a Linked List (i.e. QueueLL 'has-a' Linked List as one of its fields)

Previously we implemented a queue using a Python list. For this assignment, we want to implement a QueueLL class using our UnorderedList class (download this file and import it, do not copy and paste it into your program). We will want to implement the same methods as before, namely:

  • __init__ method - a constructor
  • __str__ method - override Python str function/method to facilitate printing of the entire queue; this should print the contents of the queue with front of the queue on left, and back of the queue on the right (similar to how our Queue class in queue.py overrides __str__ to print nicely)
  • isEmpty() return True if the queue has zero items, and False if has one or more items
  • enqueue(item) add new item to back or queue, dont return anything
  • dequeue() return and remove item at the front of the queue
  • peek() return item at the front of the queue, but dont remove it
  • size() return the number of items currently in the queue

Additionally, there should be unit tests (at least 2) to test that these methods work as expected. Below (assign2.py) is some code to get started.

Again, do not copy and paste the UnorderedList and Node class definitions into your *.py file. Instead, import them as shown on the first line of code below.

assign2.py (without Part 2):

from lists import UnorderedList import unittest class QueueLL: """ ADD DOCUMENTATION HERE, REMOVING/REPLACING COMMENTS IN ALL CAPS """ def __init__(self): """ Create the one field/attribute needed for our QueueLL class """ self.queue = UnorderedList() # ADD ALL REQUIRED METHODS HERE - SEE PART 1 DIRECTIONS IN Canvas # (REMOVE/REPLACE THIS AND ANY OTHER COMMENTS IN CAPS) class TestQueueLL(unittest.TestCase): """ ADD DOCUMENTATION HERE, REMOVING/REPLACING COMMENTS IN ALL CAPS """ # ADD AT LEAST TWO UNIT TESTS HERE TO TEST THAT QUEUELL METHODS WORK AS EXPECTED # (REMOVE/REPLACE THIS AND ANY OTHER COMMENTS IN CAPS) # SEE PART 2 BELOW FOR THE CONTINUATION OF THIS FILE

Part 2: Implement a TrafficSimulatorQueue class that inherits from QueueLL (i.e. TrafficSimulatorQueue 'is-a' type of QueueLL)

Although there are many ways to implement a traffic simulator, we want to inherit from a queue in order to make use of the behavior of a queue.

assign2.py (continued):

class TrafficSimulatorQueue(QueueLL): """ A class to simulate traffic arriving and leaving an intersection with a stop light. As with all simulations, time is discretized so that each loop iteration represents one (1) second of time. Fields: * queue [from inheritance] - the internal structure that holds the queue * traffic_light_state - current status of the traffic light, either 'red' or 'green' * time_steps_needed_for_1_car_to_exit - time steps (e.g. 'seconds') needed for front car to exit intersection * prob_arrival - probability that an automobile arrives on any given iteration/epoch * time_steps_light_is_red - number of time steps (e.g. 'seconds') the traffic light is red * time_steps_light_is_green - number of time steps (e.g. 'seconds') the traffic light is green Methods: * __init__() - constructor to initialize class fields/attributes * isEmpty() [from inheritance] * enqueue(new_item) [from inheritance] * dequeue() [from inheritance] * size() [from inheritance] * peek() [from inheritance] * setProbabilityArrival(prob_arrival) - modify the probablity that a car arrives at any given time step * setMinutesRed(min_red) - modify the number of minutes the traffic light is red for (and convert to seconds/time_steps) * setMinutesGreen(min_red) - modify the number of minutes the traffic light is red for (and convert to seconds/time_steps) * checkForArrivingAuto() - check to see if a car arrives (intended to be called at each time step) * simulateTraffic(n) - simulate traffic for n cycles of red and green lights, starting with red first and printing status, queue, etc. as the simulation is carried out """ def __init__(self): ... # INSERT YOUR DOCUMENTATION AND CODE HERE FOR ALL OF THE METHODS MENTIONED # ABOVE, NOTING THAT SOME OF THEM DO NOT NEEDED BE IMPLEMENTED BECAUSE THEY # ARE INHERITED FROM QueueLL # (BE SURE TO REPLACE/REMOVE ALL, '...', AND COMMENTS IN ALL CAPS) ... def checkForArrivingAuto(self): """ Generate a random number between 0 and 1, then see if it less than self.prob_arrival. If it is, then add a new auto/car to the queue. """ r = random.random() if r < self.prob_arrival: car_arriving = random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') # INSERT THE PROPER LINE TO COMPLETE THIS METHOD SO THAT A CAR/AUTO ENTERS THE INTERSECTION def simulateTraffic(self, number_redgreen_cycles): """ Run the simulation where each iteration of the while loop represents 1 second. """ print("Traffic light is red, current queue size = " + str(self.size())) i = # CALCULATE THE NUMBER OF TIME STEPS / ITERATIONS TO SIMULATE i_light = # NUMBER OF TIME STEPS / ITERATIONS THE LIGHT WILL BE RED while i > 0: # cars can arrive regardless of whether the light is red or green self.checkForArrivingAuto() # light is red, so just decrement the light counter, i_light # once i_light has counted down to 0, then set everything needed to # change it to greeen if self.traffic_light_state == "red": i_light -= 1 if i_light == 0: # INSERT CODE NEEDED TO HANDLE CHANGING TO A GREEN LIGHT print("Traffic light changing to green, current queue size = " + str(self.size()) + ", queue:") print(" "*2, self) # light is green, so just decrement light counter AND check to see if # time_steps_needed_for_1_car_to_exit iterations have passed (if so, removed a car from the queue) # once i_light has counted down to 0, then set everything needed to change the light to red else: i_light -= 1 if i % self.time_steps_needed_for_1_car_to_exit == 0: # INSERT CODE NEEDED TO HANDLE A CAR LEAVING THE INTERSECTION print(" "*4, "car,", car_leaving, ", exiting intersection") if i_light == 0: # INSERT CODE NEEDED TO HANDLE CHANGING TO A RED LIGHT print("Traffic light changing to red, current queue size = " + str(self.size()) + ", queue:") print(" "*2, self) i -= 1 if __name__ == '__main__': print('='*30, 'Simulation 1:', '='*30) ts = TrafficSimulatorQueue() # set probability that a car arrives on any given second (i.e. loop # iteration) to 50% ts.setProbabilityArrival(0.5) # set the light to be red for 2 minutes (needs to be converted to seconds inside) ts.setMinutesRed(1) # set the light to be green for 1 minute (needs to be converted to seconds inside) ts.setMinutesGreen(1) # run simulation for two red-green cycles (i.e. red -> green -> red -> green) ts.simulateTraffic(2) print("Traffic simulator queue size at end of simulation =", ts.size()) print("Traffic simulator queue at end of simulation:") print(ts) unittest.main() 

What to hand in?

A single *.py file should be submitted here on Canvas. Note that in order to make it fair for everyone, there will only be one (1) submission attempt allowed.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!