From 2166f52974ee6643d4f7479e52653538edb0d88b Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 2 Nov 2023 13:17:44 +1300 Subject: [PATCH 1/3] [Utilities] print the name of scalar constraints in print(::ModelLike) --- src/Utilities/print.jl | 27 ++++++++++++++++++++++++++- test/Utilities/print.jl | 28 ++++++++++++++-------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/Utilities/print.jl b/src/Utilities/print.jl index 0c282d17eb..459e885d47 100644 --- a/src/Utilities/print.jl +++ b/src/Utilities/print.jl @@ -371,6 +371,29 @@ 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 "" + 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, @@ -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(_name_prefix(options, model, cref), f_str, " ", s_str) end #------------------------------------------------------------------------ diff --git a/test/Utilities/print.jl b/test/Utilities/print.jl index 52d863d37a..178831a565 100644 --- a/test/Utilities/print.jl +++ b/test/Utilities/print.jl @@ -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) == """ @@ -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 ┌ ┐ @@ -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 @@ -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│ From dd27541060138146942528acace2d68348a769c1 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 2 Nov 2023 13:24:36 +1300 Subject: [PATCH 2/3] Print vector names --- src/Utilities/print.jl | 24 +++++++++++++++--------- test/Utilities/print.jl | 19 +++++++++++++------ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Utilities/print.jl b/src/Utilities/print.jl index 459e885d47..3f11bc6969 100644 --- a/src/Utilities/print.jl +++ b/src/Utilities/print.jl @@ -374,14 +374,8 @@ end function _name_prefix( options::_PrintOptions{MIME"text/plain"}, model::MOI.ModelLike, - cref::MOI.ConstraintIndex{ - <:Union{ - MOI.ScalarAffineFunction, - MOI.ScalarQuadraticFunction, - MOI.ScalarNonlinearFunction, - }, - }, -) + cref::MOI.ConstraintIndex{F}, +) where {F} if !MOI.supports(model, MOI.ConstraintName(), typeof(cref)) return "" end @@ -389,7 +383,19 @@ function _name_prefix( if isempty(name) return "" end - return string(name, ": ") + if F <: MOI.AbstractVectorFunction + return string(name, ":\n") + else + return string(name, ": ") + end +end + +function _name_prefix( + ::_PrintOptions{MIME"text/plain"}, + ::MOI.ModelLike, + ::MOI.ConstraintIndex{MOI.VariableIndex}, +) + return "" end _name_prefix(::_PrintOptions, ::MOI.ModelLike, ::MOI.ConstraintIndex) = "" diff --git a/test/Utilities/print.jl b/test/Utilities/print.jl index 178831a565..822916425e 100644 --- a/test/Utilities/print.jl +++ b/test/Utilities/print.jl @@ -290,9 +290,9 @@ 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 c6: x + x >= 1.0 @@ -319,12 +319,14 @@ function test_model() c5: 0.0 + 1.0 y - 1.0 z + 2.0 x² <= 1.0 VectorOfVariables-in-SecondOrderCone + c3: ┌ ┐ │x│ │y│ └ ┘ $(IN) SecondOrderCone(2) VectorAffineFunction{Float64}-in-SecondOrderCone + c4a: ┌ ┐ │1.0 │ │0.0 + 1.0 x│ @@ -332,11 +334,13 @@ function test_model() └ ┘ $(IN) SecondOrderCone(2) VectorQuadraticFunction{Float64}-in-ExponentialCone + c4b: ┌ ┐ │0.0 + 1.0 x²│ │0.0 + 1.0 y │ │1.0 │ └ ┘ $(IN) ExponentialCone() + c4c: ┌ ┐ │1.0 │ │0.0 + 1.0 x²│ @@ -499,9 +503,9 @@ 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 c6: x + x >= 1.0 @@ -528,15 +532,18 @@ function test_plain_simplified() c6: 2 x >= 1 c7: 2 x $(IN) [1, 2] c5: y - z + 2 x² <= 1 + c3: ┌ ┐ │x│ │y│ └ ┘ $(IN) SecondOrderCone(2) + c4a: ┌ ┐ │1│ │x│ │y│ └ ┘ $(IN) SecondOrderCone(2) + c4b: ┌ ┐ │x²│ │y │ From 481e4990cba598b82766cf009f7576eb6be8c204 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 3 Nov 2023 10:28:09 +1300 Subject: [PATCH 3/3] Change to printing after the constraint --- src/Utilities/print.jl | 36 +++++++++++++++--------------------- test/Utilities/print.jl | 37 +++++++++++++++---------------------- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/Utilities/print.jl b/src/Utilities/print.jl index 3f11bc6969..d8132e52bb 100644 --- a/src/Utilities/print.jl +++ b/src/Utilities/print.jl @@ -371,34 +371,28 @@ end # Constraints #------------------------------------------------------------------------ -function _name_prefix( - options::_PrintOptions{MIME"text/plain"}, - model::MOI.ModelLike, - cref::MOI.ConstraintIndex{F}, -) where {F} +function _get_name_or_empty(model, cref::MOI.ConstraintIndex) if !MOI.supports(model, MOI.ConstraintName(), typeof(cref)) return "" end - name = MOI.get(model, MOI.ConstraintName(), cref) - if isempty(name) - return "" - end - if F <: MOI.AbstractVectorFunction - return string(name, ":\n") - else - return string(name, ": ") - end + return MOI.get(model, MOI.ConstraintName(), cref) end -function _name_prefix( - ::_PrintOptions{MIME"text/plain"}, - ::MOI.ModelLike, - ::MOI.ConstraintIndex{MOI.VariableIndex}, +_get_name_or_empty(::Any, ::MOI.ConstraintIndex{MOI.VariableIndex}) = "" + +function _name_suffix( + options::_PrintOptions{MIME"text/plain"}, + model::MOI.ModelLike, + cref::MOI.ConstraintIndex, ) - return "" + name = _get_name_or_empty(model, cref) + if isempty(name) + return "" + end + return string(" (", name, ")") end -_name_prefix(::_PrintOptions, ::MOI.ModelLike, ::MOI.ConstraintIndex) = "" +_name_suffix(::_PrintOptions, ::MOI.ModelLike, ::MOI.ConstraintIndex) = "" function _to_string( options::_PrintOptions, @@ -409,7 +403,7 @@ function _to_string( s = MOI.get(model, MOI.ConstraintSet(), cref) f_str = _to_string(options, model, f) s_str = _to_string(options, s) - return string(_name_prefix(options, model, cref), f_str, " ", s_str) + 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 822916425e..4358283f3b 100644 --- a/test/Utilities/print.jl +++ b/test/Utilities/print.jl @@ -307,45 +307,41 @@ function test_model() Subject to: ScalarAffineFunction{Float64}-in-EqualTo{Float64} - c8: 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} - c6: 0.0 + 2.0 x >= 1.0 + 0.0 + 2.0 x >= 1.0 (c6) ScalarAffineFunction{Float64}-in-Interval{Float64} - c7: 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} - c5: 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 - c3: ┌ ┐ │x│ │y│ - └ ┘ $(IN) SecondOrderCone(2) + └ ┘ $(IN) SecondOrderCone(2) (c3) VectorAffineFunction{Float64}-in-SecondOrderCone - c4a: ┌ ┐ │1.0 │ │0.0 + 1.0 x│ │0.0 + 1.0 y│ - └ ┘ $(IN) SecondOrderCone(2) + └ ┘ $(IN) SecondOrderCone(2) (c4a) VectorQuadraticFunction{Float64}-in-ExponentialCone - c4b: ┌ ┐ │0.0 + 1.0 x²│ │0.0 + 1.0 y │ │1.0 │ - └ ┘ $(IN) ExponentialCone() - c4c: + └ ┘ $(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 @@ -528,27 +524,24 @@ function test_plain_simplified() Minimize: -2 + x + 3.1 y - 1.2 z Subject to: - c8: x - y == 0 - c6: 2 x >= 1 - c7: 2 x $(IN) [1, 2] - c5: y - z + 2 x² <= 1 - c3: + 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) - c4a: + └ ┘ $(IN) SecondOrderCone(2) (c3) ┌ ┐ │1│ │x│ │y│ - └ ┘ $(IN) SecondOrderCone(2) - c4b: + └ ┘ $(IN) SecondOrderCone(2) (c4a) ┌ ┐ │x²│ │y │ │1 │ - └ ┘ $(IN) ExponentialCone() + └ ┘ $(IN) ExponentialCone() (c4b) ┌ ┐ │1 │ │x²│