Esempio n. 1
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
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
0
class linear_fractional(object):
    """
        min    J = (a.T*x-b)/(c.T*x-d)
        x ∈ R^n
                s.t.  x ∈ POLYHEDRON
    """
    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
    def __repr__(self):
        s = '\n'+'_'*80 + '\n'+self.__class__.__name__+'\n' + self.__doc__ 
        if self.sol is not None:
            s += '\n x\t= ' + str(self.sol)
        return s
    def solve(self):
        x = self.QP.solve()
        print('*'*80)
        print(x)
        print('*'*80)
        if x is not None:
            if x[-1,0] != 0:
                self.sol = x[0:self.n,0]/x[-1,0]
            else:
                self.sol = None
        else:
            self.sol = None
        return self.sol
    @staticmethod
    def test():
        n = 2
        a = randn(n)
        b = randn(1)
        c = randn(n)
        d = randn(1)
        P = poly.rand(n,1,1)
        problem = linear_fractional(a,b,c,d,P)
        problem.solve()
        print(problem)
Esempio n. 5
0
 def soft_dual(self, x, y, λ=0.1):
     assert λ >= 0, "λ >= 0"
     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)     ]\
             ])
     b_ub =  np.bmat([\
             [   zeros(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
Esempio n. 6
0
'''
Created on Dec 8, 2015

@author: sandur2
'''
from constraint import Constraint
from QP import QP

c1 = Constraint(4, [1.,0.,0.,0.], [3.])
c2 = Constraint(4, [-1.,0.,0.,0.], [0.])
c3 = Constraint(4, [0.,1.,0.,0.], [3.])
c4 = Constraint(4, [0.,-1.,0.,0.], [0.])
c5 = Constraint(4, [0.,0.,1.,0.], [7.])
c6 = Constraint(4, [0.,0.,-1.,0.], [-4.])
c7 = Constraint(4, [0.,0.,0.,1.], [7.])
c8 = Constraint(4, [0.,0.,0.,-1.], [-4.])

qp = QP(4)
qp.add_constraint(c1)
qp.add_constraint(c2)
qp.add_constraint(c3)
qp.add_constraint(c4)
qp.add_constraint(c5)
qp.add_constraint(c6)
qp.add_constraint(c7)
qp.add_constraint(c8)
sol = qp.solve()
print(sol['x'])
Esempio n. 7
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]
Esempio n. 8
0
 def get_feasible(self):
     Q = eye(self.n)
     c = None
     return QP(Q, c, self).solve()