算法Sedgewick第四版-第1章基础-024-M/M/1 queue
/******************************************************************************
* Compilation: javac MM1Queue.java
* Execution: java MM1Queue lambda mu
* Dependencies: Queue.java Histogram.java
*
* Simulate an M/M/1 queue where arrivals and departures are Poisson
* processes with arrival rate lambda and service rate mu.
*
* % java MM1Queue .20 .33
*
* % java MM1Queue .20 .25
*
* % java MM1Queue .20 .21
*
*
* Remarks
* -------
* - We assume the interrarrival and service times are independent.
*
*
******************************************************************************/ public class MM1Queue { public static void main(String[] args) {
double lambda = Double.parseDouble(args[0]); // arrival rate
double mu = Double.parseDouble(args[1]); // service rate Queue<Double> q = new Queue<Double>(); // arrival times of customers
double nextArrival = StdRandom.exp(lambda); // time of next arrival
double nextDeparture = Double.POSITIVE_INFINITY; // time of next departure // histogram object
Histogram hist = new Histogram(60); // simulate an M/M/1 queue
while (true) { // it's an arrival
if (nextArrival <= nextDeparture) {
if (q.isEmpty()) nextDeparture = nextArrival + StdRandom.exp(mu);
q.enqueue(nextArrival);
nextArrival += StdRandom.exp(lambda);
} // it's a departure
else {
double wait = nextDeparture - q.dequeue();
StdOut.printf("Wait = %6.2f, queue size = %d\n", wait, q.size());
hist.addDataPoint(Math.min(60, (int) (Math.round(wait))));
hist.draw();
if (q.isEmpty()) nextDeparture = Double.POSITIVE_INFINITY;
else nextDeparture += StdRandom.exp(mu); }
} } }
算法Sedgewick第四版-第1章基础-024-M/M/1 queue的更多相关文章
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...
随机推荐
- 使用smart-npm和npm安装完毕之后发现 不是内部命令和外部命令!
我明明记得加入过path环境变量.我想一定是什么原因 一查bing.com然后发现没有在全局环境 .所以 只需要过npm install -g gulp 一句执行cmd这样就好 $ gulp提示gu ...
- Django pymysql学员管理系统
学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司急需一套方便易用的“学员管理系统”,来提 ...
- LeetCode OJ:Subsets II(子集II)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- SQL使用指南(1)—— 数据定义语言(DDL)
1.使用create 语句创建表 CREATE TABLE table_name (column_name datatype[null|not null], column_name datatype[ ...
- windowsError:32
Traceback (most recent call last): File "/usr/lib/python2.7/logging/handlers.py", line 78, ...
- loopqueue
import java.util.Arrays; public class loopQueue <E>{ public Object[] data=null; private int ma ...
- jsp有哪些内置对象?作用分别是什么?
JSP共有以下9种基本内置组件 1.request对象 客户端请求,此请求会包含来自GET/POST请求的参数通过它才能了解到客户的需求,然后做出响应. 2.response对象 响应客户请求的有关信 ...
- LoadRunner几个重要的概念:事务、集合点、思考时间
在LoadRunner的脚步编写中,有三个重要的概念:事务.集合点.思考时间 事务: 事务又称为Transaction,在LoadRunner中的定义如下:An end-to-end(browser- ...
- liferay-ui:search-container reset cur page 当点列排序时,把当前页号重置为1.
问题描述: liferay里面要用liferay-ui:search-container 来展示结果集.并要求点列时,可以排序.然后,如果当前页数不为1时,点列排序,自动设置为1. 解决: // 列排 ...
- debug时打到了URLClassLoader.class里面,
一.解决方法,查看breakpoints,看有没有在这个类里面打断点,有时会系统自动打断电在这个类里面, 二.在设置里面,找到debug,去掉debug的前面几个断电设置.