Skip to content

Commit

Permalink
Update to use Parameter set
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Feb 19, 2023
1 parent 1b56f87 commit 6a7d030
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/Bridges/Constraint/bridges/fix_parametric_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* ``f(x) \\in S`` into ``g(x) \\in S``, where ``f(x)`` is a
[`MOI.ScalarQuadraticFunction{T}`](@ref) and ``g(x)`` is a
[`MOI.ScalarAffineFunction{T}`](@ref), where all variables that are fixed
using a [`MOI.VariableIndex`](@ref)-in-[`MOI.EqualTo`](@ref) constraint and
that appear in a quadratic term are replaced by their corresponding
using a [`MOI.VariableIndex`](@ref)-in-[`MOI.Parameter`](@ref) constraint
and that appear in a quadratic term are replaced by their corresponding
constant.
For example, if `p == 3`, this bridge converts the quadratic term `0.3 * p * x`
Expand Down Expand Up @@ -163,7 +163,7 @@ function MOI.Bridges.final_touch(
for x in keys(bridge.values)
bridge.values[x] = nothing
bridge.new_coefs[x] = zero(T)
ci = MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{T}}(x.value)
ci = MOI.ConstraintIndex{MOI.VariableIndex,MOI.Parameter{T}}(x.value)
if MOI.is_valid(model, ci)
bridge.values[x] = MOI.get(model, MOI.ConstraintSet(), ci).value
end
Expand Down
34 changes: 17 additions & 17 deletions test/Bridges/Constraint/fix_parametric_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ function test_runtests_x_fixed()
"""
variables: x, y
c: 1.0 * x * y + x + y >= 1.0
x == 2.0
x in Parameter(2.0)
""",
"""
variables: x, y
c: 3.0 * y + x >= 1.0
x == 2.0
x in Parameter(2.0)
""",
)
return
Expand All @@ -45,12 +45,12 @@ function test_runtests_y_fixed()
"""
variables: x, y
c: 1.0 * x * y + x + y >= 1.0
y == 2.0
y in Parameter(2.0)
""",
"""
variables: x, y
c: 3.0 * x + y >= 1.0
y == 2.0
y in Parameter(2.0)
""",
)
return
Expand All @@ -62,14 +62,14 @@ function test_runtests_both_fixed()
"""
variables: x, y
c: 1.0 * x * y + x + y >= 1.0
x == 3.0
y == 2.0
x in Parameter(3.0)
y in Parameter(2.0)
""",
"""
variables: x, y
c: 4.0 * y + x >= 1.0
x == 3.0
y == 2.0
x in Parameter(3.0)
y in Parameter(2.0)
""",
)
return
Expand All @@ -81,12 +81,12 @@ function test_runtests_duplicates()
"""
variables: x, y
c: 1.0 * x * y + 2.0 * x * y + x + y + x >= 1.0
x == 3.0
x in Parameter(3.0)
""",
"""
variables: x, y
c: 10.0 * y + 2.0 * x >= 1.0
x == 3.0
x in Parameter(3.0)
""",
)
return
Expand All @@ -98,12 +98,12 @@ function test_runtests_squared()
"""
variables: x, y
c: 2.0 * x * x + y >= 1.0
x == 3.0
x in Parameter(3.0)
""",
"""
variables: x, y
c: 6.0 * x + y >= 1.0
x == 3.0
x in Parameter(3.0)
""",
)
return
Expand All @@ -114,7 +114,7 @@ function test_at_least_one_variable_is_not_fixed()
model = MOI.Bridges.Constraint.FixParametricVariables{Int}(inner)
x, y = MOI.add_variables(model, 2)
f = 1 * x * y + 2 * x + 3 * y
MOI.add_constraint(model, f, MOI.EqualTo(0))
MOI.add_constraint(model, f, MOI.Parameter(0))
@test_throws(
ErrorException(
"unable to use `_FixParametricVariablesBridge: at least one " *
Expand All @@ -130,19 +130,19 @@ function test_resolve_with_modified()
model = MOI.Bridges.Constraint.FixParametricVariables{Int}(inner)
x, y = MOI.add_variables(model, 2)
f = 1 * x * y + 2 * x + 3 * y
c = MOI.add_constraint(model, f, MOI.EqualTo(0))
MOI.add_constraint(model, x, MOI.EqualTo(2))
c = MOI.add_constraint(model, f, MOI.Parameter(0))
MOI.add_constraint(model, x, MOI.Parameter(2))
MOI.Bridges.final_touch(model)
z = MOI.get(inner, MOI.ListOfVariableIndices())
F = MOI.ScalarAffineFunction{Int}
cis = MOI.get(inner, MOI.ListOfConstraintIndices{F,MOI.EqualTo{Int}}())
cis = MOI.get(inner, MOI.ListOfConstraintIndices{F,MOI.Parameter{Int}}())
@test length(cis) == 1
f = MOI.get(inner, MOI.ConstraintFunction(), cis[1])
@test f 2 * z[1] + 5 * z[2]
MOI.modify(model, c, MOI.ScalarCoefficientChange(y, 4))
MOI.Bridges.final_touch(model)
z = MOI.get(inner, MOI.ListOfVariableIndices())
cis = MOI.get(inner, MOI.ListOfConstraintIndices{F,MOI.EqualTo{Int}}())
cis = MOI.get(inner, MOI.ListOfConstraintIndices{F,MOI.Parameter{Int}}())
@test length(cis) == 1
f = MOI.get(inner, MOI.ConstraintFunction(), cis[1])
@test f 2 * z[1] + 6 * z[2]
Expand Down

0 comments on commit 6a7d030

Please sign in to comment.