/******************************************************************************
* 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的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  4. 算法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 ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  8. 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

    1. package algorithms.stacks13; /******************************************************************* ...

  9. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  10. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

随机推荐

  1. SpringBoot_06_使用Swagger2构建强大的RESTful API文档

    二.参考资料 1.Spring Boot中使用Swagger2构建强大的RESTful API文档 2.

  2. 一些神奇的(优化)板子——来自Loi_black的博客

    deque<int>q; void spfa(int s) { ;i<=n;i++) d[i]=1e9; d[s]=; q.push_back(s); used[s]=; while ...

  3. 2017.12.15 python资料,转存一下。

    最近GD项目三个型号都是用Python做批量烧录和测试的.marking一下,,虽然自己不会写. 1.入门阶段 The Python Tutorial(https://docs.python.org/ ...

  4. python list的extend和append方法

    append: Appends object at the end. x = [1, 2, 3] x.append([4, 5]) print (x) gives you: [1, 2, 3, [4, ...

  5. Codeforces Round #262 (Div. 2)C(二分答案,延迟标记)

    这是最大化最小值的一类问题,这类问题通常用二分法枚举答案就行了. 二分答案时,先确定答案肯定在哪个区间内.然后二分判断,关键在于怎么判断每次枚举的这个答案行不行. 我是用a[i]数组表示初始时花的高度 ...

  6. tag问题

  7. CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)

    Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...

  8. BerOS file system

    The new operating system BerOS has a nice feature. It is possible to use any number of characters '/ ...

  9. Redis底层探秘(三):字典

    字典,又称为符号表(symbol table).关联数组(associative array)或映射(map),是一种用于保存键值对的抽象数据结构. 字典经常作为一种数据结构内置在很多高级编程语言里面 ...

  10. 《Javascript高级程序设计》阅读记录(七):第七章

    <Javascript高级程序设计>中,2-7章中已经涵盖了大部分精华内容,所以摘录到博客中,方便随时回忆.本系列基本完成,之后的章节,可能看情况进行摘录. 这个系列以往文字地址: < ...