Beispiel #1
0
def denseSolve(A,b):
    ''' solves an Ax = b matrix system with gesv'''
    if isinstance(A,np.ndarray):
        aLocal = cvxopt.matrix(A)
        bLocal = cvxopt.matrix(b)
        lapack.gesv(aLocal,bLocal)
        return np.array(bLocal).flatten()
    else:
        return linsolve(A,b)
Beispiel #2
0
def L1Regression(A,b,alpha,trace=False):
   """
    solves
    minimize_{x} ||A*x-b||^2_2 + alpha*||x||_1
   """
   (n,m)=A.size
   Id = spmatrix(1.0, range(m),range(m)) ##unit matrix
   G = matrix([A.T,-A.T])
   h = matrix(alpha, (2*m,1))
   solvers.options['show_progress'] = trace
   sol= solvers.qp(Id, b, G, h)
   z= sol['x']
   x= A.trans()*(b-z)
   A_tA = A.trans()*A
   lapack.gesv(A_tA, x)
   return x 
Beispiel #3
0
    def find_ellipsoid(self):
    
        # return None if not enough data is available
        if(self.pa.size[1] < 3 or self.pb.size[1] < 3):
            return None
        
        # homogenize coordinates
        pah = self.homogenize(self.pa)
        pbh = self.homogenize(self.pb)
        dim = pah.size[0]
        num_pah = pah.size[1]
        num_pbh = pbh.size[1]
        
        # get the c vector
        num_vars = 1+(dim-1)*(dim-1)+2*num_pah+dim*dim
        c = matrix([0.0]*num_vars, (num_vars,1))
        c[0,0] = -self.c1
        
        for i in xrange(dim-1):
            for j in xrange(dim-1):
                if(i == j):
                    c[1+i*(dim-1)+j,0] = self.c2
        for i in xrange(num_pah):
            c[1+(dim-1)*(dim-1)+i,0] = self.c3
        for i in xrange(num_pah):
            c[1+(dim-1)*(dim-1)+num_pah+i,0] = 0.0
        for i in xrange(dim*dim):
            c[1+(dim-1)*(dim-1)+num_pah+num_pah+i,0] = 0.0

        # get Gl and hl
        # separation constraint
        Gl_t = []
        for i in xrange(num_pah):
            row = [0.0]*(1+(dim-1)*(dim-1)+num_pah)+[0.0]*num_pah
            row[(1+(dim-1)*(dim-1)+num_pah)+i] = -1.0
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(pah[j,i]*pah[k,i])
            Gl_t.append(row)
        for i in xrange(num_pbh):
            row = [1.0]+[0.0]*((dim-1)*(dim-1)+2*num_pah)
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(-1.0*pbh[j,i]*pbh[k,i])
            Gl_t.append(row)
        # l1 norm constraint
        for i in xrange(num_pah):
            row = [0.0]*num_vars
            row[1+(dim-1)*(dim-1)+i] = -1.0
            row[1+(dim-1)*(dim-1)+num_pah+i] = -1.0
            Gl_t.append(row)
        for i in xrange(num_pah):
            row = [0.0]*num_vars
            row[1+(dim-1)*(dim-1)+i] = -1.0
            row[1+(dim-1)*(dim-1)+num_pah+i] = 1.0
            Gl_t.append(row)
        # construct Gl and hl
        Gl = matrix(Gl_t).trans()
        col = [0.0]*(num_pah+num_pbh)+[-1.0]*num_pah+[1.0]*num_pah
        hl = matrix(col,(3*num_pah+num_pbh,1))
        
        # get Gs and hs
        # positive semidefinite constraint
        # E must be positive semidefinite
        Gs = []
        for i in xrange(1+(dim-1)*(dim-1)+2*num_pah):
            Gs.append([0.0]*(dim*dim))
        for i in xrange(dim*dim):
            v = [0.0]*(dim*dim)
            rpos = int(math.floor(float(i)/float(dim)))
            cpos = i % dim
            if(rpos == cpos):
                v[i] = -1.0
            else:
                v[i] = -0.5
                v[cpos*dim+rpos] = -0.5
            Gs.append(v)
        Gs = [matrix(Gs)]
        hs = [matrix([0.0]*(dim*dim),(dim,dim))]
        
        # block matrix must be positive semidefinite
        Gs2 = []
        Gs2.append([0.0]*(((dim-1)*2)*((dim-1)*2)))
        for i in xrange((dim-1)*(dim-1)):
            v = [0.0]*(((dim-1)*2)*((dim-1)*2))
            rpos = int(math.floor(float(i)/float(dim-1)))
            cpos = i % (dim-1)
            if(rpos == cpos):
                v[(rpos+dim-1)*(dim-1)*2+(dim-1)+cpos] = -1.0
            else:
                v[(rpos+dim-1)*(dim-1)*2+(dim-1)+cpos] = -0.5
                v[(cpos+dim-1)*(dim-1)*2+(dim-1)+rpos] = -0.5
            Gs2.append(v)
            
        for i in xrange(2*num_pah):
            Gs2.append([0.0]*(((dim-1)*2)*((dim-1)*2)))
        for i in xrange(dim*dim):
            v = [0.0]*(((dim-1)*2)*((dim-1)*2))
            rpos = int(math.floor(float(i)/float(dim)))
            cpos = i % dim
            if(rpos == 0 or cpos == 0):
                Gs2.append(v)
            else:
                rpos = rpos-1
                cpos = cpos-1
                if(rpos == cpos):
                    v[rpos*(dim-1)*2+cpos] = -1.0
                else:
                    v[rpos*(dim-1)*2+cpos] = -0.5
                    v[cpos*(dim-1)*2+rpos] = -0.5
                Gs2.append(v)
            
        Gs.append(matrix(Gs2))
        zero_block = matrix([0.0]*((dim-1)*(dim-1)),((dim-1), (dim-1)))
        eye_block = spmatrix(1.0, range(dim-1), range(dim-1))
        hs2 = matrix([[zero_block, eye_block], [eye_block, zero_block]])
        hs.append(hs2)
        
        # get A and b
        # symmetry constraint
        A_t = []
        for i in xrange(dim):
            for j in xrange(i, dim):
                if(i != j):
                    v = [0.0]*(num_vars)
                    v[1+(dim-1)*(dim-1)+2*num_pah+i*dim+j] = 1.0
                    v[1+(dim-1)*(dim-1)+2*num_pah+j*dim+i] = -1.0
                    A_t.append(v)
        for i in xrange(dim-1):
            for j in xrange(i, dim-1):
                if(i != j):
                    v = [0.0]*(num_vars)
                    v[1+i*(dim-1)+j] = 1.0
                    v[1+j*(dim-1)+i] = -1.0
                    A_t.append(v)
        A = matrix(A_t).trans()
        b = matrix([0.0]*A.size[0],(A.size[0],1))
        
        # solve it
        passed = False
        ntime = 10
        while(passed == False and ntime > 0):
            try:
                sol = solvers.sdp(c, Gl, hl, Gs, hs, A, b)
                passed = True
            except ZeroDivisionError:
                time.sleep(0.001)
                ntime = ntime-1
            except Exception:
                time.sleep(0.001)
                ntime = ntime-1
        if(passed == False):
            tmp_nrow = self.pa.size[0]
            tmp_c = mean_ps(self.pa).trans()
            tmp_E = matrix(spmatrix(1.0, range(tmp_nrow), range(tmp_nrow)))
            tmp_rho = 1.0
            return {'c':tmp_c, 'E':tmp_E, 'rho':tmp_rho}
        
        # parse out solution
        x = sol['x']
        k = x[0]
        E_hat = matrix(x[1+(dim-1)*(dim-1)+2*num_pah:], (dim,dim))
        F = E_hat[1:,1:]
        v = E_hat[1:,0]
        s = E_hat[0,0]
        ipiv = matrix(0, (dim-1,1))
        gesv(-F, v, ipiv)
        c = v
        btm = 1-(s-c.trans()*F*c)+0.00000001
        for i in xrange(F.size[0]):
            for j in xrange(F.size[1]):
                F[i,j] = F[i,j]/btm
        E = F
        rho = k
        
        # function return
        return {'c':c, 'E':E, 'rho':rho}
Beispiel #4
0
__author__ = 'stefan'
from cvxopt import matrix, normal
from cvxopt.lapack import gesv, getrs
n = 10
A = normal(n,n)
b = normal(n)
ipiv = matrix(0, (n,1))
x = +b
gesv(A, x, ipiv)               # x = A^{-1}*b
x2 = +b
getrs(A, ipiv, x2, trans='T')  # x2 = A^{-T}*b
x += x2
print(x)
	def solve_SAT2(self,cnf,number_of_variables,number_of_clauses):
		#Solves CNFSAT by a Polynomial Time Approximation scheme:
		#	- Encode each clause as a linear equation in n variables: missing variables and negated variables are 0, others are 1
		#	- Solve previous system of equations by least squares algorithm to fit a line
		#	- Variable value above 0.5 is set to 1 and less than 0.5 is set to 0
		#	- Rounded of assignment array satisfies the CNFSAT with high probability
		#Returns: a tuple with set of satisfying assignments
		satass=[]
		x=[]
		self.solve_SAT(cnf,number_of_variables,number_of_clauses)
		for clause in self.cnfparsed:
			equation=[]
			for n in xrange(number_of_variables):
				equation.append(0)
			#print "clause:",clause
			for literal in clause:
				if literal[0] != "!":
					equation[int(literal[1:])-1]=1
				else:
					equation[int(literal[2:])-1]=0
			self.equationsA.append(equation)
		for n in xrange(number_of_clauses):
			self.equationsB.append(1)
		a = np.array(self.equationsA)
                b = np.array(self.equationsB)
		init_guess = []
		for n in xrange(number_of_variables):
			init_guess.append(0.00000000001)
		initial_guess = np.array(init_guess)
		self.A=a
		self.B=b
		#print "a:",a
		#print "b:",b
                #print "a.shape:",a.shape
                #print "b.shape:",b.shape
		matrixa=matrix(a,tc='d')
		matrixb=matrix(b,tc='d')

		x=None
		if number_of_variables == number_of_clauses:
			if self.Algorithm=="lsqr()":
				#x = np.dot(np.linalg.inv(a),b)
				#x = gmres(a,b) 
				#x = lgmres(a,b) 
				#x = minres(a,b) 
				#x = bicg(a,b) 
				#x = cg(a,b) 
				#x = cgs(a,b) 
				#x = bicgstab(a,b)
                		x = lsqr(a,b,atol=0,btol=0,conlim=0,show=True)
			if self.Algorithm=="lapack()":
				try:
					x = gesv(matrixa,matrixb)
					#x = gels(matrixa,matrixb)
					#x = sysv(matrixa,matrixb)
					#x = getrs(matrixa,matrixb)
					x = [matrixb]
				except:
					print "Exception:",sys.exc_info()
					return None 
			if self.Algorithm=="l1regls()":
				l1x = l1regls(matrixa,matrixb)
				ass=[]
				x=[]
				for n in l1x:
					ass.append(n)
				x.append(ass)
			if self.Algorithm=="lsmr()":
                		x = lsmr(a,b,atol=0,btol=0,conlim=0,show=True,x0=initial_guess)
		else:
			if self.Algorithm=="solve()":
                		x = solve(a,b)
			if self.Algorithm=="lstsq()":
                		x = lstsq(a,b,lapack_driver='gelsy')
			if self.Algorithm=="lsqr()":
                		x = lsqr(a,b,atol=0,btol=0,conlim=0,show=True)
			if self.Algorithm=="lsmr()":
                		#x = lsmr(a,b,atol=0.1,btol=0.1,maxiter=5,conlim=10,show=True)
                		x = lsmr(a,b,atol=0,btol=0,conlim=0,show=True,x0=initial_guess)
			if self.Algorithm=="spsolve()":
				x = dsolve.spsolve(csc_matrix(a),b)
			if self.Algorithm=="pinv2()":
				x=[]
				#pseudoinverse_a=pinv(a)
				pseudoinverse_a=pinv2(a,check_finite=False)
				x.append(matmul(pseudoinverse_a,b))
			if self.Algorithm=="lsq_linear()":
				x = lsq_linear(a,b,lsq_solver='exact')
			if self.Algorithm=="lapack()":
				try:
					#x = gesv(matrixa,matrixb)
					x = gels(matrixa,matrixb)
					#x = sysv(matrixa,matrixb)
					#x = getrs(matrixa,matrixb)
					x = [matrixb]
				except:
					print "Exception:",sys.exc_info()
					return None 
			if self.Algorithm=="l1regls()":
				l1x = l1regls(matrixa,matrixb)
				ass=[]
				x=[]
				for n in l1x:
					ass.append(n)
				x.append(ass)

		print "solve_SAT2(): ",self.Algorithm,": x:",x
		if x is None:
			return None
		cnt=0
		binary_parity=0
		real_parity=0.0
		if rounding_threshold == "Randomized":
			randomized_rounding_threshold=float(random.randint(1,100000))/100000.0
		else:
			min_assignment=min(x[0])
			max_assignment=max(x[0])
			randomized_rounding_threshold=(min_assignment + max_assignment)/2
		print "randomized_rounding_threshold = ", randomized_rounding_threshold
		print "approximate assignment :",x[0]
		for e in x[0]:
			if e > randomized_rounding_threshold:
				satass.append(1)
				binary_parity += 1
			else:
				satass.append(0)
				binary_parity += 0
			real_parity += e
			cnt+=1
		print "solve_SAT2(): real_parity = ",real_parity
		print "solve_SAT2(): binary_parity = ",binary_parity
		return (satass,real_parity,binary_parity,x[0])
Beispiel #6
0
    def find_ellipsoid(self):
        
        # homogenize coordinates
        pah = self.homogenize(self.pa)
        pbh = self.homogenize(self.pb)
        dim = pah.size[0]
        num_pah = pah.size[1]
        num_pbh = pbh.size[1]
        
        # get the c vector
        c = matrix([-1.0]+[0.0]*(dim*dim), (1+dim*dim,1))
        
        # get Gl and hl
        # separation constraint
        Gl_t = []
        for i in xrange(num_pah):
            row = [0.0]
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(pah[j,i]*pah[k,i])
            Gl_t.append(row)
        for i in xrange(num_pbh):
            row = [1.0]
            for j in xrange(dim):
                for k in xrange(dim):
                    row.append(-1.0*pbh[j,i]*pbh[k,i])
            Gl_t.append(row)
        Gl = matrix(Gl_t).trans()
        hl = matrix([1.0]*num_pah+[0.0]*num_pbh,(num_pah+num_pbh,1))
        
        # get Gs and hs
        # positive semidefinite constraint
        Gs = []
        Gs.append([0.0]*(dim*dim))
        for i in xrange(dim*dim):
            v = [0.0]*(dim*dim)
            rpos = int(math.floor(float(i)/float(dim)))
            cpos = i % dim
            if(rpos == cpos):
                v[i] = -1.0
            else:
                v[i] = -0.5
                v[cpos*dim+rpos] = -0.5
            Gs.append(v)
        Gs = [matrix(Gs)]
        hs = [matrix([0.0]*(dim*dim),(dim,dim))]
        
        # get A and b
        # symmetry constraint
        A_t = []
        for i in xrange(dim):
            for j in xrange(i, dim):
                if(i != j):
                    v = [0.0]*(1+dim*dim)
                    v[1+i*dim+j] = 1.0
                    v[1+j*dim+i] = -1.0
                    A_t.append(v)
        A = matrix(A_t).trans()
        b = matrix([0.0]*A.size[0],(A.size[0],1))
        
        # solve it
        passed = False
        ntime = 10
        while(passed == False and ntime > 0):
            try:
                sol = solvers.sdp(c, Gl, hl, Gs, hs, A, b)
                passed = True
            except ZeroDivisionError:
                time.sleep(0.001)
                ntime = ntime-1
            except Exception:
                time.sleep(0.001)
                ntime = ntime-1
        if(passed == False):
            return None

        # parse out solution
        x = sol['x']
        k = x[0]
        E_hat = matrix(x[1:], (dim,dim))
        F = E_hat[1:,1:]
        v = E_hat[1:,0]
        s = E_hat[0,0]
        ipiv = matrix(0, (dim-1,1))
        gesv(-F, v, ipiv)
        c = v
        btm = 1-(s-c.trans()*F*c)+0.00000001
        for i in xrange(F.size[0]):
            for j in xrange(F.size[1]):
                F[i,j] = F[i,j]/btm
        E = F
        rho = k
        
        # function return
        return {'c':c, 'E':E, 'rho':rho}