zoukankan      html  css  js  c++  java
  • python中基于queue的打印机仿真算法

      使用打印机的模型是queue中最经典的应用之一,这里就回顾一下queue在这里的使用方法和

    起的重要作用。

      为了仿真打印状态,这里需要把真实环境中的三个物理模型要建模出来,分别是:打印者,打印

    任务,和处理队列。

      首先打印者的实现如下所示:

    class Printer:
            def __init__(self,ppm):
                    self.page_rate = ppm
                    self.current_task = None
                    self.time_remaining = 0
            def tick(self):
                    if self.current_task != None:
                            self.time_remaining = self.time_remaining - 1
                            if self.time_remaining <= 0:
                                    self.current_task = None
              
            def busy(self):
                    if self.current_task != None:
                            return True
                    else:
                            return False
    
            def start_next(self,new_task):
                    self.current_task = new_task
                    self.time_remaining = new_task.get_pages() * 60 /self.page_rate

      打印任务的代码实现:

    import random
    
    class Task:
            def __init__(self,time):
                    self.timestamp = time
                    self.pages = random.randrange(1,21)
    
            def get_stamp(self):
                    return self.timestamp
    
            def get_pages(self):
                    return self.pages
    
            def wait_time(self,current_time):
                    return current_time - self.timestamp

      任务处理:

    import random
    from queue import *
    from Printer import *
    from Task import *
    
    
    print random.randrange(0, 101)
              
    def simulation(num_seconds,pages_per_minute):
              
            lab_printer = Printer(pages_per_minute)
            print_queue = Queue()
            waiting_times = []
                 
            for current_second in range(num_seconds):
                    if new_print_task:
                            task = Task(current_second)
                            print_queue.enqueue(task)
        
                    if(not lab_printer.busy()) and (not print_queue.is_empty()):
                            next_task = print_queue.dequeue()
                            waiting_times.append(next_task.wait_time(current_second))
                            lab_printer.start_next(next_task)
        
                    lab_printer.tick()
            
            average_wait = sum(waiting_times) / len(waiting_times)
            print("Average Wait %6.2f secs %3d tasks remaining."%(average_wait,print_queue.size()))
    
    
    def new_print_task():
            num = random.randrange(1,181)
            if num == 180:
                    return True
            else:
                    return False
    
    for i in range(10):
            simulation(3600,5)

      测试结果:

    Average Wait 1874.00 secs 3572 tasks remaining.
    Average Wait 1793.00 secs 3568 tasks remaining.
    Average Wait 1700.00 secs 3572 tasks remaining.
    Average Wait 1554.00 secs 3573 tasks remaining.
    Average Wait 1831.00 secs 3570 tasks remaining.
    Average Wait 1723.00 secs 3569 tasks remaining.
    Average Wait 1745.00 secs 3568 tasks remaining.
    Average Wait 1697.00 secs 3572 tasks remaining.
    Average Wait 1596.00 secs 3569 tasks remaining.
    Average Wait 1729.00 secs 3572 tasks remaining.
  • 相关阅读:
    Android之旅十六 android中各种资源的使用
    XTU OJ 1207 Welcome to XTCPC (字符串签到题)
    scala并发编程原生线程Actor、Case Class下的消息传递和偏函数实战
    【云图】怎样设置支付宝里的家乐福全国连锁店地图?
    怎样在QML中使用multitouch
    软件project师周兆熊给IT学子的倾情奉献
    Linux系统下怎样配置SSH?怎样开启SSH?
    数学之路-python计算实战(4)-Lempel-Ziv压缩(2)
    Day5上午解题报告
    一份只有巨佬才能看懂的代码
  • 原文地址:https://www.cnblogs.com/dylancao/p/8079334.html
Copyright © 2011-2022 走看看