|
| 1 | +""" |
| 2 | + struct OptimizerWithAttributes |
| 3 | + optimizer_constructor |
| 4 | + params::Vector{Pair{AbstractOptimizerAttribute,<:Any}} |
| 5 | + end |
| 6 | +
|
| 7 | +Object grouping an optimizer constructor and a list of optimizer attributes. |
| 8 | +Instances are created with [`instantiate`](@ref). |
| 9 | +""" |
| 10 | +struct OptimizerWithAttributes |
| 11 | + # Function that takes zero arguments and returns a new optimizer. |
| 12 | + # The type of the function could be |
| 13 | + # * `Function`: a function, or |
| 14 | + # * `DataType`: a type, or |
| 15 | + # * `UnionAll`: a type with missing parameters. |
| 16 | + optimizer_constructor |
| 17 | + params::Vector{Pair{AbstractOptimizerAttribute,Any}} |
| 18 | +end |
| 19 | + |
| 20 | +_to_param(param::Pair{<:AbstractOptimizerAttribute}) = param |
| 21 | +_to_param(param::Pair{String}) = RawParameter(param.first) => param.second |
| 22 | +function _to_param(param::Pair) |
| 23 | + error("Expected an optimizer attribute or a string, got `$(param.first)` which is a `$(typeof(param.first))`.") |
| 24 | +end |
| 25 | + |
| 26 | +""" |
| 27 | + OptimizerWithAttributes(optimizer_constructor, params::Pair...) |
| 28 | +
|
| 29 | +Create an [`OptimizerWithAttributes`](@ref) with the parameters `params`. |
| 30 | +""" |
| 31 | +function OptimizerWithAttributes(optimizer_constructor, args::Vararg{Pair, N}) where N |
| 32 | + if !applicable(optimizer_constructor) |
| 33 | + error(_INSTANTIATE_NOT_CALLABLE_MESSAGE) |
| 34 | + end |
| 35 | + params = Pair{AbstractOptimizerAttribute,Any}[_to_param(arg) for arg in args] |
| 36 | + return OptimizerWithAttributes(optimizer_constructor, params) |
| 37 | +end |
| 38 | + |
| 39 | +const _INSTANTIATE_NOT_CALLABLE_MESSAGE = |
| 40 | + "The provided `optimizer_constructor` is invalid. It must be callable with zero " * |
| 41 | + "arguments. For example, \"Ipopt.Optimizer\" or " * |
| 42 | + "\"() -> ECOS.Optimizer()\". It should not be an instantiated optimizer " * |
| 43 | + "like \"Ipopt.Optimizer()\" or \"ECOS.Optimizer()\". " * |
| 44 | + "(Note the difference in parentheses!)" |
| 45 | + |
| 46 | +""" |
| 47 | + _instantiate_and_check(optimizer_constructor) |
| 48 | +
|
| 49 | +Create an instance of optimizer by calling `optimizer_constructor`. |
| 50 | +Then check that the type returned is an empty [`AbstractOptimizer`](@ref). |
| 51 | +""" |
| 52 | +function _instantiate_and_check(optimizer_constructor) |
| 53 | + if !applicable(optimizer_constructor) |
| 54 | + error(_INSTANTIATE_NOT_CALLABLE_MESSAGE) |
| 55 | + end |
| 56 | + optimizer = optimizer_constructor() |
| 57 | + if !isa(optimizer, AbstractOptimizer) |
| 58 | + error("The provided `optimizer_constructor` returned an object of type " * |
| 59 | + "$(typeof(optimizer)). Expected a " * |
| 60 | + "MathOptInterface.AbstractOptimizer.") |
| 61 | + end |
| 62 | + if !is_empty(optimizer) |
| 63 | + error("The provided `optimizer_constructor` returned a non-empty optimizer.") |
| 64 | + end |
| 65 | + return optimizer |
| 66 | +end |
| 67 | + |
| 68 | +""" |
| 69 | + _instantiate_and_check(optimizer_constructor::OptimizerWithAttributes) |
| 70 | +
|
| 71 | +Create an instance of optimizer represented by [`OptimizerWithAttributes`](@ref). |
| 72 | +Then check that the type returned is an empty [`AbstractOptimizer`](@ref). |
| 73 | +""" |
| 74 | +function _instantiate_and_check(optimizer_constructor::OptimizerWithAttributes) |
| 75 | + optimizer = _instantiate_and_check(optimizer_constructor.optimizer_constructor) |
| 76 | + for param in optimizer_constructor.params |
| 77 | + set(optimizer, param.first, param.second) |
| 78 | + end |
| 79 | + return optimizer |
| 80 | +end |
| 81 | + |
| 82 | +""" |
| 83 | + instantiate(optimizer_constructor, |
| 84 | + with_bridge_type::Union{Nothing, Type}=nothing, |
| 85 | + with_names::Bool=false) |
| 86 | +
|
| 87 | +Creates an instance of optimizer either by calling |
| 88 | +`optimizer_constructor.optimizer_constructor()` and setting the parameters in |
| 89 | +`optimizer_constructor.params` if `optimizer_constructor` is a |
| 90 | +[`OptimizerWithAttributes`](@ref) or by calling `optimizer_constructor()` |
| 91 | +if `optimizer_constructor` is callable. |
| 92 | +
|
| 93 | +If `with_bridge_type` is not `nothing`, it enables all the bridges defined in |
| 94 | +the MathOptInterface.Bridges submodule with coefficient type |
| 95 | +`with_bridge_type`. |
| 96 | +
|
| 97 | +If the optimizer created by `optimizer_constructor` does not support loading the |
| 98 | +problem incrementally or does not support names and `with_names` is `true` (see |
| 99 | +[`Utilities.supports_default_copy_to`](@ref)) then a |
| 100 | +[`Utilities.CachingOptimizer`](@ref) is added to store a cache of the bridged |
| 101 | +model. |
| 102 | +Hence set `with_names` to `true` if names might be set. |
| 103 | +""" |
| 104 | +function instantiate( |
| 105 | + optimizer_constructor; with_bridge_type::Union{Nothing, Type}=nothing, |
| 106 | + with_names::Bool=false) |
| 107 | + optimizer = _instantiate_and_check(optimizer_constructor) |
| 108 | + if with_bridge_type === nothing |
| 109 | + return optimizer |
| 110 | + end |
| 111 | + if !Utilities.supports_default_copy_to(optimizer, with_names) |
| 112 | + universal_fallback = Utilities.UniversalFallback(Utilities.Model{with_bridge_type}()) |
| 113 | + optimizer = Utilities.CachingOptimizer(universal_fallback, optimizer) |
| 114 | + end |
| 115 | + return Bridges.full_bridge_optimizer(optimizer, with_bridge_type) |
| 116 | +end |
0 commit comments