"""
Large-capacity (Kelly limit) approximation of loss networks.
Native Python port of MATLAB lossn_asy.m.
References:
F. P. Kelly, "Blocking probabilities in large circuit-switched networks",
Adv. Appl. Probab. 18, 1986. F. P. Kelly, "Loss Networks", Ann. Appl.
Probab. 1(3), 1991. K. W. Ross, "Multiservice Loss Models for Broadband
Telecommunication Networks", Springer, 1995, Sec. 5.8.
"""
from typing import Tuple
import numpy as np
from ..io.logging import line_error
[docs]
def lossn_asy(nu, A, C) -> Tuple[np.ndarray, np.ndarray, np.ndarray, int]:
"""
Computes the fluid limit of a loss network by convex duality.
Scales offered loads nu and capacities C together, nu -> N nu and
C -> N C, and returns the limit of the class loss probabilities as N
grows (Kelly 1986; Ross 1995, Theorem 5.13). In this limit link j blocks
independently with probability a_j, and the a_j solve the complementary
slackness conditions::
sum_k A(j,k) nu_k prod_i (1-a_i)^A(i,k) = C_j if a_j > 0,
<= C_j if a_j = 0,
the route loss being 1 - prod_j (1-a_j)^A(j,k). With y_j = -log(1-a_j)
these are the optimality conditions of the strictly convex problem::
minimize sum_k nu_k exp(-sum_j A(j,k) y_j) + sum_j C_j y_j, y >= 0,
solved here by cyclic coordinate descent, each coordinate by Newton's
method on a concave increasing function. The result needs no summation
over states and costs O(J R) per sweep, so it scales to any capacity; it
reports zero loss on links that are underloaded at the fluid scale, so it
is accurate in heavy (overloaded) traffic and for large C. A and C need
not be integral.
Args:
nu: Offered load of route (class) r (R,).
A: Capacity requirement of link j for route r (J, R), nonnegative.
C: Capacity of link j (J,), nonnegative.
Returns:
Tuple (QLen, Loss, a, niter) where:
- QLen: Mean number of route-r calls in progress, nu_r (1-Loss_r) (R,).
- Loss: Limiting loss probability of route r (R,).
- a: Limiting blocking probability of link j (J,).
- niter: Number of coordinate-descent sweeps.
Reference:
MATLAB: lossn_asy.m
"""
nu = np.asarray(nu, dtype=np.float64).ravel()
C = np.asarray(C, dtype=np.float64).ravel()
R = nu.size
J = C.size
A = np.asarray(A, dtype=np.float64)
if A.size == J * R and A.ndim < 2:
A = A.reshape(J, R)
if A.shape != (J, R):
line_error('lossn_asy', 'A must be %dx%d (J x R).' % (J, R),
error_id='LINE:input:Argument')
if np.any(nu < 0) or np.any(A < 0) or np.any(C < 0):
line_error('lossn_asy', 'nu, A and C must be nonnegative.',
error_id='LINE:input:Argument')
tol = 1e-12
maxsweeps = 10000
# A link of zero capacity blocks every route through it with certainty;
# those routes carry nothing and drop out of the optimization
a = np.zeros(J)
dead = (C <= 0) & np.any(A > 0, axis=1)
a[dead] = 1.0
lost = np.any(A[dead, :] > 0, axis=0)
live = np.nonzero(~dead)[0]
nul = nu.copy()
nul[lost] = 0.0
y = np.zeros(J)
s = np.zeros(R) # s_k = sum_j A(j,k) y_j over live links
niter = 0
for sweep in range(1, maxsweeps + 1):
niter = sweep
change = 0.0
for j in live:
users = np.nonzero((A[j, :] > 0) & (nul > 0))[0]
if users.size == 0:
continue
Aj = A[j, users]
rest = s[users] - Aj * y[j] # contribution of the other links
base = nul[users] * np.exp(-rest)
# g(t) = C_j - sum_k A_jk base_k exp(-A_jk t) is concave increasing
if C[j] - np.sum(Aj * base) >= 0:
t = 0.0
else:
t = y[j]
if C[j] - np.sum(Aj * base * np.exp(-Aj * t)) > 0:
t = 0.0 # restart left of the root
for _ in range(100):
e = base * np.exp(-Aj * t)
g = C[j] - np.sum(Aj * e)
dg = np.sum(Aj ** 2 * e)
step = -g / dg
t = t + step
if abs(step) <= 1e-14 * max(1.0, t):
break
anew = -np.expm1(-t)
change = max(change, abs(anew + np.expm1(-y[j])))
s[users] = rest + Aj * t
y[j] = t
if change <= tol:
break
a[live] = -np.expm1(-y[live])
Loss = np.zeros(R)
for k in range(R):
if lost[k]:
Loss[k] = 1.0
else:
Loss[k] = -np.expm1(-float(A[live, k] @ y[live]))
QLen = nu * (1.0 - Loss)
return QLen, Loss, a, niter
__all__ = ['lossn_asy']