예제 #1
0
    def _cvxmod_minimize(self, lb, ub, **kwargs):
        """ solve quadratic problem using CVXMOD interface

        Keyword arguments:

        lb, ub  -- vectors of lower and upper bounds (potentially modified by
                   added tolerance)

        Modified member variables:

        status, solution, obj_value

        Returns: nothing
        """
        # shorter names
        v = self.cvxmodV
        A = self.cvxmodMatrix
        minus_w = cvxmod.matrix(self.obj.f)  # negative wildtype solution
        lb = cvxmod.matrix(lb)
        ub = cvxmod.matrix(ub)
        if self.weights is not None:
            weights = cvxmod.matrix(diag(sqrt(self.weights)))
        if self.Aineq is not None:
            Aineq = cvxmod.matrix(array(self.Aineq))
            bineq = cvxmod.matrix(self.bineq)
            if self.weights is None:
                p = cvxmod.problem(cvxmod.minimize(
                    cvxmod.norm2(v + minus_w)), [
                        cvxmod.abs(A * v) < self.matrix_tol,
                        Aineq * v <= bineq + self.matrix_tol, v >= lb, v <= ub
                    ])
            else:
                p = cvxmod.problem(
                    cvxmod.minimize(cvxmod.norm2(weights * (v + minus_w))), [
                        cvxmod.abs(A * v) < self.matrix_tol,
                        Aineq * v <= bineq + self.matrix_tol, v >= lb, v <= ub
                    ])
        else:
            if self.weights is None:
                p = cvxmod.problem(
                    cvxmod.minimize(cvxmod.norm2(v + minus_w)),
                    [cvxmod.abs(A * v) < self.matrix_tol, v >= lb, v <= ub])
            else:
                p = cvxmod.problem(
                    cvxmod.minimize(cvxmod.norm2(weights * (v + minus_w))),
                    [cvxmod.abs(A * v) < self.matrix_tol, v >= lb, v <= ub])

        self.status = cvxToSolverStatus(p.solve())

        if not v.value:
            self.solution = []
        else:
            self.solution = array(list(v.value))
        try:
            self.obj_value = p.value
        except cvxmod.OptvarValueError:
            self.obj_value = inf
예제 #2
0
파일: SVM_QP.py 프로젝트: kerinin/iEngine
	def _compute(self):
		start = datetime.datetime.now()

		gamma = self.gamma
		(N,d) = self.data.shape
		X = self.data

		Xcmf = ( (X.reshape(N,1,d) > transpose(X.reshape(N,1,d),[1,0,2])).prod(2).sum(1,dtype=float) / N ).reshape([N,1])
		sigma = .75 / sqrt(N)
		
		K = self._K( X.reshape(N,1,d), transpose(X.reshape(N,1,d), [1,0,2]), gamma ).reshape([N,N])
		#NOTE: this integral depends on K being the gaussian kernel
		Kint =  ( (1.0/gamma)*scipy.special.ndtr( (X-X.T)/gamma ) )
		
		alpha = cvxmod.optvar( 'alpha',N,1)
		alpha.pos = True
		pK = cvxmod.param( 'K',N,N )
		pK.psd = True
		pK.value = cvxopt.matrix(K,(N,N) )
		pKint = cvxmod.param( 'Kint',N,N )
		pKint.value = cvxopt.matrix(Kint,(N,N))
		#pKint.pos = True
		pXcmf = cvxmod.param( 'Xcmf',N,1)
		pXcmf.value = cvxopt.matrix(Xcmf, (N,1))
		#pXcmf.pos = True
		
		objective = cvxmod.minimize( cvxmod.atoms.quadform(alpha, pK) )
		eq1 = cvxmod.abs( pXcmf - ( pKint * alpha ) ) <= sigma
		eq2 = cvxmod.sum( alpha ) == 1.0
		
		# Solve!
		p = cvxmod.problem( objective = objective, constr = [eq1, eq2] )
		
		start = datetime.datetime.now()
		p.solve()
		duration = datetime.datetime.now() - start
		print "optimized in %ss" % (float(duration.microseconds)/1000000)
		
		beta = ma.masked_less( alpha.value, 1e-7 )
		mask = ma.getmask( beta )
		data = ma.array(X,mask=mask)
		
		self.Fl = Xcmf
		self.beta = beta.compressed().reshape([ 1, len(beta.compressed()) ])
		self.SV = data.compressed().reshape([len(beta.compressed()),1])
		print "%s SV's found" % len(self.SV)
예제 #3
0
파일: SVM_QP2.py 프로젝트: kerinin/iEngine
    def _compute(self):
        C = self.C
        gamma = self.gamma
        (N, d) = self.data.shape
        X = self.data

        Xcmf = (
            (X.reshape(N, 1, d) > transpose(X.reshape(N, 1, d), [1, 0, 2])).prod(2).sum(1, dtype=float) / N
        ).reshape([N, 1])
        sigma = 0.75 / sqrt(N)

        K = self._K(X.reshape(N, 1, d), transpose(X.reshape(N, 1, d), [1, 0, 2]), gamma).reshape([N, N])
        # NOTE: this integral depends on K being the gaussian kernel
        Kint = (1.0 / gamma) * scipy.special.ndtr((X - X.T) / gamma)

        alpha = cvxmod.optvar("alpha", N, 1)
        alpha.pos = True
        xi = cvxmod.optvar("xi", N, 1)
        xi.pos = True
        pXcmf = cvxmod.param("Xcmf", N, 1)
        pXcmf.pos = True
        pXcmf.value = cvxopt.matrix(Xcmf, (N, 1))
        pKint = cvxmod.param("Kint", N, N)
        pKint.value = cvxopt.matrix(Kint, (N, N))

        objective = cvxmod.minimize(cvxmod.sum(cvxmod.atoms.power(alpha, 2)) + (C * cvxmod.sum(xi)))
        eq1 = cvxmod.abs((pKint * alpha) - pXcmf) <= sigma + xi
        eq2 = cvxmod.sum(alpha) == 1.0

        # Solve!
        p = cvxmod.problem(objective=objective, constr=[eq1, eq2])
        p.solve()

        beta = ma.masked_less(alpha.value, 1e-7)
        mask = ma.getmask(beta)
        data = ma.array(X, mask=mask)

        self.beta = beta.compressed().reshape([1, len(beta.compressed())])
        self.SV = data.compressed().reshape([len(beta.compressed()), 1])
        print "%s SV's found" % len(self.SV)