|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Multi-server Queue\n", |
| 8 | + "\n", |
| 9 | + "## Description \n", |
| 10 | + "An [M/M/c queue](https://en.wikipedia.org/wiki/M/M/c_queue) is a basic queue with $c$ identical servers, exponentially distributed interarrival times, and exponentially distributed service times for each server. The arrival rate is defined as $\\lambda$ such that the interarrival time distribution has mean $1/\\lambda$. Similarly, the service rate is defined as $\\mu$ such that the service time distribution has mean $1/\\mu$ (for each server). The overall traffic intensity of the queue is $\\rho = \\lambda / (c \\mu)$. If the traffic intensity exceeds one, the queue is unstable and the queue length will grow indefinitely. " |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "### Install Packages" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": null, |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "Pkg.add(\"Distributions\")\n", |
| 27 | + "Pkg.add(\"SimJulia\")" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "### Load Packages" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 2, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [ |
| 42 | + { |
| 43 | + "name": "stderr", |
| 44 | + "output_type": "stream", |
| 45 | + "text": [ |
| 46 | + "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mPrecompiling module Distributions.\n", |
| 47 | + "\u001b[39m\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mPrecompiling module SimJulia.\n", |
| 48 | + "\u001b[39m" |
| 49 | + ] |
| 50 | + } |
| 51 | + ], |
| 52 | + "source": [ |
| 53 | + "using Distributions\n", |
| 54 | + "using SimJulia, ResumableFunctions" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "markdown", |
| 59 | + "metadata": {}, |
| 60 | + "source": [ |
| 61 | + "### Define Constants" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": 3, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "srand(8710) # set random number seed for reproducibility\n", |
| 71 | + "num_customers = 10 # total number of customers generated\n", |
| 72 | + "num_servers = 2 # number of servers\n", |
| 73 | + "mu = 1.0 / 2 # service rate\n", |
| 74 | + "lam = 0.9 # arrival rate\n", |
| 75 | + "arrival_dist = Exponential(1 / lam) # interarrival time distriubtion\n", |
| 76 | + "service_dist = Exponential(1 / mu); # service time distribution" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "### Define Customer Behavior" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 4, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "data": { |
| 93 | + "text/plain": [ |
| 94 | + "customer (generic function with 1 method)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + "execution_count": 4, |
| 98 | + "metadata": {}, |
| 99 | + "output_type": "execute_result" |
| 100 | + } |
| 101 | + ], |
| 102 | + "source": [ |
| 103 | + "@resumable function customer(env::Environment, server::Resource, id::Integer, time_arr::Float64, dist_serve::Distribution)\n", |
| 104 | + " @yield timeout(env, time_arr) # customer arrives\n", |
| 105 | + " println(\"Customer $id arrived: \", now(env))\n", |
| 106 | + " @yield request(server) # customer starts service\n", |
| 107 | + " println(\"Customer $id entered service: \", now(env))\n", |
| 108 | + " @yield timeout(env, rand(dist_serve)) # server is busy\n", |
| 109 | + " @yield release(server) # customer exits service\n", |
| 110 | + " println(\"Customer $id exited service: \", now(env))\n", |
| 111 | + "end" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "### Setup and Run Simulation" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 5, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [ |
| 126 | + { |
| 127 | + "name": "stdout", |
| 128 | + "output_type": "stream", |
| 129 | + "text": [ |
| 130 | + "Customer 1 arrived: 0.1229193244813443\n", |
| 131 | + "Customer 1 entered service: 0.1229193244813443\n", |
| 132 | + "Customer 2 arrived: 0.22607641035584877\n", |
| 133 | + "Customer 2 entered service: 0.22607641035584877\n", |
| 134 | + "Customer 3 arrived: 0.4570009029409502\n", |
| 135 | + "Customer 2 exited service: 1.7657345101378559\n", |
| 136 | + "Customer 3 entered service: 1.7657345101378559\n", |
| 137 | + "Customer 1 exited service: 2.154824561031012\n", |
| 138 | + "Customer 3 exited service: 2.2765287086137764\n", |
| 139 | + "Customer 4 arrived: 2.3661687470062995\n", |
| 140 | + "Customer 4 entered service: 2.3661687470062995\n", |
| 141 | + "Customer 5 arrived: 2.6110816119637885\n", |
| 142 | + "Customer 5 entered service: 2.6110816119637885\n", |
| 143 | + "Customer 5 exited service: 2.8017888690417583\n", |
| 144 | + "Customer 6 arrived: 3.019540357955037\n", |
| 145 | + "Customer 6 entered service: 3.019540357955037\n", |
| 146 | + "Customer 6 exited service: 3.351151832298383\n", |
| 147 | + "Customer 7 arrived: 3.5254699872847612\n", |
| 148 | + "Customer 7 entered service: 3.5254699872847612\n", |
| 149 | + "Customer 7 exited service: 4.261422043181396\n", |
| 150 | + "Customer 4 exited service: 4.602071952938201\n", |
| 151 | + "Customer 8 arrived: 7.27536704811686\n", |
| 152 | + "Customer 8 entered service: 7.27536704811686\n", |
| 153 | + "Customer 9 arrived: 7.491176033637809\n", |
| 154 | + "Customer 9 entered service: 7.491176033637809\n", |
| 155 | + "Customer 10 arrived: 8.39098457094977\n", |
| 156 | + "Customer 8 exited service: 8.683396356977969\n", |
| 157 | + "Customer 10 entered service: 8.683396356977969\n", |
| 158 | + "Customer 9 exited service: 8.7501656586875\n", |
| 159 | + "Customer 10 exited service: 9.049670951561666\n" |
| 160 | + ] |
| 161 | + } |
| 162 | + ], |
| 163 | + "source": [ |
| 164 | + "sim = Simulation() # initialize simulation environment\n", |
| 165 | + "server = Resource(sim, num_servers) # initialize servers\n", |
| 166 | + "arrival_time = 0.0\n", |
| 167 | + "for i = 1:num_customers # initialize customers\n", |
| 168 | + " arrival_time += rand(arrival_dist)\n", |
| 169 | + " @process customer(sim, server, i, arrival_time, service_dist)\n", |
| 170 | + "end\n", |
| 171 | + "run(sim) # run simulation" |
| 172 | + ] |
| 173 | + } |
| 174 | + ], |
| 175 | + "metadata": { |
| 176 | + "kernelspec": { |
| 177 | + "display_name": "Julia 0.6.0", |
| 178 | + "language": "julia", |
| 179 | + "name": "julia-0.6" |
| 180 | + }, |
| 181 | + "language_info": { |
| 182 | + "file_extension": ".jl", |
| 183 | + "mimetype": "application/julia", |
| 184 | + "name": "julia", |
| 185 | + "version": "0.6.0" |
| 186 | + } |
| 187 | + }, |
| 188 | + "nbformat": 4, |
| 189 | + "nbformat_minor": 2 |
| 190 | +} |
0 commit comments