Calculation and Simulation | Queuing Theory

Calculation and Simulation | Queuing Theory

Firstly, we denote some variables in the process:

VariableNotation
λ \lambda λThe arrival rate
μ \mu μThe service rate
L L LThe long-term average number of customers in the system
L q L_q LqThe long-term average length of queue
W W WThe long-term average total waiting time of each customers
W q W_q WqThe long-term average waiting time of each customers for queuing
π n \pi_n πnThe margin probability of state n (n represents the number of customers in the system)
q i j q_{ij} qijThe transition rate from state i i i to state j j j
k k kThe number of dependent queues
ρ \rho ρ ρ = λ / μ \rho = \lambda/\mu ρ=λ/μ

I. Numerical Calculation

In order to study the queuing model, we firstly study the birth   and   death   process \textbf{birth and death process} birth and death process.

在这里插入图片描述

In this process, we assume that λ i = λ \lambda_i = \lambda λi=λ, μ i = μ \mu_i = \mu μi=μ, for ∀ i \forall i i. Therefore, we can solve the transition rate:
q i j = { λ j = i + 1 μ j = i − 1 q_{ij} = \begin{cases} \lambda & j = i+1\\ \mu & j = i-1 \end{cases} qij={λμj=i+1j=i1

In birth   and   death   process \textbf{birth and death process} birth and death process, according to detailed balance equation, we have that

π i q i j = π j q j i ∑ i π i = 1 \pi_{i}q_{ij} = \pi_{j}q_{ji}\\ \sum_{i}\pi_i = 1 πiqij=πjqjiiπi=1

Denote ρ = λ / μ \rho = \lambda/\mu ρ=λ/μ. To make the process convergent, we need to make sure that ρ = λ / μ < 1 \rho = \lambda/\mu < 1 ρ=λ/μ<1. Then, we can solve the π n ( n ≥ 0 ) \pi_n (n \geq 0) πn(n0):

π n = ( 1 − ρ ) ρ n \pi_n = (1-\rho)\rho^n πn=(1ρ)ρn

And then, we can solve the number of customers in system L L L and lengths of queues L q L_q Lq (number of dependent queues k = 1 k = 1 k=1):

L = ∑ n = 0 ∞ n π n = ρ 1 − ρ L q = ∑ n = k ∞ ( n − k ) π n = ∑ n = 1 ∞ ( n − 1 ) π n = ρ 2 1 − ρ L = \sum_{n=0}^{\infty} n\pi_n = \frac{\rho}{1-\rho} \\ L_q = \sum_{n=k}^{\infty} (n-k)\pi_n = \sum_{n=1}^{\infty} (n-1)\pi_n = \frac{\rho^2}{1-\rho} L=n=0nπn=1ρρLq=n=k(nk)πn=n=1(n1)πn=1ρρ2

Then, according to the Little’s   Law \textbf{Little's Law} Little’s Law, we can solve the average waiting time for the whole system W W W and only for the queuing W q W_q Wq:

W = L / λ = ρ ( 1 − ρ ) λ = 1 μ − λ W q = L q / λ = ρ 2 ( 1 − ρ ) λ = λ μ ( μ − λ ) W = L/\lambda = \frac{\rho}{(1-\rho)\lambda} = \frac{1}{\mu-\lambda}\\ W_q = L_q/\lambda = \frac{\rho^2}{(1-\rho)\lambda} = \frac{\lambda}{\mu(\mu-\lambda)} W=L/λ=(1ρ)λρ=μλ1Wq=Lq/λ=(1ρ)λρ2=μ(μλ)λ

Also, we can find that:

W = W q + 1 μ W = W_q+\frac{1}{\mu} W=Wq+μ1

Next, we apply the result for the Birth   and   Death   Process \textbf{Birth and Death Process} Birth and Death Process to the Three   Server   Organizations \textbf{Three Server Organizations} Three Server Organizations. In all models, we apply FCFS \textbf{FCFS} FCFS strategy:

1.   Frequency-division   Multiplexing   (FDM): \textbf{1. Frequency-division Multiplexing (FDM):} 1. Frequency-division Multiplexing (FDM):

在这里插入图片描述

In Frequency-division Multiplexing model, the system is divided into k k k independent Birth and Death Process, and the arrival rate λ ′ = λ / k \lambda' = \lambda/k λ=λ/k, the service rate μ ′ = μ \mu' = \mu μ=μ. Denote ρ = λ ′ / μ ′ = λ / k μ \rho = \lambda'/\mu' = \lambda/k\mu ρ=λ/μ=λ/kμ. To make the process convergent, we need to make sure that ρ = λ / k μ < 1 \rho = \lambda/k\mu < 1 ρ=λ/kμ<1.

So, according to the result in the Birth   and   Death   Process \textbf{Birth and Death Process} Birth and Death Process, we have:

L = k × ρ 1 − ρ = k × λ k μ 1 − λ k μ = k λ k μ − λ L q = k × ρ 2 1 − ρ = k × ( λ k μ ) 2 1 − λ k μ = λ 2 k μ 2 − λ μ L = k\times \frac{\rho}{1-\rho} = k\times \frac{\frac{\lambda}{k\mu}}{1-\frac{\lambda}{k\mu}} = \frac{k\lambda}{k\mu-\lambda}\\ L_q = k\times \frac{\rho^2}{1-\rho} = k\times \frac{(\frac{\lambda}{k\mu})^2}{1-\frac{\lambda}{k\mu}} = \frac{\lambda^2}{k\mu^2-\lambda\mu} L=k×1ρρ=k×1kμλkμλ=kμλLq=k×1ρρ2=k×1kμλ(kμλ)2=kμ2λμλ2
W = 1 μ − λ / k = k k μ − λ W q = λ / k μ ( μ − λ / k ) = λ k μ 2 − λ μ W = \frac{1}{\mu-\lambda/k} = \frac{k}{k\mu-\lambda}\\ W_q = \frac{\lambda/k}{\mu(\mu-\lambda/k)} = \frac{\lambda}{k\mu^2-\lambda\mu} W=μλ/k1=kμλkWq=μ(μλ/k)λ/k=kμ2λμλ

2.   M/M/1: \textbf{2. M/M/1:} 2. M/M/1:

在这里插入图片描述

In M/M/1 model, the system is constructed by a single Birth and Death Process with arrival rate λ ′ = λ \lambda' = \lambda λ=λ, service rate μ ′ = k μ \mu' = k\mu μ=kμ. Denote ρ = λ ′ / μ ′ = λ / k μ \rho = \lambda'/\mu' = \lambda/k\mu ρ=λ/μ=λ/kμ. To make the process convergent, we need to make sure that ρ = λ / k μ < 1 \rho = \lambda/k\mu < 1 ρ=λ/kμ<1.

Therefore, according to the result in the Birth and Death Process, we have:

L = ρ 1 − ρ = λ k μ 1 − λ k μ = λ k μ − λ L q = ρ 2 1 − ρ = ( λ k μ ) 2 1 − λ k μ = λ 2 k 2 μ 2 − k λ μ L = \frac{\rho}{1-\rho} = \frac{\frac{\lambda}{k\mu}}{1-\frac{\lambda}{k\mu}} = \frac{\lambda}{k\mu-\lambda}\\ L_q = \frac{\rho^2}{1-\rho} = \frac{(\frac{\lambda}{k\mu})^2}{1-\frac{\lambda}{k\mu}} = \frac{\lambda^2}{k^2\mu^2-k\lambda\mu} L=1ρρ=1kμλkμλ=kμλλLq=1ρρ2=1kμλ(kμλ)2=k2μ2μλ2
W = L λ = 1 k μ − λ W q = L q λ = λ k 2 μ 2 − k λ μ W = \frac{L}{\lambda} = \frac{1}{k\mu-\lambda}\\ W_q = \frac{L_q}{\lambda} = \frac{\lambda}{k^2\mu^2-k\lambda\mu} W=λL=kμλ1Wq=λLq=k2μ2μλ

3.   M/M/k: \textbf{3. M/M/k:} 3. M/M/k:

在这里插入图片描述

Compared with the forming two models, the M / M / k M/M/k M/M/k is more complicated. It is different with the naive Birth and Death Process we solve as above. The new State Transition Diagram is as below:

在这里插入图片描述

In this process, we assume that λ i = λ \lambda_i = \lambda λi=λ, μ i = μ \mu_i = \mu μi=μ, for ∀ i \forall i i. Denote ρ = λ ′ / μ ′ = λ / k μ \rho = \lambda'/\mu' = \lambda/k\mu ρ=λ/μ=λ/kμ. To make the process convergent, we need to make sure that ρ = λ / k μ < 1 \rho = \lambda/k\mu < 1 ρ=λ/kμ<1.

Therefore, we can solve the transition rate:

q i j = { λ j = i + 1 i μ j = i − 1 , i ≤ k s μ j = i − 1 , i > k q_{ij} = \begin{cases} \lambda & j = i+1\\ i\mu & j = i-1, i \leq k\\ s\mu & j = i-1, i > k \end{cases} qij= λiμsμj=i+1j=i1,ikj=i1,i>k

In M / M / k M/M/k M/M/k model, according to detailed balance equation, we have that

π i q i j = π j q j i ∑ i π i = 1 \pi_{i}q_{ij} = \pi_{j}q_{ji}\\ \sum_{i}\pi_i = 1 πiqij=πjqjiiπi=1

Denote ρ = λ / k μ \rho = \lambda/k\mu ρ=λ/kμ, we can solve the π n ( n ≥ 0 ) \pi_n (n \geq 0) πn(n0):

π 0 = [ ∑ n = 0 k − 1 ( λ / μ ) n n ! + ( λ / μ ) k k ! × 1 1 − λ / k μ ] − 1 π n = { ( λ / μ ) n n ! × π 0 0 ≤ n ≤ k ( λ / μ ) n k ! × ( λ k μ ) n − k = ( λ / μ ) n k ! k n − k × π 0 n ≥ k \pi_0 = [\sum_{n=0}^{k-1}\frac{(\lambda/\mu)^n}{n!}+\frac{(\lambda/\mu)^k}{k!}\times \frac{1}{1-\lambda/k\mu}]^{-1}\\ \pi_n = \begin{cases} \frac{(\lambda/\mu)^n}{n!}\times \pi_0 & 0 \leq n \leq k\\ \frac{(\lambda/\mu)^n}{k!}\times (\frac{\lambda}{k\mu})^{n-k} = \frac{(\lambda/\mu)^n}{k!k^{n-k}}\times \pi_0 & n \geq k \end{cases} π0=[n=0k1n!(λ/μ)n+k!(λ/μ)k×1λ/kμ1]1πn={n!(λ/μ)n×π0k!(λ/μ)n×(kμλ)nk=k!knk(λ/μ)n×π00nknk

Therefore, according to the result above, we can solve that:

L q = ∑ n = k ∞ ( n − k ) π n = π 0 ( λ / μ ) k ρ k ! ( 1 − ρ ) 2 L_q = \sum_{n=k}^{\infty}(n-k)\pi_n = \frac{\pi_0(\lambda/\mu)^k\rho}{k!(1-\rho)^2} Lq=n=k(nk)πn=k!(1ρ)2π0(λ/μ)kρ
W q = L q λ = π 0 ρ λ k − 1 k ! ( 1 − ρ ) 2 μ k W_q = \frac{L_q}{\lambda} = \frac{\pi_0\rho\lambda^{k-1}}{k!(1-\rho)^2\mu^k} Wq=λLq=k!(1ρ)2μkπ0ρλk1
W = W q + 1 μ = π 0 ρ λ k − 1 + k ! ( 1 − ρ ) 2 μ k − 1 k ! ( 1 − ρ ) 2 μ k W = W_q+\frac{1}{\mu} = \frac{\pi_0\rho\lambda^{k-1} + k!(1-\rho)^2\mu^{k-1}}{k!(1-\rho)^2\mu^k} W=Wq+μ1=k!(1ρ)2μkπ0ρλk1+k!(1ρ)2μk1
L = λ W = π 0 ρ λ k + λ k ! ( 1 − ρ ) 2 μ k − 1 k ! ( 1 − ρ ) 2 μ k L = \lambda W = \frac{\pi_0\rho\lambda^{k} + \lambda k!(1-\rho)^2\mu^{k-1}}{k!(1-\rho)^2\mu^k} L=λW=k!(1ρ)2μkπ0ρλk+λk!(1ρ)2μk1

After the calculation, we can also find that when k k k gets larger, the parameters ( L , L q , W , W q L,L_q,W,W_q L,Lq,W,Wq) of M / M / k M/M/k M/M/k model will decrease.

I. Theoretical Calculation:

Then we can conclude the theoretical value of all of the parameters (number of customers and waiting time) of each model:

F D M FDM FDM M / M / 1 M/M/1 M/M/1 M / M / k M/M/k M/M/k
L L L k λ k μ − λ \frac{k\lambda}{k\mu-\lambda} kμλ λ k μ − λ \frac{\lambda}{k\mu-\lambda} kμλλ π 0 ρ λ k + λ k ! ( 1 − ρ ) 2 μ k − 1 k ! ( 1 − ρ ) 2 μ k \frac{\pi_0\rho\lambda^{k} + \lambda k!(1-\rho)^2\mu^{k-1}}{k!(1-\rho)^2\mu^k} k!(1ρ)2μkπ0ρλk+λk!(1ρ)2μk1
L q L_q Lq λ 2 k μ 2 − λ μ \frac{\lambda^2}{k\mu^2-\lambda\mu} kμ2λμλ2 λ 2 k 2 μ 2 − k λ μ \frac{\lambda^2}{k^2\mu^2-k\lambda\mu} k2μ2μλ2 π 0 ( λ / μ ) k ρ k ! ( 1 − ρ ) 2 \frac{\pi_0(\lambda/\mu)^k\rho}{k!(1-\rho)^2} k!(1ρ)2π0(λ/μ)kρ
W W W k k μ − λ \frac{k}{k\mu-\lambda} kμλk 1 k μ − λ \frac{1}{k\mu-\lambda} kμλ1 π 0 ρ λ k − 1 + k ! ( 1 − ρ ) 2 μ k − 1 k ! ( 1 − ρ ) 2 μ k \frac{\pi_0\rho\lambda^{k-1} + k!(1-\rho)^2\mu^{k-1}}{k!(1-\rho)^2\mu^k} k!(1ρ)2μkπ0ρλk1+k!(1ρ)2μk1
W q W_q Wq λ k μ 2 − λ μ \frac{\lambda}{k\mu^2-\lambda\mu} kμ2λμλ λ k 2 μ 2 − k λ μ \frac{\lambda}{k^2\mu^2-k\lambda\mu} k2μ2μλ π 0 ρ λ k − 1 k ! ( 1 − ρ ) 2 μ k \frac{\pi_0\rho\lambda^{k-1}}{k!(1-\rho)^2\mu^k} k!(1ρ)2μkπ0ρλk1

π 0 = [ ∑ n = 0 k − 1 ( λ / μ ) n n ! + ( λ / μ ) k k ! × 1 1 − λ / k μ ] − 1 \pi_0 = [\sum_{n=0}^{k-1}\frac{(\lambda/\mu)^n}{n!}+\frac{(\lambda/\mu)^k}{k!}\times \frac{1}{1-\lambda/k\mu}]^{-1} π0=[n=0k1n!(λ/μ)n+k!(λ/μ)k×1λ/kμ1]1

According to the theroetical calculation, we can find that the waiting time W , W q W,W_q W,Wq of Frequency-division Multiplexing is always k k k times as long as that of M / M / 1 M/M/1 M/M/1, and the number of customers L , L q L,L_q L,Lq is always k k k times as larger as that of M / M / 1 M/M/1 M/M/1 as well when k > 1 k > 1 k>1.

Thus, the performance of M / M / 1 M/M/1 M/M/1 is better than F D M FDM FDM.

While the parameters of M / M / k M/M/k M/M/k model is complicated, therefore, we need to use diagrams to compare their performance.

Next, we will use diagrams of parmeters L , L q , W , W q L,L_q,W,W_q L,Lq,W,Wq (with respect to the number of queues k \textbf{k} k) to display the theoretical calculation of each model:

from matplotlib import pyplot as plt
import numpy as np
import math

def FDM(lam, mu, K):
    l, l_q, w, w_q = 0,0,0,0
    L,L_q,W,W_q = np.array([]),np.array([]),np.array([]),np.array([])

    for i in range(len(K)):
        k = K[i]

        l = (k*lam)/(k*mu-lam)
        l_q = pow(lam,2)/(k*pow(mu,2)-lam*mu)
        w = k/(k*mu-lam)
        w_q = lam/(k*pow(mu,2)-lam*mu)

        L = np.append(L,l)
        L_q = np.append(L_q,l_q)
        W = np.append(W, w)
        W_q = np.append(W_q,w_q)

    return L,L_q,W,W_q

def M_M_1(lam, mu, K):
    l, l_q, w, w_q = 0,0,0,0
    L,L_q,W,W_q = np.array([]),np.array([]),np.array([]),np.array([])
    
    for i in range(len(K)):
        k = K[i]

        l = lam/(k*mu-lam)
        l_q = pow(lam,2)/(pow(k,2)*pow(mu,2)-k*lam*mu)
        w = 1/(k*mu-lam)
        w_q = lam/(pow(k,2)*pow(mu,2)-k*lam*mu)

        L = np.append(L,l)
        L_q = np.append(L_q,l_q)
        W = np.append(W, w)
        W_q = np.append(W_q,w_q)

    return L,L_q,W,W_q
    
def M_M_k(lam, mu, K):

    sum_1,rho,pi_0 = 0,0,0
    L,L_q,W,W_q = np.array([]),np.array([]),np.array([]),np.array([])

    for i in range(len(K)):
        k = K[i]
        # rho = lambda/(k*mu)
        rho = lam/(k*mu)

        sum_1 = sum([(pow(lam/mu,n)/math.factorial(n))  for n in range(0,k)])

        # the margin probability of state 0: pi_0
        pi_0 = 1/(sum_1+pow(lam/mu,k)/math.factorial(k)*(1/(1-lam/(k*mu))))

        # number of customers in the system
        l = ((pi_0*rho*pow(lam,k)+lam*math.factorial(k)*pow(1-rho,2)*pow(mu,k-1)) \
            /(math.factorial(k)*pow(1-rho,2)*pow(mu,k)))
        # length of queues
        l_q = ((pi_0*pow(lam/mu,k)*rho)/(math.factorial(k)*pow(1-rho,2)))
        # waiting time of the customers in the system
        w = (pi_0*rho*pow(lam,k-1)+math.factorial(k)*pow(1-rho,2)*pow(mu,k-1)) \
            /(math.factorial(k)*pow(1-rho,2)*pow(mu,k))
        # waiting time of the customers for queuing
        w_q = (pi_0*rho*pow(lam,k-1))/(math.factorial(k)*pow(1-rho,2)*pow(mu,k))

        L = np.append(L,l)
        L_q = np.append(L_q,l_q)
        W = np.append(W, w)
        W_q = np.append(W_q,w_q)

    return L,L_q,W,W_q

def plot_performance(Model, lam, mu, K):

    L = [np.array([])]*len(Model)
    L_q = [np.array([])]*len(Model)
    W = [np.array([])]*len(Model)
    W_q = [np.array([])]*len(Model)

    i = 0
    for model in Model:

        if model == "FDM":
            l, l_q, w, w_q = FDM(lam, mu, K)

        if model == "MM1":
            l, l_q, w, w_q = M_M_1(lam, mu, K)
        
        if model == "MMk":
            l, l_q, w, w_q = M_M_k(lam, mu, K)

        L[i] = np.append(L[i], l)
        L_q[i] = np.append(L_q[i], l_q)
        W[i] = np.append(W[i], w)
        W_q[i] = np.append(W_q[i], w_q)

        i += 1

    plt.figure(figsize=[16,8],dpi=80)
    plt.subplot(2,2,1)
    for i in range(len(Model)):
        plt.plot(K,L[i])
    plt.legend(Model)
    plt.title("Number of customers L (lambda=%.1f,mu=%.1f)"%(lam,mu))

    plt.subplot(2,2,2)
    for i in range(len(Model)):
        plt.plot(K,L_q[i])
    plt.legend(Model)
    plt.title("Length of queues L_q (lambda=%.1f,mu=%.1f)"%(lam,mu))

    plt.subplot(2,2,3)
    for i in range(len(Model)):
        plt.plot(K,W[i])
    plt.legend(Model)
    plt.title("Average Waiting Time W (lambda=%.1f,mu=%.1f)"%(lam,mu))

    plt.subplot(2,2,4)
    for i in range(len(Model)):
        plt.plot(K,W_q[i])
    plt.legend(Model)
    plt.title("Average Waiting Time for Queuing W_q (lambda=%.1f,mu=%.1f)"%(lam,mu))

# model name
Model = ["FDM","MM1","MMk"]
# number of dependent queues k
K = np.arange(1,30,1)

# arrival rate lambda
lam = 0.2
# service rate mu
mu = 0.3

plot_performance(Model, lam, mu, K)

在这里插入图片描述

From the result above, we can find that:

According to the diagram, fixing the parameters λ \lambda λ and μ \mu μ, we can find that as the value of k increases, the parameters (number of customers L , L q L,L_q L,Lq and waiting time W , W q W,W_q W,Wq) of each model will decrease. And when k k k is large, the paramerters will converge to a fixed value, which means that the performance of the model wll be improved in the long term.

When it comes to the comparison of each model, the conclusion has also been shown in the image:

All of the parameters of M / M / 1 M/M/1 M/M/1 model are smaller than those of another two models, thus, it is obvious that M / M / 1 M/M/1 M/M/1 model performs better than others. The average waiting time of customers L L L and average waiting time W W W of M / M / 1 M/M/1 M/M/1 model is much smaller than the other two models. Although when k k k is small, the length of queues L q L_q Lq and the average waiting time for queuing W q W_q Wq of M / M / 1 M/M/1 M/M/1 model is slightly larger than M / M / k M/M/k M/M/k model, but when k k k gets larger, the parameters of them will converges to a similar value. Therefore, on the whole, the M / M / 1 M/M/1 M/M/1 model performs better than others.

Also, when k k k is small, the parameters of M / M / k M/M/k M/M/k model are smaller than those of F D M FDM FDM model, and when k k k gets larger, the parameters of M / M / k M/M/k M/M/k models are still slightly smaller than those of F D M FDM FDM. Therefore, we can conclude that the performance of M / M / k M/M/k M/M/k is better than that of F D M FDM FDM.

To sumarize, we can make the conclusion of the performance of the three models:

M / M / 1 > M / M / k > F D M M/M/1 > M/M/k > FDM M/M/1>M/M/k>FDM

II. Simulation

Next, we will simulate each model according to the state transition diagram. The basics of simulation is the CTMC perspective 2.

import numpy as np
from numpy import random
from matplotlib import pyplot as plt

def Transition(model, start, lam, mu, k):
    state,time = start,0
    arrival,service = 0,0

    if model == "FDM":

        arrival = random.exponential(1/(lam/k))
        service = random.exponential(1/mu)

    if model == "MM1":

        arrival = random.exponential(1/lam)
        service = random.exponential(1/(k*mu))
    
    if model == "MMk":

        arrival = random.exponential(1/lam)
        if start != 0:
            if start < k:
                service = random.exponential(1/(start*mu))
            else:
                service = random.exponential(1/(k*mu))

    if arrival < service or start == 0:
        state = start + 1
        time = arrival
    elif arrival >= service and start > 0:
        state = start - 1
        time = service

    return state, time

def CTMC_simulation(model, N, lam, mu, k):

    start,time = 0,0
    total_customer,total_time = 0,0
    num_customer, wait_time = np.array([]),np.array([])
    average_customer,average_time = 0,0
    start_state = np.array([])

    # FDM model
    if model == "FDM":
        for i in range(k):
            num_customer, wait_time = np.array([]),np.array([])
            start_state = np.array([])

            state, time = Transition(model, start, lam, mu, k)
            num_customer = np.append(num_customer,state)
            wait_time = np.append(wait_time,time)
            start_state = np.append(start_state,start)

            for i in range(N-1):
                start = state
                state, time = Transition(model, start, lam, mu, k)
                num_customer = np.append(num_customer,state)
                wait_time = np.append(wait_time,time)
                start_state = np.append(start_state,start)

                if state > start:
                    total_customer += 1

            max_n = int(np.max(num_customer)+1)
            margin_prob = np.zeros(max_n)

            for i in range(max_n):
                num_weight = np.zeros(max_n)
                for j in range(len(start_state)):
                    if i == start_state[j]:
                        num_weight[i] += wait_time[j]
                margin_prob[i] = num_weight[i]/np.sum(wait_time)

            for i in range(max_n):
                average_customer += i*margin_prob[i]
            total_time += np.sum(wait_time*start_state)

        average_time = total_time/total_customer
        L_q = average_customer-lam/mu
        W_q = average_time - 1/mu

    # M/M/1 M/M/S model
    else:
        state, time = Transition(model, start, lam, mu, k)
        num_customer = np.append(num_customer,state)
        wait_time = np.append(wait_time,time)
        start_state = np.append(start_state,start)

        for i in range(N-1):
            start = state
            state, time = Transition(model, start, lam, mu, k)
            num_customer = np.append(num_customer,state)
            wait_time = np.append(wait_time,time)
            start_state = np.append(start_state,start)
        
            if state > start:
                total_customer += 1

        max_n = int(np.max(num_customer)+1)
        margin_prob = np.zeros(max_n)

        for i in range(max_n):
            num_weight = np.zeros(max_n)
            for j in range(len(start_state)):
                if i == start_state[j]:
                    num_weight[i] += wait_time[j]
            margin_prob[i] = num_weight[i]/np.sum(wait_time)

        for i in range(max_n):
            average_customer += i*margin_prob[i]

        average_time = np.sum(wait_time*start_state)/total_customer

        if model == "MM1":
            L_q = average_customer - lam/(k*mu)
            W_q = average_time - 1/(k*mu)
        elif model == "MMk":
            L_q = average_customer - lam/mu
            W_q = average_time - 1/mu

    L = average_customer
    W = average_time
    
    return L, L_q, W, W_q

def plot_CTMC(Model, N, lam, mu, K, num_exper):

    average_L = np.zeros([len(Model),len(K)])
    average_W = np.zeros([len(Model),len(K)])
    average_L_q = np.zeros([len(Model),len(K)])
    average_W_q = np.zeros([len(Model),len(K)])

    for n in range(num_exper):

        l,l_q,w,w_q = 0,0,0,0

        # number of customers in the system
        L = np.zeros([len(Model),len(K)])
        # length of queues
        L_q = np.zeros([len(Model),len(K)])
        # average waiting time of customers in the system
        W = np.zeros([len(Model),len(K)])
        # average waiting time of customers for queuing
        W_q = np.zeros([len(Model),len(K)])

        j = 0
        for model in Model:

            for i in range(len(K)):
                k = K[i]

                l,l_q,w,w_q = CTMC_simulation(model, N, lam, mu, k)
                L[j][i] += l
                L_q[j][i] += l_q
                W[j][i] += w
                W_q[j][i] += w_q

                # # Theorical value of W
                # W[j] = L[j]/lam

            j += 1

    for i in range(len(Model)):
        average_L[i] = L[i]/num_exper
        average_W[i] = W[i]/num_exper
        average_L_q[i] = L_q[i]/num_exper
        average_W_q[i] = W_q[i]/num_exper

    plt.figure(figsize=[16,8],dpi=80)
    plt.subplot(2,2,1)
    for i in range(len(Model)):
        plt.plot(K,average_L[i])
    plt.legend(Model)
    plt.title("Number of customers L (lambda=%.1f,mu=%.1f)"%(lam,mu))

    plt.subplot(2,2,2)
    for i in range(len(Model)):
        plt.plot(K,average_L_q[i])
    plt.legend(Model)
    plt.title("Length of queues L_q (lambda=%.1f,mu=%.1f)"%(lam,mu))

    plt.subplot(2,2,3)
    for i in range(len(Model)):
        plt.plot(K,average_W[i])
    plt.legend(Model)
    plt.title("Average Waiting Time W (lambda=%.1f,mu=%.1f)"%(lam,mu))

    plt.subplot(2,2,4)
    for i in range(len(Model)):
        plt.plot(K,average_W_q[i])
    plt.legend(Model)
    plt.title("Average Waiting Time for Queuing W_q (lambda=%.1f,mu=%.1f)"%(lam,mu))

    return average_L, average_W

# number of experiment
num_exper = 100
# total times of CTMC experiment
N = 600
# model name
Model = ["FDM","MM1","MMk"]

# arrival rate
lam = 0.2
# service rate
mu = 0.3
# number of queues
K = np.arange(1,31,1)

L,W = plot_CTMC(Model, N, lam, mu, K, num_exper)

在这里插入图片描述

From the simulation above, we can find that the results of simulation is similar to the theoretical calculation. And according to the results shown in the diagrams, the performance of models is:

M / M / 1 > M / M / k > F D M M/M/1 > M/M/k > FDM M/M/1>M/M/k>FDM

III Pros and Cons of Each System

1. FDM
Strengths:

In FDM system, the entire process is divided into k k k independent parts, which makes the FDM system is most robust. This feature allows any part of the model to be abnormal without affecting other processes, which is beneficial in reality.

Weaknesses:

The parameters of FDM ( L , L q , W , W q L,L_q,W,W_q L,Lq,W,Wq) of FDM are always the largest among the three models, and thus, FDM model doesn’t perform well.

2. M/M/1
Strengths:

M / M / 1 M/M/1 M/M/1 system has the best performance among the three systems. The parameters of it are relatively smaller. Therefore, the system can serve the most customers within a certain time and has the shortest queues.

Weaknesses:

The system is centralized to one process and less robust, which will lead to the most prone to failure in reality.

3. M/M/k
Strengths:

M / M / k M/M/k M/M/k system has relatively better performances. When k k k is small, the length of queues L q L_q Lq and the average waiting time for queuing of M / M / 1 M/M/1 M/M/1 is the smallest. And it divides its service process into k k k parts, which makes it more robust.

Weaknesses:

Compared with the M / M / 1 M/M/1 M/M/1 system, M / M / k M/M/k M/M/k system behaves worse in performance. And M / M / k M/M/k M/M/k system is not as robust as FDM system.

IV. Explanation

In the process of simulation, it can be found that due to the dispersion of service resources, although the FDM and M / M / 1 M/M/1 M/M/1 model have more robustness, they will also lead to the waste of certain service resources. Different from the MM1 model, these two models split the service resources into k parts, which leads to the fact that when a customer enters, the M / M / 1 M/M/1 M/M/1 model will complete the service at the fastest speed (rate = k μ k\mu kμ), while the other two The speed (rate = μ \mu μ) of the model is slower in comparison.

Also, we can explain the phenomenon that the performance of M / M / 1 M/M/1 M/M/1 is better than M / M / k M/M/k M/M/k according to the State   Transition   Diagram \textbf{State Transition Diagram} State Transition Diagram:
We can find that when the number of customer in the system n < k n < k n<k, the service rate of M / M / k M/M/k M/M/k is n μ n\mu nμ, while the service rate of M / M / 1 M/M/1 M/M/1 is k μ k\mu kμ. And when n ≥ k n \geq k nk, the service rate of them are the same: k μ k\mu kμ. Also, the arrival rate of each model in all states are the same λ \lambda λ. Therefore, the performance of M / M / 1 M/M/1 M/M/1 is better than M / M / k M/M/k M/M/k.

Therefore, it is intuitive that the performance of M/M/1 \textbf{M/M/1} M/M/1 model is the best among the three models. M/M/k \textbf{M/M/k} M/M/k is the second and FDM \textbf{FDM} FDM is the worst.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值