Source code for line_solver.api.lossn.kr

"""
Kaufman-Roberts recursion for the multirate single-link loss system.

Native Python port of MATLAB lossn_kr.m,
lossn_kr_dim.m and lossn_kr_asy.m.

References:
    J. S. Kaufman, "Blocking in a Shared Resource Environment", IEEE Trans.
    Commun. 29(10), 1981. J. W. Roberts, "A Service System with Heterogeneous
    User Requirements", 1981. K. W. Ross, "Multiservice Loss Models for
    Broadband Telecommunication Networks", Springer, 1995, Sec. 2.2-2.7.
    M. I. Reiman, "A critically loaded multirate link with trunk
    reservation", Queueing Systems 9, 1991.
"""

import math
from typing import Tuple

import numpy as np

from ..io.logging import line_error

_ARG = 'LINE:input:Argument'


def _check_rho_b(caller: str, rho, b) -> Tuple[np.ndarray, np.ndarray]:
    rho = np.asarray(rho, dtype=np.float64).ravel()
    b = np.asarray(b, dtype=np.float64).ravel()
    K = rho.size
    if b.size != K:
        line_error(caller, 'b has %d entries but rho has %d.' % (b.size, K), error_id=_ARG)
    if np.any(rho < 0):
        line_error(caller, 'rho must be nonnegative.', error_id=_ARG)
    if np.any(b < 0) or np.any(np.abs(b - np.round(b)) > 1e-9):
        line_error(caller, 'b must contain nonnegative integers.', error_id=_ARG)
    return rho, np.round(b).astype(np.int64)


[docs] def lossn_kr(rho, b, C) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: """ Exact occupancy distribution and per-class blocking of a stochastic knapsack. A single link of C bandwidth units is offered K classes of calls. Class k arrives as a Poisson process with offered load rho_k (arrival rate times mean holding time) and holds b_k units for the whole call; a call that finds fewer than b_k free units is lost. The occupancy distribution q(n) = P(n units busy), n = 0..C, satisfies the Kaufman-Roberts recursion:: n q(n) = sum_k b_k rho_k q(n - b_k), q(n) = 0 for n < 0, and a class-k call is blocked when more than C - b_k units are busy:: B_k = sum_{n = C-b_k+1}^{C} q(n). The result is insensitive to the holding-time distributions beyond their means. A class with b_k = 0 is never blocked; a class with b_k > C always is. Args: rho: Offered load of class k (K,), nonnegative. b: Bandwidth units held by a class-k call (K,), nonnegative integers. C: Link capacity in bandwidth units (nonnegative integer). Returns: Tuple (q, B, QLen) where: - q: Occupancy distribution, q[n] = P(n units busy), n = 0..C (C+1,). - B: Blocking probability of class k (K,). - QLen: Mean number of class-k calls in progress, rho_k (1 - B_k) (K,). Reference: MATLAB: lossn_kr.m """ caller = 'lossn_kr' rho, b = _check_rho_b(caller, rho, b) K = rho.size if np.size(C) != 1 or float(np.ravel(C)[0]) < 0 \ or abs(float(np.ravel(C)[0]) - round(float(np.ravel(C)[0]))) > 1e-9: line_error(caller, 'C must be a nonnegative integer.', error_id=_ARG) C = int(round(float(np.ravel(C)[0]))) q = np.zeros(C + 1) q[0] = 1.0 for n in range(1, C + 1): s = 0.0 for k in range(K): if b[k] > 0 and n - b[k] >= 0: s += b[k] * rho[k] * q[n - b[k]] q[n] = s / n if q[n] > 1e250: q = q / q[n] # rescale; only the normalized distribution is used q = q / np.sum(q) B = np.zeros(K) for k in range(K): if b[k] > 0: B[k] = min(1.0, float(np.sum(q[max(0, C - b[k] + 1):]))) QLen = rho * (1.0 - B) return q, B, QLen
[docs] def lossn_kr_dim(rho, b, Btarget) -> Tuple[int, np.ndarray]: """ Dimensions a multirate link by the Kaufman-Roberts recursion. Returns the smallest integer capacity C such that every class of the stochastic knapsack (see lossn_kr) has blocking B_k(C) no larger than its target. The unnormalized Kaufman-Roberts terms:: n q(n) = sum_k b_k rho_k q(n - b_k), q(0) = 1, do not depend on C, so the search extends a single recursion one unit at a time and evaluates B_k(C) = sum_{n>C-b_k} q(n) / sum_{n<=C} q(n) at each step, at a total cost of O(C K). The search scans every C upwards rather than bisecting because the blocking of a wide class need not decrease monotonically in C (Ross 1995, Sec. 2.6). Args: rho: Offered load of class k (K,), nonnegative. b: Bandwidth units held by a class-k call (K,), nonnegative integers. Btarget: Blocking target, scalar or (K,), each in (0, 1]. Returns: Tuple (C, B) where: - C: Smallest capacity with B_k(C) <= Btarget_k for all k. - B: Blocking probabilities at that capacity (K,). Reference: MATLAB: lossn_kr_dim.m """ caller = 'lossn_kr_dim' rho, b = _check_rho_b(caller, rho, b) K = rho.size Btarget = np.asarray(Btarget, dtype=np.float64).ravel() if Btarget.size == 1: Btarget = Btarget[0] * np.ones(K) if Btarget.size != K: line_error(caller, 'Btarget has %d entries but rho has %d.' % (Btarget.size, K), error_id=_ARG) if np.any(Btarget <= 0) or np.any(Btarget > 1): line_error(caller, 'Btarget must lie in (0,1].', error_id=_ARG) Cmax = int(1e8) # far beyond any knapsack the recursion can be asked to size q = np.zeros(1024) q[0] = 1.0 total = 1.0 C = 0 while True: B = np.zeros(K) for k in range(K): if b[k] > 0: B[k] = min(1.0, float(np.sum(q[max(0, C - b[k] + 1):C + 1])) / total) if np.all(B <= Btarget): return C, B if C >= Cmax: line_error(caller, 'No capacity up to %d meets the blocking targets.' % Cmax, error_id='LINE:numerical:Infeasible') C += 1 if C + 1 > q.size: q = np.concatenate([q, np.zeros(q.size)]) s = 0.0 for k in range(K): if b[k] > 0 and C - b[k] >= 0: s += b[k] * rho[k] * q[C - b[k]] q[C] = s / C total = total + q[C] if q[C] > 1e250: scale = q[C] q[:C + 1] = q[:C + 1] / scale # only ratios of the terms are used total = total / scale
def _normal_hazard(x: float) -> float: """phi(x)/Phi(x). For x far in the lower tail both factors underflow, so the ratio is taken from the asymptotic Mills-ratio series instead.""" if x >= -35: return math.exp(-x ** 2 / 2) / math.sqrt(2 * math.pi) / (0.5 * math.erfc(-x / math.sqrt(2))) return -x / (1 - 1 / x ** 2 + 3 / x ** 4 - 15 / x ** 6)
[docs] def lossn_kr_asy(rho, b, C) -> Tuple[np.ndarray, float, float, float]: """ Normal approximation of multirate single-link blocking for large C. In the critically loaded regime, where C and the offered loads grow together with sum_k b_k rho_k / C -> 1, the normalized occupancy of a stochastic knapsack converges to a truncated multivariate normal law (Kelly 1991; Ross 1995, Theorem 2.8) and blocking is asymptotically proportional to the call size (Ross 1995, Corollary 2.7). For a knapsack of fixed capacity C the resulting approximation is:: alpha = (C - sum_k b_k rho_k) / sqrt(C), sigma^2 = sum_k b_k^2 rho_k / C, delta = phi(alpha/sigma) / (sigma Phi(alpha/sigma)), B_k = b_k delta / sqrt(C), with phi and Phi the standard normal density and distribution function. B_k is capped at 1. The approximation is accurate when |alpha| is small (roughly below 1); far from critical loading use lossn_kr for the exact values, or lossn_asy for the fluid (Kelly) limit. Args: rho: Offered load of class k (K,), nonnegative. b: Bandwidth units held by a class-k call (K,), nonnegative. C: Link capacity (positive). Returns: Tuple (B, alpha, sigma, delta) where: - B: Approximate blocking probability of class k (K,). - alpha: Normalized spare capacity (C - sum b rho)/sqrt(C). - sigma: Normalized occupancy standard deviation. - delta: Blocking per unit of call size, times sqrt(C). Reference: MATLAB: lossn_kr_asy.m """ caller = 'lossn_kr_asy' rho = np.asarray(rho, dtype=np.float64).ravel() b = np.asarray(b, dtype=np.float64).ravel() K = rho.size if b.size != K: line_error(caller, 'b has %d entries but rho has %d.' % (b.size, K), error_id=_ARG) if np.any(rho < 0) or np.any(b < 0): line_error(caller, 'rho and b must be nonnegative.', error_id=_ARG) if np.size(C) != 1 or float(np.ravel(C)[0]) <= 0: line_error(caller, 'C must be a positive scalar.', error_id=_ARG) C = float(np.ravel(C)[0]) alpha = (C - float(np.sum(b * rho))) / math.sqrt(C) sigma = math.sqrt(float(np.sum(b ** 2 * rho)) / C) if sigma == 0: # no class asks for any bandwidth: nothing is ever blocked return np.zeros(K), alpha, sigma, 0.0 delta = _normal_hazard(alpha / sigma) / sigma B = np.minimum(1.0, b * delta / math.sqrt(C)) return B, alpha, sigma, delta
__all__ = ['lossn_kr', 'lossn_kr_dim', 'lossn_kr_asy']