Skip to content

Commit bc2310a

Browse files
committed
Add tests
1 parent cf2764a commit bc2310a

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

src/RelativeEntropy/bridges/sage.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ function MOI.Bridges.Constraint.bridge_constraint(
1414
) where {T,F,G}
1515
m = size(set.α, 1)
1616
ν = Matrix{MOI.VariableIndex}(undef, m, m)
17-
age_constraints =
18-
Vector{MOI.ConstraintIndex{MOI.VectorOfVariables,SignomialAGECone}}(
19-
undef,
20-
m,
21-
)
17+
A = SignomialAGECone
18+
c = Vector{MOI.ConstraintIndex{MOI.VectorOfVariables,A}}(undef, m)
2219
for k in 1:m
23-
ν[k, :], age_constraints[k] =
24-
MOI.add_constrained_variables(model, SignomialAGECone(set.α, k))
20+
ν[k, :], c[k] = MOI.add_constrained_variables(model, A(set.α, k))
2521
end
2622
scalars = MOI.Utilities.eachscalar(func)
2723
n = size(set.α, 2)
@@ -36,7 +32,7 @@ function MOI.Bridges.Constraint.bridge_constraint(
3632
MOI.EqualTo(zero(T)),
3733
)
3834
end
39-
return SAGEBridge{T,F,G}(ν, age_constraints, equality_constraints)
35+
return SAGEBridge{T,F,G}(ν, c, equality_constraints)
4036
end
4137

4238
function MOI.supports_constraint(

src/RelativeEntropy/bridges/signomial.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
"""
2+
SignomialBridge{T,S,P,F} <: MOI.Bridges.Constraint.AbstractBridge
3+
4+
We use the Signomial Representative `SR` equation of [MCW21].
5+
6+
[MCW20] Riley Murray, Venkat Chandrasekaran, Adam Wierman
7+
"Newton Polytopes and Relative Entropy Optimization"
8+
https://arxiv.org/abs/1810.01614
9+
[MCW21] Murray, Riley, Venkat Chandrasekaran, and Adam Wierman.
10+
"Signomial and polynomial optimization via relative entropy and partial dualization."
11+
Mathematical Programming Computation 13 (2021): 257-295.
12+
https://arxiv.org/pdf/1907.00814.pdf
13+
"""
114
struct SignomialBridge{T,S,P,F} <: MOI.Bridges.Constraint.AbstractBridge
2-
constraints::Vector{MOI.ConstraintIndex{F,S}}
15+
constraints::MOI.ConstraintIndex{F,S}
316
end
417

518
_signomial(set::PolynomialSAGECone) = SignomialSAGECone(set.α)
@@ -13,17 +26,18 @@ function MOI.Bridges.Constraint.bridge_constraint(
1326
func::F,
1427
set,
1528
) where {T,S,P,F}
16-
g = MOI.scalarize(func)
29+
g = MOI.Utilities.scalarize(func)
1730
for i in eachindex(g)
18-
if isodd(sum(set.α[i, :]))
31+
if any(isodd, set.α[i, :])
1932
vi = MOI.add_variable(model)
2033
# vi ≤ -|g[i]|
21-
MOI.add_constraint(model, one(T) * vi - g[i], MOI.LessThan(zero(T)))
22-
MOI.add_constraint(model, one(T) * vi + g[i], MOI.LessThan(zero(T)))
34+
MOI.Utilities.normalize_and_add_constraint(model, one(T) * vi - g[i], MOI.LessThan(zero(T)))
35+
MOI.Utilities.normalize_and_add_constraint(model, one(T) * vi + g[i], MOI.LessThan(zero(T)))
2336
g[i] = vi
2437
end
2538
end
26-
constraint = MOI.add_constraint(model, g, _signomial(set))
39+
@show g
40+
constraint = MOI.add_constraint(model, MOI.Utilities.vectorize(g), _signomial(set))
2741
return SignomialBridge{T,S,P,F}(constraint)
2842
end
2943

test/relative_entropy.jl

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,58 @@ import ECOS
1111
using JuMP
1212
using PolyJuMP
1313

14-
function _test_motzkin(x, y, T, solver, set)
14+
function _test_motzkin(x, y, T, solver, set, feasible, square, neg)
1515
model = Model(solver)
16+
a = square ? x^2 : x
17+
b = square ? y^2 : y
1618
PolyJuMP.setpolymodule!(model, PolyJuMP.RelativeEntropy)
17-
motzkin = x^4 * y^2 + x^2 * y^4 + one(T) - 3x^2 * y^2
19+
if neg
20+
motzkin = -a^2 * b - a * b^2 + one(T) - 3a * b
21+
else
22+
motzkin = a^2 * b + a * b^2 + one(T) - 3a * b
23+
end
24+
@show motzkin
1825
@constraint(model, motzkin in set)
1926
optimize!(model)
20-
@test termination_status(model) == MOI.OPTIMAL
21-
@test primal_status(model) == MOI.FEASIBLE_POINT
27+
inner = model.moi_backend.optimizer.model
28+
println(inner)
29+
vis = MOI.get(inner, MOI.ListOfVariableIndices())
30+
@show MOI.get(inner, MOI.VariablePrimal(), vis)
31+
if feasible
32+
@test termination_status(model) == MOI.OPTIMAL
33+
@test primal_status(model) == MOI.FEASIBLE_POINT
34+
else
35+
@test termination_status(model) == MOI.INFEASIBLE
36+
end
2237
end
2338

2439
function test_motzkin(x, y, T, solver)
25-
_test_motzkin(
26-
x,
27-
y,
28-
T,
29-
solver,
30-
PolyJuMP.RelativeEntropy.SignomialAGESet(x^2 * y^2),
31-
)
32-
return _test_motzkin(
33-
x,
34-
y,
35-
T,
36-
solver,
37-
PolyJuMP.RelativeEntropy.SignomialSAGESet(),
38-
)
40+
set = PolyJuMP.RelativeEntropy.SignomialAGESet(x^2 * y^2)
41+
_test_motzkin(x, y, T, solver, set, true, true, false)
42+
set = PolyJuMP.RelativeEntropy.SignomialAGESet(x * y)
43+
_test_motzkin(x, y, T, solver, set, true, false, false)
44+
set = PolyJuMP.RelativeEntropy.SignomialAGESet(x^4 * y^2)
45+
_test_motzkin(x, y, T, solver, set, false, true, false)
46+
set = PolyJuMP.RelativeEntropy.SignomialAGESet(x^2 * y)
47+
_test_motzkin(x, y, T, solver, set, false, false, false)
48+
set = PolyJuMP.RelativeEntropy.SignomialSAGESet()
49+
_test_motzkin(x, y, T, solver, set, true, true, false)
50+
_test_motzkin(x, y, T, solver, set, true, false, false)
51+
_test_motzkin(x, y, T, solver, set, false, true, true)
52+
_test_motzkin(x, y, T, solver, set, false, false, true)
53+
set = PolyJuMP.RelativeEntropy.PolynomialAGESet(x^2 * y^2)
54+
_test_motzkin(x, y, T, solver, set, true, true, false)
55+
set = PolyJuMP.RelativeEntropy.PolynomialAGESet(x * y)
56+
_test_motzkin(x, y, T, solver, set, false, false, false)
57+
set = PolyJuMP.RelativeEntropy.PolynomialAGESet(x^4 * y^2)
58+
_test_motzkin(x, y, T, solver, set, false, true, false)
59+
set = PolyJuMP.RelativeEntropy.PolynomialAGESet(x^2 * y)
60+
_test_motzkin(x, y, T, solver, set, false, false, false)
61+
set = PolyJuMP.RelativeEntropy.PolynomialSAGESet()
62+
_test_motzkin(x, y, T, solver, set, true, true, false)
63+
set = PolyJuMP.RelativeEntropy.PolynomialSAGESet()
64+
_test_motzkin(x, y, T, solver, set, false, false, false)
65+
return
3966
end
4067

4168
import ECOS

0 commit comments

Comments
 (0)