Skip to content

Commit

Permalink
Allow MOI.ModelLike as the optimizer instead of MOI.AbstractOptimizer (
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Feb 7, 2024
1 parent 65250ba commit e2149bf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ value_type(::Type{<:AbstractModel}) = Float64
mutable struct GenericModel{T<:Real} <: AbstractModel
# In MANUAL and AUTOMATIC modes, CachingOptimizer.
# In DIRECT mode, will hold an AbstractOptimizer.
moi_backend::MOI.AbstractOptimizer
moi_backend::MOI.ModelLike
# List of shapes of constraints that are not `ScalarShape` or `VectorShape`.
shapes::Dict{MOI.ConstraintIndex,AbstractShape}
# List of bridges to add in addition to the ones added in
Expand Down
9 changes: 9 additions & 0 deletions src/optimizer_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,15 @@ function optimize!(
if mode(model) != DIRECT && MOIU.state(backend(model)) == MOIU.NO_OPTIMIZER
throw(NoOptimizer())
end
optimizer = unsafe_backend(model)
if !(optimizer isa MOI.AbstractOptimizer)
error(
"Cannot call `optimize!` because the provided optimizer is not " *
"a subtype of `MOI.AbstractOptimizer`.\n\nThe optimizer is:\n\n" *
sprint(show, optimizer) *
"\n",
)
end
try
MOI.optimize!(backend(model))
catch err
Expand Down
7 changes: 6 additions & 1 deletion src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,12 @@ function show_backend_summary(io::IO, model::GenericModel)
println(io, "CachingOptimizer state: ", MOIU.state(backend(model)))
end
# The last print shouldn't have a new line
print(io, "Solver name: ", solver_name(model))
name = try
solver_name(model)
catch
"unknown"
end
print(io, "Solver name: ", name)
return
end

Expand Down
28 changes: 28 additions & 0 deletions test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1216,4 +1216,32 @@ function test_show_variable_not_owned()
return
end

function test_direct_mps_model()
model = direct_model(MOI.FileFormats.MPS.Model())
@test occursin("unknown", sprint(show, model))
@variable(model, x >= 0)
io = IOBuffer()
write(io, backend(model))
seekstart(io)
data = String(take!(io))
@test startswith(data, "NAME")
@test endswith(data, "ENDATA\n")
return
end

function test_caching_mps_model()
model = Model(MOI.FileFormats.MPS.Model)
@test occursin("unknown", sprint(show, model))
@variable(model, x >= 0)
@test_throws(
ErrorException(
"Cannot call `optimize!` because the provided optimizer is not " *
"a subtype of `MOI.AbstractOptimizer`.\n\nThe optimizer is:\n\n" *
"A Mathematical Programming System (MPS) model\n",
),
optimize!(model),
)
return
end

end # module TestModels

0 comments on commit e2149bf

Please sign in to comment.