Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate in(x, ::Symbol) to create PSD constraint #578

Merged
merged 8 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/examples/general_examples/basic_usage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ p.optval
x = Variable()
y = Variable((2, 2))
## SDP constraints
p = minimize(x + y[1, 1], isposdef(y), x >= 1, y[2, 1] == 1)
p = minimize(x + y[1, 1], y ⪰ 0, x >= 1, y[2, 1] == 1)
solve!(p, SCS.Optimizer; silent_solver = true)
evaluate(y)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ M = diagm(b) * (I(n) - A * A') * diagm(b)
U = ComplexVariable(n, n)
objective = inner_product(U, M)
c1 = diag(U) == 1
c2 = U in :SDP
c2 = isposdef(U)
p = minimize(objective, c1, c2)
solve!(p, SCS.Optimizer; silent_solver = true)
evaluate(U)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function get_visibility(K)
P = [[ComplexVariable(2, 2) for i in 1:2] for j in 1:6]
q = Variable(6, Positive())
t = Variable(1, Positive())
constraints = [P[i][j] in :SDP for i in 1:6 for j in 1:2]
constraints = [isposdef(P[i][j]) for i in 1:6 for j in 1:2]
constraints += sum(q) == 1
constraints += t <= 1
constraints += [P[i][1] + P[i][2] == q[i] * I(2) for i in 1:6]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ c1 = Constraint[];
for i in 2:n
push!(c1, sum(W[i, :] .* (Y[i, :]')) == inj[i])
end
c2 = W in :SDP
c2 = isposdef(W)
c3 = real(W[1, 1]) == 1.06^2;
push!(c1, c2)
push!(c1, c3)
Expand Down
8 changes: 5 additions & 3 deletions docs/src/manual/complex-domain_optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ using Convex, SCS
A = [ 0.47213595 0.11469794+0.48586827im; 0.11469794-0.48586827im 0.52786405]
B = ComplexVariable(2, 2)
ρ = kron(A, B)
constraints = [partialtrace(ρ, 1, [2; 2]) == [1 0; 0 0]
tr(ρ) == 1
ρ in :SDP]
constraints = [
partialtrace(ρ, 1, [2; 2]) == [1 0; 0 0],
tr(ρ) == 1,
isposdef(ρ),
]
p = satisfy(constraints)
solve!(p, SCS.Optimizer; silent_solver = true)
p.status
Expand Down
2 changes: 1 addition & 1 deletion docs/src/manual/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ x = Variable(3, 3)
y = Variable(3, 1)
z = Variable()
# constrain [x y; y' z] to be positive semidefinite
constraint = ([x y; y' z] in :SDP)
constraint = isposdef([x y; y' z])
# or equivalently,
constraint = ([x y; y' z] ⪰ 0)
```
Expand Down
2 changes: 1 addition & 1 deletion src/Convex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export conv,
lieb_ando,
# Constraints
Constraint,
isposdef,
⪰,
⪯,
socp,
Expand Down Expand Up @@ -99,6 +98,7 @@ for k in (
:dot,
:eigmax,
:eigmin,
:isposdef,
:logdet,
:norm,
:norm2,
Expand Down
19 changes: 6 additions & 13 deletions src/constraints/PositiveSemidefiniteConeConstraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@ function populate_dual!(
return
end

# TODO: Remove isposdef, change tests to use in. Update documentation and
# notebooks
LinearAlgebra.isposdef(x::AbstractExpr) = in(x, :SDP)

odow marked this conversation as resolved.
Show resolved Hide resolved
function Base.in(x::AbstractExpr, y::Symbol)
if !(y in (:semidefinite, :SDP))
error("Set $y not understood")
end
function LinearAlgebra.isposdef(x::AbstractExpr)
if iscomplex(x)
return PositiveSemidefiniteConeConstraint(
[real(x) -imag(x); imag(x) real(x)],
Expand All @@ -70,20 +63,20 @@ function Base.in(x::AbstractExpr, y::Symbol)
return PositiveSemidefiniteConeConstraint(x)
end

⪰(x::AbstractExpr, y::AbstractExpr) = in(x - y, :SDP)
⪰(x::AbstractExpr, y::AbstractExpr) = isposdef(x - y)

function ⪰(x::AbstractExpr, y::Value)
if all(y .== 0)
return in(x, :SDP)
return isposdef(x)
end
return in(x - constant(y), :SDP)
return isposdef(x - constant(y))
end

function ⪰(x::Value, y::AbstractExpr)
if all(x .== 0)
return in(-y, :SDP)
return isposdef(-y)
end
return in(constant(x) - y, :SDP)
return isposdef(constant(x) - y)
end

⪯(x::AbstractExpr, y::AbstractExpr) = ⪰(y, x)
Expand Down
12 changes: 12 additions & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ Base.:>(lhs::Value, rhs::AbstractExpr) = >(constant(lhs), rhs)
@deprecate get_vectorized_size(x::AbstractExpr) length(x)

@deprecate operatornorm(x::AbstractExpr) LinearAlgebra.opnorm(x)

function Base.in(x::AbstractExpr, y::Symbol)
if !(y in (:semidefinite, :SDP))
error("Set $y not understood")
end
@warn(
"Using `x in $y` to construct a semidefinite constraint is " *
"deprecated. Use `isposdef(x)` or `x ⪰ 0` instead.",
maxlog = 1,
)
return isposdef(x)
end
13 changes: 3 additions & 10 deletions src/problem_depot/problems/sdp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,7 @@ end
) where {T,test}
x = Variable(Positive())
y = Variable((3, 3))
p = minimize(
x + y[1, 1],
LinearAlgebra.isposdef(y),
x >= 1,
y[2, 1] == 1;
numeric_type = T,
)

p = minimize(x + y[1, 1], y ⪰ 0, x >= 1, y[2, 1] == 1; numeric_type = T)
# @fact problem_vexity(p) --> ConvexVexity()
handle_problem!(p)
if test
Expand Down Expand Up @@ -425,7 +418,7 @@ end
constraints = [
partialtrace(ρ, 1, [2; 2]) ==
[0.09942819 0.29923607; 0.29923607 0.90057181],
ρ in :SDP,
isposdef(ρ),
]
p = satisfy(constraints; numeric_type = T)

Expand Down Expand Up @@ -658,7 +651,7 @@ end
A = A + A' # now A is hermitian
x = ComplexVariable(n, n)
objective = sumsquares(A - x)
c1 = x in :SDP
c1 = isposdef(x)
p = minimize(objective, c1; numeric_type = T)

handle_problem!(p)
Expand Down
7 changes: 7 additions & 0 deletions test/test_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,13 @@ function test_deprecation_norm()
return
end

function test_deprecation_in_symbol()
x = Variable(2, 2)
@test_logs (:warn,) (x in :SDP)
@test in(x, :semidefinite) isa Convex.PositiveSemidefiniteConeConstraint
return
end

function test_dcp_rules()
vexities = (
Convex.ConcaveVexity(),
Expand Down
Loading