示例#1
0
    def departure(self) -> MarkovArrival:
        arrival, service = self._get_casted_arrival_and_service()

        # Aliasing matrices from arrival MAP and service PH
        d0 = arrival.d0
        d1 = arrival.d1
        w = arrival.order
        iw = np.eye(w)
        s = service.s
        tau = service.init_probs
        v = service.order
        iv = np.eye(v)
        ev = np.ones((v, 1))
        m = self.capacity - 1
        b = v * w
        ob = np.zeros((b, b))

        # Building blocks
        d0_iv = np.kron(d0, iv)
        d1_iv = np.kron(d1, iv)
        d0_s = np.kron(d0, iv) + np.kron(iw, s)
        ct = np.kron(-s.dot(ev), tau)
        iw_ct = np.kron(iw, ct)
        r0 = np.kron(d1, np.kron(tau, ev))
        ra = np.kron(d0 + d1, iv) + np.kron(iw, s)

        # Building departure D0 and D1
        d0_dep = cbdiag(self.capacity, ((0, d0_s), (1, d1_iv)))
        d0_dep[m * b:, m * b:] = ra
        d0_left_col = np.vstack((d0_iv, ) + (ob, ) * self.capacity)
        d0_top_row = np.hstack((r0, ) + (ob, ) * m)
        d0_dep = np.hstack((d0_left_col, np.vstack((d0_top_row, d0_dep))))
        D1_dep = cbdiag(self.capacity + 1, ((-1, iw_ct), ))

        return MarkovArrival(d0_dep, D1_dep)
示例#2
0
    def departure(self):
        # Aliasing matrices from arrival MAP and service PH
        D0 = self.arrival.D0
        D1 = self.arrival.D1
        W = self.arrival.order
        Iw = np.eye(W)
        S = self.service.subgenerator
        tau = self.service.pmf0
        V = self.service.order
        Iv = np.eye(V)
        Ev = np.ones((V, 1))
        M = self.capacity - 1
        B = V * W
        Ob = np.zeros((B, B))

        # Building blocks
        D0_Iv = np.kron(D0, Iv)
        D1_Iv = np.kron(D1, Iv)
        D0_S = np.kron(D0, Iv) + np.kron(Iw, S)
        Ct = np.kron(-S.dot(Ev), tau)
        Iw_Ct = np.kron(Iw, Ct)
        R0 = np.kron(D1, np.kron(tau, Ev))
        Ra = np.kron(D0 + D1, Iv) + np.kron(Iw, S)

        # Building departure D0 and D1
        D0_dep = cbdiag(self.capacity, ((0, D0_S), (1, D1_Iv)))
        D0_dep[M * B:, M * B:] = Ra
        D0_left_col = np.vstack((D0_Iv, ) + (Ob, ) * self.capacity)
        D0_top_row = np.hstack((R0, ) + (Ob, ) * M)
        D0_dep = np.hstack((D0_left_col, np.vstack((D0_top_row, D0_dep))))
        D1_dep = cbdiag(self.capacity + 1, ((-1, Iw_Ct), ))

        return MAP(D0_dep, D1_dep)
示例#3
0
    def test_cbdiag_2x2(self):
        b1 = [[1, 1], [1, 1]]
        b2 = [[2, 2], [2, 2]]
        b3 = [[3, 3], [3, 3]]

        m1 = qumo.cbdiag(1, [(0, b1), (1, b2), (-1, b3)])
        m2 = qumo.cbdiag(2, [(0, b1), (1, b2), (-1, b3)])
        m3 = qumo.cbdiag(3, [(0, b1), (1, b2), (-1, b3)])
        m4 = qumo.cbdiag(3, [(0, b2), (1, b3), (2, b1)])

        np.testing.assert_allclose(m1, [[1, 1], [1, 1]])
        np.testing.assert_allclose(m2, [[1, 1, 2, 2], [1, 1, 2, 2],
                                        [3, 3, 1, 1], [3, 3, 1, 1]])
        np.testing.assert_allclose(m3, [[1, 1, 2, 2, 0, 0],
                                        [1, 1, 2, 2, 0, 0],
                                        [3, 3, 1, 1, 2, 2],
                                        [3, 3, 1, 1, 2, 2],
                                        [0, 0, 3, 3, 1, 1],
                                        [0, 0, 3, 3, 1, 1]])
        np.testing.assert_allclose(m4, [[2, 2, 3, 3, 1, 1],
                                        [2, 2, 3, 3, 1, 1],
                                        [0, 0, 2, 2, 3, 3],
                                        [0, 0, 2, 2, 3, 3],
                                        [0, 0, 0, 0, 2, 2],
                                        [0, 0, 0, 0, 2, 2]])
示例#4
0
 def departure(self):
     n = self.capacity
     a = self.arrival_rate
     b = self.service_rate
     d0 = cbdiag(n + 1, [(0, [[-(a + b)]]), (1, [[a]])])
     d0[0, 0] += b
     d0[n, n] += a
     d1 = cbdiag(n + 1, [(-1, [[b]])])
     return MAP(d0, d1)
示例#5
0
 def departure(self):
     n = self.capacity
     a = self.arrival.rate
     b = self.service.rate
     d0 = cbdiag(n + 1, [(0, np.asarray([[-(a + b)]])),
                         (1, np.asarray([[a]]))])
     d0[0, 0] += b
     d0[n, n] += a
     d1 = cbdiag(n + 1, [(-1, np.asarray([[b]]))])
     return MarkovArrival(d0, d1)
示例#6
0
    def erlang(shape, rate):
        """MAP representation of Erlang process with the given shape and rate.

        :param shape number of phases
        :param rate rate at each phase
        """
        d0 = cbdiag(shape, [(0, [[-rate]]), (1, [[rate]])])
        d1 = np.zeros((shape, shape))
        d1[shape-1, 0] = rate
        return MAP(d0, d1)
示例#7
0
    def erlang(shape: int, rate: float) -> 'MarkovArrival':
        """
        MAP representation of Erlang process with the given shape and rate.

        Parameters
        ----------
        shape : int
            Number of phases
        :param : float
            Rate at each phase
        """
        d0 = cbdiag(shape, [(0, np.asarray([[-rate]])),
                            (1, np.asarray([[rate]]))])
        d1 = np.zeros((shape, shape))
        d1[shape - 1, 0] = rate
        return MarkovArrival(d0, d1)
示例#8
0
def fit_map_horvath(ph, lags):
    """Find D1 matrix using a D0 as subgenerator of the given PH and lags.

    Args:
        ph (`pyqunet.distributions.PH`): a PH distribution approximating D0
        lags (array-like): a list of lag-k auto-correlation coefficients
    """
    if len(lags) > 1:
        return fit_map_horvath(ph, [lags[0]])

    N = ph.order
    D0 = ph.subgenerator
    En = np.ones((N, 1))
    pi = ph.init_probs

    num_lags = len(lags)
    if num_lags == 0:
        D1_row_sum = (-D0).dot(En).reshape(N)
        D1 = np.asarray([D1_row_sum[i] * pi for i in range(N)])
        return MAP(D0, D1)

    ph_moments = stats.moment(ph, 2)
    ph_moments, mu = stats.normalize_moments(ph_moments)

    D0ni = np.linalg.inv(-D0) / mu
    D0ni2 = D0ni.dot(D0ni)
    rate = ph.param * mu
    lag1 = lags[0]

    d = (-D0 * mu).dot(En).reshape((N, 1))
    gamma = pi.dot(D0ni).reshape((1, N))
    block_gamma = cbdiag(N, [(0, gamma)])
    block_eye = np.hstack([np.eye(N)] * N)
    A = np.vstack([block_eye, block_gamma])
    b = np.vstack([d, pi.reshape((N, 1))])

    delta = pow(rate, 2) * pi.dot(D0ni2)
    f = D0ni.dot(En)
    # noinspection PyTypeChecker
    v = lag1 * (2 * pow(rate, 2) * pi.dot(D0ni2).dot(En).reshape(1)[0] - 1) + 1
    c = np.hstack([f[i] * delta for i in range(N)])

    if num_lags == 1:
        A = np.vstack([A, c])
        b = np.vstack([b, [[v]]]).reshape(2 * N + 1)
        ret = scipy.optimize.lsq_linear(A,
                                        b, (0, np.inf),
                                        tol=1e-10,
                                        method='bvls')
        # noinspection PyUnresolvedReferences
        x = ret.x
        assert isinstance(x, np.ndarray)
        D1 = x.reshape((N, N)).transpose() / mu

        try:
            return MAP(D0, D1, rtol=1e-3, atol=1e-4)
        except ValueError:
            if np.abs(lags[0] < 1e-5):
                return fit_map_horvath(ph, [])
            else:
                return fit_map_horvath(ph, [lags[0] * 0.5])
    else:

        def residual(xi, n, input_lags):
            d1i = xi.reshape(n, n).transpose() / mu
            m = MAP(D0, d1i, check=False)
            estimated_lags = [m.lag(i + 1) for i in range(len(input_lags))]
            system_diff = np.asarray(A.dot(xi) - b).flatten() * 1000
            lags_diff = np.asarray(input_lags - estimated_lags)
            diff = np.hstack((system_diff, lags_diff))
            return diff

        lags = np.asarray(lags)

        params = {
            'fun': residual,
            'x0':
            np.asarray([d[i] * pi for i in range(N)]).transpose().flatten(),
            'bounds': (0, np.inf),
            'kwargs': {
                'input_lags': lags,
                'n': N,
            },
        }

        result = scipy.optimize.least_squares(**params)
        # noinspection PyUnresolvedReferences
        D1 = result.x.reshape(N, N).transpose() / mu
        return MAP(D0, D1, check=False)
示例#9
0
 def erlang(shape, rate):
     s = cbdiag(shape, ((0, [[-rate]]), (1, [[rate]])))
     pmf0 = np.asarray((1.0, ) + (0.0, ) * (shape - 1))
     return PhaseType(s, pmf0)