Skip to content

Commit

Permalink
Convert pass_names to AbstractOptimizerAttribute (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Apr 4, 2022
1 parent f79a6ed commit 548c342
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/MOI/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function _check_ret_optimize(model)
end

"""
Optimizer(env::Union{Nothing, Env} = nothing; pass_names::Bool = false)
Optimizer(env::Union{Nothing, Env} = nothing)
Create a new Optimizer object.
Expand All @@ -140,23 +140,29 @@ first argument.
Set optimizer attributes using `MOI.RawOptimizerAttribute` or
`JuMP.set_optimizer_atttribute`.
## Names
## Example
```julia
using JuMP, CPLEX
const env = CPLEX.Env()
model = JuMP.Model(() -> CPLEX.Optimizer(env)
set_optimizer_attribute(model, "CPXPARAM_ScreenOutput", 0)
```
## `CPLEX.PassNames`
By default, variable and constraint names are stored in the MOI wrapper, but are
_not_ passed to the inner CPLEX model object because doing so can lead to a
large performance degradation. The downside of not passing names is that various
log messages from CPLEX will report names like constraint "R1" and variable "C2"
instead of their actual names. You can change this behavior using
`pass_names = true` to force CPLEX.jl to pass variable and constraint names to
the inner CPLEX model object.
## Example
`CPLEX.PassNames` to force CPLEX.jl to pass variable and constraint names to the
inner CPLEX model object:
```julia
using JuMP, CPLEX
const env = CPLEX.Env()
model = JuMP.Model(() -> CPLEX.Optimizer(env; pass_names = true))
set_optimizer_attribute(model, "CPXPARAM_ScreenOutput", 0)
model = JuMP.Model(CPLEX.Optimizer)
set_optimizer_attribute(model, CPLEX.PassNames(), true)
```
"""
mutable struct Optimizer <: MOI.AbstractOptimizer
Expand Down Expand Up @@ -250,10 +256,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
# this into consideration.
pass_names::Bool

function Optimizer(
env::Union{Nothing,Env} = nothing;
pass_names::Bool = false,
)
function Optimizer(env::Union{Nothing,Env} = nothing)
model = new()
model.lp = C_NULL
model.env = env === nothing ? Env() : env
Expand All @@ -270,7 +273,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
Dict{Int,Tuple{_ConstraintInfo,MOI.VectorAffineFunction{Float64}}}()
model.callback_variable_primal = Float64[]
model.certificate = Float64[]
model.pass_names = pass_names
model.pass_names = false
MOI.empty!(model)
finalizer(model) do m
ret = CPXfreeprob(m.env, Ref(m.lp))
Expand Down Expand Up @@ -355,6 +358,20 @@ function MOI.is_empty(model::Optimizer)
return true
end

"""
PassNames() <: MOI.AbstractOptimizerAttribute
An optimizer attribute to control whether CPLEX.jl should pass names to the
inner CPLEX model object. See the docstring of `CPLEX.Optimizer` for more
information.
"""
struct PassNames <: MOI.AbstractOptimizerAttribute end

function MOI.set(model::Optimizer, ::PassNames, value::Bool)
model.pass_names = value
return
end

MOI.get(::Optimizer, ::MOI.SolverName) = "CPLEX"

MOI.get(::Optimizer, ::MOI.SolverVersion) = string(_CPLEX_VERSION)
Expand Down
8 changes: 8 additions & 0 deletions test/MathOptInterface/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ function test_getlongparam()
return
end

function test_PassNames()
model = CPLEX.Optimizer()
@test model.pass_names == false
MOI.set(model, CPLEX.PassNames(), true)
@test model.pass_names == true
return
end

end # module TestMOIwrapper

TestMOIwrapper.runtests()

2 comments on commit 548c342

@odow
Copy link
Member Author

@odow odow commented on 548c342 Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/57880

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.2 -m "<description of version>" 548c342cd77c518ed0f9d1d5a18008bc9e0dd712
git push origin v0.9.2

Please sign in to comment.