Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 _get_name_or_empty(model, cref::MOI.ConstraintIndex)
if !MOI.supports(model, MOI.ConstraintName(), typeof(cref))
return ""
end
return MOI.get(model, MOI.ConstraintName(), cref)
end

_get_name_or_empty(::Any, ::MOI.ConstraintIndex{MOI.VariableIndex}) = ""

function _name_suffix(
options::_PrintOptions{MIME"text/plain"},
model::MOI.ModelLike,
cref::MOI.ConstraintIndex,
)
name = _get_name_or_empty(model, cref)
if isempty(name)
return ""
end
return string(" (", name, ")")
end

_name_suffix(::_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(f_str, " ", s_str, _name_suffix(options, model, cref))
end

#------------------------------------------------------------------------
Expand Down
54 changes: 27 additions & 27 deletions test/Utilities/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,14 @@ function test_model()
c2: y in ZeroOne()
c2: z in Integer()
c3: [x, y] in SecondOrderCone(2)
c4: [1, x, y] in SecondOrderCone(2)
c4: [1.0 * x * x, y, 1] in ExponentialCone()
c4: [1, 1.0 * x * x, y] in ExponentialCone()
c4a: [1, x, y] in SecondOrderCone(2)
c4b: [1.0 * x * x, y, 1] in ExponentialCone()
c4c: [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,41 +307,41 @@ function test_model()
Subject to:

ScalarAffineFunction{Float64}-in-EqualTo{Float64}
0.0 + 1.0 x - 1.0 y == 0.0
0.0 + 1.0 x - 1.0 y == 0.0 (c8)

ScalarAffineFunction{Float64}-in-GreaterThan{Float64}
0.0 + 2.0 x >= 1.0
0.0 + 2.0 x >= 1.0 (c6)

ScalarAffineFunction{Float64}-in-Interval{Float64}
0.0 + 2.0 x $(IN) [1.0, 2.0]
0.0 + 2.0 x $(IN) [1.0, 2.0] (c7)

ScalarQuadraticFunction{Float64}-in-LessThan{Float64}
0.0 + 1.0 y - 1.0 z + 2.0 x² <= 1.0
0.0 + 1.0 y - 1.0 z + 2.0 x² <= 1.0 (c5)

VectorOfVariables-in-SecondOrderCone
┌ ┐
│x│
│y│
└ ┘ $(IN) SecondOrderCone(2)
└ ┘ $(IN) SecondOrderCone(2) (c3)

VectorAffineFunction{Float64}-in-SecondOrderCone
┌ ┐
│1.0 │
│0.0 + 1.0 x│
│0.0 + 1.0 y│
└ ┘ $(IN) SecondOrderCone(2)
└ ┘ $(IN) SecondOrderCone(2) (c4a)

VectorQuadraticFunction{Float64}-in-ExponentialCone
┌ ┐
│0.0 + 1.0 x²│
│0.0 + 1.0 y │
│1.0 │
└ ┘ $(IN) ExponentialCone()
└ ┘ $(IN) ExponentialCone() (c4b)
┌ ┐
│1.0 │
│0.0 + 1.0 x²│
│0.0 + 1.0 y │
└ ┘ $(IN) ExponentialCone()
└ ┘ $(IN) ExponentialCone() (c4c)

VariableIndex-in-GreaterThan{Float64}
x >= 0.1
Expand Down Expand Up @@ -499,14 +499,14 @@ function test_plain_simplified()
c2: y in ZeroOne()
c2: z in Integer()
c3: [x, y] in SecondOrderCone(2)
c4: [1, x, y] in SecondOrderCone(2)
c4: [1.0 * x * x, y, 1] in ExponentialCone()
c4: [1, 1.0 * x * x, y] in ExponentialCone()
c4a: [1, x, y] in SecondOrderCone(2)
c4b: [1.0 * x * x, y, 1] in ExponentialCone()
[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,24 +524,24 @@ 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
x - y == 0 (c8)
2 x >= 1 (c6)
2 x $(IN) [1, 2] (c7)
y - z + 2 x² <= 1 (c5)
┌ ┐
│x│
│y│
└ ┘ $(IN) SecondOrderCone(2)
└ ┘ $(IN) SecondOrderCone(2) (c3)
┌ ┐
│1│
│x│
│y│
└ ┘ $(IN) SecondOrderCone(2)
└ ┘ $(IN) SecondOrderCone(2) (c4a)
┌ ┐
│x²│
│y │
│1 │
└ ┘ $(IN) ExponentialCone()
└ ┘ $(IN) ExponentialCone() (c4b)
┌ ┐
│1 │
│x²│
Expand Down