Skip to content

Commit

Permalink
✅ Split model_printing in two functions
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jan 4, 2019
1 parent c0dfb4c commit ff3e01a
Showing 1 changed file with 182 additions and 151 deletions.
333 changes: 182 additions & 151 deletions test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,7 @@ end
end
end

"""
printing_test(ModelType::Type{<:JuMP.AbstractModel},
moi_backend::Bool)
Test printing of models of type `ModelType`. The expected model printing depends
on `moi_backend` which indicates whether the model is stored in an MOI backend
or is stored in its JuMP form (i.e. as `AbstractVariable`s,
`AbstractConstraint`s and `AbstractJuMPScalar` for the objective function).
"""
function printing_test(ModelType::Type{<:JuMP.AbstractModel},
moi_backend::Bool)
function printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "VariableRef" begin
m = ModelType()
@variable(m, 0 <= x <= 2)
Expand Down Expand Up @@ -346,6 +336,11 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel},
io_test(REPLMode, quad_constr, "2 x$sq $le 1.0")
# TODO: Test in IJulia mode.
end
end

# Test printing of models of type `ModelType` for which the model is stored in
# an MOI backend
function model_printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "Model" begin
repl(s) = JuMP.math_symbol(REPLMode, s)
le, ge, eq, fa = repl(:leq), repl(:geq), repl(:eq), repl(:for_all)
Expand Down Expand Up @@ -375,105 +370,165 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel},

VariableType = typeof(a)

if moi_backend
io_test(REPLMode, model_1, """
Max a - b + 2 a1 - 10 x
Subject to
x binary
u[1] binary
u[2] binary
u[3] binary
a1 integer
b1 integer
c1 integer
z integer
fi $eq 9.0
a $ge 1.0
c $ge -1.0
a1 $ge 1.0
c1 $ge -1.0
b $le 1.0
c $le 1.0
b1 $le 1.0
c1 $le 1.0
a + b - 10 c - 2 x + c1 $le 1.0
a*b $le 2.0
[-a + 1, u[1], u[2], u[3]] $inset MathOptInterface.SecondOrderCone(4)
""", repl=:print)
else
# TODO variable constraints
io_test(REPLMode, model_1, """
Max a - b + 2 a1 - 10 x
Subject to
a + b - 10 c - 2 x + c1 $le 1.0
a*b $le 2.0
[-a + 1, u[1], u[2], u[3]] $inset MathOptInterface.SecondOrderCone(4)
""", repl=:print)
end
io_test(REPLMode, model_1, """
Max a - b + 2 a1 - 10 x
Subject to
x binary
u[1] binary
u[2] binary
u[3] binary
a1 integer
b1 integer
c1 integer
z integer
fi $eq 9.0
a $ge 1.0
c $ge -1.0
a1 $ge 1.0
c1 $ge -1.0
b $le 1.0
c $le 1.0
b1 $le 1.0
c1 $le 1.0
a + b - 10 c - 2 x + c1 $le 1.0
a*b $le 2.0
[-a + 1, u[1], u[2], u[3]] $inset MathOptInterface.SecondOrderCone(4)
""", repl=:print)

io_test(REPLMode, model_1, """
A JuMP Model
Maximization problem with:
Variables: 13
Objective function type: JuMP.GenericAffExpr{Float64,$VariableType}
`MathOptInterface.SingleVariable`-in-`MathOptInterface.ZeroOne`: 4 constraints
`MathOptInterface.SingleVariable`-in-`MathOptInterface.Integer`: 4 constraints
`MathOptInterface.SingleVariable`-in-`MathOptInterface.EqualTo{Float64}`: 1 constraint
`MathOptInterface.SingleVariable`-in-`MathOptInterface.GreaterThan{Float64}`: 4 constraints
`MathOptInterface.SingleVariable`-in-`MathOptInterface.LessThan{Float64}`: 4 constraints
`MathOptInterface.ScalarAffineFunction{Float64}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`MathOptInterface.ScalarQuadraticFunction{Float64}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`MathOptInterface.VectorAffineFunction{Float64}`-in-`MathOptInterface.SecondOrderCone`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: a, a1, b, b1, c, c1, fi, u, x, y, z""", repl=:show)

io_test(IJuliaMode, model_1, """
\\begin{alignat*}{1}\\max\\quad & a - b + 2 a1 - 10 x\\\\
\\text{Subject to} \\quad & x binary\\\\
& u_{1} binary\\\\
& u_{2} binary\\\\
& u_{3} binary\\\\
& a1 integer\\\\
& b1 integer\\\\
& c1 integer\\\\
& z integer\\\\
& fi = 9.0\\\\
& a \\geq 1.0\\\\
& c \\geq -1.0\\\\
& a1 \\geq 1.0\\\\
& c1 \\geq -1.0\\\\
& b \\leq 1.0\\\\
& c \\leq 1.0\\\\
& b1 \\leq 1.0\\\\
& c1 \\leq 1.0\\\\
& a + b - 10 c - 2 x + c1 \\leq 1.0\\\\
& a\\times b \\leq 2.0\\\\
& [-a + 1, u_{1}, u_{2}, u_{3}] \\in MathOptInterface.SecondOrderCone(4)\\\\
\\end{alignat*}
""")

#------------------------------------------------------------------

if moi_backend
io_test(REPLMode, model_1, """
A JuMP Model
Maximization problem with:
Variables: 13
Objective function type: JuMP.GenericAffExpr{Float64,$VariableType}
`MathOptInterface.SingleVariable`-in-`MathOptInterface.ZeroOne`: 4 constraints
`MathOptInterface.SingleVariable`-in-`MathOptInterface.Integer`: 4 constraints
`MathOptInterface.SingleVariable`-in-`MathOptInterface.EqualTo{Float64}`: 1 constraint
`MathOptInterface.SingleVariable`-in-`MathOptInterface.GreaterThan{Float64}`: 4 constraints
`MathOptInterface.SingleVariable`-in-`MathOptInterface.LessThan{Float64}`: 4 constraints
`MathOptInterface.ScalarAffineFunction{Float64}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`MathOptInterface.ScalarQuadraticFunction{Float64}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`MathOptInterface.VectorAffineFunction{Float64}`-in-`MathOptInterface.SecondOrderCone`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: a, a1, b, b1, c, c1, fi, u, x, y, z""", repl=:show)
else
io_test(REPLMode, model_1, """
A JuMP Model
Maximization problem with:
Variables: 13
Objective function type: JuMP.GenericAffExpr{Float64,$VariableType}
Constraints: 3
Names registered in the model: a, a1, b, b1, c, c1, fi, u, x, y, z""", repl=:show)
end
model_2 = ModelType()
@variable(model_2, x, Bin)
@variable(model_2, y, Int)
@constraint(model_2, x*y <= 1)

if moi_backend
io_test(IJuliaMode, model_1, """
\\begin{alignat*}{1}\\max\\quad & a - b + 2 a1 - 10 x\\\\
\\text{Subject to} \\quad & x binary\\\\
& u_{1} binary\\\\
& u_{2} binary\\\\
& u_{3} binary\\\\
& a1 integer\\\\
& b1 integer\\\\
& c1 integer\\\\
& z integer\\\\
& fi = 9.0\\\\
& a \\geq 1.0\\\\
& c \\geq -1.0\\\\
& a1 \\geq 1.0\\\\
& c1 \\geq -1.0\\\\
& b \\leq 1.0\\\\
& c \\leq 1.0\\\\
& b1 \\leq 1.0\\\\
& c1 \\leq 1.0\\\\
& a + b - 10 c - 2 x + c1 \\leq 1.0\\\\
& a\\times b \\leq 2.0\\\\
& [-a + 1, u_{1}, u_{2}, u_{3}] \\in MathOptInterface.SecondOrderCone(4)\\\\
\\end{alignat*}
""")
else
io_test(IJuliaMode, model_1, """
\\begin{alignat*}{1}\\max\\quad & a - b + 2 a1 - 10 x\\\\
\\text{Subject to} \\quad & a + b - 10 c - 2 x + c1 \\leq 1.0\\\\
& a\\times b \\leq 2.0\\\\
& [-a + 1, u_{1}, u_{2}, u_{3}] \\in MathOptInterface.SecondOrderCone(4)\\\\
\\end{alignat*}
""")
end
io_test(REPLMode, model_2, """
A JuMP Model
Feasibility problem with:
Variables: 2
`MathOptInterface.SingleVariable`-in-`MathOptInterface.ZeroOne`: 1 constraint
`MathOptInterface.SingleVariable`-in-`MathOptInterface.Integer`: 1 constraint
`MathOptInterface.ScalarQuadraticFunction{Float64}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x, y""", repl=:show)

model_3 = ModelType()
@variable(model_3, x)
@constraint(model_3, x <= 3)

io_test(REPLMode, model_3, """
A JuMP Model
Feasibility problem with:
Variable: 1
`MathOptInterface.ScalarAffineFunction{Float64}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x""", repl=:show)
end
end

# Test printing of models of type `ModelType` for which the model is stored in
# its JuMP form, e.g., as `AbstractVariable`s and `AbstractConstraint`s.
function model_extension_printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "Model" begin
repl(s) = JuMP.math_symbol(REPLMode, s)
le, ge, eq, fa = repl(:leq), repl(:geq), repl(:eq), repl(:for_all)
inset, dots = repl(:in), repl(:dots)
infty, union = repl(:infty), repl(:union)
Vert, sub2 = repl(:Vert), repl(:sub2)
for_all = repl(:for_all)

#------------------------------------------------------------------

model_1 = ModelType()
@variable(model_1, a>=1)
@variable(model_1, b<=1)
@variable(model_1, -1<=c<=1)
@variable(model_1, a1>=1,Int)
@variable(model_1, b1<=1,Int)
@variable(model_1, -1<=c1<=1,Int)
@variable(model_1, x, Bin)
@variable(model_1, y)
@variable(model_1, z, Int)
@variable(model_1, u[1:3], Bin)
@variable(model_1, fi == 9)
@objective(model_1, Max, a - b + 2a1 - 10x)
@constraint(model_1, a + b - 10c - 2x + c1 <= 1)
@constraint(model_1, a*b <= 2)
@constraint(model_1, [1 - a; u] in SecondOrderCone())

VariableType = typeof(a)

# TODO variable constraints
io_test(REPLMode, model_1, """
Max a - b + 2 a1 - 10 x
Subject to
a + b - 10 c - 2 x + c1 $le 1.0
a*b $le 2.0
[-a + 1, u[1], u[2], u[3]] $inset MathOptInterface.SecondOrderCone(4)
""", repl=:print)

io_test(REPLMode, model_1, """
A JuMP Model
Maximization problem with:
Variables: 13
Objective function type: JuMP.GenericAffExpr{Float64,$VariableType}
Constraints: 3
Names registered in the model: a, a1, b, b1, c, c1, fi, u, x, y, z""", repl=:show)

io_test(IJuliaMode, model_1, """
\\begin{alignat*}{1}\\max\\quad & a - b + 2 a1 - 10 x\\\\
\\text{Subject to} \\quad & a + b - 10 c - 2 x + c1 \\leq 1.0\\\\
& a\\times b \\leq 2.0\\\\
& [-a + 1, u_{1}, u_{2}, u_{3}] \\in MathOptInterface.SecondOrderCone(4)\\\\
\\end{alignat*}
""")

#------------------------------------------------------------------

Expand All @@ -482,54 +537,29 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel},
@variable(model_2, y, Int)
@constraint(model_2, x*y <= 1)

if moi_backend
io_test(REPLMode, model_2, """
A JuMP Model
Feasibility problem with:
Variables: 2
`MathOptInterface.SingleVariable`-in-`MathOptInterface.ZeroOne`: 1 constraint
`MathOptInterface.SingleVariable`-in-`MathOptInterface.Integer`: 1 constraint
`MathOptInterface.ScalarQuadraticFunction{Float64}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x, y""", repl=:show)
else
io_test(REPLMode, model_2, """
A JuMP Model
Feasibility problem with:
Variables: 2
Constraint: 1
Names registered in the model: x, y""", repl=:show)
end
io_test(REPLMode, model_2, """
A JuMP Model
Feasibility problem with:
Variables: 2
Constraint: 1
Names registered in the model: x, y""", repl=:show)

model_2 = ModelType()
@variable(model_2, x)
@constraint(model_2, x <= 3)

if moi_backend
io_test(REPLMode, model_2, """
A JuMP Model
Feasibility problem with:
Variable: 1
`MathOptInterface.ScalarAffineFunction{Float64}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x""", repl=:show)
else
io_test(REPLMode, model_2, """
A JuMP Model
Feasibility problem with:
Variable: 1
Constraint: 1
Names registered in the model: x""", repl=:show)
end
model_3 = ModelType()
@variable(model_3, x)
@constraint(model_3, x <= 3)

io_test(REPLMode, model_3, """
A JuMP Model
Feasibility problem with:
Variable: 1
Constraint: 1
Names registered in the model: x""", repl=:show)
end
end

@testset "Printing for JuMP.Model" begin
printing_test(Model, true)
printing_test(Model)
model_printing_test(Model)
@testset "Model with nonlinear terms" begin
eq = JuMP.math_symbol(REPLMode, :eq)
model = Model()
Expand Down Expand Up @@ -574,5 +604,6 @@ end
end

@testset "Printing for JuMPExtension.MyModel" begin
printing_test(JuMPExtension.MyModel, false)
printing_test(JuMPExtension.MyModel)
model_extension_printing_test(JuMPExtension.MyModel)
end

0 comments on commit ff3e01a

Please sign in to comment.