Source code for line_solver.api.lossn.links

"""
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






__all__ = ['lossn_links']