Skip to content
Closed
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
4 changes: 1 addition & 3 deletions src/MathOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ abstract type ModelLike end
# embed it in a `Ref`
Base.broadcastable(model::ModelLike) = Ref(model)

Base.show(io::IO, model::ModelLike) = Utilities.print_with_acronym(io, summary(model))


"""
AbstractOptimizer

Expand Down Expand Up @@ -138,6 +135,7 @@ include("constraints.jl")
include("modifications.jl")
include("variables.jl")
include("nlp.jl")
include("print.jl")

# submodules
include("Utilities/Utilities.jl") # MOI.Utilities
Expand Down
58 changes: 58 additions & 0 deletions src/Test/modellike.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ function default_status_test(model::MOI.ModelLike)
@test MOI.get(model, MOI.DualStatus()) == MOI.NO_SOLUTION
end

# Helper function to test IO methods work correctly
function io_test(mode, obj, exp_str, args...; repl=:both, kws...)
if mode == MOI.REPLMode
repl != :show && @test sprint(print, obj, args...; kws...) == exp_str
repl != :print && @test sprint(show, obj, args...; kws...) == exp_str
else
@test sprint(show, "text/latex", obj, args...; kws...) == string("\$\$ ", exp_str, " \$\$")
end
end

function nametest(model::MOI.ModelLike)
@testset "Variables" begin
MOI.empty!(model)
Expand Down Expand Up @@ -52,6 +62,24 @@ function nametest(model::MOI.ModelLike)
@test MOI.get(model, MOI.ConstraintIndex, "c2") == c1
MOI.set(model, MOI.ConstraintName(), c2, "c1")
@test MOI.get(model, MOI.ConstraintIndex, "c1") == c2
io_test(MOI.IJuliaMode, model, """
\\begin{alignat*}{1}\\text{feasibility}\\\\
\\text{Subject to} \\quad & noname \\geq 0.0\\\\
& noname \\leq 1.0\\\\
\\end{alignat*}
""")
io_test(MOI.REPLMode, model, """
Feasibility
Subject to
c2 : noname ≥ 0.0
c1 : noname ≤ 1.0
""", repl=:print)
io_test(MOI.REPLMode, model, """
Feasibility
Subject to
c2 : x[$(x.value)] ≥ 0.0
c1 : x[$(x.value)] ≤ 1.0
""", repl=:print, context = :variable_name => MOI.name_or_default_name)
MOI.set(model, MOI.ConstraintName(), c1, "c1")
@test_throws ErrorException MOI.get(model, MOI.ConstraintIndex, "c1")
end
Expand All @@ -69,6 +97,18 @@ function nametest(model::MOI.ModelLike)
@test MOI.get(model, MOI.ConstraintIndex, "c2") == c1
MOI.set(model, MOI.ConstraintName(), c2, "c1")
@test MOI.get(model, MOI.ConstraintIndex, "c1") == c2
io_test(MOI.REPLMode, model, """
Feasibility
Subject to
c2 : noname ≥ 0.0
c1 : noname ≤ 1.0
""", repl=:print)
io_test(MOI.REPLMode, model, """
Feasibility
Subject to
c2 : x[$(x.value)] ≥ 0.0
c1 : x[$(x.value)] ≤ 1.0
""", repl=:print, context = :variable_name => MOI.name_or_default_name)
MOI.set(model, MOI.ConstraintName(), c1, "c1")
@test_throws ErrorException MOI.get(model, MOI.ConstraintIndex, "c1")
end
Expand Down Expand Up @@ -198,6 +238,24 @@ function nametest(model::MOI.ModelLike)
end
end

io_test(MOI.IJuliaMode, model, """
\\begin{alignat*}{1}\\text{feasibility}\\\\
\\text{Subject to} \\quad & -VarX + Var2 = 0.0\\\\
& VarX + Var2 \\leq 1.0\\\\
& [Vary1, Vary2, Vary3, Vary4] \\in MathOptInterface.Nonpositives(4)\\\\
& Varx \\geq 0.0\\\\
\\end{alignat*}
""")

io_test(MOI.REPLMode, model, """
Feasibility
Subject to
Con2 : -VarX + Var2 = 0.0
Con1 : VarX + Var2 ≤ 1.0
Con4 : [Vary1, Vary2, Vary3, Vary4] ∈ MathOptInterface.Nonpositives(4)
Con3 : Varx ≥ 0.0
""", repl=:print)

MOI.delete(model, v[2])
@test MOI.get(model, MOI.VariableIndex, "Var2") === nothing

Expand Down
Loading