/******************************************************************************
* 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. hdoj-1004-Let the Balloon Rise(map排序)

    map按照value排序 #include <iostream> #include <algorithm> #include <cstring> #include ...

  2. Codeforces Round #276 (Div. 2)D - Maximum Value(筛法)

    就是一种筛法思想的应用. #include<iostream> #include<cstdio> #include<cstdlib> #include<cst ...

  3. LeetCode Longest Uncommon Subsequence II

    原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list ...

  4. hadoop-hive学习笔记

    create table hive_1(id string,name string ,gender string)row format delimited fields terminated by ' ...

  5. unitychan-crs 头发随动脚本

    // //SpringCollider for unity-chan! // //Original Script is here: //ricopin / SpringCollider.cs //Ro ...

  6. 学习动态性能表(14)--v$parameter&v$system_parameter

    学习动态性能表 第14篇--V$PARAMETER&V$SYSTEM_PARAMETER  2007.6.11 这两个视图列出的各参数项名称以及参数值.V$PARAMETER显示执行查询的se ...

  7. (转)C#正则表达式Regex类的用法

    原文地址如下:http://www.studyofnet.com/news/297.html 一.C#正则表达式符号模式 字 符 描 述 \ 转义字符,将一个具有特殊功能的字符转义为一个普通字符,或反 ...

  8. IDEA 设置导出

    见附件 https://files.cnblogs.com/files/chuancheng/settings.7z

  9. 蓝桥杯 算法训练 ALGO-141 P1102

    算法训练 P1102   时间限制:1.0s   内存限制:256.0MB 定义一个学生结构体类型student,包括4个字段,姓名.性别.年龄和成绩.然后在主函数中定义一个结构体数组(长度不超过10 ...

  10. Linux 驱动编程知识

    1.包含的头文件 1.1 GPIO相关操作 #include <asm/arch/gpio.h>