Skip to content

Commit

Permalink
[Utilities] print the name of scalar constraints in print(::ModelLike)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Nov 2, 2023
1 parent b439822 commit 2166f52
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
27 changes: 26 additions & 1 deletion src/Utilities/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,39 @@ end
# Constraints
#------------------------------------------------------------------------

function _name_prefix(
options::_PrintOptions{MIME"text/plain"},
model::MOI.ModelLike,
cref::MOI.ConstraintIndex{
<:Union{
MOI.ScalarAffineFunction,
MOI.ScalarQuadraticFunction,
MOI.ScalarNonlinearFunction,
},
},
)
if !MOI.supports(model, MOI.ConstraintName(), typeof(cref))
return ""

Check warning on line 386 in src/Utilities/print.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/print.jl#L386

Added line #L386 was not covered by tests
end
name = MOI.get(model, MOI.ConstraintName(), cref)
if isempty(name)
return ""
end
return string(name, ": ")
end

_name_prefix(::_PrintOptions, ::MOI.ModelLike, ::MOI.ConstraintIndex) = ""

function _to_string(
options::_PrintOptions,
model::MOI.ModelLike,
cref::MOI.ConstraintIndex,
)
f = MOI.get(model, MOI.ConstraintFunction(), cref)
s = MOI.get(model, MOI.ConstraintSet(), cref)
return string(_to_string(options, model, f), " ", _to_string(options, s))
f_str = _to_string(options, model, f)
s_str = _to_string(options, s)
return string(_name_prefix(options, model, cref), f_str, " ", s_str)
end

#------------------------------------------------------------------------
Expand Down
28 changes: 14 additions & 14 deletions test/Utilities/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ function test_model()
c4: [1, 1.0 * x * x, y] in ExponentialCone()
c2: x in ZeroOne()
c5: 2.0 * x * x + y + -1 * z <= 1.0
c5: x + x >= 1.0
c5: x + x in Interval(1.0, 2.0)
c5: x + -1 * y == 0.0
c6: x + x >= 1.0
c7: x + x in Interval(1.0, 2.0)
c8: x + -1 * y == 0.0
""",
)
@test sprint(print, model) == """
Expand All @@ -307,16 +307,16 @@ function test_model()
Subject to:
ScalarAffineFunction{Float64}-in-EqualTo{Float64}
0.0 + 1.0 x - 1.0 y == 0.0
c8: 0.0 + 1.0 x - 1.0 y == 0.0
ScalarAffineFunction{Float64}-in-GreaterThan{Float64}
0.0 + 2.0 x >= 1.0
c6: 0.0 + 2.0 x >= 1.0
ScalarAffineFunction{Float64}-in-Interval{Float64}
0.0 + 2.0 x $(IN) [1.0, 2.0]
c7: 0.0 + 2.0 x $(IN) [1.0, 2.0]
ScalarQuadraticFunction{Float64}-in-LessThan{Float64}
0.0 + 1.0 y - 1.0 z + 2.0 x² <= 1.0
c5: 0.0 + 1.0 y - 1.0 z + 2.0 x² <= 1.0
VectorOfVariables-in-SecondOrderCone
┌ ┐
Expand Down Expand Up @@ -504,9 +504,9 @@ function test_plain_simplified()
c4: [1, 1.0 * x * x, y] in ExponentialCone()
c2: x in ZeroOne()
c5: 2.0 * x * x + y + -1 * z <= 1.0
c5: x + x >= 1.0
c5: x + x in Interval(1.0, 2.0)
c5: x + -1 * y == 0.0
c6: x + x >= 1.0
c7: x + x in Interval(1.0, 2.0)
c8: x + -1 * y == 0.0
""",
)
model_string = sprint() do io
Expand All @@ -524,10 +524,10 @@ function test_plain_simplified()
Minimize: -2 + x + 3.1 y - 1.2 z
Subject to:
x - y == 0
2 x >= 1
2 x $(IN) [1, 2]
y - z + 2 x² <= 1
c8: x - y == 0
c6: 2 x >= 1
c7: 2 x $(IN) [1, 2]
c5: y - z + 2 x² <= 1
┌ ┐
│x│
│y│
Expand Down

0 comments on commit 2166f52

Please sign in to comment.