Skip to content

Commit

Permalink
Merge pull request #75 from StructJuMP/bl/print
Browse files Browse the repository at this point in the history
Implement printing
  • Loading branch information
blegat committed Sep 3, 2019
2 parents a35bd52 + a29e1fc commit 87e0ce8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
32 changes: 29 additions & 3 deletions src/StructJuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,11 @@ function JuMP.set_objective(m::StructuredModel, sense::MOI.OptimizationSense,
m.objective_function = f
end
JuMP.objective_sense(m::StructuredModel) = m.objective_sense
function JuMP.objective_function(m::StructuredModel, FT::Type)
m.objective_function isa FT || error("The objective function is not of type $FT")
m.objective_function
JuMP.objective_function_type(model::StructuredModel) = typeof(model.objective_function)
JuMP.objective_function(model::StructuredModel) = model.objective_function
function JuMP.objective_function(model::StructuredModel, FT::Type)
model.objective_function isa FT || error("The objective function is not of type $FT")
model.objective_function
end

# Names
Expand All @@ -275,6 +277,30 @@ function JuMP.set_name(cref::StructuredConstraintRef, name::String)
cref.model.connames[cref.idx] = name
end

# Show
function JuMP.show_backend_summary(io::IO, model::StructuredModel) end
function JuMP.show_objective_function_summary(io::IO, model::StructuredModel)
println(io, "Objective function type: ",
JuMP.objective_function_type(model))
end
function JuMP.objective_function_string(print_mode, model::StructuredModel)
return JuMP.function_string(print_mode, JuMP.objective_function(model))
end
_plural(n) = (isone(n) ? "" : "s")
function JuMP.show_constraints_summary(io::IO, model::StructuredModel)
n = length(model.constraints)
print(io, "Constraint", _plural(n), ": ", n)
end
function JuMP.constraints_string(print_mode, model::StructuredModel)
strings = String[]
# Sort by creation order, i.e. ConstraintIndex value
constraints = sort(collect(model.constraints), by = c -> c.first.value)
for (index, constraint) in constraints
push!(strings, JuMP.constraint_string(print_mode, constraint))
end
return strings
end

include("BendersBridge.jl")

end
33 changes: 25 additions & 8 deletions test/printhook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,38 @@ using StructJuMP
@testset "printhook" begin

numScen = 2
m = StructuredModel(num_scenarios=numScen)
parent = StructuredModel(num_scenarios=numScen)

@variable(m, 0 <= x <= 1)
@variable(m, 0 <= y <= 1)
@variable(parent, 0 <= x <= 1)
@variable(parent, 0 <= y <= 1)

@constraint(m, x + y == 1)
@objective(m, Min, x*x + y)
@constraint(parent, x + y == 1)
@objective(parent, Min, x*x + y)

for i in 1:numScen
bl = StructuredModel(parent=m, id=i)
bl = StructuredModel(parent=parent, id=i)
@variable(bl, w >= 0)
@constraint(bl, w - x - y <= 1)
@objective(bl, Min, w*w + w)
end

str = string(m)
@test occursin("Child", str)
str = string(parent)
@test sprint(print, parent) == """
Min x² + y
Subject to
x + y = 1.0
"""
@test sprint(show, parent) == """
A JuMP Model
Minimization problem with:
Variables: 2
Objective function type: GenericQuadExpr{Float64,StructJuMP.StructuredVariableRef}
Constraint: 1
Names registered in the model: x, y"""
@test sprint(show, "text/latex", parent) == """
\$\$ \\begin{alignat*}{1}\\min\\quad & x^2 + y\\\\
\\text{Subject to} \\quad & x + y = 1.0\\\\
\\end{alignat*}
\$\$"""
#@test occursin("Child", str)
end
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include("printhook.jl")
#
include("printhook.jl")

include("benderstest.jl")
#
#include("farmer.jl")
Expand Down

0 comments on commit 87e0ce8

Please sign in to comment.