def get_weights(self) -> pd.Series:
        assets_number = self.cov_matrix.shape[1]
        st_devs = self.std_of_assets.values
        st_devs = st_devs.reshape((1, -1))  # make it a horizontal vector

        P = matrix(self.cov_matrix.values)
        q = matrix(0.0, (assets_number, 1))

        A = matrix(st_devs)
        b = matrix([1.0])

        G_lower_bound, h_lower_bound = each_weight_greater_than_0_constraint(
            assets_number)

        if self.upper_constraint is not None:
            G_upper_bound, h_upper_bound = self._get_upper_bound_constraints(
                P, q, G_lower_bound, h_lower_bound, A, b)
            G, h = merge_constraints(G_lower_bound, h_lower_bound,
                                     G_upper_bound, h_upper_bound)
        else:
            G, h = G_lower_bound, h_lower_bound

        result = qp(P, q, G, h, A, b, options=self.optimizer_options)
        dummy_weights = np.array(result['x']).squeeze()
        scaled_weights = dummy_weights / dummy_weights.sum()
        weights_series = pd.Series(data=scaled_weights,
                                   index=self.cov_matrix.columns.values)

        return weights_series
Example #2
0
 def __init__(self):
     base_device.__init__(self)
     self.u = mul(matrix(self.u), matrix(system.PV.u))
     self._data.update({
         'bus': None,
         'Type': 1,
         'vrmax': 3.3,
         'vrmin': -2.6,
         'K0': 0,
         'T1': 0,
         'T2': 0,
         'T3': 0,
         'T4': 0,
         'Te': 0,
         'Tr': 0,
         'Ae': 0,
         'Be': 0
     })
     self._type = 'Avr1'
     self._name = 'Avr1'
     self._bus = {'bus': ['a', 'v']}
     self._algebs = ['vref']  # 后续得修改
     self._states = ['vm', 'vr1', 'vr2', 'vf']  # 后续得修改
     self._params.extend([
         'u', 'vrmax', 'vrmin', 'K0', 'T1', 'T2', 'T3', 'T4', 'Te', 'Tr',
         'Ae', 'Be'
     ])
     self._voltages = ['V0']  # 后续得修改
     self._powers = ['Pg']  # 后续得修改
     self.ba = []
     self.bv = []
Example #3
0
def solve_only_equalities_qp(kktsolver, fP, fA, resx0, resy0, dims):
    # Solve
    #
    #     [ P  A' ] [ x ]   [ -q ]
    #     [       ] [   ] = [    ].
    #     [ A  0  ] [ y ]   [  b ]

    #  print(G)
    #  factor = kkt_chol2(G, dims, A)
    #  W = {'d': matrix(0.0, (0, 1)), 'di': matrix(0.0, (0, 1)), 'beta': [], 'v': [], 'r': [], 'rti': []}
    #  print(P)
    #  solver = factor(W, P)

    try:
        f3 = kktsolver({'d': matrix(0.0, (0, 1)), 'di':
                        matrix(0.0, (0, 1)), 'beta': [], 'v': [], 'r': [], 'rti': []})

        #  factor = kkt_chol2(G, dims, A)
        #  W = {'d': matrix(0.0, (0, 1)), 'di': matrix(0.0, (0, 1)), 'beta': [], 'v': [], 'r': [], 'rti': []}
        #  f3 = factor(W, P)

    except ArithmeticError:
        raise ValueError("Rank(A) < p or Rank([P; A; G]) < n")

    x = -1.0 * matrix(q)
    y = matrix(b)
    f3(x, y, matrix(0.0, (0, 1)))
    #  solver(x, y, matrix(0.0, (0, 1)))
    return {'status': 'optimal', 'x': x, 'y': y}
Example #4
0
def read(dataset = "training", path = "."):
    """
    Python function for importing the MNIST data set.
    """
    digits = [1,2,3,4,5,6,7,8,9,0]
    if dataset is "training":
        fname_img = os.path.join(path, 'train-images-idx3-ubyte')
        fname_lbl = os.path.join(path, 'train-labels-idx1-ubyte')
    elif dataset is "testing":
        fname_img = os.path.join(path, 't10k-images-idx3-ubyte')
        fname_lbl = os.path.join(path, 't10k-labels-idx1-ubyte')
    else:
        raise ValueError, "dataset must be 'testing' or 'training'"

    flbl = open(fname_lbl, 'rb')
    magic_nr, size = struct.unpack(">II", flbl.read(8))
    lbl = array("b", flbl.read())
    flbl.close()

    fimg = open(fname_img, 'rb')
    magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
    img = array("B", fimg.read())
    fimg.close()

    ind = [ k for k in xrange(size) if lbl[k] in digits ]
    images =  matrix(0, (len(ind), rows*cols))
    labels = matrix(0, (len(ind), 1))
    for i in xrange(len(ind)):
        images[i, :] = img[ ind[i]*rows*cols : (ind[i]+1)*rows*cols ]
        labels[i] = lbl[ind[i]]

    return images, labels
Example #5
0
        def classifier2(Y, soft=False):

            M = Y.size[0]

            # K = Y*X' / sigma
            K = matrix(theta, (width, M))
            blas.gemm(X,
                      Y,
                      K,
                      transB='T',
                      alpha=1.0 / sigma,
                      beta=-1.0,
                      m=width)

            K = exp(K)
            K = div(K - K**-1, K + K**-1)

            # complete K
            lapack.potrs(L11, K)
            K = matrix([K, matrix(0., (N - width, M))], (N, M))
            chompack.trsm(Lc, K, trans='N')
            chompack.trsm(Lc, K, trans='T')

            x = matrix(b, (M, 1))
            blas.gemv(K, z, x, trans='T', beta=1.0)

            if soft: return x
            else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
Example #6
0
def numerical_hessian(x_k, f_k, func, epsilon, *args):
    n_params, one = x_k.size
    assert one==1
    H = cvx.matrix(0.0, (n_params,n_params))
    epsilon2 = epsilon**2
    
    ei = cvx.matrix(0.0, x_k.size)
    esame = cvx.matrix(0.0, x_k.size)
    ecross = cvx.matrix(0.0, x_k.size)
    for i in range(n_params):
        esame[i], ecross[i] = +epsilon, +epsilon
        for j in range(i):            
            esame[j], ecross[j] = +epsilon, -epsilon

            lhs = func(x_k+esame, *args) - func(x_k+ecross, *args)
            rhs = func(x_k-ecross, *args) - func(x_k-esame, *args)            
            H[i,j] = (lhs - rhs) / (4*epsilon2)
            H[j,i] = H[i,j]

            esame[j], ecross[j] = 0.0, 0.0
        esame[i], ecross[i] = 0.0, 0.0
                
        ei[i] = epsilon        
        H[i,i] = (func(x_k+ei, *args) + func(x_k-ei, *args) - 2*f_k) / epsilon2        
        ei[i] = 0.0        

    return H
Example #7
0
def calc_alpha(data,kernel,threshold,p=1):
    size = len(data)
    P = matrix(P_matrix(data,kernel,p))
    q = matrix(q_vector(size))
    G = matrix(G_matrix(size))
    h = matrix(h_vector(size))
    return [[data[i],a] for i,a in enumerate(list(qp(P,q,G,h)['x'])) if a>threshold]
Example #8
0
    def write_sdpa(self, fname=None, compress=False):
        """Writes problem data to sparse SDPA data file"""
        import bz2
        if not self._blockstruct: self._blockstruct = matrix(self.n, (1, 1))

        if fname is None:
            fname = self._pname

        fname += ".dat-s"
        if compress:
            if not os.path.isfile(fname) and not os.path.isfile(fname +
                                                                ".bz2"):
                f = open(fname, 'w')
            elif os.path.isfile(fname):
                raise IOError, "file %s already exists" % (fname)
            else:
                raise IOError, "file %s already exists" % (fname + ".bz2")
        elif not os.path.isfile(fname):
            f = open(fname, 'w')
        else:
            raise IOError, "file %s already exists" % (fname)
        misc.sdpa_write(f,
                        self._A,
                        matrix(self._b),
                        self._blockstruct,
                        neg=True)
        f.close()
        if compress:
            f = open(fname + ".bz2", 'wb')
            f.write(bz2.compress(open(fname).read()))
            f.close()
            os.remove(fname)
Example #9
0
def read(digits, path = "./../../data/", data="alphabets-font-images.idx", label="alphabets-font-labels.idx", ignoreList = []):
    """
    Python function for importing the MNIST data set.
    """

    fname_img = os.path.join(path, data)#'alphabets-font-images.idx')
    fname_lbl = os.path.join(path, label)#'alphabets-font-labels.idx')

    flbl = open(fname_lbl, 'rb')
    magic_nr, size = struct.unpack(">II", flbl.read(8))
    lbl = array("b", flbl.read())
    flbl.close()

    fimg = open(fname_img, 'rb')
    magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
    img = array("B", fimg.read())
    fimg.close()

    #print '(^_^)'
    #print 'ignoreList='+str(ignoreList)
    #print ('----')
    
    ind = [ k for k in xrange(size) if ((not lbl[k] in ignoreList) and  (lbl[k] in digits or digits == [])) ]
    images =  matrix(0, (len(ind), rows*cols))
    labels = matrix(0, (len(ind), 1))
    for i in xrange(len(ind)):
        images[i, :] = img[ ind[i]*rows*cols : (ind[i]+1)*rows*cols ]
        labels[i] = lbl[ind[i]]

    return images, labels
Example #10
0
def main():
	data = generateData()

	q = numpy.empty((DATAPOINTS,1))
	q[:] = -1.0

	h = numpy.empty((DATAPOINTS,1))
	h[:] = 0.0

	G = numpy.empty((DATAPOINTS,DATAPOINTS))
	numpy.fill_diagonal(G, -1)

	P = createP(data)

	r = qp(matrix(P),matrix(q),matrix(G),matrix(h))

	alpha = list(r['x'])
	
	xrange = numpy.arange(-4, 4, 0.05)
	yrange = numpy.arange(-4, 4, 0.05)
	support = getSupportVectors(alpha, data)
	
	grid = matrix([[indicator(x,y,support) for y in yrange] for x in xrange])

	pylab.contour(xrange, yrange, grid, (-1.0, 0.0, 1.0), 
			colors=('red', 'black', 'blue'),
			linewidths=(1, 1, 1))
	
	pylab.show()
Example #11
0
def read(digits, path = "./../../data/"):
    """
    Python function for importing the MNIST data set.
    """

    fname_img = os.path.join(path, 'img.idx')
    fname_lbl = os.path.join(path, 'label.idx')

    flbl = open(fname_lbl, 'rb')
    magic_nr, size = struct.unpack(">II", flbl.read(8))
    lbl = array("b", flbl.read())
    flbl.close()

    fimg = open(fname_img, 'rb')
    magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
    img = array("B", fimg.read())
    fimg.close()

    ind = [ k for k in xrange(size) if (lbl[k] in digits) ]
    images =  matrix(0, (len(ind), rows*cols))
    labels = matrix(0, (len(ind), 1))
    for i in xrange(len(ind)):
        images[i, :] = img[ ind[i]*rows*cols : (ind[i]+1)*rows*cols ]
        labels[i] = lbl[ind[i]]

    return images, labels
Example #12
0
def QuadOpt(x, P, slack, C):
    # intialize parameters
    # slack variable introduced
    if slack == 1:
        h = np.row_stack((np.zeros((len(x), 1)), C * np.ones((len(x), 1))))
        G = np.row_stack((np.diag([-1.0] * len(x)), np.diag([1.0] * len(x))))
    else:
        h = np.zeros((len(x), 1))
        G = np.diag([-1.0] * len(x))
    q = -1 * np.ones((len(x), 1))

    # call qp
    r = qp(matrix(P), matrix(q), matrix(G), matrix(h))
    alpha = list(r['x'])

    # pick out non-zero alpha
    # slack variable introduced
    support = list()
    for i in range(len(alpha)):
        if slack == 1:
            if (alpha[i] > 10e-5) and (alpha[i] < C):
                support.append((x[i][0], x[i][1], x[i][2], alpha[i]))
        else:
            if alpha[i] > 10e-5:
                support.append((x[i][0], x[i][1], x[i][2], alpha[i]))
    return support
Example #13
0
def numerical_hessian(x_k, f_k, func, epsilon, *args):
    n_params, one = x_k.size
    assert one == 1
    H = cvx.matrix(0.0, (n_params, n_params))
    epsilon2 = epsilon**2

    ei = cvx.matrix(0.0, x_k.size)
    esame = cvx.matrix(0.0, x_k.size)
    ecross = cvx.matrix(0.0, x_k.size)
    for i in range(n_params):
        esame[i], ecross[i] = +epsilon, +epsilon
        for j in range(i):
            esame[j], ecross[j] = +epsilon, -epsilon

            lhs = func(x_k + esame, *args) - func(x_k + ecross, *args)
            rhs = func(x_k - ecross, *args) - func(x_k - esame, *args)
            H[i, j] = (lhs - rhs) / (4 * epsilon2)
            H[j, i] = H[i, j]

            esame[j], ecross[j] = 0.0, 0.0
        esame[i], ecross[i] = 0.0, 0.0

        ei[i] = epsilon
        H[i, i] = (func(x_k + ei, *args) + func(x_k - ei, *args) -
                   2 * f_k) / epsilon2
        ei[i] = 0.0

    return H
Example #14
0
	def findSplittingPlane(self,Mid, data,show_contour, C=-1):
		# having a default value on C is horrible but im to lazy to make it neat for such a small program
		P_Matrix = [[0.0 for x in range(len(data))] for x in range(len(data))] 
		# build p P_matrix
		for idi, i in enumerate(data):
			for idj, j in enumerate(data):
				# if i==j: is diagonal a special case?
				# 	continue
				P_Matrix[idi][idj] = self.getPelem(i,j,self.kernel)
		if(PRINT_P_MATRIX): self.printTwoDimensional(P_Matrix)
		# Build vectors for qp
		q_vec = [-1.0 for x in range(len(data))]
		h_vec = [0.0 for x in range(len(data))]
		G_Matrix = [[ -1.0 if x==y else 0.0  for y in range(len(data))] for x in range(len(data))]

		# call qp
		r = qp(matrix(P_Matrix), matrix(q_vec), matrix(G_Matrix), matrix(h_vec))
		alpha = list(r['x'])
		alphaValues =[]
		# pick out the non zero alpha values
		for idx,elem in enumerate(alpha):
			if(elem > pow(10,-5)):
				if( C!=-1 and elem <= C):
					continue
				data[idx].alpha = elem
				alphaValues.append(data[idx])

		# implemnt the indication function
		xrange = numpy.arange(-4,4,0.05)
		yrange = numpy.arange(-4,4,0.05)
		grid=matrix([[self.indicator(x,y, alphaValues)
					for y in yrange]
					for x in xrange])

		if(PLOT_OUPUT): self.plotData(Mid, data,xrange,yrange,grid, show_contour)
Example #15
0
File: base.py Project: cvxopt/smcp
    def _gen_bandsdp(self,n,m,bw,seed):
        """Random data generator for SDP with band structure"""
        setseed(seed)

        I = matrix([ i for j in range(n) for i in range(j,min(j+bw+1,n))])
        J = matrix([ j for j in range(n) for i in range(j,min(j+bw+1,n))])
        V = spmatrix(0.,I,J,(n,n))
        Il = misc.sub2ind((n,n),I,J)
        Id = matrix([i for i in range(len(Il)) if I[i]==J[i]])

        # generate random y with norm 1
        y0 = normal(m,1)
        y0 /= blas.nrm2(y0)

        # generate random S0
        S0 = mk_rand(V,cone='posdef',seed=seed)
        X0 = mk_rand(V,cone='completable',seed=seed)

        # generate random A1,...,Am and set A0 = sum Ai*yi + S0
        A_ = normal(len(I),m+1,std=1./len(I))
        u = +S0.V
        blas.gemv(A_[:,1:],y0,u,beta=1.0)
        A_[:,0] = u
        # compute b
        x = +X0.V
        x[Id] *= 0.5
        self._b = matrix(0.,(m,1))
        blas.gemv(A_[:,1:],x,self._b,trans='T',alpha=2.0)
        # form A
        self._A = spmatrix(A_[:],
                     [i for j in range(m+1) for i in Il],
                     [j for j in range(m+1) for i in Il],(n**2,m+1))

        self._X0 = X0; self._y0 = y0; self._S0 = S0
Example #16
0
File: base.py Project: cvxopt/smcp
    def solve_cvxopt(self,primalstart=None,dualstart=None,\
                         options=None,kktsolver=None):
        """
        Passes problem data to CVXOPT sdp solver.

        ARGUMENTS


        RETURNS
        sol       Dictionary
        """

        from cvxopt import solvers
        if options:
            for key in list(options.keys()):
                solvers.options[key] = options[key]

        if primalstart:
            y0,S0 = dualstart['y'],dualstart['s']
            PS = {'x':y0,'s':[matrix(S0[:])]}
        else:
            PS = None

        if dualstart:
            X0 = primalstart['x']
            DS = {'y':None,'zl':[matrix(X0[:])]}
        else: DS = None

        return solvers.conelp(c = -self._b,
                              G = self._A[:,1:],
                              h = matrix(self.get_A(0)[:]),
                              dims = {'l':0,'q':[],'s':[self.n]},
                              primalstart = PS, dualstart = DS,
                              kktsolver = kktsolver)
Example #17
0
    def intervention(self, t):
        #
        for item in range(system.Line.n):
            if t == self.tf[item]:      #发生故障
                print('线路%i在t = %s s断开' % (item+1, self.tf[item]))

                #启用故障

                system.Line.u[item] = 0

                #存储故障前
                self._ang = matrix(system.DAE.y[system.Bus.a])
                self._vol = matrix(system.DAE.y[system.Bus.n:len(system.DAE.y)])

            if t == self.tc[item]:    #故障清除

                print('线路%i在t = %s s恢复连接' % (item+1, self.tc[item]))

                # 禁用故障
                system.Line.u[item] = 1


                #恢复电压
                if system.Settings.resetangles:
                    system.DAE.y[system.Bus.a] = matrix(self._ang)
                    system.DAE.y[system.Bus.n:len(system.DAE.y)] = matrix(self._vol)

        system.Line.build_y()
Example #18
0
def CVXOPT_LP_Solver(p, solverName):
    #os.close(1); os.close(2)
    if solverName == 'native_CVXOPT_LP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol    
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)
    #WholeRepr2LinConst(p)

    # CVXOPT have some problems with x0 so currently I decided to avoid using the one
    #if  p.x0.size>0 and p.x0.flatten()[0] != None and all(isfinite(p.x0)):
    #    sol= cvxopt_solvers.solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName)
    #else:

    if (len(p.intVars)>0 or len(p.binVars)>0) and solverName == 'glpk':
        from cvxopt.glpk import ilp
        c = Matrix(p.f)
        A, b = Matrix(p.Aeq),  Matrix(p.beq)
        G, h = Matrix(p.A),  Matrix(p.b)
        if A is None:
            A = matrix(0.0,  (0, p.n))
            b = matrix(0.0,(0,1))
        if G is None:
            G = matrix(0.0,  (0, p.n))
            h = matrix(0.0,(0,1))

        (status, x) = ilp(c, G, h, A, b, set(p.intVars), B=set(p.binVars))
        if status == 'optimal': p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        elif status == 'maxiters exceeded': p.istop = IS_MAX_ITER_REACHED
        elif status == 'time limit exceeded': p.istop = IS_MAX_TIME_REACHED
        elif status == 'unknown': p.istop = UNDEFINED
        else: p.istop = FAILED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        if x is None:
            p.xf = nan*ones(p.n)
        else:
            p.xf = array(x).flatten()#w/o flatten it yields incorrect result in ff!
        p.ff = sum(p.dotmult(p.f, p.xf))
        p.msg = status
    else:
#        if len(p.b) != 0: 
#            s0 = matrix(p.b - dot(p.A, p.x0))
#        else:
#            s0 = matrix()
#        primalstart = {'x': matrix(p.x0), 's': s0}
#        sol = cvxopt_solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName, primalstart)
        sol = cvxopt_solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName)
        p.msg = sol['status']
        if p.msg == 'optimal' :  p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        else: p.istop = -100
        if sol['x'] is not None:
            p.xf = asarray(sol['x']).flatten()
            # ! don't involve p.ff  - it can be different because of goal and additional constant from FuncDesigner
            p.duals = concatenate((asarray(sol['y']).flatten(), asarray(sol['z']).flatten()))
        else:
            p.ff = nan
            p.xf = nan*ones(p.n)
Example #19
0
def read(digits, dataset="training", path="."):
    """
    Python function for importing the MNIST data set.
    """
    #print("dataset is ",dataset)
    if dataset is "training":
        fname_img = os.path.join(path, '\\train-images-idx3-ubyte')
        fname_lbl = os.path.join(path, '\\train-labels-idx1-ubyte')
    elif dataset is "testing":
        fname_img = os.path.join(path, '\\t10k-images-idx3-ubyte')
        fname_lbl = os.path.join(path, '\\t10k-labels-idx1-ubyte')
    else:
        raise ValueError("dataset must be 'testing' or 'training'")

    flbl = open(fname_lbl, 'rb')
    magic_nr, size = struct.unpack(">II", flbl.read(8))
    lbl = array("b", flbl.read())
    flbl.close()

    fimg = open(fname_img, 'rb')
    magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
    img = array("B", fimg.read())
    fimg.close()

    ind = [k for k in range(size) if lbl[k] in digits]
    images = matrix(0, (len(ind), rows * cols))
    labels = matrix(0, (len(ind), 1))
    for i in range(len(ind)):
        images[i, :] = img[ind[i] * rows * cols:(ind[i] + 1) * rows * cols]
        labels[i] = lbl[ind[i]]

    return images, labels
def read_mps_preprocess(filepath):
    problem = op()
    problem.fromfile(filepath)
    mat_form = problem._inmatrixform(format='dense')
    format = 'dense'
    assert mat_form
    lp, vmap, mmap = mat_form

    variables = lp.variables()
    x = variables[0]
    c = lp.objective._linear._coeff[x]
    inequalities = lp._inequalities
    G = inequalities[0]._f._linear._coeff[x]
    h = -inequalities[0]._f._constant
    equalities = lp._equalities
    A, b = None, None
    if equalities:
        A = equalities[0]._f._linear._coeff[x]
        b = -equalities[0]._f._constant
    elif format == 'dense':
        A = matrix(0.0, (0, len(x)))
        b = matrix(0.0, (0, 1))
    else:
        A = spmatrix(0.0, [], [], (0, len(x)))  # CRITICAL
        b = matrix(0.0, (0, 1))

    c = np.array(c).flatten()
    G = np.array(G)
    h = np.array(h).flatten()
    A = np.array(A)
    b = np.array(b).flatten()

    return c, G, h, A, b
Example #21
0
    def gcall(self):

        if self.n == 0:
            return

        slip = system.DAE.x[self.slip]

        V = mul(self.u, system.DAE.y[self.v])  # 后续得修改
        t = system.DAE.y[self.a]
        st = sin(t)
        ct = cos(t)

        Vr = mul(V, st)
        Vm = mul(V, ct)

        e1r = system.DAE.x[self.e1r]
        e1m = system.DAE.x[self.e1m]

        a03 = mul(self.rs, self.rs) + mul(self.x1, self.x1)
        a13 = div(self.rs, a03)
        a23 = div(self.x1, a03)
        a33 = self.x0 - self.x1

        Im = mul(-a23, -Vr - e1r + mul(a13, (Vm - e1m)))
        Ir = mul(a13, -Vr - e1r) + mul(a23, Vm - e1m)

        system.DAE.g = system.DAE.g \
                       + spmatrix(mul(-Vr, Ir)+mul(Vm, Im), self.a, matrix(0, (self.n, 1)), (system.DAE.ny, 1)) \
                       + spmatrix(mul(Vr, Im) + mul(Vm, Ir), self.v, matrix(0, (self.n, 1)), (system.DAE.ny, 1))
Example #22
0
    def __init__(self):
        self.g = []  # 代数方程
        self.f = []  # 微分方程
        self.y = []  # 代数变量
        self.x = []  # 状态变量
        self.Y = matrix()  # 导纳矩阵
        self.Y_G = matrix()  # 导纳矩阵实部
        self.Y_B = matrix()  # 导纳矩阵虚部
        self.Gy = []  # 代数方程的雅可比矩阵
        self.Fx = []
        self.Fy = []
        self.Gx = []
        self.nx = 0  # 状态变量个数
        self.ny = 0  # 代数变量个数,默认为0
        self.n_bus = 0  # 母线节点数,默认为0
        self.t = []

        self.factorize = []  # 如果是True,则对雅可比矩阵进行因式分解
        self._params = [
            'g', 'x', 'y', 'f', 'Fx', 'Fy', 'Gx', 'Y', 'Y_G', 'Y_B', 'Gy'
        ]

        self.factorize = True  # 如果是True,则对雅可比矩阵进行因式分解
        self.tn = []  # 时域仿真的微分方程
        self.Ac = []  # 时域仿真雅可比方程
Example #23
0
    def solve(self):
        c_ = matrix(self.c)

        # adding the dummy constraint
        A_ = np.vstack((self.A, -self.c))
        b_ = np.hstack((self.b, [1.0]))

        G = matrix(A_)
        h = matrix(b_)

        self.result = solvers.lp(c_, G, h, solver=solver)

        primal_objective_ = self.result['primal objective']
        status_ = self.result['status']
        if primal_objective_ < -0.5:
            logger.warning('cvxopt status={} with dummy'.format(status_))
            self.status = UNBOUNDED
        else:
            logger.warning('cvxopt status={}'.format(status_))
            self.status = OPTIMAL

        self.objective = primal_objective_

        _x = self.result['x']
        self._x = np.array(_x).flatten()

        _z = self.result['z']
        self._z = np.array(_z).flatten()[:-1]  # remove the dummy constraint
Example #24
0
    def intervention(self, t):
        #

        for item in range(self.n):
            if t == self.tf[item]:  #发生故障
                print('故障发生在t = %s s' % self.tf[item])

                #启用故障

                self.u[item] = 1
                system.DAE.factorize = True

                #存储故障前
                self._ang = matrix(system.DAE.y[system.Bus.a])
                self._vol = matrix(
                    system.DAE.y[system.Bus.n:len(system.DAE.y)])

            if t == self.tc[item]:  #故障清除

                print('在t = %s s清除故障' % self.tc[item])

                # 禁用故障
                self.u[item] = 0
                system.DAE.factorize = True

                #恢复电压
                if system.Settings.resetangles:
                    system.DAE.y[system.Bus.a] = matrix(self._ang)
                    system.DAE.y[system.Bus.n:len(system.DAE.y)] = matrix(
                        self._vol)
Example #25
0
    def Gycall(self):
        system.DAE.y = matrix(system.DAE.y)
        U = exp(system.DAE.y[system.Bus.a] * 1j)
        V = mul(system.DAE.y[system.Bus.v] + 0j, U)
        I = self.Y * V
        nb = len(system.Bus.a)
        diagU = spmatrix(U, system.Bus.a, system.Bus.a, (nb, nb), 'z')
        diagV = spmatrix(V, system.Bus.a, system.Bus.a, (nb, nb), 'z')
        diagI = spmatrix(I, system.Bus.a, system.Bus.a, (nb, nb), 'z')
        dS = self.Y * diagU

        dS = diagV * dS.H.T

        dS += diagI.H.T * diagU
        dR = diagI
        dR = dR - self.Y * diagV

        dR = diagV.H.T * dR

        # system.DAE.Gy = matrix(0.0, (system.DAE.ny, system.DAE.ny))
        #
        # system.DAE._list2matrix()
        # system.DAE.Gy = spmatrix(dR.imag().V, dR.imag().I, dR.imag().J, (system.DAE.ny, system.DAE.ny)) \
        #                 + spmatrix(dR.real().V, dR.real().I, dR.real().J+system.Bus.n, (system.DAE.ny, system.DAE.ny)) \
        #                 + spmatrix(dS.real().V, dS.real().I+system.Bus.n, dS.real().J, (system.DAE.ny, system.DAE.ny)) \
        #                 + spmatrix(dS.imag().V, dS.imag().I+system.Bus.n, dS.imag().J+system.Bus.n, (system.DAE.ny, system.DAE.ny))

        Gy = sparse([[dR.imag(), dR.real()], [dS.real(), dS.imag()]])
        # system.DAE.Gy = zeros([system.DAE.ny,system.DAE.ny])
        system.DAE.Gy = spmatrix(Gy.V, Gy.I, Gy.J,
                                 (system.DAE.ny, system.DAE.ny))

        system.DAE.Gy = matrix(system.DAE.Gy)
Example #26
0
    def gcall(self):

        if self.n == 0:
            return

        system.DAE.y = matrix(system.DAE.y)
        V = system.DAE.y[self.v]
        V2 = mul(V, V)
        p = mul(matrix(self.g), V2)
        q = mul(matrix(self.b), V2)

        # #V = matrix(0, (system.Shunt.n, 1))
        # V = [1.0] * system.Shunt.n
        # print(V)
        # for i in range(system.Shunt.n):
        #     V[i] = system.DAE.y[self.v[i]]
        # print(V)
        # V = np.array(V)
        # V2 = V * V
        # print(V2)
        # J = [1] * system.Shunt.n
        # print(self.a)
        # print(J)
        # p = [0] * system.Shunt.n
        # q = [0] * system.Shunt.n

        # for i in range(system.Shunt.n):
        #      p[i] = self.g[i] * V2[i]
        #      q[i] = self.b[i] * V2[i]

        for key, value in zip(self.a, p):
            system.DAE.g[key] += value
        for key, value in zip(self.v, q):
            system.DAE.g[key] -= value
Example #27
0
    def yinit(self, dae):
        zeros = [0.0] * (2 * self.n)
        dae.y = zeros[:]
        dae.g = zeros[:]

        for i in range(self.n):
            if self.V_0[i] < 0.5:
                print(
                    'Warning: Bus %i initial guess voltage magnitudes are too low.'
                    % i)
            if self.V_0[i] > 1.5:
                print(
                    'Warning: Bus %i initial guess voltage magnitudes are too high.'
                    % i)
        system.DAE.y = matrix(system.DAE.y)
        system.DAE.y[self.v] = self.V_0

        aref = min(abs(matrix(self.theta0)))
        for i in range(self.n):
            if self.theta0[i] - aref < -1.5708:
                print(
                    'Warning: Bus %i initial guess voltage phases are too low.'
                    % i)
            if self.theta0[i] - aref > 1.5708:
                print(
                    'Warning: Bus %i initial guess voltage phases are too high.'
                    % i)
        system.DAE.y[self.a] = self.theta0
Example #28
0
    def fcall(self):

        if self.n == 0:
            return

        tg1 = system.DAE.x[self.tg1]
        tg2 = system.DAE.x[self.tg2]
        tg3 = system.DAE.x[self.tg3]
        wref = system.DAE.y[self.wref]


        gain = div(matrix(1.0, (self.n, 1)), self.R)

        a_s = div(matrix(1.0, (self.n, 1)), self.Ts)
        ac = div(matrix(1.0, (self.n, 1)), self.Tc)
        a5 = div(matrix(1.0, (self.n, 1)), self.T5)
        K1 = mul(self.T3, ac)
        K2 = 1 - K1
        K3 = mul(self.T4, a5)
        K4 = 1 - K3
        system.Syn6.omega = matrix(system.Syn6.omega)
        omega = system.Syn6.omega[self.a]

        tin = self.Porder + mul(gain, wref - system.DAE.x[omega])
        for i in range(self.n):
            tin[i] = max(tin[i], self.Pmin[i])
            tin[i] = min(tin[i], self.Pmax[i])

        system.DAE.f[self.tg1] = mul(self.u, a_s, -tg1+tin)
        system.DAE.f[self.tg2] = mul(self.u, ac, -tg2 + mul(K2, tg1))
        system.DAE.f[self.tg3] = mul(self.u, a5, -tg2 + mul(K4, tg2+mul(K1, tg1)))
Example #29
0
File: base.py Project: cvxopt/smcp
    def _gen_mtxnormsdp(self,p,q,r,d,seed):
        """
        Random data generator for matrix norm minimization SDP.
        """
        setseed(seed)
        n = p + q;

        I = matrix([ i for j in range(q) for i in range(q,n)])
        J = matrix([ j for j in range(q) for i in range(q,n)])
        Il = list(misc.sub2ind((n,n),I,J))

        if type(d) is float:
            nz = min(max(1, int(round(d*p*q))), p*q)
            A = sparse([[spmatrix(normal(p*q,1),Il,[0 for i in range(p*q)],(n**2,1))],
                        [spmatrix(normal(nz*r,1),
                                  [i for j in range(r) for i in random.sample(Il,nz)],
                                  [j for j in range(r) for i in range(nz)],(n**2,r))]])
        elif type(d) is list:
            if len(d) == r:
                nz = [min(max(1, int(round(v*p*q))), p*q) for v in d]
                nnz = sum(nz)
                A = sparse([[spmatrix(normal(p*q,1),Il,[0 for i in range(p*q)],(n**2,1))],
                            [spmatrix(normal(nnz,1),
                                      [i for j in range(r) for i in random.sample(Il,nz[j])],
                                      [j for j in range(r) for i in range(nz[j])],(n**2,r))]])
        else: raise TypeError
        self._A = sparse([[A],[spmatrix(-1.,list(range(0,n**2,n+1)),[0 for i in range(n)],(n**2,1))]])

        self._b = matrix(0.,(r+1,1))
        self._b[-1] = -1.
def sum_weights_equal_1_constraint(
        assets_number: int) -> Tuple[matrix, matrix]:
    """
    Creates a constraint which assures that all weights sum up to 1.
    """
    A = matrix(1.0, (1, assets_number))
    b = matrix(1.0)
    return A, b
Example #31
0
def find_alphas(P, q, G, h):
    # Utilisation de cvxopt: cf enonce du TP
    r = qp(matrix(P), matrix(q), matrix(G), matrix(h))
    alphas = np.array(list(r['x']))
    # remplacer les valeurs trop petites par 0, garder le reste
    alphas = np.where(np.fabs(alphas) < 10e-5, 0, alphas)

    return alphas
Example #32
0
 def test_negative(self):
     t = self.solver.calculate(
         self.tols_vector, self.flows_vector.transpose(),
         self.incidence_matrix,
         np.zeros(self.solver.data['graph']['nodeCount']),
         matrix(self.A_for_negative_test_2.transpose()),
         matrix(self.b_for_negative_test_2.transpose()))
     self.assertEqual(t, None)
Example #33
0
def build_qGh(n):
    print('Building q, G, h...')
    q = matrix(-1., (n, 1))
    h = matrix(0., (n, 1))
    G = matrix(0., (n, n))
    G[::(n + 1)] = -1

    return q, h, G
Example #34
0
def find_alphas(P, q, G, h):
    # Utilisation de cvxopt: cf enonce du TP
    r = qp(matrix(P), matrix(q), matrix(G), matrix(h))
    alphas = np.array(list(r['x']))
    # remplacer les valeurs trop petites par 0, garder le reste
    alphas = np.where(np.fabs(alphas) < 10e-5, 0, alphas)

    return alphas
def l1regls(A, b, gamma): 
    """ 
        minimize  0.5 * ||A*x-b||_2^2  + gamma*sum_k |x_k| 

    with complex data. 
    """ 

    A=matrix(A)
    b=matrix(b)
    m, n = A.size 

    # Solve as 
    # 
    #     minimize  0.5 * || AA*u - bb ||_2^2 + gamma* sum(t) 
    #     subject   || (u[k], u[k+n]) ||_2 <= t[k], k = 0, ..., n-1. 
    # 
    # with real data and u = ( Re(x), Im(x) ). 

    AA = matrix([ [A.real(), A.imag()], [-A.imag(), A.real()] ]) 
    bb = matrix([ b.real(), b.imag() ]) 
    
    # P = [AA'*AA, 0; 0, 0] 
    P = matrix(0.0, (3*n, 3*n)) 
    P[0:2*n,0:2*n]=AA.T*AA

    # q = [-AA'*bb; gamma*ones] 
    q = matrix([-AA.T * bb, matrix(gamma, (n,1))]) 
    
    # n second order cone constraints || (u[k], u[k+n]) ||_2 <= t[k] 
    I = matrix(0.0, (n,n)) 
    I[::n+1] = -1.0 
    G = matrix(0.0, (3*n, 3*n)) 
    G[1::3, :n] = I 
    G[2::3, n:2*n] = I 
    G[::3, -n:] = I 
    
    
    
    def Gfun(x, y, alpha = 1.0, beta = 0.0, trans = 'N'):
        gx=matrix(0.0,x.size)        
        if trans=='N':
            gx[1::3,:]=-x[  :  n,:]
            gx[2::3,:]=-x[ n:2*n,:]
            gx[ ::3,:]=-x[-n:   ,:]
        elif trans=='T':
            gx[  :  n,:]=-x[1::3,:]
            gx[ n:2*n,:]=-x[2::3,:]
            gx[-n:   ,:]=-x[ ::3,:]
        y[:,:]=alpha*gx+beta*y
        
    h = matrix(0.0, (3*n, 1)) 

    dims = {'l': 0, 'q': n*[3], 's': []} 
    factor=mykktchol(P)
    sol = solvers.coneqp(P, q, Gfun, h, dims,kktsolver=factor) 
    
    return sol['x'][:n] + 1j*sol['x'][n:2*n] 
Example #36
0
 def classifier(Y, soft = False):
     M = Y.size[0]
     x = matrix(b, (M,1))
     #print('b ( = w_0)')
     #print(b)
     #print('about to print long vector?')
     blas.gemv(Y, w, x, beta = 1.0)
     if soft: return x
     else:    return matrix([ 2*(xk > 0.0) - 1 for xk in x ])
Example #37
0
        def classifier(Y, soft=False):
            M = Y.size[0]
            # K = Y*X' / sigma
            K = matrix(0.0, (M, Nr))
            blas.gemm(Y, Xr, K, transB='T', alpha=1.0 / sigma)

            x = K**degree * zr + b
            if soft: return x
            else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
Example #38
0
def maximize_fapx_cvxopt( node, problem, scoop=False):
    '''
    Finds the value of y solving maximize sum(fapx(x)) subject to:
                       constr
                       l <= x <= u
    fapx is calculated by approximating the functions given in fs
    as the concave envelope of the function that is tight, 
    for each coordinate i, at the points in tight[i]
    fs should be a list of tuples containing the function and its derivative
    corresponding to each coordinate of the vector x.
  
    Returns a dict containing the optimal variable y as a list, 
    the optimal value of the approximate objective,
    and the value of sum(f(x)) at x.
    
    scoop = True optionally dumps all problem parameters into a file
    which can be parsed and solved using the scoop second order cone 
    modeling language
    '''
    n = len(node.l);
    l = matrix(node.l); u = matrix(node.u)

    x = problem.variable
    constr = problem.constr
        
    # add box constraints
    box = [x[i]<=u[i] for i in xrange(n)] + [x[i]>=l[i] for i in xrange(n)]

    # find approximation to concave envelope of each function
    (fapx,slopes,offsets,fapxs) = utilities.get_fapx(node.tight,problem.fs,l,u,y=x)
    
    if problem.check_z:
        utilities.check_z(problem,node,fapx,x)
        
    if scoop:
        utilities.scoop(p,node,slopes,offsets)
    
    obj = sum( fapx )
    o = op(obj,constr + box)
    try:
        o.solve(solver = 'glpk')
    except:
        o.solve()
    if not o.status == 'optimal':
        if o.status == 'unknown':
            raise ImportError('Unable to solve subproblem. Please try again after installing cvxopt with glpk binding.')
        else:
            # This node is dead, since the problem is infeasible
            return False
    else:
        # find the difference between the fapx and f for each coordinate i
        fi = numpy.array([problem.fs[i][0](x.value[i]) for i in range(n)])
        fapxi = numpy.array([list(-fun.value())[0] for fun in fapx])
        #if verbose: print 'fi',fi,'fapxi',fapxi
        maxdiff_index = numpy.argmax( fapxi - fi )
        results = {'x': list(x.value), 'fapx': -list(obj.value())[0], 'f': float(sum(fi)), 'maxdiff_index': maxdiff_index}
        return results
Example #39
0
def robustLS_toep(q, r, delta=None, seed=0):
    """
    Random data generator for matrix norm minimization SDP.
    
    Al,b = robustLS_toep(q,r,delta=0.1,seed=0])
    
    PURPOSE
    Generates random data for a robust least squares problem
    with Toeplitz structure:
     
      minimize e_wc(x)^2
    
    where
    
      e_wc(x)=sup_{norm(u)<=1} norm((Ab+A1*u1+...+Ar*ur)*x - b),
    
      Ab,A1,...,Ar \in \reals^{p \times q}        
    
    The parameter r determines the number of nonzero diagonals
    in Ab (1 <= r <= p).

    ARGUMENTS   
    q         integer
    
    r         integer

    delta     float
    
    RETURNS
    Al        list of CVXOPT matrices, [Ab,A1,...,Ar]

    b         CVXOPT matrix

    """
    setseed(seed)

    p = q + r - 1
    Alist = [
        sparse(
            misc.toeplitz(
                matrix([normal(r, 1), matrix(0., (p - r, 1))], (p, 1)),
                matrix(0., (q, 1))))
    ]
    x = normal(q, 1)
    b = normal(p, 1, std=0.1)
    b += Alist[0] * x

    if delta is None:
        delta = 1. / r

    for i in xrange(r):
        Alist.append(
            spmatrix(delta, xrange(i, min(p, q + i)), xrange(min(q, p - i)),
                     (p, q)))

    return robustLS_SDP(Alist, b)
Example #40
0
def train(training_data: List[Tuple[float, float, float]],
          kernel: Callable = linear_kernel,
          slack: bool = False,
          C: float = None) -> Callable:
    if slack:
        assert C

    X = [(e[0], e[1]) for e in training_data]
    T = [e[2] for e in training_data]
    N = len(training_data)

    # build P
    P = numpy.empty(shape=(N, N))
    for i in range(N):
        for j in range(N):
            P[i][j] = T[i] * T[j] * kernel(X[i], X[j])

    # build q
    q = numpy.ones(N) * -1

    # build h
    h = None
    if slack:
        h = numpy.empty(2 * N)
        for i in range(N):
            h[i] = 0
        for i in range(N, 2 * N):
            h[i] = C
    else:
        h = numpy.zeros(N)

    # build G
    G = numpy.zeros(shape=(2 * N, N)) if slack else numpy.zeros(shape=(N, N))
    for i, j in zip(range(N), range(N)):
        G[i][j] = -1

    if slack:
        for i, j in zip(range(N, 2 * N), range(N)):
            G[i][j] = 1

    r = qp(matrix(P), matrix(q), matrix(G), matrix(h))
    alpha = list(r['x'])

    # get all non-zero alphas and corresponding points and classes
    alpha_nz = []
    for i in range(len(alpha)):
        if alpha[i] >= FLOAT_THRESHOLD:
            alpha_nz.append((alpha[i], X[i], T[i]))

    def indicator(nX):
        sum = 0.0
        for (a, x, t) in alpha_nz:
            sum += a * t * kernel(nX, x)
        return sum

    return indicator
Example #41
0
def numerical_gradient(x_k, func, epsilon, *args):
    n_params, one = x_k.size
    assert one==1
    
    ei = cvx.matrix(0.0, x_k.size)
    grad = cvx.matrix(0.0, x_k.size)
    for i in range(n_params):
        ei[i]   = epsilon
        grad[i] = ( func(x_k+ei, *args) - func(x_k-ei, *args) ) / (2*epsilon)
        ei[i]   = 0.0
    return grad.T
Example #42
0
def generateAlpha(P,q,h,G):
  threshold = 0.00001
  r = qp(matrix(P), matrix(q), matrix(G, (2*len(data), len(data)), 'd'), matrix(h))
  if(r['status'] != 'optimal'):
	print "I failed =("
	exit(-1)
  alpha = list(r['x'])
  for a in xrange(len(alpha)):
    if abs(alpha[a]) < threshold:
      alpha[a] = 0.0
  return alpha
Example #43
0
 def update_h(self):
     def updatesingleH(i):
         # optimize alpha using qp solver from cvxopt
         FA = base.matrix(np.float64(np.dot(-self.W.T, self.data[:,i])))
         al = solvers.qp(HA, FA, INQa, INQb)
         self.H[:,i] = np.array(al['x']).reshape((1,-1))
                                                             
     # float64 required for cvxopt
     HA = base.matrix(np.float64(np.dot(self.W.T, self.W)))            
     INQa = base.matrix(-np.eye(self._num_bases))
     INQb = base.matrix(0.0, (self._num_bases,1))            
 
     map(updatesingleH, xrange(self._num_samples))                        
Example #44
0
    def update_w(self):
        def updatesingleW(i):
        # optimize alpha using qp solver from cvxopt
            FA = base.matrix(np.float64(np.dot(-self.H, self.data[i,:].T)))
            al = solvers.qp(HA, FA, INQa, INQb)                
            self.W[i,:] = np.array(al['x']).reshape((1,-1))            
                                
        # float64 required for cvxopt
        HA = base.matrix(np.float64(np.dot(self.H, self.H.T)))                    
        INQa = base.matrix(-np.eye(self._num_bases))
        INQb = base.matrix(0.0, (self._num_bases,1))            

        map(updatesingleW, xrange(self._data_dimension))
Example #45
0
File: base.py Project: cvxopt/smcp
    def _gen_randsdp(self,V,m,d,seed):
        """
        Random data generator
        """
        setseed(seed)
        n = self._n
        V = chompack.tril(V)
        N = len(V)
        I = V.I; J = V.J
        Il = misc.sub2ind((n,n),I,J)
        Id = matrix([i for i in range(len(Il)) if I[i]==J[i]])

        # generate random y with norm 1
        y0 = normal(m,1)
        y0 /= blas.nrm2(y0)

        # generate random S0, X0
        S0 = mk_rand(V,cone='posdef',seed=seed)
        X0 = mk_rand(V,cone='completable',seed=seed)

        # generate random A1,...,Am
        if type(d) is float:
            nz = min(max(1, int(round(d*N))), N)
            A = sparse([[spmatrix(normal(N,1),Il,[0 for i in range(N)],(n**2,1))],
                        [spmatrix(normal(nz*m,1),
                                  [i for j in range(m) for i in random.sample(Il,nz)],
                                  [j for j in range(m) for i in range(nz)],(n**2,m))]])
        elif type(d) is list:
            if len(d) == m:
                nz = [min(max(1, int(round(v*N))), N) for v in d]
                nnz = sum(nz)
                A = sparse([[spmatrix(normal(N,1),Il,[0 for i in range(N)],(n**2,1))],
                            [spmatrix(normal(nnz,1),
                                      [i for j in range(m) for i in random.sample(Il,nz[j])],
                                      [j for j in range(m) for i in range(nz[j])],(n**2,m))]])
        else: raise TypeError

        # compute A0
        u = +S0.V
        for k in range(m):
            base.gemv(A[:,k+1][Il],matrix(y0[k]),u,beta=1.0,trans='N')
        A[Il,0] = u
        self._A = A

        # compute b
        X0[Il[Id]] *= 0.5
        self._b = matrix(0.,(m,1))
        u = matrix(0.)
        for k in range(m):
            base.gemv(A[:,k+1][Il],X0.V,u,trans='T',alpha=2.0)
            self._b[k] = u
Example #46
0
def svmMain(indata, kernel, figure, par1, par2, C):
	print "Defining the variables needed for vecktor machine"
	N = len(indata)
	P = matrix(buildPMatrix(indata, kernel, par1, par2))
	#G = spmatrix(-1.0, range(2*N), range(N))
	G = np.zeros((2*N,N))
	for i in range(0,N):
		G[i,i] = -1.0
	for i in range(N+1,2*N):
		G[i,i-N] = 1.0
	#print G
	G = matrix(G)
	q = matrix(np.linspace(-1,-1,N))
	h = matrix(np.zeros(2*N))
	for i in range(N, (2*N)-1):
		h[i] = C
	#print h

	print "Calling qp, solving the linear equation"
	r = qp(P, q, G, h)
	alpha = list(r['x'])

	print "Finding the non zero alpha that minimizes the linear equation"
	indicationMapping = []
	epsylon = 0.00001
	for i in range(N):
		if alpha[i] > epsylon:
			indicationMapping.append([alpha[i],data[i]])

	print "Found ", len(indicationMapping), " non zero alphas"

	print "Printing data!"
	figure.hold(True)
	figure.plot([p[0] for p in classA],
		[p[1] for p in classA],
		'bo')
	figure.plot([p[0] for p in classB],
		[p[1] for p in classB],
		'ro')


	print "Setting up massive grid"
	xrange = np.arange(-4, 4, 0.05)
	yrange = np.arange(-4, 4, 0.05)

	grid = matrix([[indicator(x, y, indicationMapping, kernel, par1, par2)
		for y in yrange]
		for x in xrange])

	figure.contour(xrange, yrange, grid, (-1.0, 0.0, 1.0),
	colors=('red', 'black', 'blue'), linewidths=(1, 3, 1))
Example #47
0
    def solve_optimization(self, train_x, labels):
        Q = self.build_Q(train_x, labels)
        p = [-1.0] * train_x.shape[0]

        if self.with_slack:
            h = [0.0] * train_x.shape[0] + [self.C] * train_x.shape[0]
            G = np.concatenate((np.identity(train_x.shape[0]) * -1,
                                np.identity(train_x.shape[0])))
        else:
            h = [0.0] * train_x.shape[0]
            G = np.identity(train_x.shape[0]) * -1

        optimized = qp(matrix(Q), matrix(p), matrix(G), matrix(h))
        if optimized['status'] == 'optimal':
            alphas = list(optimized['x'])

            self.support_vector = [(alpha, train_x[i,:],labels[i])
                                   for i,alpha in enumerate(alphas)
                                   if alpha>self.epsilon]
            return True
        else:
            print "No valid separating hyperplane found"
            print "Find a best hyperplane for the mixture data..."
            h = [0.0] * train_x.shape[0] + [self.C] * train_x.shape[0]
            G = np.concatenate((np.identity(train_x.shape[0]) * -1,
                                np.identity(train_x.shape[0])))
            optimized = qp(matrix(Q), matrix(p), matrix(G), matrix(h))
            alphas = list(optimized['x'])

            self.support_vector = [(alpha, train_x[i,:],labels[i])
                                   for i,alpha in enumerate(alphas)
                                   if alpha>self.epsilon]
            return False
Example #48
0
File: base.py Project: cvxopt/smcp
def robustLS_toep(q,r,delta=None,seed=0):
    """
    Random data generator for matrix norm minimization SDP.

    Al,b = robustLS_toep(q,r,delta=0.1,seed=0])

    PURPOSE
    Generates random data for a robust least squares problem
    with Toeplitz structure:

      minimize e_wc(x)^2

    where

      e_wc(x)=sup_{norm(u)<=1} norm((Ab+A1*u1+...+Ar*ur)*x - b),

      Ab,A1,...,Ar \in \reals^{p \times q}

    The parameter r determines the number of nonzero diagonals
    in Ab (1 <= r <= p).

    ARGUMENTS
    q         integer

    r         integer

    delta     float

    RETURNS
    Al        list of CVXOPT matrices, [Ab,A1,...,Ar]

    b         CVXOPT matrix

    """
    setseed(seed)

    p = q+r-1
    Alist = [sparse(misc.toeplitz(matrix([normal(r,1),matrix(0.,(p-r,1))],(p,1)),matrix(0.,(q,1))))]
    x = normal(q,1)
    b = normal(p,1,std=0.1)
    b += Alist[0]*x

    if delta is None:
        delta = 1./r

    for i in range(r):
        Alist.append(spmatrix(delta,range(i,min(p,q+i)),range(min(q,p-i)),(p,q)))

    return robustLS_SDP(Alist, b)
def CVXOPT_SOCP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_SOCP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol    
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)
    #FIXME: if problem is search for MAXIMUM, not MINIMUM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    f = copy(p.f).reshape(-1,1)

    # CVXOPT has some problems with x0 so currently I decided to avoid using the one
    
    Gq, hq = [], []
    C, d, q, s = p.C, p.d, p.q, p.s
    for i in range(len(q)):
        Gq.append(Matrix(Vstack((-atleast_1d(q[i]),-atleast_1d(C[i]) if not isspmatrix(C[i]) else C[i]))))
        hq.append(matrix(hstack((atleast_1d(s[i]), atleast_1d(d[i]))), tc='d'))
    sol = cvxopt_solvers.socp(Matrix(p.f), Gl=Matrix(p.A), hl = Matrix(p.b), Gq=Gq, hq=hq, A=Matrix(p.Aeq), b=Matrix(p.beq), solver=solverName)
    p.msg = sol['status']
    if p.msg == 'optimal' :  p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
    else: p.istop = -100
    if sol['x'] is not None:
        p.xf = asarray(sol['x']).flatten()
        p.ff = sum(p.dotmult(p.f, p.xf))
        
    else:
        p.ff = nan
        p.xf = nan*ones(p.n)
Example #50
0
def build_p_matrix(points, classes, K):
	N = len(points)
	P = matrix(0.0, (N, N))
	for i in range(0, N):
		for j in range(0, N):
			P[i, j] = classes[i] * classes[j] * K(points[i], points[j])
	return P
Example #51
0
	def build_inv_weight(w, exact_mask=None):
		# as it is this function now if, for a certain k, w[k,k] is zero and 
		# exact_mask[k] is true at the same time, the first take precedence,
		# that is the corresponing parameter will not be matched at all. 
		I = []
		J = []
		E = []
		kk = 0
		for k in range(0,w.size[0]):
			 if not w[k,k] == 0:
			 	I.append(kk)
				J.append(k)
				E.append(1.0)
				kk += 1
			 if exact_mask is not None and w[k,k] == 0:
				del exact_mask[kk]
		I.append(kk-1)
		J.append(k)
		E.append(0.0)
		M = cb.spmatrix(E, I, J)
		w_r = M*w*M.T
		i_w_r = cb.matrix(linalg.inv(w_r))
		if exact_mask is None:
			return i_w_r, M
		for k in range(0,len(exact_mask)):
			if exact_mask[k]:
				i_w_r[k,:] = 0.0
				i_w_r[:,k] = 0.0
		return i_w_r, M
Example #52
0
def DictToList(d):
    i = 0
    r = []
    while i in d.keys():
        r.append(matrix(d[i], tc = 'd'))
        i += 1
    return r
Example #53
0
def contour(list):
    xrange = numpy.arange(-5, 5, 0.05)
    yrange = numpy.arange(-5, 5, 0.05)

    grid = matrix([[indicator(x, y, list) for y in yrange] for x in xrange])
    pylab.contour(xrange, yrange, grid, (-1.0, 0.0, 1.0), colors=('red', 'black', 'blue'), linewidths=(1, 3, 1))
    pylab.show()
def uniform(nrows, ncols=1, a=0, b=1):
    '''
    Randomly generates a matrix with uniformly distributed entries.
    
    uniform(nrows, ncols=1, a=0, b=1)

    PURPOSE
    Returns a matrix with typecode 'd' and size nrows by ncols, with
    its entries randomly generated from a uniform distribution on the
    interval (a,b).

    ARGUMENTS
    nrows     number of rows

    ncols     number of columns

    a         lower bound

    b         upper bound
    '''

    try:    
        from cvxopt import gsl
    except:
        from cvxopt.base import matrix
        from random import uniform
        return matrix([uniform(a, b) for k in range(nrows*ncols)],
                      (nrows,ncols), 'd' )

    return gsl.uniform(nrows, ncols, a, b)
def normal(nrows, ncols=1, mean=0.0, std=1.0):
    '''
    Randomly generates a matrix with normally distributed entries.

    normal(nrows, ncols=1, mean=0, std=1)
  
    PURPOSE
    Returns a matrix with typecode 'd' and size nrows by ncols, with
    its entries randomly generated from a normal distribution with mean
    m and standard deviation std.

    ARGUMENTS
    nrows     number of rows

    ncols     number of columns

    mean      approximate mean of the distribution
    std       standard deviation of the distribution
    '''

    try:    
        from cvxopt import gsl
    except:
        from cvxopt.base import matrix
        from random import gauss
        return matrix([gauss(mean, std) for k in range(nrows*ncols)],
                      (nrows,ncols), 'd' )
        
    return gsl.normal(nrows, ncols, mean, std)
Example #56
0
def main():
    print("Running main method.")
    print("")
    
    # Generate the datapoints and plot them.
    classA, classB = gen_datapoints_helper()

    kernels = [LINEAR, POLYNOMIAL, RADIAL_BASIS, SIGMOID]
    for k in kernels:
        pylab.figure()
        pylab.title(kernel_name(k))
        pylab.plot([p[0] for p in classA],
                    [p[1] for p in classA],
                    'bo')
        pylab.plot([p[0] for p in classB],
                    [p[1] for p in classB],
                    'ro')

        # Add the sets together and shuffle them around.
        datapoints = gen_datapoints_from_classes(classA, classB)

        t= train(datapoints, k)

        # Plot the decision boundaries.
        xr=numpy.arange(-4, 4, 0.05)
        yr=numpy.arange(-4, 4, 0.05)
        grid=matrix([[indicator(t, x, y, k) for y in yr] for x in xr])
        pylab.contour(xr, yr, grid, (-1.0, 0.0, 1.0), colors=('red', 'black', 'blue'), linewidths=(1, 3, 1))

    # Now that we are done we show the ploted datapoints and the decision
    # boundary.
    pylab.show()
Example #57
0
def buildPMatrix(datapoints, kernel, par1, par2):
	tempMatrix = matrix(0.1,(len(datapoints),len(datapoints)))
	for i in range(len(datapoints)):
		for j in range(len(datapoints)):
			#print "solving P, point (x, y) (",i,", ",j,")"
			tempMatrix[i,j] = datapoints[i][2]*datapoints[j][2]*kernel(
				[datapoints[i][0], datapoints[i][1]], [datapoints[j][0], datapoints[j][1]], par1, par2)
	return tempMatrix
Example #58
0
def plot_decision_boundary(machine):
    x_range = numpy.arange(-4, 4, 0.05)
    y_range = numpy.arange(-4, 4, 0.05)
    grid = matrix([[machine.indicator((x, y)) for y in y_range] for x in x_range])
    pylab.contour(x_range, y_range, grid,
                  (-1.0, 0.0, 1.0),
                  colors=('red', 'black', 'blue'),
                  linewidths=(1, 3, 1))