Queuing applications - a print job

Queuing applications - a print job

Print job problem: Analog Process

The passage of time in accordance with the second unit

按照概率生成打印作业,加入打印队列
如果打印机空闲,且队列不空,则取出队首作业
打印,记录此作业等待时间
如果打印机忙,则按照打印速度进行1秒打印
如果当前作业打印完成,则打印机进入空闲。

Time runs out, began to count the average waiting time.

Job Waiting Time

When generating operations, records generated timestamps
When you start printing, the current time minus the time to generate

Print time job

When generating jobs, record the number of pages of the job
at the start of printing, the printing speed can be divided by the number of pages

Code Display

from pythonds.basic.Queue import Queue

import random

class Printer:
    def __init__(self, ppm):
        self.pagerate = ppm                   #打印速度
        self.currentTask = None               #打印任务
        self.timeRemaining = 0                #任务倒计时

    def tick(self):       #打印1秒
        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 newPrintTask():
    num = random.randrange(1, 181)                 #1/180概率生成作业
    if num == 180:
        return True
    else:
        return False

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.isEmpty()):
            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())
for i in range(10):
    simulation(3600, 5)

Think

In order to evaluate the print mode is set to make decisions, we use the simulation program to evaluate the task waiting time

  通过两种情况模拟仿真结果的分析, 我们认识到如果有那么多学生要拿着打印好的程序源代码赶去上课的话,那么,必须得牺牲打印质量,提高打印速度。
System simulation of reality
在不耗费现实资源的情况下——有时候真实的实验是无法进行的,可以以不同的设定,反复多次模拟来帮助我们进行决策。

Print job problem: Discussion

More realistic simulation, from more sophisticated modeling of the problem, as well as real data set and run
It can also be extended to other similar decision support problems
Published 25 original articles · won praise 3 · Views 490

Guess you like

Origin blog.csdn.net/qq_44045101/article/details/103214179