def learnMMMF(y, c): n,m = y.shape n_obs = np.count_nonzero(y) obs = np.nonzero(y) prob = pic.Problem() #create a Problem instance # vars A = prob.add_variable('A', (n,n)) B = prob.add_variable('B', (m,m)) X = prob.add_variable('X', (n,m)) t = prob.add_variable('t', 1) e = [] for i in range(n_obs): e.append(prob.add_variable('e[{0}]'.format(i))) # constraints prob.add_constraint(((A & X)//(X.T & B)) >> 0) prob.add_constraint(pic.diag_vect(A) <= t) prob.add_constraint(pic.diag_vect(B) <= t) for i in e: prob.add_constraint(i >= 0) for x in enumerate(zip(obs[0], obs[1])): # each observation ind = x[0] i, a = int(x[1][0]), int(x[1][1]) prob.add_constraint((y[i,a] * X[i,a]) >= (1 - e[ind])) # objective prob.set_objective('min', t + c * pic.sum(e)) # solve time_start = time.clock() prob.solve(verbose=1, solver='sdpa', solve_via_dual = False, noduals=True) time_end = time.clock() ORIG = mat MATLAB = matlab PYTHON = X.value print('orig') print(ORIG) print('matlab') print(MATLAB) print('python') print(PYTHON) print('diff') print(MATLAB - PYTHON)
def learnMMMF(y, c): n, m = y.shape n_obs = np.count_nonzero(y) obs = np.nonzero(y) prob = pic.Problem() #create a Problem instance # vars A = prob.add_variable('A', (n, n)) B = prob.add_variable('B', (m, m)) X = prob.add_variable('X', (n, m)) t = prob.add_variable('t', 1) e = [] for i in range(n_obs): e.append(prob.add_variable('e[{0}]'.format(i))) # constraints prob.add_constraint(((A & X) // (X.T & B)) >> 0) prob.add_constraint(pic.diag_vect(A) <= t) prob.add_constraint(pic.diag_vect(B) <= t) for i in e: prob.add_constraint(i >= 0) for x in enumerate(zip(obs[0], obs[1])): # each observation ind = x[0] i, a = int(x[1][0]), int(x[1][1]) prob.add_constraint((y[i, a] * X[i, a]) >= (1 - e[ind])) # objective prob.set_objective('min', t + c * pic.sum(e)) # solve time_start = time.clock() prob.solve(verbose=1, solver='sdpa', solve_via_dual=False, noduals=True) time_end = time.clock() ORIG = mat MATLAB = matlab PYTHON = X.value print('orig') print(ORIG) print('matlab') print(MATLAB) print('python') print(PYTHON) print('diff') print(MATLAB - PYTHON)
def setUp(self): # Set the dimensionality. n = self.n = 4 # Primal problem. self.P = P = picos.Problem() self.X = X = P.add_variable("X", (n, n), "integer") P.set_objective("max", X | 1) P.add_constraint(picos.diag_vect(X) == 1) # The following constraint is necessary because PICOS does not support # integer symmetric matrices via a variable type. P.add_constraint(X == X.T) P.add_constraint(X >> 0)
def solve_picos(self): #http://www.orsj.or.jp/archive2/or63-12/or63_12_755.pdf #とけてるのかどうかわからん p = pic.Problem() X = p.add_variable('X', (self.n, self.n), 'symmetric') gL = nx.laplacian_matrix(self.G, weight='w', nodelist=self.G.nodes) gL = gL.toarray().astype(np.double) L = pic.new_param('L', 1 / 4 * gL) p.add_constraint(pic.diag_vect(X) == 1) p.add_constraint(X >> 0) p.set_objective('max', L | X) p.solve() print('bound from the SDP relaxation: {0}'.format(p.obj_value()))
def setUp(self): # Set the dimensionality. n = self.n = 4 # Primal problem. self.P = P = picos.Problem() self.X = X = P.add_variable("X", (n, n), "symmetric") P.set_objective("max", X | 1) self.CT = P.add_constraint(picos.diag_vect(X) == 1) self.CX = P.add_constraint(X >> 0) # Dual problem. self.D = D = picos.Problem() self.mu = mu = D.add_variable("mu", n) self.Z = Z = D.add_variable("Z", (n, n), "symmetric") D.set_objective("min", mu | 1) D.add_constraint(1 - picos.diag(mu) + Z == 0) D.add_constraint(Z >> 0)
def solve_SDP(f, grp_irreducible_rep): num_quats = f.shape[0] num_elems_grp = f.shape[2] num_irreducible_reps = grp_irreducible_rep.shape[0] d, is_all_real = irreducible_rep_analyze(grp_irreducible_rep) # convert grp_irreducible_rep into picos parameters dim and rho. ## dim dim = [pic.new_param('d[{k}]'.format(k = k), x) for k, x in enumerate(d)] ## rho rho = [[pic.new_param('rho[{k}, {g}]'.format(k = k, g = g), x) for g, x in enumerate(y)] for k, y in enumerate(grp_irreducible_rep)] # convert grp_irreducible_rep and f inot picos parameters C C = num_irreducible_reps * [None] for k in range(num_irreducible_reps): Ck = np.zeros((num_quats * d[k], num_quats * d[k]), dtype = np.float64 if is_all_real else np.complex128) for (i, j, g), _ in np.ndenumerate(f): Ck[i * d[k] : (i + 1) * d[k], j * d[k] : (j + 1) * d[k]] += f[j, i, g] * grp_irreducible_rep[k, g].conj().transpose() Ck *= (d[k] / num_elems_grp) C[k] = pic.new_param('C[{k}]'.format(k = k), Ck) # declare PICOS problem problem = pic.Problem() # construct variables X. # note that X[0] is fixed by constraints. # so we only construct X[1] .. X[K-1]. X = num_irreducible_reps * [None] for k in range(1, num_irreducible_reps): X[k] = problem.add_variable('X[{k}]'.format(k = k), \ (num_quats * d[k], num_quats * d[k]), \ vtype = 'symmetric' if is_all_real else 'hermitian') # add constraints # add semi-positive defineness constraint problem.add_list_of_constraints([X[k] >> 0 for k in range(1, num_irreducible_reps)]) # add identity matrix constraint problem.add_list_of_constraints([pic.diag_vect(X[k]) == 1 for k in range(1, num_irreducible_reps)]) # add NUG relaxed constraints for (i, j, g), _ in np.ndenumerate(f): if is_all_real: problem.add_constraint(pic.sum([dim[k] * \ pic.trace(rho[k][g] * X[k][i * dim[k] : (i + 1) * dim[k], \ j * dim[k] : (j + 1) * dim[k]]) \ for k in range(1, num_irreducible_reps)]) >= -1) else: problem.add_constraint(pic.sum([dim[k] * \ pic.trace(rho[k][g] * X[k][i * dim[k] : (i + 1) * dim[k], \ j * dim[k] : (j + 1) * dim[k]]) \ for k in range(1, num_irreducible_reps)]).real >= -1) problem.add_constraint(pic.sum([dim[k] * \ pic.trace(rho[k][g] * X[k][i * dim[k] : (i + 1) * dim[k], \ j * dim[k] : (j + 1) * dim[k]]) \ for k in range(1, num_irreducible_reps)]).imag >= 0) problem.add_constraint(pic.sum([dim[k] * \ pic.trace(rho[k][g] * X[k][i * dim[k] : (i + 1) * dim[k], \ j * dim[k] : (j + 1) * dim[k]]) \ for k in range(1, num_irreducible_reps)]).imag <= 0) # set objective if is_all_real: obj = pic.sum([pic.trace(C[k] * X[k]) for k in range(1, num_irreducible_reps)]) problem.set_objective('min', obj) else: obj = pic.sum([pic.trace(C[k] * X[k]) for k in range(1, num_irreducible_reps)]).real problem.set_objective('min', obj) problem.add_constraint(pic.sum([pic.trace(C[k] * X[k]) for k in range(1, num_irreducible_reps)]).imag >= 0) problem.add_constraint(pic.sum([pic.trace(C[k] * X[k]) for k in range(1, num_irreducible_reps)]).imag <= 0) # solve problem.set_option('verbose', 0) problem.solve(solver = 'cvxopt') # get X X[0] = np.ones((num_quats, num_quats), dtype = np.float64 if is_all_real else np.complex128) for k in range(1, num_irreducible_reps): X[k] = np.array(X[k].value, dtype = np.float64 if is_all_real else np.complex128) return X
def find_max_cut(graph, positions): # Move G to LCF notation. G = nx.convert_matrix.from_scipy_sparse_matrix(graph) pos = {i: positions[i] for i in range(len(positions))} # Generate edge capacities. c = {} for e in sorted(G.edges(data=True)): capacity = 1 e[2]['capacity'] = capacity c[(e[0], e[1])] = capacity c[(e[1], e[0])] = capacity # Convert the capacities to a PICOS expression. cc = pic.new_param('c', c) # Set source and sink nodes for flow computation. s = 16 t = 10 # Set node colors. N = len(positions) node_colors = ['lightgrey'] * N node_colors[s] = 'lightgreen' # Source is green. node_colors[t] = 'lightblue' # Sink is blue. # Define a plotting helper that closes the old and opens a new figure. def new_figure(): try: global fig pylab.close(fig) except NameError: pass fig = pylab.figure(figsize=(11, 8)) fig.gca().axes.get_xaxis().set_ticks([]) fig.gca().axes.get_yaxis().set_ticks([]) # Plot the graph with the edge capacities. new_figure() nx.draw_networkx(G, pos, node_color=node_colors) labels = { e: '{} | {}'.format(c[(e[0], e[1])], c[(e[1], e[0])]) for e in G.edges if e[0] < e[1] } nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) pylab.show() # Make G undirected. G = nx.Graph(G) # Allocate weights to the edges. for (i, j) in G.edges(): G[i][j]['weight'] = 1 maxcut = pic.Problem() # Add the symmetric matrix variable. X = maxcut.add_variable('X', (N, N), 'symmetric') # Retrieve the Laplacian of the graph. LL = 1 / 4. * nx.laplacian_matrix(G).todense() L = pic.new_param('L', LL) # Constrain X to have ones on the diagonal. maxcut.add_constraint(pic.diag_vect(X) == 1) # Constrain X to be positive semidefinite. maxcut.add_constraint(X >> 0) # Set the objective. maxcut.set_objective('max', L | X) # print(maxcut) # Solve the problem. maxcut.solve(solver='cvxopt') # print('bound from the SDP relaxation: {0}'.format(maxcut.obj_value())) # Use a fixed RNG seed so the result is reproducable. cvx.setseed(1) # Perform a Cholesky factorization. V = X.value cvxopt.lapack.potrf(V) for i in range(N): for j in range(i + 1, N): V[i, j] = 0 # Do up to 100 projections. Stop if we are within a factor 0.878 of the SDP # optimal value. count = 0 obj_sdp = maxcut.obj_value() obj = 0 while (count < 100 or obj < 0.878 * obj_sdp): r = cvx.normal(20, 1) x = cvx.matrix(np.sign(V * r)) o = (x.T * L * x).value if o > obj: x_cut = x obj = o count += 1 x = x_cut # Extract the cut and the seperated node sets. S1 = [n for n in range(N) if x[n] < 0] S2 = [n for n in range(N) if x[n] > 0] cut = [(i, j) for (i, j) in G.edges() if x[i] * x[j] < 0] leave = [e for e in G.edges if e not in cut] # Close the old figure and open a new one. new_figure() # Assign colors based on set membership. node_colors = [('lightgreen' if n in S1 else 'lightblue') for n in range(N)] # Draw the nodes and the edges that are not in the cut. nx.draw_networkx(G, pos, node_color=node_colors, edgelist=leave) labels = {e: '{}'.format(G[e[0]][e[1]]['weight']) for e in leave} nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) # Draw the edges that are in the cut. nx.draw_networkx_edges(G, pos, edgelist=cut, edge_color='r') labels = {e: '{}'.format(G[e[0]][e[1]]['weight']) for e in cut} nx.draw_networkx_edge_labels(G, pos, edge_labels=labels, font_color='r') # Show the relaxation optimum value and the cut capacity. rval = maxcut.obj_value() sval = sum(G[e[0]][e[1]]['weight'] for e in cut) fig.suptitle( 'SDP relaxation value: {0:.1f}\nCut value: {1:.1f} = {2:.3f}×{0:.1f}'. format(rval, sval, sval / rval), fontsize=16, y=0.97) # Show the figure. pylab.show() return S1, S2