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
76 changes: 35 additions & 41 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,30 @@ mutable struct Model <: AbstractModel
end

"""
Model(; caching_mode::MOIU.CachingOptimizerMode=MOIU.AUTOMATIC)
Model(
[optimizer_factory];
caching_mode::MOIU.CachingOptimizerMode = MOIU.AUTOMATIC,
bridge_constraints::Bool = true,
auto_bridge::Bool = false,
)

Return a new JuMP model based on a `CachingOptimizer` in `caching_mode`.

See [`set_optimizer`](@ref) for the description of the `optimizer_factory` and
`bridge_constraints` arguments.

If `optimizer_factory` is not provided, use [`set_optimizer`](@ref) to set the
optimizer before calling [`optimize!`](@ref).

Return a new JuMP model without any optimizer; the model is stored the model in
a cache. The mode of the `CachingOptimizer` storing this cache is
`caching_mode`. Use [`set_optimizer`](@ref) to set the optimizer before
calling [`optimize!`](@ref).
If `auto_bridge == true`, the `CachingOptimizer` will automatically add bridges
to the underlying optimizer only if necessary, and ignore the
`bridge_constraints` argument.
"""
function Model(;
function Model(
optimizer_factory = nothing;
caching_mode::MOIU.CachingOptimizerMode = MOIU.AUTOMATIC,
bridge_constraints::Bool = true,
auto_bridge::Bool = false,
solver = nothing,
)
if solver !== nothing
Expand All @@ -254,41 +269,20 @@ function Model(;
"(https://jump.dev/JuMP.jl/latest/) for latest syntax.",
)
end
universal_fallback = MOIU.UniversalFallback(MOIU.Model{Float64}())
caching_opt = MOIU.CachingOptimizer(universal_fallback, caching_mode)
return direct_model(caching_opt)
end

"""
Model(optimizer_factory;
caching_mode::MOIU.CachingOptimizerMode=MOIU.AUTOMATIC,
bridge_constraints::Bool=true)

Return a new JuMP model with the provided optimizer and bridge settings. This
function is equivalent to:
```julia
model = Model()
set_optimizer(model, optimizer_factory,
bridge_constraints=bridge_constraints)
return model
```
See [`set_optimizer`](@ref) for the description of the `optimizer_factory` and
`bridge_constraints` arguments.

## Examples

The following creates a model with the optimizer set to `Ipopt`:
```julia
model = Model(Ipopt.Optimizer)
```
"""
function Model(optimizer_factory; bridge_constraints::Bool = true, kwargs...)
model = Model(; kwargs...)
set_optimizer(
model,
optimizer_factory,
bridge_constraints = bridge_constraints,
cache = MOIU.UniversalFallback(MOIU.Model{Float64}())
caching_opt = MOIU.CachingOptimizer(
cache;
mode = caching_mode,
auto_bridge = auto_bridge,
)
model = direct_model(caching_opt)
if optimizer_factory !== nothing
set_optimizer(
model,
optimizer_factory,
bridge_constraints = !auto_bridge && bridge_constraints
)
end
return model
end

Expand Down Expand Up @@ -718,7 +712,7 @@ the attribute MathOptInterface.SolveTime()" if the attribute is
not implemented.
"""
function solve_time(model::Model)
return MOI.get(model, MOI.SolveTime())
return MOI.get(model, MOI.SolveTimeSec())
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/aff_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ end
function AffExpr(m::Model, f::MOI.ScalarAffineFunction)
aff = AffExpr()
for t in f.terms
add_to_expression!(aff, t.coefficient, VariableRef(m, t.variable_index))
add_to_expression!(aff, t.coefficient, VariableRef(m, t.variable))
end
aff.constant = f.constant
return aff
Expand Down
4 changes: 2 additions & 2 deletions src/quad_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ function QuadExpr(m::Model, f::MOI.ScalarQuadraticFunction)
AffExpr(m, MOI.ScalarAffineFunction(f.affine_terms, f.constant)),
)
for t in f.quadratic_terms
v1 = t.variable_index_1
v2 = t.variable_index_2
v1 = t.variable_1
v2 = t.variable_2
coef = t.coefficient
if v1 == v2
coef /= 2
Expand Down
2 changes: 1 addition & 1 deletion test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ end
MOI.set(mockoptimizer, MOI.SimplexIterations(), 1)
MOI.set(mockoptimizer, MOI.BarrierIterations(), 1)
MOI.set(mockoptimizer, MOI.NodeCount(), 1)
MOI.set(mockoptimizer, MOI.SolveTime(), 5.0)
MOI.set(mockoptimizer, MOI.SolveTimeSec(), 5.0)

@test sprint(show, solution_summary(model)) == """
* Solver : Mock
Expand Down