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.
  • 相关阅读:
    【C语言】学习笔记8——结构struct(1)
    【算法导论】最大子数组——暴力求解
    【算法导论】最大子数组——递归
    【算法导论】二分查找
    【Linux】Linux服务器(centos7)环境搭建java/python3/nginx
    【算法导论】归并排序
    【C语言】第一个C语言小程序 —— 日期算法和万年历2
    【C语言】第一个C语言小程序 —— 日期算法和万年历
    【python】面向对象
    【java】对jdbc操作结果简单的映射封装
  • 原文地址:https://www.cnblogs.com/dylancao/p/8079334.html
Copyright © 2011-2022 走看看