diff --git a/src/Utilities/print.jl b/src/Utilities/print.jl index 0c282d17eb..d8132e52bb 100644 --- a/src/Utilities/print.jl +++ b/src/Utilities/print.jl @@ -371,6 +371,29 @@ 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, @@ -378,7 +401,9 @@ function _to_string( ) 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 #------------------------------------------------------------------------ diff --git a/test/Utilities/print.jl b/test/Utilities/print.jl index 52d863d37a..4358283f3b 100644 --- a/test/Utilities/print.jl +++ b/test/Utilities/print.jl @@ -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) == """ @@ -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 @@ -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 @@ -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²│