示例#1
0
文件: sdpclstr.py 项目: cqql/tum-cs
def sdp(P, k):
    """Solve the SDP relaxation.

    Parameters
    ----------
    P : np.array
        m*N matrix of N m-dimensional points stacked columnwise
    k : int
        Number of clusters

    Returns
    -------
    The SDP minimizer S_D
    """
    N = P.shape[1]

    # Compute pairwise distances
    D = scipy.spatial.distance.pdist(P.T)
    D = scipy.spatial.distance.squareform(D)

    # Create the semi-definite program
    sdp = pic.Problem()
    Dparam = pic.new_param("D", D)
    X = sdp.add_variable("X", (N, N), vtype="symmetric")
    sdp.add_constraint(pic.trace(X) == k)
    sdp.add_constraint(X * "|1|({},1)".format(N) == 1)
    sdp.add_constraint(X > 0)
    sdp.add_constraint(X >> 0)
    sdp.minimize(Dparam | X)

    # Extract the SDP solution
    X_D = np.array(X.value)

    return X_D
示例#2
0
      k = k - (r+1)
      r = r + 1

maxval = 0
for t in range(T):
  A = np.zeros(shape=(N,N))
  I = np.eye(N)
  for k in range(m):
    i,j = unpack(k)
    u = 2 * randint(0,1) - 1
    A[i,j] = u
    A[j,i] = u
  print ''
  print 'A:'
  print A
  A = pic.new_param('A', A)
  #J = pic.new_param('J', np.ones(shape=(N,N)))
  sdp = pic.Problem()
  P = sdp.add_variable('P',(N,N),'symmetric')
  sdp.add_constraint(P>>0)
  sdp.add_constraint(P<<I)
  sdp.set_objective('max', A | P)
  print sdp
  sdp.solve(verbose = 1, maxit=50)
  val = sdp.obj_value()
  print 'value: {0}'.format(val)

  solution = P.value
  np.set_printoptions(precision=3,threshold='nan',linewidth=1000,suppress=True)
  print 'solution:'
  print solution
示例#3
0
文件: mincut.py 项目: gsagnol/picos
for i,e in enumerate(G.edges()):
        c[e]=((-2)**i)%17 #an arbitrary sequence of numbers


#-------------#
#   min cut   #
#-------------#

mincut=pic.Problem()

#source and sink nodes
s=16
t=10

#convert the capacities as a picos expression
cc=pic.new_param('c',c)

#cut variable
d={}
for e in G.edges():
        d[e]=mincut.add_variable('d[{0}]'.format(e),1)
        
#potentials
p=mincut.add_variable('p',N)

#potential inequality
mincut.add_list_of_constraints(
        [d[i,j] > p[i]-p[j]
        for (i,j) in G.edges()],        #list of constraints
        ['i','j'],'edges')              #indices and set they belong to
示例#4
0
M = 3

X = sdp.add_variable('X',(2*(M+N)+1,2*(M+N)+1),'symmetric')
sdp.add_constraint(X>>0)
sdp.add_constraint(X[0,0]<=1)
sdp.add_constraint(X>=0)
for i in range(N+M):
  sdp.add_constraint(X[i+1,i+1]==X[i+1,0])
  sdp.add_constraint(X[i+1,i+1]==X[i+1,i+N+M+1])
  sdp.add_constraint(X[i+N+M+1,i+N+M+1]==X[i+N+M+1,0])
for i in range(M):
  sdp.add_constraint(X[i+N+M+1,i+N+M+1]==1)
  for j in range(M,N+M):
    sdp.add_constraint(X[i+1,j+N+M+1]==0)
diagC = np.array([0] + [1] * M + [0]*(2*N+M))
C = pic.new_param('C',np.diag(diagC))
diagD = np.array([0] + [0] * M + [1] * N + [0] * (N+M))
D = pic.new_param('D',np.diag(diagD))
sdp.add_constraint(D|X >= 2);
sdp.set_objective('max', C | X)

print sdp
sdp.solve(verbose = 1, maxit=50)

print 'value: {0}'.format(sdp.obj_value())

solution = X.value

np.set_printoptions(precision=3,threshold='nan',linewidth=1000,suppress=True)
print 'solution:'
print np.array(solution)
示例#5
0
                [0,3,2,0,0],
                [1,0,0,2,0]])
  ]
  
c = cvx.matrix([1,2,3,4,5])

#create the problems

#--------------------------------------#
#         D-optimal design             #
#--------------------------------------#
prob_D = pic.Problem()
AA=[cvx.sparse(a,tc='d') for a in A]
s=len(AA)
m=AA[0].size[0]
AA=pic.new_param('A',AA)
mm=pic.new_param('m',m)
L=prob_D.add_variable('L',(m,m))
V=[prob_D.add_variable('V['+str(i)+']',AA[i].T.size) for i in range(s)]
w=prob_D.add_variable('w',s)
u={}
for k in ['01','23','4.','0123','4...','01234']:
        u[k] = prob_D.add_variable('u['+k+']',1)
prob_D.add_constraint(
                pic.sum([AA[i]*V[i]
                    for i in range(s)],'i','[s]')
                 == L)
#L lower inferior
prob_D.add_list_of_constraints( [L[i,j] == 0
                                for i in range(m)
                                for j in range(i+1,m)],['i','j'],'upper triangle')
示例#6
0
print minus

X = sdp.add_variable('X',(2*N+1,2*N+1),'symmetric')
sdp.add_constraint(X>>0)
sdp.add_constraint(X[2*N,2*N]<=1)
sdp.add_constraint(X>=0)
for i in range(N):
  sdp.add_constraint(X[i,i]==X[i,2*N])
  sdp.add_constraint(X[i,i]==X[i,i+N])
  sdp.add_constraint(X[i+N,i+N]==X[i+N,2*N])
for (i,j) in plus:
  sdp.add_constraint(X[i,i]==X[i,j+N])
for (i,j) in minus:
  sdp.add_constraint(X[i,j+N]==0)
diag = np.array([1]*N + [0]*(N+1))
C = pic.new_param('C',np.diag(diag))
print X
print C
sdp.set_objective('max', C | X)

print sdp
sdp.solve(verbose = 1, maxit=50)

print 'value: {0}'.format(sdp.obj_value())

solution = X.value

np.set_printoptions(precision=2,threshold='nan',linewidth=1000,suppress=True)
print 'solution:'
print np.array(solution)
示例#7
0
                [1 ,-5,-5]])

#size of the data
s = len(A)
m = A[0].size[0]
l = [ Ai.size[1] for Ai in A ]
r = K.size[1]

#creates a problem and the optimization variables
prob = pic.Problem()
mu = prob.add_variable('mu',s)
Z  = [prob.add_variable('Z[' + str(i) + ']', (l[i],r))
      for i in range(s)]

#convert the constants into params of the problem
A = pic.new_param('A',A)
K = pic.new_param('K',K)

#add the constraints
prob.add_constraint( pic.sum([ A[i]*Z[i] for i in range(s)], #summands
                            'i',                            #name of the index
                            '[s]'                           #set to which the index belongs
                           ) == K
                   )
prob.add_list_of_constraints( [ abs(Z[i]) < mu[i] for i in range(s)], #constraints
                              'i',                                    #index of the constraints
                              '[s]'                                   #set to which the index belongs
                            )

#sets the objective
prob.set_objective('min', 1 | mu ) # scalar product of the vector of all ones with mu
示例#8
0
文件: maxcut.py 项目: gsagnol/picos
import cvxopt.lapack
import numpy as np

#make G undirected
G=nx.Graph(G)

#allocate weights to the edges
for (i,j) in G.edges():
        G[i][j]['weight']=c[i,j]+c[j,i]


maxcut = pic.Problem()
X=maxcut.add_variable('X',(N,N),'symmetric')

#Laplacian of the graph  
L=pic.new_param('L',1/4.*nx.laplacian(G))

#ones on the diagonal
maxcut.add_constraint(pic.tools.diag_vect(X)==1)
#X positive semidefinite
maxcut.add_constraint(X>>0)

#objective
maxcut.set_objective('max',L|X)

#print maxcut
maxcut.solve(verbose = 0)

#Cholesky factorization
V=X.value
示例#9
0
                [1 ,-5,-5]])

#size of the data
s = len(A)
m = A[0].size[0]
l = [ Ai.size[1] for Ai in A ]
r = K.size[1]

#creates a problem and the optimization variables
prob = pic.Problem()
mu = prob.add_variable('mu',s)
Z  = [prob.add_variable('Z[' + str(i) + ']', (l[i],r))
        for i in range(s)]

#convert the constants into params of the problem
A = pic.new_param('A',A)
K = pic.new_param('K',K)

#add the constraints
prob.add_constraint( pic.sum([ A[i]*Z[i] for i in range(s)], #summands
                                'i',                            #name of the index
                                '[s]'                           #set to which the index belongs
                                ) == K
                        )
prob.add_list_of_constraints( [ abs(Z[i]) < mu[i] for i in range(s)], #constraints
                                'i',                                    #index of the constraints
                                '[s]'                                   #set to which the index belongs
                                )

#sets the objective
prob.set_objective('min', 1 | mu ) # scalar product of the vector of all ones with mu
示例#10
0
print ''
N = A.shape[0]
if N != A.shape[1]:
  raise Exception('matrix is not square')
for i in range(N):
  for j in range(N):
    if random() < p:
      A[i,j] = A[i,j] / p
    else:
      A[i,j] = 0
print 'Ar:'
print A
print ''

I = np.eye(N)
A = pic.new_param('A', A)
J = pic.new_param('J', np.ones(shape=(N,N)))
sdp = pic.Problem()
#M = sdp.add_variable('M',(N,N))
#W1 = sdp.add_variable('W1',(N,N),'symmetric')
#W2 = sdp.add_variable('W2',(N,N),'symmetric')
#sdp.add_constraint([[W1,M],[M.T,W2]]>>0)
U = sdp.add_variable('U',(2*N,2*N),'symmetric')
M = U[0:N,N:2*N]
W1 = U[0:N,0:N]
W2 = U[N:2*N,N:2*N]
sdp.add_constraint(U>>0)
sdp.add_constraint(M>0)
sdp.add_constraint(M<J)
sdp.set_objective('max', (A | M) - mu * (trace(W1)+trace(W2)))
print sdp
示例#11
0
        Ms[i,j] = M[i,j] / p
      else:
        Ms[i,j] = 0
  return Ms

r = 4
k = 10
M = adv(r,k)
mu = r*k/3
print M
N1 = M.shape[0]
N2 = M.shape[1]
Ms = samp(M,11.0/N2)
print Ms

A = pic.new_param('A', Ms)
J = pic.new_param('J', np.ones(shape=(N1,N2)))
sdp = pic.Problem()
#M = sdp.add_variable('M',(N,N))
#W1 = sdp.add_variable('W1',(N,N),'symmetric')
#W2 = sdp.add_variable('W2',(N,N),'symmetric')
#sdp.add_constraint([[W1,M],[M.T,W2]]>>0)
U = sdp.add_variable('U',(N1+N2,N1+N2),'symmetric')
Mr = U[0:N1,N1:N1+N2]
W1 = U[0:N1,0:N1]
W2 = U[N1:N1+N2,N1:N1+N2]
sdp.add_constraint(U>>0)
sdp.add_constraint(Mr>0)
sdp.add_constraint(Mr<J)
sdp.set_objective('max', (A | Mr) - 0.5 * mu * (trace(W1)+trace(W2)))
print sdp