def graph_implementation(arg_objs, size, data=None): """Reduces the atom to an affine expression and list of constraints. Parameters ---------- arg_objs : list LinExpr for each argument. size : tuple The size of the resulting expression. data : Additional data required by the atom. Returns ------- tuple (LinOp for objective, list of constraints) """ #x = arg_objs[0] t = lu.create_var(size) # log(1 + exp(x)) <= t <=> exp(-t) + exp(x - t) <= 1 ''' obj0, constr0 = exp.graph_implementation([lu.neg_expr(t)], size) obj1, constr1 = exp.graph_implementation([lu.sub_expr(x, t)], size) lhs = lu.sum_expr([obj0, obj1]) ones = lu.create_const(np.mat(np.ones(size)), size) constr = constr0 + constr1 + [lu.create_leq(lhs, ones)] ''' a = arg_objs[0] b = arg_objs[1] e_a, e_a_cons = exp.graph_implementation([lu.neg_expr(a)], (1,1)) e_b, e_b_cons = exp.graph_implementation([lu.neg_expr(b)], (1,1)) obj0, constr0 = log.graph_implementation( [lu.sub_expr(e_b,e_a)], (1,1) ) lhs = obj0 constr = constr0 + e_a_cons + e_b_cons + [lu.create_leq(lhs, t)] + [lu.create_leq(e_a, e_b)] return (t, constr)
def graph_implementation(arg_objs, shape, data=None): """Reduces the atom to an affine expression and list of constraints. Parameters ---------- arg_objs : list LinExpr for each argument. shape : tuple The shape of the resulting expression. data : Additional data required by the atom. Returns ------- tuple (LinOp for objective, list of constraints) """ x = arg_objs[0] ones = lu.create_const(np.mat(np.ones(x.shape)), x.shape) xp1 = lu.sum_expr([x, ones]) return log.graph_implementation([xp1], shape, data)
def graph_implementation(arg_objs, size, data=None): """Reduces the atom to an affine expression and list of constraints. Parameters ---------- arg_objs : list LinExpr for each argument. size : tuple The size of the resulting expression. data : Additional data required by the atom. Returns ------- tuple (LinOp for objective, list of constraints) """ x = arg_objs[0] ones = lu.create_const(np.mat(np.ones(x.size)), x.size) xp1 = lu.sum_expr([x, ones]) return log.graph_implementation([xp1], size, data)
def graph_implementation(arg_objs, size, data=None): """Reduces the atom to an affine expression and list of constraints. Creates the equivalent problem:: maximize sum(log(D[i, i])) subject to: D diagonal diag(D) = diag(Z) Z is upper triangular. [D Z; Z.T A] is positive semidefinite The problem computes the LDL factorization: .. math:: A = (Z^TD^{-1})D(D^{-1}Z) This follows from the inequality: .. math:: \det(A) >= \det(D) + \det([D, Z; Z^T, A])/\det(D) >= \det(D) because (Z^TD^{-1})D(D^{-1}Z) is a feasible D, Z that achieves det(A) = det(D) and the objective maximizes det(D). Parameters ---------- arg_objs : list LinExpr for each argument. size : tuple The size of the resulting expression. data : Additional data required by the atom. Returns ------- tuple (LinOp for objective, list of constraints) """ A = arg_objs[0] # n by n matrix. n, _ = A.size X = lu.create_var((2 * n, 2 * n)) Z = lu.create_var((n, n)) D = lu.create_var((n, n)) # Require that X is symmetric (which implies # A is symmetric). # X == X.T obj, constraints = transpose.graph_implementation([X], (n, n)) constraints.append(lu.create_eq(X, obj)) # Require that X and A are PSD. constraints += [SDP(X), SDP(A)] # Fix Z as upper triangular, D as diagonal, # and diag(D) as diag(Z). for i in xrange(n): for j in xrange(n): if i == j: # D[i, j] == Z[i, j] Dij = index.get_index(D, constraints, i, j) Zij = index.get_index(Z, constraints, i, j) constraints.append(lu.create_eq(Dij, Zij)) if i != j: # D[i, j] == 0 Dij = index.get_index(D, constraints, i, j) constraints.append(lu.create_eq(Dij)) if i > j: # Z[i, j] == 0 Zij = index.get_index(Z, constraints, i, j) constraints.append(lu.create_eq(Zij)) # Fix X using the fact that A must be affine by the DCP rules. # X[0:n, 0:n] == D index.block_eq(X, D, constraints, 0, n, 0, n) # X[0:n, n:2*n] == Z, index.block_eq(X, Z, constraints, 0, n, n, 2 * n) # X[n:2*n, n:2*n] == A index.block_eq(X, A, constraints, n, 2 * n, n, 2 * n) # Add the objective sum(log(D[i, i]) log_diag = [] for i in xrange(n): Dii = index.get_index(D, constraints, i, i) obj, constr = log.graph_implementation([Dii], (1, 1)) constraints += constr log_diag.append(obj) obj = lu.sum_expr(log_diag) return (obj, constraints)
def graph_implementation(arg_objs, size, data=None): """Reduces the atom to an affine expression and list of constraints. Creates the equivalent problem:: maximize sum(log(D[i, i])) subject to: D diagonal diag(D) = diag(Z) Z is upper triangular. [D Z; Z.T A] is positive semidefinite The problem computes the LDL factorization: .. math:: A = (Z^TD^{-1})D(D^{-1}Z) This follows from the inequality: .. math:: \det(A) >= \det(D) + \det([D, Z; Z^T, A])/\det(D) >= \det(D) because (Z^TD^{-1})D(D^{-1}Z) is a feasible D, Z that achieves det(A) = det(D) and the objective maximizes det(D). Parameters ---------- arg_objs : list LinExpr for each argument. size : tuple The size of the resulting expression. data : Additional data required by the atom. Returns ------- tuple (LinOp for objective, list of constraints) """ A = arg_objs[0] # n by n matrix. n, _ = A.size X = lu.create_var((2 * n, 2 * n)) X, constraints = Semidef(2 * n).canonical_form Z = lu.create_var((n, n)) D = lu.create_var((n, 1)) # Require that X and A are PSD. constraints += [SDP(A)] # Fix Z as upper triangular, D as diagonal, # and diag(D) as diag(Z). Z_lower_tri = lu.upper_tri(lu.transpose(Z)) constraints.append(lu.create_eq(Z_lower_tri)) # D[i, i] = Z[i, i] constraints.append(lu.create_eq(D, lu.diag_mat(Z))) # Fix X using the fact that A must be affine by the DCP rules. # X[0:n, 0:n] == D index.block_eq(X, lu.diag_vec(D), constraints, 0, n, 0, n) # X[0:n, n:2*n] == Z, index.block_eq(X, Z, constraints, 0, n, n, 2 * n) # X[n:2*n, n:2*n] == A index.block_eq(X, A, constraints, n, 2 * n, n, 2 * n) # Add the objective sum(log(D[i, i]) obj, constr = log.graph_implementation([D], (n, 1)) return (lu.sum_entries(obj), constraints + constr)
def graph_implementation(arg_objs, size, data=None): """Reduces the atom to an affine expression and list of constraints. Creates the equivalent problem:: maximize sum(log(D[i, i])) subject to: D diagonal diag(D) = diag(Z) Z is upper triangular. [D Z; Z.T A] is positive semidefinite The problem computes the LDL factorization: .. math:: A = (Z^TD^{-1})D(D^{-1}Z) This follows from the inequality: .. math:: \det(A) >= \det(D) + \det([D, Z; Z^T, A])/\det(D) >= \det(D) because (Z^TD^{-1})D(D^{-1}Z) is a feasible D, Z that achieves det(A) = det(D) and the objective maximizes det(D). Parameters ---------- arg_objs : list LinExpr for each argument. size : tuple The size of the resulting expression. data : Additional data required by the atom. Returns ------- tuple (LinOp for objective, list of constraints) """ A = arg_objs[0] # n by n matrix. n, _ = A.size X = lu.create_var((2*n, 2*n)) Z = lu.create_var((n, n)) D = lu.create_var((n, 1)) # Require that X and A are PSD. constraints = [SDP(X), SDP(A)] # Fix Z as upper triangular, D as diagonal, # and diag(D) as diag(Z). Z_lower_tri = lu.upper_tri(lu.transpose(Z)) constraints.append(lu.create_eq(Z_lower_tri)) # D[i, i] = Z[i, i] constraints.append(lu.create_eq(D, lu.diag_mat(Z))) # Fix X using the fact that A must be affine by the DCP rules. # X[0:n, 0:n] == D index.block_eq(X, lu.diag_vec(D), constraints, 0, n, 0, n) # X[0:n, n:2*n] == Z, index.block_eq(X, Z, constraints, 0, n, n, 2*n) # X[n:2*n, n:2*n] == A index.block_eq(X, A, constraints, n, 2*n, n, 2*n) # Add the objective sum(log(D[i, i]) obj, constr = log.graph_implementation([D], (n, 1)) return (lu.sum_entries(obj), constraints + constr)