Skip to content

Commit 30825a2

Browse files
authored
Rename GenericConstraint into Constraint and remove abstract type (#665)
1 parent c20c7ec commit 30825a2

21 files changed

+120
-159
lines changed

docs/src/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ changes.
3030
* The structs `LtConstraint`, `GtConstraint`, `EqConstraint`
3131
`SOCConstraint`, `ExpConstraint`, `GeoMeanEpiConeConstraint`,
3232
`GeoMeanHypoConeConstraint`, and `SDPConstraint` have been replaced by
33-
`GenericConstraint{S}` where `S<:MOI.AbstractSet` (#590), (#597), (#598),
33+
`Constraint{S}` where `S<:MOI.AbstractSet` (#590), (#597), (#598),
3434
(#599), (#601), (#602), (#604), (#623), (#632), (#648)
3535
* The set `GeomMeanEpiCone` has been renamed to `GeometricMeanEpiConeSquare`
3636
and `GeomMeanHypoCone` has been renamed to `GeometricMeanHypoConeSquare`

docs/src/manual/operations.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ solver (including SCS and Mosek).
116116
| `rootdet(X)` | n-th root-determinant of the $n$-by-$n$ matrix X, that is $det(X)^{1/n}$ | concave | not monotonic | |
117117
| `matrixfrac(x, P)` | $x^TP^{-1}x$ | convex | not monotonic | IC: P is positive semidefinite |
118118
| `sumlargesteigs(x, k)` | sum of top $k$ eigenvalues of $x$ | convex | not monotonic | IC: P symmetric |
119-
| `Convex.GenericConstraint((T, A, B), GeometricMeanHypoConeSquare(t, size(T, 1)))` | $T \preceq A \#_t B = A^{1/2} (A^{-1/2} B A^{-1/2})^t A^{1/2}$ | concave | increasing | IC: $A \succeq 0$, $B \succeq 0$, $t \in [0,1]$ |
120-
| `Convex.GenericConstraint((T, A, B), GeometricMeanEpiConeSquare(t, size(T, 1)))` | $T \succeq A \#_t B = A^{1/2} (A^{-1/2} B A^{-1/2})^t A^{1/2}$ | convex | not monotonic | IC: $A \succeq 0$, $B \succeq 0$, $t \in [-1, 0] \cup [1, 2]$ |
119+
| `Convex.Constraint((T, A, B), GeometricMeanHypoConeSquare(t, size(T, 1)))` | $T \preceq A \#_t B = A^{1/2} (A^{-1/2} B A^{-1/2})^t A^{1/2}$ | concave | increasing | IC: $A \succeq 0$, $B \succeq 0$, $t \in [0,1]$ |
120+
| `Convex.Constraint((T, A, B), GeometricMeanEpiConeSquare(t, size(T, 1)))` | $T \succeq A \#_t B = A^{1/2} (A^{-1/2} B A^{-1/2})^t A^{1/2}$ | convex | not monotonic | IC: $A \succeq 0$, $B \succeq 0$, $t \in [-1, 0] \cup [1, 2]$ |
121121
| `quantum_entropy(X)` | $-\textrm{Tr}(X \log X)$ | concave | not monotonic | IC: $X \succeq 0$; uses natural log |
122122
| `quantum_relative_entropy(A, B)` | $\textrm{Tr}(A \log A - A \log B)$ | convex | not monotonic | IC: $A \succeq 0$, $B \succeq 0$; uses natural log |
123123
| `trace_logm(X, C)` | $\textrm{Tr}(C \log X)$ | concave in X | not monotonic | IC: $X \succeq 0$, $C \succeq 0$, $C$ constant; uses natural log |
124124
| `trace_mpower(A, t, C)` | $\textrm{Tr}(C A^t)$ | concave in A for $t \in [0,1]$, convex for $t \in [-1,0] \cup [1,2]$ | not monotonic | IC: $X \succeq 0$, $C \succeq 0$, $C$ constant, $t \in [-1, 2]$ |
125125
| `lieb_ando(A, B, K, t)` | $\textrm{Tr}(K' A^{1-t} K B^t)$ | concave in A,B for $t \in [0,1]$, convex for $t \in [-1,0] \cup [1,2]$ | not monotonic | IC: $A \succeq 0$, $B \succeq 0$, $K$ constant, $t \in [-1, 2]$ |
126-
| `Convex.GenericConstraint((T, X, Y), RelativeEntropyEpiConeSquare(size(X, 1), m, k, e))` | $T \succeq e' X^{1/2} \log(X^{1/2} Y^{-1} X^{1/2}) X^{1/2} e$ | convex | not monotonic | IC: $e$ constant; uses natural log |
126+
| `Convex.Constraint((T, X, Y), RelativeEntropyEpiConeSquare(size(X, 1), m, k, e))` | $T \succeq e' X^{1/2} \log(X^{1/2} Y^{-1} X^{1/2}) X^{1/2} e$ | convex | not monotonic | IC: $e$ constant; uses natural log |
127127

128128
## Exponential + SDP representable Functions
129129

src/constraints/GenericConstraint.jl renamed to src/Constraint.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
# Use of this source code is governed by a BSD-style license that can be found
44
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause
55

6-
mutable struct GenericConstraint{S<:MOI.AbstractSet} <: Constraint
6+
mutable struct Constraint{S<:MOI.AbstractSet}
77
child::AbstractExpr
88
set::S
99
dual::Union{Value,Nothing}
1010

11-
function GenericConstraint(child::AbstractExpr, set::MOI.AbstractSet)
11+
function Constraint(child::AbstractExpr, set::MOI.AbstractSet)
1212
return new{typeof(set)}(child, set, nothing)
1313
end
1414
end
1515

16-
function GenericConstraint{S}(child::AbstractExpr) where {S<:MOI.AbstractSet}
17-
return GenericConstraint(child, set_with_size(S, size(child)))
16+
function Constraint{S}(child::AbstractExpr) where {S<:MOI.AbstractSet}
17+
return Constraint(child, set_with_size(S, size(child)))
1818
end
1919

20-
iscomplex(c::GenericConstraint) = iscomplex(c.child)
20+
iscomplex(c::Constraint) = iscomplex(c.child)
2121

2222
function set_with_size(
2323
::Type{S},
@@ -32,13 +32,13 @@ function set_with_size(
3232
return MOI.Utilities.set_with_dimension(S, sz[1])
3333
end
3434

35-
head(io::IO, c::GenericConstraint) = head(io, c.set)
35+
head(io::IO, c::Constraint) = head(io, c.set)
3636

3737
function head(io::IO, set::MOI.AbstractSet)
3838
return print(io, replace("$(typeof(set))", "MathOptInterface" => "MOI"))
3939
end
4040

41-
AbstractTrees.children(c::GenericConstraint) = (c.child,)
41+
AbstractTrees.children(c::Constraint) = (c.child,)
4242

4343
# A fallback. Define a new method if `MOI.Utilities.distance_to_set`
4444
# is not defined.
@@ -50,7 +50,7 @@ function is_feasible(x::Number, set::MOI.AbstractVectorSet, tol)
5050
return is_feasible([x], set, tol)
5151
end
5252

53-
vexity(c::GenericConstraint) = vexity(vexity(c.child), c.set)
53+
vexity(c::Constraint) = vexity(vexity(c.child), c.set)
5454

5555
function vexity(
5656
vex,
@@ -86,7 +86,7 @@ function vexity(::Any, set::MOI.AbstractSet)
8686
)
8787
end
8888

89-
function _add_constraint!(context::Context, c::GenericConstraint)
89+
function _add_constraint!(context::Context, c::Constraint)
9090
if vexity(c.child) == ConstVexity()
9191
# This `evaluate` call is safe, since even if it refers to a `fix!`'d variable,
9292
# it happens when we are formulating the problem (not at expression-time), so there
@@ -103,19 +103,15 @@ end
103103

104104
function populate_dual!(
105105
model::MOI.ModelLike,
106-
c::GenericConstraint,
106+
c::Constraint,
107107
indices::MOI.ConstraintIndex,
108108
)
109109
ret = MOI.get(model, MOI.ConstraintDual(), indices)
110110
c.dual = output(reshape(ret, c.child.size))
111111
return
112112
end
113113

114-
function populate_dual!(
115-
model::MOI.ModelLike,
116-
c::GenericConstraint,
117-
indices::NTuple{2},
118-
)
114+
function populate_dual!(model::MOI.ModelLike, c::Constraint, indices::NTuple{2})
119115
re = MOI.get(model, MOI.ConstraintDual(), indices[1])
120116
imag = MOI.get(model, MOI.ConstraintDual(), indices[2])
121117
c.dual = output(reshape(re + im * imag, c.child.size))
@@ -169,7 +165,7 @@ function Base.:>=(lhs::AbstractExpr, rhs::AbstractExpr)
169165
)
170166
end
171167
lhs, rhs = _promote_size(lhs, rhs)
172-
return GenericConstraint{MOI.Nonnegatives}(lhs - rhs)
168+
return Constraint{MOI.Nonnegatives}(lhs - rhs)
173169
end
174170

175171
Base.:>=(lhs::AbstractExpr, rhs::Value) = >=(lhs, constant(rhs))
@@ -201,7 +197,7 @@ function Base.:<=(lhs::AbstractExpr, rhs::AbstractExpr)
201197
)
202198
end
203199
lhs, rhs = _promote_size(lhs, rhs)
204-
return GenericConstraint{MOI.Nonpositives}(lhs - rhs)
200+
return Constraint{MOI.Nonpositives}(lhs - rhs)
205201
end
206202

207203
Base.:<=(lhs::AbstractExpr, rhs::Value) = <=(lhs, constant(rhs))
@@ -227,7 +223,7 @@ end
227223

228224
function Base.:(==)(lhs::AbstractExpr, rhs::AbstractExpr)
229225
lhs, rhs = _promote_size(lhs, rhs)
230-
return GenericConstraint{MOI.Zeros}(lhs - rhs)
226+
return Constraint{MOI.Zeros}(lhs - rhs)
231227
end
232228

233229
Base.:(==)(lhs::AbstractExpr, rhs::Value) = ==(lhs, constant(rhs))
@@ -260,11 +256,11 @@ end
260256

261257
function LinearAlgebra.isposdef(x::AbstractExpr)
262258
if iscomplex(x)
263-
return GenericConstraint{MOI.PositiveSemidefiniteConeSquare}(
259+
return Constraint{MOI.PositiveSemidefiniteConeSquare}(
264260
[real(x) -imag(x); imag(x) real(x)],
265261
)
266262
end
267-
return GenericConstraint{MOI.PositiveSemidefiniteConeSquare}(x)
263+
return Constraint{MOI.PositiveSemidefiniteConeSquare}(x)
268264
end
269265

270266
(x::AbstractExpr, y::AbstractExpr) = isposdef(x - y)

src/Convex.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export conv,
5757
Constraint,
5858
,
5959
,
60-
Constraint, # useful for making abstractly-typed vectors via `Constraint[]`
6160
# Variables
6261
constant,
6362
ComplexVariable,
@@ -213,11 +212,12 @@ include("Context.jl")
213212
### modeling framework
214213
include("dcp.jl")
215214
include("expressions.jl")
215+
include("Constraint.jl")
216216
include("variable.jl")
217217
include("variable_template.jl")
218218
include("constant.jl")
219219

220-
for (root, _, files) in walkdir(joinpath(@__DIR__, "constraints"))
220+
for (root, _, files) in walkdir(joinpath(@__DIR__, "sets"))
221221
for file in files
222222
include(joinpath(root, file))
223223
end

src/atoms/ExpAtom.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ A naive implementation of this method is:
6060
t = Variable(m, n)
6161
for i in 1:m, j in 1:n
6262
f = vcat(x[i, j], 1, t[i, j])[collect(permutation)]
63-
add_constraint!(context, GenericConstraint{MOI.ExponentialCone}(f))
63+
add_constraint!(context, Constraint{MOI.ExponentialCone}(f))
6464
end
6565
return conic_form!(context, t)
6666
end

src/atoms/QuadOverLinAtom.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function new_conic_form!(context::Context{T}, q::QuadOverLinAtom) where {T}
3636
t = Variable()
3737
x, y = q.children
3838
f = vcat(t, (1 / T(2)) * y, x)
39-
add_constraint!(context, GenericConstraint{MOI.RotatedSecondOrderCone}(f))
39+
add_constraint!(context, Constraint{MOI.RotatedSecondOrderCone}(f))
4040
return conic_form!(context, t)
4141
end
4242

src/atoms/QuantumEntropyAtom.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function new_conic_form!(
8282
end
8383
I = Matrix(one(T) * LinearAlgebra.I(size(X, 1)))
8484
set = RelativeEntropyEpiConeSquare(size(X, 1), atom.m, atom.k)
85-
add_constraint!(context, GenericConstraint((τ, X, I), set))
85+
add_constraint!(context, Constraint((τ, X, I), set))
8686
# It's already a real mathematically, but need to make it a real type.
8787
return conic_form!(context, real(-LinearAlgebra.tr(τ)))
8888
end

src/atoms/QuantumRelativeEntropyAtom.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function new_conic_form!(
9595
I = Matrix(one(T) * LinearAlgebra.I(size(A, 1)))
9696
τ, X, Y = Variable(), kron(A, I), kron(I, conj(B))
9797
set = RelativeEntropyEpiConeSquare(size(X, 1), atom.m, atom.k, vec(I))
98-
add_constraint!(context, GenericConstraint((τ, X, Y), set))
98+
add_constraint!(context, Constraint((τ, X, Y), set))
9999
return conic_form!(context, τ)
100100
end
101101

src/atoms/RelativeEntropyAtom.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function new_conic_form!(context::Context{T}, e::RelativeEntropyAtom) where {T}
4646
# But MOI has the reversed convention:
4747
# MOI.RelativeEntropyCone has the order (u, y, x)
4848
f = vcat(u, y, x)
49-
add_constraint!(context, GenericConstraint{MOI.RelativeEntropyCone}(f))
49+
add_constraint!(context, Constraint{MOI.RelativeEntropyCone}(f))
5050
return conic_form!(context, u)
5151
end
5252

src/atoms/TraceLogmAtom.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function new_conic_form!(context::Context{T}, atom::TraceLogmAtom) where {T}
103103
end
104104
I = Matrix(one(T) * LinearAlgebra.I(size(X, 1)))
105105
set = RelativeEntropyEpiConeSquare(size(X, 1), atom.m, atom.k)
106-
add_constraint!(context, GenericConstraint((τ, I, X), set))
106+
add_constraint!(context, Constraint((τ, I, X), set))
107107
# It's already a real mathematically, but need to make it a real type.
108108
return conic_form!(context, real(-LinearAlgebra.tr(atom.C * τ)))
109109
end

0 commit comments

Comments
 (0)