Ejemplo n.º 1
0
 def test():
     print('\n'+'_'*80 + '\nk_means [see ./fig/k_means.png]')
     import random
     n = 2
     m = 20
     k = 4
     s = randn(n,m)+np.kron(ones(1,m),10*randn(n,1))
     for i in range(k-1):
         s = np.hstack((s,randn(n,m)+np.kron(ones(1,m),10*randn(n,1))))
     k_means(s,k).plot()
Ejemplo n.º 2
0
 def __init__(self,s):
     """
         x ∈ R^n
     """
     s = sampled_convex_set.remove_interior_points(s)
     n = s.shape[0]
     m = s.shape[1]
     A_ub = np.bmat([[zeros(m,n),-eye(m)],[zeros(m,n),eye(m)]])
     b_ub = np.bmat([[zeros(m)],[ones(m)]])
     A_eq = np.bmat([[-eye(n),s],[zeros(1,n),ones(1,m)]])
     b_eq = np.bmat([[zeros(n)],[ones(1)]])
     self.P = poly(A_ub,b_ub,A_eq,b_eq)
Ejemplo n.º 3
0
 def __init__(self,a,b,c,d,P):
     n = P.n 
     m = P.A_ub.shape[0]
     r = P.A_eq.shape[0]
     A_ub = np.bmat([[P.A_ub,-P.b_ub],[zeros(1,n),-ones(1)]])
     b_ub = zeros(m+1)
     A_eq = np.bmat([[P.A_eq,-P.b_eq],[c.T,-d]])
     b_eq = np.bmat([[zeros(r)],[ones(1)]])
     P = poly(A_ub,b_ub,A_eq,b_eq)
     Q = None
     c = np.bmat([[a],[b]])
     self.QP = QP(None,c,P)
     self.n = n
Ejemplo n.º 4
0
    def xposxneg_2_xy(x_pos, x_neg):  # TODO: validate
        """
            x_pos ∈ R^{n,m_pos}
            x_neg ∈ R^{n,m_neg}

            to

            x ∈ R^{n,m}
            y ∈ {-1,1}^m
        """
        n = x_pos.shape[0]
        m_pos = x_pos.shape[1]
        m_neg = x_neg.shape[1]
        assert x_neg.shape[0] == n
        x = np.bmat([[x_pos, x_neg]])
        y = np.bmat([[ones(m_pos)], [-ones(m_neg)]])
        return (x, y)  # = self.xposxneg_2_xy(x_pos,x_neg)
Ejemplo n.º 5
0
 def bias(self, x_pos, x_neg):
     m_pos = x_pos.shape[1]
     m_neg = x_neg.shape[1]
     Q = None
     c =     np.bmat([\
             [   zeros(1)    ],\
             [   ones(m_pos) ],\
             [   ones(m_neg) ]\
             ])
     A_ub =  np.bmat([\
             [  ones(m_pos),  -eye(m_pos),             zeros(m_pos,m_neg) ],\
             [ -ones(m_neg),   zeros(m_neg,m_pos),    -eye(m_neg)         ],\
             [  zeros(m_pos), -eye(m_pos),             zeros(m_pos,m_neg) ],\
             [  zeros(m_neg),  zeros(m_neg,m_pos),    -eye(m_neg)         ]\
             ])
     b_ub = np.bmat([\
             [ -ones(m_pos)  ],\
             [ -ones(m_neg)  ],\
             [  zeros(m_pos) ],\
             [  zeros(m_neg) ]\
             ])
     for i in range(m_pos):
         b_ub[i, 0] += self.a(x_pos[:, i])
     for i in range(m_neg):
         b_ub[i + m_pos, 0] -= self.a(x_neg[:, i])
     P = poly(A_ub, b_ub)
     sol = QP(Q, c, P).solve()
     b = sol[0, 0]
     return b
Ejemplo n.º 6
0
 def soft_dual_alternative(self, x, y, λ=0.1):
     assert λ >= 0 and λ <= 1, "λ ∈ R[0,1]"
     m = y.shape[0]
     c_dual = None
     Q_dual = eye(m)
     for i in range(m):
         for j in range(m):
             Q_dual[i, j] = y[i, 0] * y[j, 0] * self.K(x[:, i], x[:, j])
     A_ub =  np.bmat([\
             [   -eye(m)     ],\
             [    eye(m)     ],\
             [    ones(1,m)  ]\
             ])
     b_ub =  np.bmat([\
             [   zeros(m)    ],\
             [   ones(m)/float(m) ],\
             [   λ*ones(1)   ]\
             ])
     A_eq = y.T
     b_eq = zeros(1)
     P_dual = poly(A_ub, b_ub, A_eq, b_eq)
     z = QP(Q_dual, c_dual, P_dual).solve()
     return z
Ejemplo n.º 7
0
 def dual(self, x, y):
     m = y.shape[0]
     c_dual = -ones(m)
     Q_dual = eye(m)
     for i in range(m):
         for j in range(m):
             Q_dual[i, j] = y[i, 0] * y[j, 0] * self.K(x[:, i], x[:, j])
     A_ub = -eye(m)
     b_ub = zeros(m)
     A_eq = y.T
     b_eq = zeros(1)
     P_dual = poly(A_ub, b_ub, A_eq, b_eq)
     z = QP(Q_dual, c_dual, P_dual).solve()
     return z
Ejemplo n.º 8
0
    def __init__(self, x_pos, x_neg, p='inf', λ=1):
        """
            x_pos ∈ R^{n,m_pos}
            x_neg ∈ R^{n,m_neg}
        """
        n = x_pos.shape[0]
        m_pos = x_pos.shape[1]
        m_neg = x_neg.shape[1]
        assert x_neg.shape[0] is n
        if p is 1:
            Q = None
            c = np.bmat([[zeros(n + 1)], [λ * ones(m_pos + m_neg)], [ones(n)]])
            A_ub =  np.bmat([\
                    [ -x_pos.T, ones(m_pos), -eye(m_pos), zeros(m_pos,m_neg), zeros(m_pos,n) ],\
                    [ x_neg.T, -ones(m_neg), zeros(m_neg,m_pos), -eye(m_neg), zeros(m_neg,n) ],\
                    [ -eye(n), zeros(n), zeros(n,m_pos), zeros(n,m_neg), -eye(n) ],\
                    [ eye(n), zeros(n), zeros(n,m_pos), zeros(n,m_neg), -eye(n) ],\
                    [ zeros(m_pos,n), zeros(m_pos), -eye(m_pos), zeros(m_pos,m_neg), zeros(m_pos,n) ],\
                    [ zeros(m_neg,n), zeros(m_neg), zeros(m_neg,m_pos), -eye(m_neg), zeros(m_neg,n) ]])
            b_ub =  np.bmat([\
                    [ -ones(m_pos) ],\
                    [ -ones(m_neg) ],\
                    [ zeros(n) ],\
                    [ zeros(n) ],\
                    [ zeros(m_pos) ],\
                    [ zeros(m_neg) ]])
            P = poly(A_ub, b_ub)
            sol = QP(Q, c, P).solve()
            self.a = sol[0:n, 0]
            self.b = sol[n, 0]
        if p is 2:
            Q = zeros(n + 1 + m_pos + m_neg, n + 1 + m_pos + m_neg)
            Q[0:n, 0:n] = 2 * eye(n)
            c = zeros(n + 1 + m_pos + m_neg, 1)
            c[(n + 1 + 1 - 1):(n + 1 + m_pos +
                               m_neg)] = λ * ones(m_pos + m_neg, 1)

            A_ub =  np.bmat([\
                    [ -x_pos.T, ones(m_pos), -eye(m_pos), zeros(m_pos,m_neg) ],\
                    [ x_neg.T, -ones(m_neg), zeros(m_neg,m_pos), -eye(m_neg) ],\
                    [ zeros(m_pos,n), zeros(m_pos), -eye(m_pos), zeros(m_pos,m_neg) ],\
                    [ zeros(m_neg,n), zeros(m_neg), zeros(m_neg,m_pos), -eye(m_neg) ]])
            b_ub = np.bmat([\
                    [ -ones(m_pos) ],\
                    [ -ones(m_neg) ],\
                    [ zeros(m_pos) ],\
                    [ zeros(m_neg) ]\
                    ])
            P = poly(A_ub, b_ub)
            sol = QP(Q, c, P).solve()
            self.a = sol[0:n, 0]
            self.b = sol[n, 0]
        if p is 'inf':
            Q = None
            c = np.bmat([[zeros(n + 1)], [λ * ones(m_pos + m_neg)], [ones(1)]])
            A_ub =  np.bmat([\
                    [ -x_pos.T, ones(m_pos), -eye(m_pos), zeros(m_pos,m_neg), zeros(m_pos) ],\
                    [ x_neg.T, -ones(m_neg), zeros(m_neg,m_pos), -eye(m_neg), zeros(m_neg) ],\
                    [ -eye(n), zeros(n), zeros(n,m_pos), zeros(n,m_neg), -ones(n) ],\
                    [ eye(n), zeros(n), zeros(n,m_pos), zeros(n,m_neg), -ones(n) ],\
                    [ zeros(m_pos,n), zeros(m_pos), -eye(m_pos), zeros(m_pos,m_neg), zeros(m_pos) ],\
                    [ zeros(m_neg,n), zeros(m_neg), zeros(m_neg,m_pos), -eye(m_neg), zeros(m_neg) ]])
            b_ub = np.bmat([\
                    [ -ones(m_pos) ],\
                    [ -ones(m_neg) ],\
                    [ zeros(n) ],\
                    [ zeros(n) ],\
                    [ zeros(m_pos) ],\
                    [ zeros(m_neg) ]])
            P = poly(A_ub, b_ub)
            sol = QP(Q, c, P).solve()
            self.a = sol[0:n, 0]
            self.b = sol[n, 0]
Ejemplo n.º 9
0
 def test():
     P1 = poly.rand(2,4,0,1).affine_transform(eye(2),2*ones(2,1))
     P2 = poly.rand(2,4,0,1).affine_transform(eye(2),-2*ones(2,1))
     problem = interset(P1,P2,2)
     problem.solve()
     print(problem)