Example #1
0
    def _cal_prob_topic(self, dict_network, list_dict_subnetwork):
        prob_master = np.array([row[1] for row in
                                sorted(zip(dict_network["vertex"], dict_network["pagerank"]),
                                       key=lambda x: x[0])])

        for i, dict_subnetwork in enumerate(list_dict_subnetwork):
            if i == 0:
                prob_sub = np.array([row[1] for row in
                                     sorted(zip(dict_subnetwork["vertex"], dict_subnetwork["pagerank"]),
                                            key=lambda x: x[0])])
            else:
                list_tmp = np.array([row[1] for row in
                                     sorted(zip(dict_subnetwork["vertex"], dict_subnetwork["pagerank"]),
                                            key=lambda x: x[0])])
                prob_sub = np.vstack((prob_sub, list_tmp))

        H = 2 * prob_sub.dot(prob_sub.T)
        f = -2 * prob_master.dot(prob_sub.T)
        Aeq = np.ones(len(list_dict_subnetwork))
        beq = 1
        lb = np.zeros(len(list_dict_subnetwork))

        p = QP(H, f, Aeq=Aeq, beq=beq, lb=lb)
        r = p.solve("cvxopt_qp", iprint=-1)
        k_opt = r.xf
        return k_opt
Example #2
0
def PolytopProjection(data, T=1.0, isProduct=False, solver=None):
    if solver is None:
        solver = 'cvxopt_qp'

    #data = float128(data)
    if isProduct:
        H = data
        n = data.shape[0]
        m = len(T)
    else:
        H = dot(data, data.T)
        n, m = data.shape
    #print H.shape
    #print 'PolytopProjection: n=%d, m=%d, H.shape[0]= %d, H.shape[1]= %d ' %(n, m, H.shape[0], H.shape[1])
    #T = abs(dot(H, ones(n)))
    f = -asfarray(T) * ones(n)
    p = QP(H, f, lb=zeros(n), iprint=-1, maxIter=150)

    xtol = 1e-6
    if max(T) < 1e5 * xtol: xtol = max(T) / 1e5
    r = p._solve(solver, ftol=1e-16, xtol=xtol, maxIter=10000)
    sol = r.xf

    if isProduct:
        return r.xf
    else:
        s = dot(data.T, r.xf)
        return s.flatten(), r.xf
def PolytopProjection(data, T = 1.0, isProduct = False, solver = None):
    if solver is None: 
        solver = 'cvxopt_qp'
        
    #data = float128(data)
    if isProduct:
        H = data
        n = data.shape[0]
        m = len(T)
    else:
        H = dot(data, data.T)
        n, m = data.shape
    #print H.shape
    #print 'PolytopProjection: n=%d, m=%d, H.shape[0]= %d, H.shape[1]= %d ' %(n, m, H.shape[0], H.shape[1])
    #T = abs(dot(H, ones(n)))
    f = -asfarray(T) *ones(n)
    p = QP(H, f, lb = zeros(n), iprint = -1, maxIter = 150)

    xtol = 1e-6
    if max(T) < 1e5*xtol: xtol = max(T)/1e5
    r = p._solve(solver, ftol = 1e-16, xtol = xtol, maxIter = 10000)
    sol = r.xf

    if isProduct:
        return r.xf
    else:
        s = dot(data.T, r.xf)
        return s.flatten(), r.xf        
    def _train(self, train_data, param):
        """
        training procedure using training examples and labels
        
        @param train_data: Data relevant to SVM training
        @type train_data: dict<str, list<instances> >
        @param param: Parameters for the training procedure
        @type param: ParameterSvm
        """

        # fix parameters
        M = len(train_data)

        predictors = {}

        # extract training data
        for (task_id, instance_set) in train_data.items():

            print "train task id:", task_id
            assert (instance_set[0].dataset.organism == task_id)

            examples = [inst.example for inst in instance_set]
            labels = [inst.label for inst in instance_set]

            # shogun data
            feat = StringCharFeatures(DNA)
            feat.set_string_features(examples)
            lab = Labels(numpy.double(labels))

            # create kernel
            k = WeightedDegreeStringKernel(feat, feat, param.wdk_degree, 0)

            y = numpy.array(labels)

            km = k.get_kernel_matrix()
            km = numpy.transpose(y.flatten() * (km * y.flatten()).transpose())

            N = len(labels)
            f = -numpy.ones(N)

            # set up QP
            p = QP(km,
                   f,
                   Aeq=y,
                   beq=0,
                   lb=numpy.zeros(N),
                   ub=param.cost * numpy.ones(N))

            # run solver
            r = p.solve('cvxopt_qp', iprint=0)

            alphas = r.xf
            objective = r.ff

            print "objective:", objective

            predictors[task_id] = (alphas, param.wdk_degree, examples, labels)

        return predictors
Example #5
0
def pointProjection(x, lb, ub, A, b, Aeq, beq):
    from openopt import QP
    # projection of x to set of linear constraints
    n = x.size
    # TODO: INVOLVE SPARSE CVXOPT MATRICES
    p = QP(H=eye(n), f=-x, A=A, b=b, Aeq=Aeq, beq=beq, lb=lb, ub=ub)
    #r = p.solve('cvxopt_qp', iprint = -1)
    r = p.solve('nlp:scipy_slsqp', contol=1e-8, iprint=-1)
    return r.xf
Example #6
0
def pointProjection(x,  lb, ub, A, b, Aeq, beq):
    from openopt import QP
    # projection of x to set of linear constraints
    n = x.size
    # TODO: INVOLVE SPARSE CVXOPT MATRICES
    p = QP(H = eye(n), f = -x, A = A, b=b, Aeq=Aeq, beq=beq, lb=lb, ub=ub)
    #r = p.solve('cvxopt_qp', iprint = -1)
    r = p.solve('nlp:scipy_slsqp', contol = 1e-8, iprint = -1)
    return r.xf
Example #7
0
def svdd(X, K, C):
    n = K.shape[0]  # num. of samples

    # for QP sover
    H = 2 * K  # n x n
    f = -np.diag(K)  # n x 1
    l = np.size(X[0, :])
    Aeq = np.ones(n)  # p x n
    beq = 1.0  # p x 1

    lb = np.zeros(n)
    ub = np.zeros(n) + C

    p = QP(H, f, Aeq=Aeq, beq=beq, lb=lb, ub=ub)
    r = p.solve('nlp:ralg', iprint=-2)  #, r = p.solve('nlp:algencan')
    f_opt, x_opt = r.ff, r.xf
    alpha = np.array(x_opt).reshape(-1)

    epsilon = C * 1e-4
    svi = np.arange(l)[alpha > epsilon]

    nsv = len(svi)

    sv = X[:, svi]
    alpha_sv = alpha[svi].reshape(-1, 1)

    #pylab.scatter(sv[0, :], sv[1, :], s = 50, c = 'b')
    #pylab.scatter(X[0, :], X[1, :], c = 'r')
    #pylab.show()

    #print 'Support Vectors : %d (%3.1f%%)' % (nsv, 100.0*float(nsv)/float(l))

    svii = np.arange(l)[(alpha > epsilon) & (alpha < (C - epsilon))]

    l_svii = len(svii)

    #print '# of points in boundary', l_svii

    if l_svii > 0:
        Kt = K[:, svi]

        Ksv = Kt[svi, :]
        Kbound = Kt[svii, :]
        Kbound_diag = np.diag(K)[svii]

        b = np.dot(np.dot(alpha_sv.T, Ksv), alpha_sv).reshape(-1)
        R2_all = Kbound_diag.reshape(-1) - 2 * np.dot(alpha_sv.T,
                                                      Kbound).reshape(-1) + b
        R2 = R2_all.mean()

        bias = R2 - b
    else:
        print 'No support vectors on the decision boundary'

    svdd_model = {'alpha': alpha_sv, 'sv': sv, 'R2': R2, 'bias': bias}
    return svdd_model
Example #8
0
 def test_cplex(self):
     p = QP(diag([1, 2, 3]),
            [15, 8, 80],
            A = matrix('1 2 3; 8 15 80'),
            b = [150, 800],
            Aeq = [0, 1, -1],
            beq = 25.5,
            ub = [15,inf,inf])
     r = p._solve('cplex', iprint = 0)
     f_opt, x_opt = r.ff, r.xf
     np.testing.assert_almost_equal(f_opt,  -1190.35)
     np.testing.assert_almost_equal(x_opt, [-15. ,  -2.3, -27.8 ])
Example #9
0
File: nw.py Project: xiaoyugit/cnp
 def tilt(self):
     ######construct constraint weight matrix and optimize p############
     ndim=self.ndim
     wmatrix=self.ndim*self.m_fd()
     wmatrix_2=self.ndim*self.k()*self.ydat
     p0=ones(ndim)/ndim
     lb=zeros(ndim)
     ub=ones(ndim)
     Aeq=ones(ndim)
     beq=1. 
     #A=vstack((-wmatrix,-wmatrix_2,-wmatrix_2))
     #b=hstack((zeros(len(wmatrix)),ones(len(wmatrix_2)),zeros(len(wmatrix_2))))
     A=vstack((-wmatrix,-wmatrix_2))
     b=hstack((zeros(len(wmatrix)),ones(len(wmatrix_2))))
     p=QP(diag(ones(ndim)),2*p0*ones(ndim),lb=lb,ub=ub,Aeq=Aeq,beq=beq,A=A,b=b,name="find optimal p that satisfies m'>0 and m<1")
     r=p._solve('cvxopt_qp')
     solution=r.xf
     self.solution=solution
     self.ydat=ndim*solution*self.ydat
Example #10
0
    def solve(self, C, xt, lt, task_indicator, M):
        """
        solve dual using cvxopt
        """

        num_xt = len(xt)

        # set up quadratic term
        Q = np.zeros((num_xt, num_xt))

        # compute quadratic term
        for i in xrange(num_xt):
            for j in xrange(num_xt):

                s = task_indicator[i]
                t = task_indicator[j]
                
                Q[i,j] = M[s,t] * lt[i] * lt[j] * np.dot(xt[i], xt[j])

        # set up linear term
        p = -np.ones(num_xt)

        # if we would like to use bias
        #b = np.zeros((M,1))
        #label_matrix = numpy.zeros((M,N))

        # set up QP
        p = QP(Q, p, lb=np.zeros(num_xt), ub=C*np.ones(num_xt)) #Aeq=label_matrix, beq=b
        p.debug=1
        
        # run solver
        r = p.solve('cvxopt_qp', iprint = 0)

        # recover result
        self.alphas = r.xf
        self.dual_obj = self.obj = r.ff

        # compute W from alphas
        self.W = alphas_to_w(self.alphas, xt, lt, task_indicator, M)


        return True
    def test_cvxopt(self):
        Q = matrix([[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]])
        p = matrix([15.0, 8.0, 80.0])
        G = matrix([[1.0, 2.0, 3.0], [8.0, 15.0, 80.0], [0, 0, 0]])
        h = matrix([150.0, 800.0, 0], (3, 1))
        A = matrix([0.0, 1.0, -1.0], (1,3))
        b = matrix(25.5)

        sol = cvxopt.solvers.qp(Q, p, G, h, A, b)

        p = QP(diag([1, 2, 3]),
               [15, 8, 80],
               A = np.matrix("1 2 3; 8 15 80"),
               b = [150, 800],
               Aeq = [0, 1, -1],
               beq = 25.5)
        r = p._solve('cvxopt_qp', iprint = 0)
        f_opt, x_opt = r.ff, r.xf

        np.testing.assert_almost_equal(f_opt,  sol['primal objective'], decimal=5)
        np.testing.assert_almost_equal(x_opt, np.squeeze(sol['x']), decimal=5)
Example #12
0
    def minimize(self, **kwargs):
        """ solve the quadratic problem using OpenOpt

        Returns:
        obj_value, solution

        obj_value -- value of the objective function at the discovered solution
        solution  -- the solution flux vector (indexed like matrix columns)
        """
        qp = QP(self.obj.H, self.obj.f, A=self.Aineq, Aeq=self.Aeq,
                b=self.bineq, beq=self.beq, lb=self.lb, ub=self.ub, **kwargs)
        qp.debug=1
        r = qp.solve(self.solver)

        if r.istop <= 0 or r.ff != r.ff:  # check halting condition
            self.obj_value = 0
            self.solution = []
            self.istop = r.istop
        else:
            self.obj_value = r.ff
            self.solution = r.xf
            self.istop = r.istop

        return self.obj_value, self.solution
Example #13
0
a = oovar('a')
b = oovars(n, domain=int)('b')
c = oovar('c')
d = oovar('d', domain=bool)

B = 10 * sin(arange(n))  # arange(n) = [0, 1, 2, ..., n-1]

objective = (a - 1)**2 + 10 * sum(
    (b - B)**
    2) + 3 * (1 + 10 *
              (1 + 100 * c**2)) + 100 * (d - 5)**2 + (0.1 * a + 1) * (c + d)
constraints = [
    a > 0, a < 1.03, b > 0, b < 10, c > 0, c < 10.1, c**2 < 5, a + b < 7
]

startpoint = {a: 10, b: zeros(n), c: 10, d: 0}
p = QP(objective, startpoint, constraints=constraints)
solver = 'cplex'

r = p.solve(solver)
'''
istop: 1000 (Cplex status: "integer optimal, tolerance"; exit code: 102)
Solver:   Time Elapsed = 0.18 	CPU Time Elapsed = 0.15
objFunValue: 5025.3035 (feasible, MaxResidual = 0)
'''
print(a(r))  # 0.0
print(
    b(r)
)  # array([ 0.,  7.,  7.,  1.,  0.,  0.,  0.,  7.,  7.,  4.,  0.,  0.,  0., 4.,  7.])
print(r(c, d))  # [0.0, 1.0]
Example #14
0
"""
Example:
Concider the problem
0.5 * (x1^2 + 2x2^2 + 3x3^2) + 15x1 + 8x2 + 80x3 -> min        (1)
subjected to
x1 + 2x2 + 3x3 <= 150            (2)
8x1 +  15x2 +  80x3 <= 800    (3)
x2 - x3 = 25.5                           (4)
x1 <= 15                                  (5)
x1, x3 are integers
"""

from numpy import diag, matrix, inf
from openopt import QP
p = QP(diag([1, 2, 3]), [15, 8, 80],
       A=matrix('1 2 3; 8 15 80'),
       b=[150, 800],
       Aeq=[0, 1, -1],
       beq=25.5,
       ub=[15, inf, inf])
# or p = QP(H=diag([1,2,3]), f=[15,8,80], A= ...)
r = p.solve('cplex', intVars=[0, 2], iprint=0)
f_opt, x_opt = r.ff, r.xf
# x_opt = array([-15. ,  -2.5, -28. ])
# f_opt = -1190.25
Example #15
0
    def _train(self, train_data, param):
        """
        training procedure using training examples and labels
        
        @param train_data: Data relevant to SVM training
        @type train_data: dict<str, list<instances> >
        @param param: Parameters for the training procedure
        @type param: ParameterSvm
        """

        # fix dimensions
        M = len(train_data)

        N = 0
        for key in train_data.keys():
            N += len(train_data[key])

        # init containers
        examples = []
        labels = []

        # vector to indicate to which task each example belongs
        task_vector = []
        task_num = 0
        tmp_examples = 0

        label_matrix = numpy.zeros((M, N))

        # extract training data
        for (task_id, instance_set) in train_data.items():

            print "train task id:", task_id
            #assert(instance_set[0].dataset.organism==task_id)

            examples.extend([inst.example for inst in instance_set])

            tmp_labels = [inst.label for inst in instance_set]
            labels.extend(tmp_labels)

            begin_idx = tmp_examples
            end_idx = tmp_examples + len(tmp_labels)

            # fill matrix row
            label_matrix[task_num, begin_idx:end_idx] = tmp_labels

            task_vector.extend([task_num] * len(instance_set))

            task_num += 1
            tmp_examples += len(tmp_labels)

        # fetch gammas from parameter object
        # TODO: compute gammas outside of this
        gammas = numpy.ones((M, M)) + numpy.eye(M)
        #gammas = numpy.eye(M)

        # create kernel
        kernel = shogun_factory.create_kernel(examples, param)

        y = numpy.array(labels)

        print "computing kernel matrix"

        km = kernel.get_kernel_matrix()
        km = reweight_kernel_matrix(km, gammas, task_vector)

        # "add" labels to Q-matrix
        km = numpy.transpose(y.flatten() * (km * y.flatten()).transpose())

        print "done computing kernel matrix, calling solver"

        f = -numpy.ones(N)
        b = numpy.zeros((M, 1))

        # set up QP
        p = QP(km,
               f,
               Aeq=label_matrix,
               beq=b,
               lb=numpy.zeros(N),
               ub=param.cost * numpy.ones(N))
        p.debug = 1

        # run solver
        r = p.solve('cvxopt_qp', iprint=0)

        print "done with training"

        alphas = r.xf
        objective = r.ff

        print "alphas:", alphas

        predictors = {}

        for (k, task_id) in enumerate(train_data.keys()):
            # pack all relevant information in predictor
            predictors[task_id] = (alphas, param, task_vector, k, gammas,
                                   examples, labels)

        return predictors
    def _train(self, instance_sets, param):
    
   
        #fix parameters
        M = len(instance_sets)
        Cs = numpy.ones((M,M))    
   
        
        #init containers
        example_sets = []
        label_sets = []
        

        #perform data conversion -> strToVec
        for instance_set in instance_sets:
            
            example_set = helper.gen_features([inst.example for inst in instance_set])
            print "example set:", example_set.shape
            label_set = numpy.array([inst.label for inst in instance_set])
            print "label set:", label_set.shape
            print "num positive labels", sum(label_set)+len(label_set)
            
            example_sets.append(example_set)
            label_sets.append(label_set)


        #determine starting point
        M = len(example_sets)
        d = len(example_sets[0][0])
        
        print "M:", M, "d:", d
        
        dim_x0 = d*M
        numpy.random.seed(123967)
        x0 = numpy.random.randn(dim_x0)
        

        """
        Example:
        Consider the problem
        0.5 * (x1^2 + 2x2^2 + 3x3^2) + 15x1 + 8x2 + 80x3 -> min        (1)
        subjected to
        x1 + 2x2 + 3x3 <= 150            (2)
        8x1 +  15x2 +  80x3 <= 800    (3)
        x2 - x3 = 25                              (4)
        x1 <= 15                                  (5)
        """

        #TODO map variables
        
        from numpy import diag, matrix, inf
        from openopt import QP
        p = QP(diag([1,2,3]), [15,8,80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25, ub = [15,inf,inf])
        # or p = QP(H=diag([1,2,3]), f=[15,8,80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25, ub = [15,inf,inf])
        r = p.solve('cvxopt_qp', iprint = 0)
        #r = p.solve('nlp:ralg', xtol=1e-7, alp=3.9, plot=1)#, r = p.solve('nlp:algencan')
        #f_opt, x_opt = r.ff, r.xf
        f_opt = r.xf
        # x_opt = array([-14.99999995,  -2.59999996, -27.59999991])
        # f_opt = -1191.90000013



        return numpy.reshape(xopt, (M,d))
Example #17
0
    def __solver__(self, p):
        n = p.n
        x0 = copy(p.x0)
        xPrev = x0.copy()
        xf = x0.copy()
        xk = x0.copy()
        p.xk = x0.copy()

        f0 = p.f(x0)

        fk = f0
        ff = f0
        p.fk = fk

        df0 = p.df(x0)

        #####################################################################

        ##        #handling box-bounded problems
        ##        if p.__isNoMoreThanBoxBounded__():
        ##            for k in range(int(p.maxIter)):
        ##
        ##        #end of handling box-bounded problems
        isBB = p.__isNoMoreThanBoxBounded__()
        ##        isBB = 0
        H = diag(ones(p.n))
        if not p.userProvided.c:
            p.c = lambda x: array([])
            p.dc = lambda x: array([]).reshape(0, p.n)
        if not p.userProvided.h:
            p.h = lambda x: array([])
            p.dh = lambda x: array([]).reshape(0, p.n)

        p.use_subproblem = 'QP'

        #p.use_subproblem = 'LLSP'

        for k in range(p.maxIter + 4):
            if isBB:
                f0 = p.f(xk)
                df = p.df(xk)
                direction = -df
                f1 = p.f(xk + direction)
                ind_l = direction <= p.lb - xk
                direction[ind_l] = (p.lb - xk)[ind_l]
                ind_u = direction >= p.ub - xk
                direction[ind_u] = (p.ub - xk)[ind_u]
                ff = p.f(xk + direction)
##                print 'f0', f0, 'f1', f1, 'ff', ff
            else:

                mr = p.getMaxResidual(xk)
                if mr > p.contol: mr_grad = p.getMaxConstrGradient(xk)
                lb = p.lb - xk  #- p.contol/2
                ub = p.ub - xk  #+ p.contol/2
                c, dc, h, dh, df = p.c(xk), p.dc(xk), p.h(xk), p.dh(xk), p.df(
                    xk)
                A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq))
                b = concatenate((-c, p.b - p.matmult(p.A, xk)))  #+ p.contol/2
                beq = concatenate((-h, p.beq - p.matmult(p.Aeq, xk)))

                if b.size != 0:
                    isFinite = isfinite(b)
                    ind = where(isFinite)[0]
                    A, b = A[ind], b[ind]
                if beq.size != 0:
                    isFinite = isfinite(beq)
                    ind = where(isFinite)[0]
                    Aeq, beq = Aeq[ind], beq[ind]

                if p.use_subproblem == 'LP':  #linear
                    linprob = LP(df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub)
                    linprob.iprint = -1
                    r2 = linprob.solve(
                        'cvxopt_glpk')  # TODO: replace lpSolve by autoselect
                    if r2.istop <= 0:
                        p.istop = -12
                        p.msg = "failed to solve LP subproblem"
                        return
                elif p.use_subproblem == 'QP':  #quadratic
                    qp = QP(H=H,
                            f=df,
                            A=A,
                            Aeq=Aeq,
                            b=b,
                            beq=beq,
                            lb=lb,
                            ub=ub)
                    qp.iprint = -1
                    r2 = qp.solve(
                        'cvxopt_qp')  # TODO: replace solver by autoselect
                    #r2 = qp.solve('qld') # TODO: replace solver by autoselect
                    if r2.istop <= 0:
                        for i in range(4):
                            if p.debug:
                                p.warn("iter " + str(k) + ": attempt Num " +
                                       str(i) +
                                       " to solve QP subproblem has failed")
                            #qp.f += 2*N*sum(qp.A,0)
                            A2 = vstack((A, Aeq, -Aeq))
                            b2 = concatenate(
                                (b, beq, -beq)) + pow(10, i) * p.contol
                            qp = QP(H=H, f=df, A=A2, b=b2, iprint=-5)
                            qp.lb = lb - pow(10, i) * p.contol
                            qp.ub = ub + pow(10, i) * p.contol
                            # I guess lb and ub don't matter here
                            try:
                                r2 = qp.solve(
                                    'cvxopt_qp'
                                )  # TODO: replace solver by autoselect
                            except:
                                r2.istop = -11
                            if r2.istop > 0: break
                        if r2.istop <= 0:
                            p.istop = -11
                            p.msg = "failed to solve QP subproblem"
                            return
                elif p.use_subproblem == 'LLSP':
                    direction_c = getConstrDirection(p,
                                                     xk,
                                                     regularization=1e-7)
                else:
                    p.err('incorrect or unknown subproblem')

            if isBB:
                X0 = xk.copy()
                N = 0
                result, newX = chLineSearch(p, X0, direction, N, isBB)
            elif p.use_subproblem != 'LLSP':
                duals = r2.duals
                N = 1.05 * abs(duals).sum()
                direction = r2.xf
                X0 = xk.copy()
                result, newX = chLineSearch(p, X0, direction, N, isBB)
            else:  # case LLSP
                direction_f = -df
                p2 = NSP(LLSsubprobF, [0.8, 0.8],
                         ftol=0,
                         gtol=0,
                         xtol=1e-5,
                         iprint=-1)
                p2.args.f = (xk, direction_f, direction_c, p, 1e20)
                r_subprob = p2.solve('ralg')
                alpha = r_subprob.xf
                newX = xk + alpha[0] * direction_f + alpha[1] * direction_c

                #                dw = (direction_f * direction_c).sum()
                #                cos_phi = dw/p.norm(direction_f)/p.norm(direction_c)
                #                res_0, res_1 = p.getMaxResidual(xk), p.getMaxResidual(xk+1e-1*direction_c)
                #                print cos_phi, res_0-res_1

                #                res_0 = p.getMaxResidual(xk)
                #                optimConstrPoint = getDirectionOptimPoint(p, p.getMaxResidual, xk, direction_c)
                #                res_1 = p.getMaxResidual(optimConstrPoint)
                #
                #                maxConstrLimit = p.contol

                #xk = getDirectionOptimPoint(p, p.f, optimConstrPoint, -optimConstrPoint+xk+direction_f, maxConstrLimit = maxConstrLimit)
                #print 'res_0', res_0, 'res_1', res_1, 'res_2', p.getMaxResidual(xk)
                #xk = getDirectionOptimPoint(p, p.f, xk, direction_f, maxConstrLimit)
                #newX = xk.copy()
                result = 0


#                x_0 = X0.copy()
#                N = j = 0
#                while p.getMaxResidual(x_0) > Residual0 + 0.1*p.contol:
#                    j += 1
#                    x_0 = xk + 0.75**j * (X0-xk)
#                X0 = x_0
#                result, newX = 0, X0
#                print 'newIterResidual = ', p.getMaxResidual(x_0)

            if result != 0:
                p.istop = result
                p.xf = newX
                return

            xk = newX.copy()
            fk = p.f(xk)

            p.xk, p.fk = copy(xk), copy(fk)
            #p._df = p.df(xk)
            ####################
            p.iterfcn()

            if p.istop:
                p.xf = xk
                p.ff = fk
                #p._df = g FIXME: implement me
                return
Example #18
0
def train_dh(bags, basis, bdim, theta, r, args):
    if _SOLVER == 'cvxopt':
        import cvxopt
        from cvxopt import matrix
        from cvxopt.solvers import qp
        cvxopt.solvers.options['show_progress'] = False

    elif _SOLVER == 'openopt':
        from openopt import QP
        import warnings
        warnings.simplefilter(action="ignore", category=FutureWarning)

    elif _SOLVER == 'gurobi':
        import sys
        sys.path.append(
            "/home/local/bin/gurobi650/linux64/lib/python3.4_utf32/gurobipy")
        import gurobipy
        from MI.gurobi_helper.helper import quadform, dot, mvmul

    p_bags = MI.extract_bags(bags, 1)
    u_bags = MI.extract_bags(bags, 0)
    N1 = len(p_bags)
    N0 = len(u_bags)
    N = N1 + N0
    d = bdim
    P1 = np.array([basis(B).T for B in p_bags])
    P0 = np.array([basis(B).T for B in u_bags])
    H = np.r_[np.c_[r * np.eye(d),
                    np.zeros((d, 1)),
                    np.zeros((d, N0))], np.c_[np.zeros((1, d)), 0,
                                              np.zeros((1, N0))],
              np.c_[np.zeros((N0, d)),
                    np.zeros((N0, 1)),
                    np.zeros((N0, N0))]]
    f = np.r_[-theta / N1 * P1.T.sum(axis=1).reshape((-1, 1)), [[-theta]],
              1. / N0 * np.ones((N0, 1))]
    L = np.r_[np.c_[0.5 * P0, 0.5 * np.ones((N0, 1)), -np.eye(N0)],
              np.c_[P0, np.ones((N0, 1)), -np.eye(N0)], np.c_[np.zeros(
                  (N0, d)), np.zeros((N0, 1)), -np.eye(N0)]]
    k = np.r_[-0.5 * np.ones((N0, 1)), np.zeros((N0, 1)), -np.zeros((N0, 1))]

    if _SOLVER == 'cvxopt':
        result = qp(matrix(H), matrix(f), matrix(L), matrix(k))
        gamma = np.array(result['x'])

    elif _SOLVER == 'openopt':
        problem = QP(H + 1e-3 * np.eye(H.shape[0]), f, A=L, b=k)
        result = problem.solve('qlcp')
        gamma = result.xf

    elif _SOLVER == 'gurobi':
        # model and target variables
        m = gurobipy.Model('qp')
        m.setParam('OutputFlag', False)
        opt_dim = H.shape[0]
        x = [
            m.addVar(lb=-gurobipy.GRB.INFINITY, name='x{}'.format(i))
            for i in range(opt_dim)
        ]
        m.update()

        # objective function and constraints
        obj = 0.5 * quadform(H.tolist(), x) + dot(f.reshape(-1).tolist(), x)
        constrs = [lhs <= rhs for lhs, rhs in zip(mvmul(L.tolist(), x), k)]

        # solve
        m.setObjective(obj)
        for i, constr in enumerate(constrs):
            m.addConstr(constr, 'c{}'.format(i))

        try:
            m.optimize()
            gamma = np.array([v.x for v in m.getVars()])

        except gurobipy.GurobiError:
            raise ValueError()

    alpha = gamma[:d]
    beta = gamma[d]
    clf = lambda X: alpha.T.dot(basis(X)) + beta

    return clf
Example #19
0
Concider the MIQCQP problem
0.5 * (x1^2 + 2x2^2 + 3x3^2) + 15x1 + 8x2 + 80x3 -> min        (1)
subjected to
x1 + 2x2 + 3x3 <= 150            (2)
8x1 +  15x2 +  80x3 <= 800    (3)
x2 - x3 = 25.5                           (4)
x1 <= 15                                  (5)
x1^2 + 2.5 x2^2 + 3 x3^2 + 0.1 x1 + 0.2 x2 + 0.3 x3 - 1000 <= 0 (6)
2 x1^2 + x2^2 + 3 x3^2 + 0.1 x1 + 0.5 x2 + 0.3 x3  <= 1000 (7)
x1, x3 are integers
"""

from numpy import diag, matrix, inf
from openopt import QP

H = diag([1.0, 2.0,3.0])
f = [15,8,80]
A = matrix('1 2 3; 8 15 80')
b = [150, 800]

# Qc should be list or tuple of triples (P, q, s): 0.5 x^T P x + q x + s <= 0
QC = ((diag([1.0, 2.5, 3.0]), [0.1, 0.2, 0.3], -1000), (diag([2.0, 1.0, 3.0]), [0.1, 0.5, 0.3], -1000))

p = QP(H, f, A = A, b = b, Aeq = [0, 1, -1], beq = 25.5, ub = [15,inf,inf], QC = QC, intVars = [0, 2])
# or p = QP(H=diag([1,2,3]), f=[15,8,80], ...)

r = p.solve('cplex', iprint = 0, plot=1)
f_opt, x_opt = r.ff, r.xf
# x_opt = array([ -2.99999999,   9.5       , -16.        ])
# f_opt = -770.24999989134858
Example #20
0
Concider the MIQCQP problem
0.5 * (x1^2 + 2x2^2 + 3x3^2) + 15x1 + 8x2 + 80x3 -> min        (1)
subjected to
x1 + 2x2 + 3x3 <= 150            (2)
8x1 +  15x2 +  80x3 <= 800    (3)
x2 - x3 = 25.5                           (4)
x1 <= 15                                  (5)
x1^2 + 2.5 x2^2 + 3 x3^2 + 0.1 x1 + 0.2 x2 + 0.3 x3 - 1000 <= 0 (6)
2 x1^2 + x2^2 + 3 x3^2 + 0.1 x1 + 0.5 x2 + 0.3 x3  <= 1000 (7)
x1, x3 are integers
"""

from numpy import diag, matrix, inf
from openopt import QP

H = diag([1.0, 2.0,3.0])
f = [15,8,80]
A = matrix('1 2 3; 8 15 80')
b = [150, 800]

# QC should be list or tuple of triples (P, q, s): 0.5 x^T P x + q x + s <= 0
QC = ((diag([1.0, 2.5, 3.0]), [0.1, 0.2, 0.3], -1000), (diag([2.0, 1.0, 3.0]), [0.1, 0.5, 0.3], -1000))

p = QP(H, f, A = A, b = b, Aeq = [0, 1, -1], beq = 25.5, ub = [15,inf,inf], QC = QC, name='OpenOpt QCQP example 1')
# or p = QP(H=diag([1,2,3]), f=[15,8,80], ...)

r = p.solve('cplex', iprint = 0, plot=1)
f_opt, x_opt = r.ff, r.xf
# x_opt = array([ -2.99999999,   9.5       , -16.        ])
# f_opt = -770.24999989134858
Example #21
0
from numpy import diag, matrix, inf
from openopt import QP

H = diag([1.0, 2.0, 3.0])
f = [15, 8, 80]
A = matrix('1 2 3; 8 15 80')
b = [150, 800]

# QC should be list or tuple of triples (P, q, s): 0.5 x^T P x + q x + s <= 0
QC = ((diag([1.0, 2.5, 3.0]), [0.1, 0.2,
                               0.3], -1000), (diag([2.0, 1.0,
                                                    3.0]), [0.1, 0.5,
                                                            0.3], -1000))

p = QP(H,
       f,
       A=A,
       b=b,
       Aeq=[0, 1, -1],
       beq=25.5,
       ub=[15, inf, inf],
       QC=QC,
       name='OpenOpt QCQP example 1')
# or p = QP(H=diag([1,2,3]), f=[15,8,80], ...)

r = p.solve('cplex', iprint=0, plot=1)
f_opt, x_opt = r.ff, r.xf
# x_opt = array([ -2.99999999,   9.5       , -16.        ])
# f_opt = -770.24999989134858
Example #22
0
	def fit(self, X, Y):
		
		xdim = X.shape[0]
		ydim = Y.shape[0]
		
		if xdim != ydim:
			raise Exception('Input dimension does not match!')
		
		kernel=[[0.0 for i in range(2*xdim)] for j in range(2*xdim)]
		
		for i in range(xdim):
			for j in range(xdim):
				kernel[i][j] = self._kernel(X[i], X[j])
				kernel[i + xdim][j + xdim] = self._kernel(X[i], X[j])
		
		#----------------------------------------------------------------------------------------------------------------
		#negating the values for a_n'
		for i in range(xdim):
			for j in range(xdim):
				kernel[i + xdim][j] = (-1.0) * self._kernel(X[i], X[j])
				kernel[i][j + xdim] = (-1.0) * self._kernel(X[i], X[j])
		
		#--------------------------------------------------------------------------------------------------------------
		#coeff of 2nd term to minimize
		f=[0.0 for i in range(2 * xdim)]
		for i in range(xdim):
			f[i] = -float(Y[i]) + self.eps
		for i in range(xdim, 2 * xdim):
			f[i] = float(Y[i - xdim])+ self.eps
		
		#-----------------------------------------------------------------------------------------------------
		#constraints
		lower_limit = [0.0 for i in range(2 * xdim)]
		upper_limit = [float(self.C) for i in range(2 * xdim)]
		Aeq = [1.0 for i in range(2 * xdim)]
		for i in range(xdim, 2 * xdim):
			Aeq[i]=-1.0
		beq=0.0
			
		#----------------------------------------------------------------------------------------------------
		
		#coeff for 3rd constraint
		#kernel=H
		eq = QP(np.asmatrix(kernel),np.asmatrix(f),
		  lb=np.asmatrix(lower_limit),
		  ub=np.asmatrix(upper_limit),
		  Aeq=Aeq,beq=beq)
		p = eq._solve('cvxopt_qp', iprint = 0)
		f_opt, x = p.ff, p.xf
		
		#---------------------------------------------------------------------------------------
		self.support_vectors=[]
		self.support_vectors_Y=[]

		self.coeff=[]
		#support vectors: points such that an-an' ! = 0
		for i in range(xdim):
			if not((x[i]-x[xdim+i])==0):
				self.support_vectors.append( X[i] )
				self.support_vectors_Y.append(Y[i])
				self.coeff.append( x[i]-x[xdim+i] )
		
		
		low=min(abs(x))
		for i in range(xdim):
			if not(abs(x[i]-x[xdim+i]) < low + 0.005):
				self.support_vector.append( X[i] )
				self.support_vector_Y.append(Y[i])
				
		
		bias=0.0
		for i in range(len(X)):
			bias=bias+float(Y[i] - self.eps - self._product(coeff, self.support_vectors, X[i]))
		#generally bias is average as written in the book
		self.bias=bias/len(X)
    def _train(self, train_data, param):
        """
        training procedure using training examples and labels
        
        @param train_data: Data relevant to SVM training
        @type train_data: dict<str, list<instances> >
        @param param: Parameters for the training procedure
        @type param: ParameterSvm
        """
 
        
                
        # fix parameters
        M = len(train_data)

        
        # init containers
        examples = []
        labels = []


        # vector to indicate to which task each example belongs
        task_vector = []
        task_num = 0

        # extract training data
        for (task_id, instance_set) in train_data.items():
  
            print "train task id:", task_id
            assert(instance_set[0].dataset.organism==task_id)
            
            examples.extend([inst.example for inst in instance_set])
            labels.extend([inst.label for inst in instance_set])

            task_vector.extend([task_num]*len(instance_set))

            task_num += 1


        # shogun data
        feat = StringCharFeatures(DNA)
        feat.set_string_features(examples)
        lab = Labels(numpy.double(labels))


        # fetch gammas from parameter object
        gammas = param.taxonomy.data
        from expenv_runner import TaskMap
        tm = TaskMap(param.taxonomy.data)
        # fetch gammas from parameter object
        gammas = tm.taskmap2matrix(train_data.keys())
        print gammas

        assert(gammas.shape[0] == len(train_data))

        # create kernel
        k = WeightedDegreeStringKernel(feat, feat, param.wdk_degree, 0)


        y = numpy.array(labels)

        km = k.get_kernel_matrix()
        km = reweight_kernel_matrix(km, gammas, task_vector)

        km = numpy.transpose(y.flatten() * (km*y.flatten()).transpose())

        N = len(labels)
        f = -numpy.ones(N)

        # set up QP
        p = QP(km, f, Aeq=y, beq=0, lb=numpy.zeros(N), ub=param.cost*numpy.ones(N))

        # run solver
        r = p.solve('cvxopt_qp', iprint = 0)


        alphas = r.xf
        objective = r.ff

        predictors = {}


        for (k, task_id) in enumerate(train_data.keys()):
            # pack all relevant information in predictor
            predictors[task_id] = (alphas, param.wdk_degree, task_vector, k, gammas, examples, labels)

        return predictors
Example #24
0
    def __solver__(self, p):
        n = p.n
        x0 = copy(p.x0)
        xPrev = x0.copy()
        xf = x0.copy()
        xk = x0.copy()
        p.xk = x0.copy()

        f0 = p.f(x0)

        fk = f0
        ff = f0
        p.fk = fk

        df0 = p.df(x0)

        #####################################################################


##        #handling box-bounded problems
##        if p.__isNoMoreThanBoxBounded__():
##            for k in range(int(p.maxIter)):
##
##        #end of handling box-bounded problems
        isBB = p.__isNoMoreThanBoxBounded__()
##        isBB = 0
        H = diag(ones(p.n))
        if not p.userProvided.c:
            p.c = lambda x : array([])
            p.dc = lambda x : array([]).reshape(0, p.n)
        if not p.userProvided.h:
            p.h = lambda x : array([])
            p.dh = lambda x : array([]).reshape(0, p.n)

        p.use_subproblem = 'QP'

        #p.use_subproblem = 'LLSP'

        for k in range(p.maxIter+4):
            if isBB:
                f0 = p.f(xk)
                df = p.df(xk)
                direction = -df
                f1 = p.f(xk+direction)
                ind_l = direction<=p.lb-xk
                direction[ind_l] = (p.lb-xk)[ind_l]
                ind_u = direction>=p.ub-xk
                direction[ind_u] = (p.ub-xk)[ind_u]
                ff = p.f(xk + direction)
##                print 'f0', f0, 'f1', f1, 'ff', ff
            else:

                mr = p.getMaxResidual(xk)
                if mr > p.contol: mr_grad = p.getMaxConstrGradient(xk)
                lb = p.lb - xk #- p.contol/2
                ub = p.ub - xk #+ p.contol/2
                c, dc, h, dh, df = p.c(xk), p.dc(xk), p.h(xk), p.dh(xk), p.df(xk)
                A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq))
                b = concatenate((-c, p.b-p.matmult(p.A,xk))) #+ p.contol/2
                beq = concatenate((-h, p.beq-p.matmult(p.Aeq,xk)))

                if b.size != 0:
                    isFinite = isfinite(b)
                    ind = where(isFinite)[0]
                    A, b = A[ind], b[ind]
                if beq.size != 0:
                    isFinite = isfinite(beq)
                    ind = where(isFinite)[0]
                    Aeq, beq = Aeq[ind], beq[ind]


                if p.use_subproblem == 'LP': #linear
                    linprob = LP(df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub)
                    linprob.iprint = -1
                    r2 = linprob.solve('cvxopt_glpk') # TODO: replace lpSolve by autoselect
                    if r2.istop <= 0:
                        p.istop = -12
                        p.msg = "failed to solve LP subproblem"
                        return
                elif p.use_subproblem == 'QP': #quadratic
                    qp = QP(H=H,f=df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub = ub)
                    qp.iprint = -1
                    r2 = qp.solve('cvxopt_qp') # TODO: replace solver by autoselect
                    #r2 = qp.solve('qld') # TODO: replace solver by autoselect
                    if r2.istop <= 0:
                        for i in range(4):
                            if p.debug: p.warn("iter " + str(k) + ": attempt Num " + str(i) + " to solve QP subproblem has failed")
                            #qp.f += 2*N*sum(qp.A,0)
                            A2 = vstack((A, Aeq, -Aeq))
                            b2 = concatenate((b, beq, -beq)) + pow(10,i)*p.contol
                            qp = QP(H=H,f=df, A=A2, b=b2, iprint = -5)
                            qp.lb = lb - pow(10,i)*p.contol
                            qp.ub = ub + pow(10,i)*p.contol
                            # I guess lb and ub don't matter here
                            try:
                                r2 = qp.solve('cvxopt_qp') # TODO: replace solver by autoselect
                            except:
                                r2.istop = -11
                            if r2.istop > 0: break
                        if r2.istop <= 0:
                            p.istop = -11
                            p.msg = "failed to solve QP subproblem"
                            return
                elif p.use_subproblem == 'LLSP':
                    direction_c = getConstrDirection(p,  xk, regularization = 1e-7)
                else: p.err('incorrect or unknown subproblem')


            if isBB:
                X0 = xk.copy()
                N = 0
                result, newX = chLineSearch(p, X0, direction, N, isBB)
            elif p.use_subproblem != 'LLSP':
                duals = r2.duals
                N = 1.05*abs(duals).sum()
                direction = r2.xf
                X0 = xk.copy()
                result, newX = chLineSearch(p, X0, direction, N, isBB)
            else: # case LLSP
                direction_f = -df
                p2 = NSP(LLSsubprobF, [0.8, 0.8], ftol=0, gtol=0, xtol = 1e-5, iprint = -1)
                p2.args.f =  (xk, direction_f, direction_c, p, 1e20)
                r_subprob = p2.solve('ralg')
                alpha = r_subprob.xf
                newX = xk + alpha[0]*direction_f + alpha[1]*direction_c

#                dw = (direction_f * direction_c).sum()
#                cos_phi = dw/p.norm(direction_f)/p.norm(direction_c)
#                res_0, res_1 = p.getMaxResidual(xk), p.getMaxResidual(xk+1e-1*direction_c)
#                print cos_phi, res_0-res_1

#                res_0 = p.getMaxResidual(xk)
#                optimConstrPoint = getDirectionOptimPoint(p, p.getMaxResidual, xk, direction_c)
#                res_1 = p.getMaxResidual(optimConstrPoint)
#
#                maxConstrLimit = p.contol



                #xk = getDirectionOptimPoint(p, p.f, optimConstrPoint, -optimConstrPoint+xk+direction_f, maxConstrLimit = maxConstrLimit)
                #print 'res_0', res_0, 'res_1', res_1, 'res_2', p.getMaxResidual(xk)
                #xk = getDirectionOptimPoint(p, p.f, xk, direction_f, maxConstrLimit)
                #newX = xk.copy()
                result = 0
#                x_0 = X0.copy()
#                N = j = 0
#                while p.getMaxResidual(x_0) > Residual0 + 0.1*p.contol:
#                    j += 1
#                    x_0 = xk + 0.75**j * (X0-xk)
#                X0 = x_0
#                result, newX = 0, X0
#                print 'newIterResidual = ', p.getMaxResidual(x_0)

            if result != 0:
                p.istop = result
                p.xf = newX
                return

            xk = newX.copy()
            fk = p.f(xk)

            p.xk, p.fk = copy(xk), copy(fk)
            #p._df = p.df(xk)
            ####################
            p.iterfcn()

            if p.istop:
                p.xf = xk
                p.ff = fk
                #p._df = g FIXME: implement me
                return
    def _train(self, instance_sets, param):

        #fix parameters
        M = len(instance_sets)
        Cs = numpy.ones((M, M))

        #init containers
        example_sets = []
        label_sets = []

        #perform data conversion -> strToVec
        for instance_set in instance_sets:

            example_set = helper.gen_features(
                [inst.example for inst in instance_set])
            print "example set:", example_set.shape
            label_set = numpy.array([inst.label for inst in instance_set])
            print "label set:", label_set.shape
            print "num positive labels", sum(label_set) + len(label_set)

            example_sets.append(example_set)
            label_sets.append(label_set)

        #determine starting point
        M = len(example_sets)
        d = len(example_sets[0][0])

        print "M:", M, "d:", d

        dim_x0 = d * M
        numpy.random.seed(123967)
        x0 = numpy.random.randn(dim_x0)
        """
        Example:
        Consider the problem
        0.5 * (x1^2 + 2x2^2 + 3x3^2) + 15x1 + 8x2 + 80x3 -> min        (1)
        subjected to
        x1 + 2x2 + 3x3 <= 150            (2)
        8x1 +  15x2 +  80x3 <= 800    (3)
        x2 - x3 = 25                              (4)
        x1 <= 15                                  (5)
        """

        #TODO map variables

        from numpy import diag, matrix, inf
        from openopt import QP
        p = QP(diag([1, 2, 3]), [15, 8, 80],
               A=matrix('1 2 3; 8 15 80'),
               b=[150, 800],
               Aeq=[0, 1, -1],
               beq=25,
               ub=[15, inf, inf])
        # or p = QP(H=diag([1,2,3]), f=[15,8,80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25, ub = [15,inf,inf])
        r = p.solve('cvxopt_qp', iprint=0)
        #r = p.solve('nlp:ralg', xtol=1e-7, alp=3.9, plot=1)#, r = p.solve('nlp:algencan')
        #f_opt, x_opt = r.ff, r.xf
        f_opt = r.xf
        # x_opt = array([-14.99999995,  -2.59999996, -27.59999991])
        # f_opt = -1191.90000013

        return numpy.reshape(xopt, (M, d))
Example #26
0
	h = []
	for i in range(n):
		if i == j:
			h.append(12+n/(i+1.0))
		elif i == j + 1:
			h.append(0)
		elif j == i + 2:
			h.append(0)
		else:
			h.append(15/((i+1)+0.1*(j+1)))
	H.append(h)
			
#print H
#H = [[15,0,4.83871],
#     [12.5,13.5,0],
#	 	 [0,6.52174,13]]
#print H
x = fd.oovars(n)
f = fd.dot(fd.dot(x, H), x)
startPoint = {x: np.zeros(n)}
constraints = []
constraints.append(x > np.zeros(n))
constraints.append(fd.sum(x) == 1)
p = QP(f, startPoint, constraints=constraints)
r = p.solve("qlcp")
x_opt = r(x)
print(math.sqrt(r.ff))

# problem
# http://abel.ee.ucla.edu/cvxopt/examples/tutorial/qp.html
    def _train(self, train_data, param):
        """
        training procedure using training examples and labels
        
        @param train_data: Data relevant to SVM training
        @type train_data: dict<str, list<instances> >
        @param param: Parameters for the training procedure
        @type param: ParameterSvm
        """
 
        
                
        # fix dimensions
        M = len(train_data)

        N = 0
        for key in train_data.keys():
            N += len(train_data[key])
        
        # init containers
        examples = []
        labels = []


        # vector to indicate to which task each example belongs
        task_vector = []
        task_num = 0
        tmp_examples = 0

        label_matrix = numpy.zeros((M,N))


        # extract training data
        for (task_id, instance_set) in train_data.items():
  
            print "train task id:", task_id
            #assert(instance_set[0].dataset.organism==task_id)
            
            examples.extend([inst.example for inst in instance_set])
            
            tmp_labels = [inst.label for inst in instance_set]
            labels.extend(tmp_labels)
            
            begin_idx = tmp_examples
            end_idx = tmp_examples + len(tmp_labels) 
            
            # fill matrix row
            label_matrix[task_num, begin_idx:end_idx] = tmp_labels

            task_vector.extend([task_num]*len(instance_set))

            task_num += 1
            tmp_examples += len(tmp_labels)


        # fetch gammas from parameter object
        # TODO: compute gammas outside of this
        gammas = numpy.ones((M,M)) + numpy.eye(M)
        #gammas = numpy.eye(M)
        

        # create kernel
        kernel = shogun_factory.create_kernel(examples, param)


        y = numpy.array(labels)

        print "computing kernel matrix"

        km = kernel.get_kernel_matrix()
        km = reweight_kernel_matrix(km, gammas, task_vector)

        # "add" labels to Q-matrix
        km = numpy.transpose(y.flatten() * (km*y.flatten()).transpose())

        print "done computing kernel matrix, calling solver"


        f = -numpy.ones(N)
        b = numpy.zeros((M,1))

        # set up QP
        p = QP(km, f, Aeq=label_matrix, beq=b, lb=numpy.zeros(N), ub=param.cost*numpy.ones(N))
        p.debug=1
        
        # run solver
        r = p.solve('cvxopt_qp', iprint = 0)

        print "done with training"

        alphas = r.xf
        objective = r.ff


        print "alphas:", alphas

        predictors = {}

        for (k, task_id) in enumerate(train_data.keys()):
            # pack all relevant information in predictor
            predictors[task_id] = (alphas, param, task_vector, k, gammas, examples, labels)

        return predictors
##################################################################

km = wdk.get_kernel_matrix()

for i in xrange(N):
    for j in xrange(N):
        km[i, j] = km[i, j] * relate_tasks(i, j)
        #km = km*1.0

print km
#precompute kernel matrix using shogun
y = numpy.array(labels)
K = numpy.transpose(y.flatten() * (km * y.flatten()).transpose())
f = -numpy.ones(N)
C = 1.0

# Important!! QP does not accept ndarray as a type, it must be an array
p = QP(K, f, Aeq=y, beq=0, lb=numpy.zeros(N), ub=C * numpy.ones(N))
r = p.solve('cvxopt_qp', iprint=0)

#print "cvxopt objective:", r.ff
print "externally modified kernel. objective:", r.ff

ck = CustomKernel()
ck.set_full_kernel_matrix_from_full(km)
#
svm = LibSVM(1, ck, lab)
svm.train()

print "externally modified kernel. objective:", svm.get_objective()
Example #29
0
x1 + 2x2 + 3x3 <= 150                                          (2)
8x1 +  15x2 +  80x3 <= 800                                     (3)
x2 - x3 = 25                                                   (4)
x1 <= 15                                                       (5)

"""

from numpy import diag, matrix, inf
from openopt import QP

H = diag([1., 2., 3.]) 
f = [15., 8., 80.]
p = QP(H, 
       f, 
       A = matrix('1 2 3; 8 15 80'), 
       b = [150, 800], 
       Aeq = [0, 1, -1], 
       beq = 25, 
       ub = [15,inf,inf])

r = p.solve('qlcp', iprint = 0)

f_opt, x_opt = r.ff, r.xf

xx = matrix(r.xf)
val = .5*xx*matrix(H)*xx.T + matrix(f)*xx.T


# or p = QP(H=diag([1,2,3]), f=[15,8,80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25, ub = [15,inf,inf])
#r = p.solve('cvxopt_qp', iprint = 0)
#r = p.solve('nlp:ralg', xtol=1e-7, alp=3.9, plot=1)#, r = p.solve('nlp:algencan')
Example #30
0
from FuncDesigner import *
from openopt import QP
from numpy import zeros, arange

n = 15
a = oovar('a')
b = oovars(n, domain = int)('b')
c = oovar('c')
d = oovar('d', domain = bool)

B = 10*sin(arange(n)) # arange(n) = [0, 1, 2, ..., n-1]

objective = (a-1)**2 + 10*sum((b-B)**2) + 3*(1+10*(1+100*c**2)) + 100*(d-5)**2 + (0.1*a+1)*(c+d)
constraints = [a>0, a<1.03, b>0, b<10, c>0,  c<10.1, c**2 < 5, a+b<7]

startpoint = {a: 10, b: zeros(n), c:10, d:0}
p = QP(objective, startpoint, constraints = constraints)
solver = 'cplex'

r = p.solve(solver)
'''
istop: 1000 (Cplex status: "integer optimal, tolerance"; exit code: 102)
Solver:   Time Elapsed = 0.18 	CPU Time Elapsed = 0.15
objFunValue: 5025.3035 (feasible, MaxResidual = 0)
'''
print(a(r)) # 0.0
print(b(r)) # array([ 0.,  7.,  7.,  1.,  0.,  0.,  0.,  7.,  7.,  4.,  0.,  0.,  0., 4.,  7.])
print(r(c,d)) # [0.0, 1.0]

Example #31
0
"""
Example:
Concider the problem
0.5 * (x1^2 + 2x2^2 + 3x3^2) + 15x1 + 8x2 + 80x3 -> min        (1)
subjected to
x1 + 2x2 + 3x3 <= 150            (2)
8x1 +  15x2 +  80x3 <= 800    (3)
x2 - x3 = 25.5                           (4)
x1 <= 15                                  (5)
x1, x3 are integers
"""

from numpy import diag, matrix, inf
from openopt import QP
p = QP(diag([1, 2, 3]), [15, 8, 80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25.5, ub = [15,inf,inf])
# or p = QP(H=diag([1,2,3]), f=[15,8,80], A= ...)
r = p.solve('cplex', intVars= [0, 2],  iprint = 0)
f_opt, x_opt = r.ff, r.xf
# x_opt = array([-15. ,  -2.5, -28. ])
# f_opt = -1190.25
km = wdk.get_kernel_matrix()

for i in xrange(N):
    for j in xrange(N):
        km[i,j] = km[i,j]*relate_tasks(i,j)
        #km = km*1.0

print km
#precompute kernel matrix using shogun
y = numpy.array(labels)
K = numpy.transpose(y.flatten() * (km*y.flatten()).transpose())
f = -numpy.ones(N)
C = 1.0

# Important!! QP does not accept ndarray as a type, it must be an array
p = QP(K, f, Aeq=y, beq=0, lb=numpy.zeros(N), ub=C*numpy.ones(N))
r = p.solve('cvxopt_qp', iprint = 0)

#print "cvxopt objective:", r.ff
print "externally modified kernel. objective:", r.ff

ck = CustomKernel()
ck.set_full_kernel_matrix_from_full(km)
#
svm = LibSVM(1, ck, lab)
svm.train()

print "externally modified kernel. objective:", svm.get_objective()


Example #33
0
#-----------------------------------------------------------------------------------------------------
#constraints
lower_limit=[0.0 for i in range(2*tot_values)]
upper_limit=[float(C) for i in range(2*tot_values)]
Aeq = [1.0 for i in range(2*tot_values)]
for i in range(tot_values,2*tot_values):
	Aeq[i]=-1.0
beq=0.0


#----------------------------------------------------------------------------------------------------

#coeff for 3rd constraint
#kernel=H
eq = QP(np.asmatrix(kernel),np.asmatrix(f),lb=np.asmatrix(lower_limit),ub=np.asmatrix(upper_limit),Aeq=Aeq,beq=beq)
p = eq._solve('cvxopt_qp', iprint = 0)
f_optimized, x = p.ff, p.xf

#---------------------------------------------------------------------------------------
support_vectors=[]
support_vectors_Y=[]
support_vector=[]
support_vector_Y=[]

coeff=[]
b=0.0
#support vectors: points such that an-an' ! = 0
for i in range(tot_values):
	if not((x[i]-x[tot_values+i])==0):
		support_vectors.append( X[i] )
Example #34
0
#constraints
lower_limit = [0.0 for i in range(2 * tot_values)]
upper_limit = [float(C) / tot_values for i in range(2 * tot_values)]
Aeq = [1.0 for i in range(2 * tot_values)]
for i in range(tot_values, 2 * tot_values):
    Aeq[i] = -1.0
beq = 0.0
A = [1.0 for i in range(2 * tot_values)]
b = nu * float(C)

#coeff for 3rd constraint
#kernel=H
p = QP(np.asmatrix(kernel),
       np.asmatrix(f),
       lb=np.asmatrix(lower_limit),
       ub=np.asmatrix(upper_limit),
       Aeq=Aeq,
       beq=beq,
       A=A,
       b=b)
# or p = QP(H=diag([1,2,3]), f=[15,8,80], A= ...)
r = p._solve('cvxopt_qp', iprint=0)
f_opt, x = r.ff, r.xf

support_vectors = []
support_vectors_Y = []
coeff = []
b = 0.0
#support vectors: points such that an-an' ! = 0
for i in range(tot_values):
    if not ((x[i] - x[tot_values + i]) == 0):
        support_vectors.append(X[i])
Example #35
0
"""
Example:
Concider the problem
0.5 * (x1^2 + 2x2^2 + 3x3^2) + 15x1 + 8x2 + 80x3 -> min        (1)
subjected to
x1 + 2x2 + 3x3 <= 150            (2)
8x1 +  15x2 +  80x3 <= 800    (3)
x2 - x3 = 25.5                           (4)
x1 <= 15                                  (5)
"""

from numpy import diag, matrix, inf
from openopt import QP
p = QP(diag([1, 2, 3]), [15, 8, 80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25.5, ub = [15,inf,inf])
# or p = QP(H=diag([1,2,3]), f=[15,8,80], A= ...)
r = p._solve('cvxopt_qp', iprint = 0)
f_opt, x_opt = r.ff, r.xf
# x_opt = array([-15. ,  -2.5, -28. ])
# f_opt = -1190.25