"""
Per-link blocking and occupancy of a loss network.
Native Python port of MATLAB lossn_links.m.
"""
from typing import Tuple
import numpy as np
from ..io.logging import line_error
_MCI_FIELDS = ('samples', 'gamma', 'seed', 'alpha', 'revenue')
def _mci_kwargs(options) -> dict:
"""Keyword arguments of lossn_mci from an options dict or attribute object.
Missing or empty fields take the lossn_mci default, as getfielddef in MATLAB."""
out = {}
if options is None:
return out
for f in _MCI_FIELDS:
if isinstance(options, dict):
v = options.get(f, None)
else:
v = getattr(options, f, None)
if v is None or (np.ndim(v) > 0 and np.size(v) == 0):
continue
out[f] = v
if 'samples' in out:
out['samples'] = int(out['samples'])
return out
def _exact_solver(method: str):
if method == 'rec':
from .rec import lossn_rec
return lossn_rec
from .manjunath import lossn_manjunath
return lossn_manjunath
def _prepare(caller: str, nu, A, C):
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(caller, 'A must be %dx%d (J x R).' % (J, R), error_id='LINE:input:Argument')
return nu, A, C, R, J
[docs]
def lossn_links(nu, A, C, method: str = 'exact', options=None
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
"""
Link-level performance measures of a product-form loss network.
For a loss network with offered loads nu, requirement matrix A and
capacities C (A n <= C), returns for every link j and route r the
probability E(j,r) that link j alone lacks the A(j,r) free units a
route-r call needs, the mean occupancy of each link, and the route
losses. With the exact methods the link blocking is::
E(j,r) = 1 - G(C - A(j,r) e_j) / G(C),
where G is the product-form normalization constant and e_j the j-th unit
vector, computed from one extra constant per distinct (j, A(j,r)) pair.
A route-r call is lost when ANY of its links blocks, so Loss_r is at
least max_j E(j,r). The mean occupancy of link j is sum_r A(j,r) QLen_r.
Supported methods:
'exact' (or 'manjunath', 'ms') Manjunath-style exact recursion
'rec' recursive decision diagram (Kirby-Wang style) normalization
'erlangfp' Erlang fixed point; E is the per-link Kaufman-Roberts
blocking on the reduced loads
'asy' Kelly limit; E(j,r) = 1 - (1-a_j)^A(j,r)
'mci' Monte Carlo summation; E from the same samples as Loss
Args:
nu: Offered load of route r (R,).
A: Capacity requirement of link j for route r (J, R).
C: Capacity of link j (J,).
method: 'exact' (default), 'rec', 'erlangfp', 'asy' or 'mci'.
options: lossn_mci options (samples, seed, alpha, gamma) as a dict or
attribute object, used by 'mci'.
Returns:
Tuple (E, Occ, Loss, QLen) where:
- E: Blocking probability of route r on link j (J, R; 0 where A(j,r)=0).
- Occ: Mean number of busy units on link j (J,).
- Loss: Loss probability of route r (R,).
- QLen: Mean number of route-r calls in progress (R,).
Reference:
MATLAB: lossn_links.m
"""
if method is None or method == '':
method = 'exact'
nu, A, C, R, J = _prepare('lossn_links', nu, A, C)
m = str(method).lower()
if m in ('exact', 'manjunath', 'ms', 'rec'):
solve = _exact_solver(m)
QLen, Loss, lG, _ = solve(nu, A, C)
E = np.zeros((J, R))
for j in range(J):
for a in np.unique(A[j, A[j, :] > 0]):
cols = A[j, :] == a
if C[j] - a < 0:
E[j, cols] = 1.0
else:
Cs = C.copy()
Cs[j] = C[j] - a
_, _, lGs, _ = solve(nu, A, Cs)
E[j, cols] = min(1.0, max(0.0, -np.expm1(lGs - lG)))
elif m == 'erlangfp':
from .erlang import lossn_erlangfp
QLen, Loss, E, _ = lossn_erlangfp(nu, A, C)
elif m == 'asy':
from .asy import lossn_asy
QLen, Loss, alink, _ = lossn_asy(nu, A, C)
E = np.zeros((J, R))
for j in range(J):
for r in range(R):
if A[j, r] > 0:
if alink[j] >= 1:
E[j, r] = 1.0
else:
E[j, r] = -np.expm1(A[j, r] * np.log1p(-alink[j]))
elif m == 'mci':
from .mci import lossn_mci
QLen, Loss, _, ci, _ = lossn_mci(nu, A, C, **_mci_kwargs(options))
E = ci['linkLossPoint']
else:
line_error('lossn_links', "Unknown method '%s'; use exact, rec, erlangfp, asy or mci."
% method, error_id='LINE:input:Method')
QLen = np.asarray(QLen, dtype=np.float64).ravel()
Loss = np.asarray(Loss, dtype=np.float64).ravel()
Occ = A @ QLen
return E, Occ, Loss, QLen
__all__ = ['lossn_links']