Source code for line_solver.api.lossn.revsens

"""
Revenue sensitivities and implied costs of a loss network.

Native Python port of MATLAB lossn_revsens.m.

References:
    K. W. Ross, "Multiservice Loss Models for Broadband Telecommunication
    Networks", Springer, 1995, Sec. 5.7 and 6.3. F. P. Kelly, "Routing in
    circuit-switched networks: optimization, shadow prices and
    decentralization", Adv. Appl. Probab. 20, 1988.
"""

from math import gcd
from typing import Optional, Tuple

import numpy as np

from ..io.logging import line_error
from .links import _exact_solver, _mci_kwargs, _prepare


[docs] def lossn_revsens(nu, A, C, w, method: str = 'exact', options=None ) -> Tuple[np.ndarray, np.ndarray, float, np.ndarray, Optional[dict]]: """ Derivative of the long-run revenue rate with respect to offered loads. A loss network with offered loads nu (unit mean holding times), requirement matrix A and capacities C earns w_k per unit time for each route-k call in progress, so its revenue rate is W = sum_k w_k QLen_k. This function returns the sensitivity dW/dnu_k and the implied costs: the expected revenue lost over the lifetime of an admitted route-k call through the extra blocking it causes to later calls. They satisfy (Ross 1995, Theorem 5.11):: dW/dnu_k = (1 - B_k) (w_k - c_k), c_k = W(C) - W(C - A e_k), so admitting a route-k call raises revenue only if w_k exceeds c_k. The link implied cost (shadow price) of route k on link j is W(C) - W(C - A(j,k) e_j), the revenue lost when link j alone loses the units of one route-k call. Supported methods: 'exact' (or 'manjunath', 'ms'), 'rec' Theorem 5.11 with exact normalization constants 'erlangfp' reduced-load approximation of the implied costs: the link costs c(j,k) solve Ross (1995) eq. 5.21, c(j,k) = sum_l D(j,l,k) eta(j,l) [w_l - sum_{i~=j} c(i,l)], with eta the Erlang fixed point reduced loads and D the change of the Kaufman-Roberts blocking of route l on link j when its capacity drops by A(j,k); then dW/dnu_k = prod_j (1 - E(j,k)) (w_k - sum_j c(j,k)) 'mci' Monte Carlo summation: dW/dnu_l = Cov(X_l, r(X))/nu_l from the importance samples, with delta-method confidence intervals Args: nu: Offered load of route k (R,). A: Capacity requirement of link j for route k (J, R). C: Capacity of link j (J,). w: Revenue per unit time of a route-k call in progress (R,). method: 'exact' (default), 'rec', 'erlangfp' or 'mci'. options: lossn_mci options (samples, seed, alpha, gamma) as a dict or attribute object, used by 'mci'. Returns: Tuple (dW, c, W, clink, ci) where: - dW: Revenue sensitivity dW/dnu_k (R,). - c: Route implied cost c_k (R,; NaN if a route-k call never fits). - W: Revenue rate sum_k w_k QLen_k. - clink: Link implied cost of route k on link j (J, R; 0 where A(j,k)=0). - ci: 'mci' only: dict with 'revsens' (R,), 'revsensCI' (R,2) and 'level'; None for the other methods. Reference: MATLAB: lossn_revsens.m """ if method is None or method == '': method = 'exact' nu, A, C, R, J = _prepare('lossn_revsens', nu, A, C) w = np.asarray(w, dtype=np.float64).ravel() if w.size != R: line_error('lossn_revsens', 'w must have %d entries.' % R, error_id='LINE:input:Argument') ci = None m = str(method).lower() if m in ('exact', 'manjunath', 'ms', 'rec'): solve = _exact_solver(m) QLen, Loss, _, _ = solve(nu, A, C) W = float(w @ np.ravel(QLen)) c = np.full(R, np.nan) dW = np.zeros(R) for k in range(R): Ck = C - A[:, k] if np.any(Ck < 0): continue # never admitted: B_k = 1 and dW_k = 0 if np.all(A[:, k] == 0): c[k] = 0.0 else: Qk = solve(nu, A, Ck)[0] c[k] = W - float(w @ np.ravel(Qk)) dW[k] = (1 - Loss[k]) * (w[k] - c[k]) clink = 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: clink[j, cols] = np.nan else: Cs = C.copy() Cs[j] = C[j] - a Qs = solve(nu, A, Cs)[0] clink[j, cols] = W - float(w @ np.ravel(Qs)) elif m == 'erlangfp': dW, c, W, clink = _revsens_erlangfp(nu, A, C, w) elif m == 'mci': from .mci import lossn_mci kw = _mci_kwargs(options) kw['revenue'] = w _, _, _, cim, _ = lossn_mci(nu, A, C, **kw) W = cim['revenue'] dW = cim['revsens'] c = cim['impliedCost'] clink = cim['linkImpliedCost'] ci = {'revsens': cim['revsens'], 'revsensCI': cim['revsensCI'], 'level': cim['level']} else: line_error('lossn_revsens', "Unknown method '%s'; use exact, rec, erlangfp or mci." % method, error_id='LINE:input:Method') return dW, c, W, clink, ci
def _revsens_erlangfp(nu, A, C, w): from .erlang import _rat_den, lossn_erlangfp from .kr import lossn_kr R = nu.size J = C.size # Same integral rescaling of each link as lossn_erlangfp, so that the # Kaufman-Roberts recursion counts whole units Aint = np.zeros((J, R), dtype=np.int64) Cint = np.zeros(J, dtype=np.int64) for j in range(J): m = 1 for x in list(A[j, :]) + [C[j]]: d = _rat_den(float(x)) m = m * d // gcd(m, d) Aint[j, :] = np.round(m * A[j, :]).astype(np.int64) Cint[j] = int(np.floor(m * C[j] + 1e-9)) QLen, _, E, _ = lossn_erlangfp(nu, A, C) W = float(w @ QLen) # Reduced load of route l on link j: thinned by every other link it uses eta = np.zeros((J, R)) for j in range(J): for l in range(R): if Aint[j, l] > 0: t = nu[l] for i in range(J): if i != j and Aint[i, l] > 0: t = t * (1 - E[i, l]) eta[j, l] = t # Unknowns c(j,k) for every pair with Aint(j,k) > 0, numbered in the # column-major order of MATLAB find pk, pj = np.nonzero(Aint.T > 0) P = pj.size idx = np.zeros((J, R), dtype=np.int64) idx[pj, pk] = np.arange(P) M = np.eye(P) rhs = np.zeros(P) for j in range(J): users = np.nonzero(Aint[j, :] > 0)[0] _, Q0, _ = lossn_kr(eta[j, :], Aint[j, :], Cint[j]) for a in np.unique(Aint[j, users]): if Cint[j] - a < 0: Qa = np.ones(R) else: _, Qa, _ = lossn_kr(eta[j, :], Aint[j, :], Cint[j] - a) D = Qa - Q0 # extra blocking of each route l for k in users[Aint[j, users] == a]: p = idx[j, k] for l in users: coef = D[l] * eta[j, l] rhs[p] += coef * w[l] for i in range(J): if i != j and Aint[i, l] > 0: M[p, idx[i, l]] += coef cvec = np.linalg.solve(M, rhs) if P > 0 else np.zeros(0) clink = np.zeros((J, R)) clink[pj, pk] = cvec c = np.sum(clink, axis=0) dW = np.prod(1 - E, axis=0) * (w - c) return dW, c, W, clink __all__ = ['lossn_revsens']