Extremely stuck on this assignment, please read it fully as the assignment is sl
ID: 3804471 • Letter: E
Question
Extremely stuck on this assignment, please read it fully as the assignment is slightly confusing: PYTHON
The code provided is the code needed to do the program below, using the code provided modify it to the speifications in the instructions below. Basically figure out the times if another printer was added to do tasks and output it in a seperate file. The program will take a file which has the specifiations of a printer and determine the time it takes to do a task.
This assignment, modify the printer simulation which is given in the code provided below (Attached at the bottom of these instructions), to make it possible to investigate how adding another printer to the lab would affect wait times. Given a configuration with two printers, if one printer is busy and a print task is submitted to a empty queue, the task is dispatched to the idle printer. If both printers are busy, the task is enqueued to a common queue until a printer frees up. If both printers are free when a task is submitted, the task is assigned to the printer with the higher page rate.
Note that the simulation should still allow the user to specify a configuration with 1 printer. Also, assumptions about how frequently print tasks are submitted should remain unchanged.
The modified simulation should also make it possible for the user to specify the minimum and maximum size of a print task (In the code provided, the range is 1-20). As before, the size of the print task is a random integer that falls within the specified range.
The simulation configurations should be specified in an input file printer_config.txt that the program reads. The program should not prompt the user for any input. The input file should contain the following values in the exact order specified, one per line:
1. Duration of simulation in seconds (valid range is 3600-36000).
2. Number of simulation experiments (valid range is 1-100. In the existing program, this value is set to 10).
3. Minimum task size (must be in range 1-100)
4. Maximum task size (must be in range 1-100 and >= minimum)
5. Number of printers (must be 1 or 2)
6. Page rate of printer 1 (must be in the range 1-50)
7. Page rate of printer 2 (if number of printers is 1, this value would not be specified)
The input file contains 6 or 7 integers, depending on whether 1 or 2 printers were specified.
Your program must implement input validation (Using exception handling and conditional statements as appropriate) to detect format error and out of range errors. Appropriate error messages should be output to the terminal, after which point the program should terminate.
The program should output to a file named printer_out.txt the average wait time for each experiment along with the length of the queue when the simulation terminated, and finally the overall average wait time for all experiments. To help you test your program, provided are 5 sample runs of the solution with different input files (attached photo). Run your program with the same inputs. The results you obtain should come very close from the ones shown in the table.
Make sure your code is readable and well commented, especially code you’ve modified or added.
The printer code:
from Queue import Queue
import random
class Printer:
def __init__(self, ppm):
self.pagerate = ppm
self.currentTask = None
self.timeRemaining = 0
def tick(self):
if self.currentTask != None:
self.timeRemaining = self.timeRemaining - 1
if self.timeRemaining <= 0:
self.currentTask = None
def busy(self):
if self.currentTask != None:
return True
else:
return False
def startNext(self,newtask):
self.currentTask = newtask
self.timeRemaining = newtask.getPages() * 60/self.pagerate
class Task:
def __init__(self,time):
self.timestamp = time
self.pages = random.randrange(1,21)
def getStamp(self):
return self.timestamp
def getPages(self):
return self.pages
def waitTime(self, currenttime):
return currenttime - self.timestamp
def simulation(numSeconds, pagesPerMinute):
labprinter = Printer(pagesPerMinute)
printQueue = Queue()
waitingtimes = []
for currentSecond in range(numSeconds):
if newPrintTask():
task = Task(currentSecond)
printQueue.enqueue(task)
if (not labprinter.busy()) and (not printQueue.is_empty()):
nexttask = printQueue.dequeue()
waitingtimes.append(nexttask.waitTime(currentSecond))
labprinter.startNext(nexttask)
labprinter.tick()
averageWait = sum(waitingtimes)/len(waitingtimes)
print("Average Wait %6.2f secs %3d tasks remaining."
%(averageWait,printQueue.size()))
def newPrintTask():
num = random.randrange(1,181)
if num == 180:
return True
else:
return False
def main():
# run simulation 10 times
for i in range(10):
simulation(3600, 10)
if __name__ == "__main__":
main()
Explanation / Answer
import random
class Printer:
def __init__(self, ppm):
self.pagerate = ppm
self.currentTask = None
self.timeRemaining = 0
def tick(self):
if self.currentTask != None:
self.timeRemaining = self.timeRemaining - 1
if self.timeRemaining <= 0:
self.currentTask = None
def busy(self):
if self.currentTask != None:
return True
else:
return False
def startNext(self,newtask):
self.currentTask = newtask
self.timeRemaining = newtask.getPages() * 60/self.pagerate
class Task:
def __init__(self,time):
self.timestamp = time
self.pages = random.randrange(1,21)
def getStamp(self):
return self.timestamp
def getPages(self):
return self.pages
def waitTime(self, currenttime):
return currenttime - self.timestamp
def simulation(numSeconds, pagesPerMinute):
labprinter = Printer(pagesPerMinute)
printQueue = Queue()
waitingtimes = []
for currentSecond in range(numSeconds):
if newPrintTask():
task = Task(currentSecond)
printQueue.enqueue(task)
if (not labprinter.busy()) and (not printQueue.is_empty()):
nexttask = printQueue.dequeue()
waitingtimes.append(nexttask.waitTime(currentSecond))
labprinter.startNext(nexttask)
labprinter.tick()
averageWait = sum(waitingtimes)/len(waitingtimes)
print("Average Wait %6.2f secs %3d tasks remaining."
%(averageWait,printQueue.size()))
def newPrintTask():
num = random.randrange(1,181)
if num == 180:
return True
else:
return False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.