Ejemplo n.º 1
0
 def testOnes_inv(self):
     A1 = [np.array([[1., 1.], [1., 1.]]), np.array([[1., 1.], [1., 1.]])]
     x1 = np.array([1., 1., 1., 1.])
     y1 = np.array([4, 4, 4, 4])
     kp = KronProd(invert(A1))
     x = kp.dot(y1)
     np.testing.assert_almost_equal(x, x1, decimal=7, verbose=True)
Ejemplo n.º 2
0
 def testOnes(self):
     A1 = [np.array([[1., 1.], [1., 1.]]), np.array([[1., 1.], [1., 1.]])]
     x1 = np.array([1., 1., 1., 1.])
     y1 = np.array([4., 4., 4., 4.])
     kp = KronProd(A1)
     y = kp.dot(x1)
     self.assertSequenceEqual(list(y), list(y1))
Ejemplo n.º 3
0
def inverseTest(A, x):
    foo = time.time()
    big_A = reduce(np.kron, A)
    big_A = np.linalg.inv(big_A)
    big_y1 = np.matmul(big_A, x)
    time_full = time.time() - foo

    foo = time.time()
    newA = []
    for i in range(len(A)):
        newA.append(np.linalg.inv(A[i]))
    big_A = reduce(np.kron, newA)
    big_y2 = np.matmul(big_A, x)
    time_kron = time.time() - foo

    foo = time.time()
    newA = []
    for i in range(len(A)):
        newA.append(np.linalg.inv(A[i]))
    kp1 = KronProd(newA)
    Y1 = kp1.dot(x)
    time_kron_dyn = time.time() - foo

    if (verifyCorrectness(big_y1, big_y2, Y1)):
        print("Inverse Test Passed")
    else:
        print("Inverse Test Failed!")

    return time_full, time_kron, time_kron_dyn
Ejemplo n.º 4
0
 def testBig_inv(self):
     n = 2  # number of factors
     p = 100  # dimension of factor
     r_As = [ortho_group.rvs(dim=p) for i in range(n)]
     As = [m / m.sum(axis=1)[:, None] for m in r_As]  # normalize each row
     y = np.random.rand(p**n)
     kp = KronProd(invert(As))
     x = kp.dot(y)
     print("efficient calc: ", x)
Ejemplo n.º 5
0
 def testBig(self):
     n = 2  # number of factors
     p = 100  # dimension of factor
     r_As = [np.random.rand(p, p) for i in range(n)]
     As = [m / m.sum(axis=1)[:, None] for m in r_As]  # normalize each row
     x = np.random.rand(p**n)
     kp = KronProd(As)
     Y = kp.dot(x)
     print("efficient calc: ", Y)
Ejemplo n.º 6
0
 def testBig_pInv(self):
     n = 2  # number of factors
     p = 100  # dimension of factor
     r_As = [np.random.rand(p, p) for i in range(n)]
     #Make first and second row the same so that way it becomes a non-invertible matrix
     for A in r_As:
         A[1, :] = A[0, :]
     As = [m / m.sum(axis=1)[:, None] for m in r_As]  # normalize each row
     x = np.random.rand(p**n)
     kp = KronProd(invert(As))
     Y = kp.dot(x)
     print("efficient calc: ", Y)
Ejemplo n.º 7
0
    def _evalPolicyMatrix(self):
        # Evaluate the value function of the policy using linear equations.
        #
        # Arguments
        # ---------
        # Let S = number of states, A = number of actions
        # P(SxSxA) = transition matrix
        #      P could be an array with 3 dimensions or a cell array (1xA),
        #      each cell containing a matrix (SxS) possibly sparse
        # R(SxSxA) or (SxA) = reward matrix
        #      R could be an array with 3 dimensions (SxSxA) or
        #      a cell array (1xA), each cell containing a sparse matrix (SxS)
        #      or a 2D array(SxA) possibly sparse
        # discount = discount rate in ]0; 1[
        # policy(S) = a policy
        #
        # Evaluation
        # ----------
        # Vpolicy(S) = value function of the policy
        #
        #Ppolicy, Rpolicy = self._computePpolicyPRpolicy()
        # V = PR + gPV  => (I-gP)V = PR  => V = inv(I-gP)* PR
        #self.V = _np.linalg.solve(
        #    (_sp.eye(self.S, self.S) - self.discount * Ppolicy), Rpolicy)

        #Get appropiately size identity matrices
        i_as = []
        for i in self.P[0].n:
            i_as.append(_np.eye(i))
        kpI = KronProd(list(reversed(i_as)))

        #Get max value vector
        vectors = []
        for aa in range(self.A):
            #apply discount to a gP
            gP = self.P[aa]
            rP = self.R[aa]
            Y1 = kpI.dot(rP)
            Y2 = gP.dot(rP)
            vectors.append(Y1-Y2)

        newV = []
        for i in range(len(vectors[0])):
            local_max = float("-inf")
            for j in range(len(vectors)):
                if vectors[j][i] > local_max:
                    local_max = vectors[j][i]
            newV.append(local_max)

        self.V = _np.asarray(newV)
Ejemplo n.º 8
0
 def testInts(self):
     A1 = [
         np.array([[1.0, 0.0], [0.0, 0.0]]),
         np.array([[1., 1.], [0., 0.]])
     ]
     x1 = np.array([1., 2., 3., 4.])
     big_A = reduce(np.kron, A1)
     print(big_A)
     print(x1)
     big_y = np.matmul(big_A, x1)
     print("full calc: ", big_y)
     kp = KronProd(A1)
     Y = kp.dot(x1)
     print("efficient calc: ", Y)
     self.assertSequenceEqual(list(Y), list(big_y))
Ejemplo n.º 9
0
    def testRandom_inv(self):
        n = 5  # number of factors
        p = 5  # dimension of factor
        r_As = [ortho_group.rvs(dim=p) for i in range(n)]
        As = [m / m.sum(axis=1)[:, None] for m in r_As]  # normalize each row
        y = np.random.rand(p**n)

        big_A = reduce(np.kron, As)
        big_x = np.linalg.solve(big_A, y)
        print("full calc: ", big_x)

        kp = KronProd(invert(As))
        x = kp.dot(y)
        print("efficient calc: ", x)

        np.testing.assert_almost_equal(x, big_x, decimal=7, verbose=True)
Ejemplo n.º 10
0
    def testRandom(self):
        n = 5  # number of factors
        p = 5  # dimension of factor
        r_As = [np.random.rand(p, p) for i in range(n)]
        As = [m / m.sum(axis=1)[:, None] for m in r_As]  # normalize each row
        x = np.random.rand(p**n)

        big_A = reduce(np.kron, As)
        big_y = np.matmul(big_A, x)
        print("full calc: ", big_y)

        kp = KronProd(As)
        Y = kp.dot(x)
        print("efficient calc: ", Y)

        np.testing.assert_almost_equal(big_y, Y, decimal=7, verbose=True)
Ejemplo n.º 11
0
 def testInts_pInv(self):
     A1 = [ np.array([[1.0, 0.0], [0.0,0.0]]),
                 np.array([[1.,1.], [0.,0.]])]
     y1 = np.array([1.,2.,3.,4.])
     A1_inv = []
     for a in A1:
         A1_inv.append(np.linalg.pinv(a))
     big_A = reduce(np.kron, A1_inv)
     big_x = big_A.dot(y1)
     print("FOO")
     print("full calc: ",big_x)
     kp = KronProd(invert(A1))
     x = kp.dot(y1)
     print("efficient calc: ", x)
     print("BAR")
     self.assertSequenceEqual(list(x), list(big_x))
Ejemplo n.º 12
0
 def testFail(self):
     #For some reason this breaks the property of kron product - (A_1 kron A_2)^+ = A_1^+ kron A_2^+
     #The equivalent test for this is testRandom_pInv, but the reduce is performed after pinv. If it isn't then we get this error.
     #This error didn't seem to happen for n=p=3 or 2 but does occur sometimes when n=p=4 and somewhat frequently when n=p=5.
     n = 4
     p = 4
     As = As_fail
     y = Y_fail
     big_A = reduce(np.kron, As)
     big_A_inv = np.linalg.pinv(big_A)
     big_x = big_A_inv.dot(y)
     kp = KronProd(invert(As))
     x = kp.dot(y)
     if (np.allclose(x, big_x) == False):
         return
     else:
         self.fail("No equivalent!")
Ejemplo n.º 13
0
def dotTest(A, x):
    foo = time.time()
    big_A = reduce(np.kron, A)
    big_y1 = np.matmul(big_A, x)
    time_full = time.time() - foo

    foo = time.time()
    kp1 = KronProd(A)
    Y1 = kp1.dot(x)
    time_kron_dyn = time.time() - foo

    if (verifyCorrectness(big_y1, big_y1, Y1)):
        print("Dot Test Passed")
    else:
        print("Dot Test Failed!")

    return time_full, time_kron_dyn
Ejemplo n.º 14
0
    def testRandom_pInv(self):
        n = 5  # number of factors
        p = 5  # dimension of factor
        r_As = [ortho_group.rvs(dim=p) for i in range(n)]
        #Make first and second row the same so that way it becomes a non-invertible matrix
        for A in r_As:
            A[1, :] = A[0, :]
        As = [m / m.sum(axis=1)[:, None] for m in r_As]  # normalize each row
        y = np.random.rand(p**n)
        As_inv = []
        for a in As:
            As_inv.append(np.linalg.pinv(a))

        big_A = reduce(np.kron, As_inv)
        big_x = big_A.dot(y)
        print("[test_kron_inv - testRandom_pInv] full calc: ", big_x)

        kp = KronProd(invert(As))
        x = kp.dot(y)
        print("[test_kron_inv - testRandom_pInv] efficient calc: ", x)

        np.testing.assert_almost_equal(big_x, x, decimal=7, verbose=True)
Ejemplo n.º 15
0
 def _computeTransition(self, transition):
     if self.sparse:
         return tuple(KronProdSparse(transition[a]) for a in range(self.A))
     else:
         return tuple(KronProd(transition[a]) for a in range(self.A))