Skip to content

Commit 475e2a1

Browse files
authored
Add support for constraint with AlgebraElement (#129)
* Add support for constraint with AlgebraElement * Fix format * Add tests
1 parent 1978d1d commit 475e2a1

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

src/constraint.jl

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export moments
22

3+
const _AE_APL = Union{SA.AlgebraElement,MP.AbstractPolynomialLike}
4+
35
abstract type PolynomialSet end
46

57
_in(::MIME) = Sys.iswindows() ? "in" : ""
@@ -13,16 +15,10 @@ struct ZeroPoly <: PolynomialSet end
1315
struct NonNegPoly <: PolynomialSet end
1416
struct PosDefPolyMatrix <: PolynomialSet end
1517

16-
function JuMP.function_string(
17-
::MIME"text/plain",
18-
p::Union{SA.AlgebraElement,MP.AbstractPolynomialLike},
19-
)
18+
function JuMP.function_string(::MIME"text/plain", p::_AE_APL)
2019
return sprint(show, MIME"text/plain"(), p)
2120
end
22-
function JuMP.function_string(
23-
mime::MIME"text/latex",
24-
p::Union{SA.AlgebraElement,MP.AbstractPolynomialLike},
25-
)
21+
function JuMP.function_string(mime::MIME"text/latex", p::_AE_APL)
2622
return SA.trim_LaTeX(mime, sprint(show, MIME"text/latex"(), p))
2723
end
2824

@@ -203,7 +199,7 @@ non_constant_coefficients(p) = non_constant(MP.coefficients(p))
203199
## ZeroPoly
204200
function JuMP.build_constraint(
205201
error_fn::Function,
206-
p::MP.AbstractPolynomialLike,
202+
p::_AE_APL,
207203
s::ZeroPoly;
208204
domain::SS.AbstractSemialgebraicSet = SS.FullSpace(),
209205
kws...,
@@ -238,7 +234,7 @@ function JuMP.build_constraint(
238234
end
239235
function JuMP.build_constraint(
240236
error_fn::Function,
241-
p::MP.AbstractPolynomialLike,
237+
p::_AE_APL,
242238
s::MOI.EqualTo;
243239
kws...,
244240
)
@@ -304,15 +300,15 @@ end
304300
# `NonNegPoly`
305301
function JuMP.build_constraint(
306302
error_fn::Function,
307-
p::MP.AbstractPolynomialLike,
303+
p::_AE_APL,
308304
s::MOI.GreaterThan;
309305
kws...,
310306
)
311307
return JuMP.build_constraint(error_fn, p - s.lower, NonNegPoly(); kws...)
312308
end
313309
function JuMP.build_constraint(
314310
error_fn::Function,
315-
p::MP.AbstractPolynomialLike,
311+
p::_AE_APL,
316312
s::MOI.LessThan;
317313
kws...,
318314
)
@@ -324,7 +320,7 @@ end
324320
# need a more specific here to avoid ambiguity
325321
function JuMP.build_constraint(
326322
error_fn::Function,
327-
p::AbstractMatrix{<:MP.AbstractPolynomialLike},
323+
p::AbstractMatrix{<:_AE_APL},
328324
s::PSDCone;
329325
kws...,
330326
)
@@ -334,7 +330,7 @@ end
334330
# Needed for the syntax `@constraint(model, A >= B, PSDCone())`
335331
function JuMP.build_constraint(
336332
error_fn::Function,
337-
f::AbstractMatrix{<:MP.AbstractPolynomialLike},
333+
f::AbstractMatrix{<:_AE_APL},
338334
s::MOI.GreaterThan,
339335
extra::PSDCone,
340336
)
@@ -345,7 +341,7 @@ end
345341
# Needed for the syntax `@constraint(model, A <= B, PSDCone())`
346342
function JuMP.build_constraint(
347343
error_fn::Function,
348-
f::AbstractMatrix{<:MP.AbstractPolynomialLike},
344+
f::AbstractMatrix{<:_AE_APL},
349345
s::MOI.LessThan,
350346
extra::PSDCone,
351347
)

src/variable.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@ function JuMP.value(
1515
return MP.polynomial(JuMP.value.(f, MP.terms(p)), MP.SortedUniqState())
1616
end
1717

18+
function JuMP.value(
19+
f::Function,
20+
p::SA.AlgebraElement{A,<:JuMP.AbstractJuMPScalar},
21+
) where {A}
22+
return SA.AlgebraElement(JuMP.value.(f, SA.coeffs(p)), parent(p))
23+
end
24+
1825
function JuMP.value(p::MP.AbstractPolynomialLike{<:JuMP.AbstractJuMPScalar})
1926
return JuMP.value(JuMP.value, p)
2027
end
2128

29+
function JuMP.value(p::SA.AlgebraElement{A,<:JuMP.AbstractJuMPScalar}) where {A}
30+
return JuMP.value(JuMP.value, p)
31+
end
32+
2233
abstract type AbstractPoly end
2334

2435
"""

test/variable.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,13 @@ function test_value_function(var)
177177
p = α * x * y + β * x^2
178178
JuMP.fix(α, 2)
179179
JuMP.fix(β, 3)
180-
@test_broken JuMP.value(p) == 2x * y + 3x^2
181-
@test JuMP.value(fix_value, p) == 2x * y + 3x^2
180+
expected = 2x * y + 3x^2
181+
@test_broken JuMP.value(p) == expected
182+
@test JuMP.value(fix_value, p) == expected
183+
a = MB.algebra_element(p)
184+
exp_a = MB.algebra_element(expected)
185+
@test_broken JuMP.value(a) == exp_a
186+
@test JuMP.value(fix_value, a) == exp_a
182187
end
183188

184189
function runtests(var)

0 commit comments

Comments
 (0)