|
| 1 | +# Copyright (c) 2017: Miles Lubin and contributors |
| 2 | +# Copyright (c) 2017: Google Inc. |
| 3 | +# |
| 4 | +# Use of this source code is governed by an MIT-style license that can be found |
| 5 | +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. |
| 6 | + |
| 7 | +""" |
| 8 | + VectorFunctionizeBridge{T} |
| 9 | +
|
| 10 | +`VectorFunctionizeBridge` implements the following reformulations: |
| 11 | +
|
| 12 | + * ``\\min \\{x\\}`` into ``\\min\\{1x + 0\\}`` |
| 13 | + * ``\\max \\{x\\}`` into ``\\max\\{1x + 0\\}`` |
| 14 | +
|
| 15 | +where `T` is the coefficient type of `1` and `0`. |
| 16 | +
|
| 17 | +## Source node |
| 18 | +
|
| 19 | +`VectorFunctionizeBridge` supports: |
| 20 | +
|
| 21 | + * [`MOI.ObjectiveFunction{MOI.VectorOfVariables}`](@ref) |
| 22 | +
|
| 23 | +## Target nodes |
| 24 | +
|
| 25 | +`VectorFunctionizeBridge` creates: |
| 26 | +
|
| 27 | + * One objective node: [`MOI.ObjectiveFunction{MOI.VectorAffineFunction{T}}`](@ref) |
| 28 | +""" |
| 29 | +struct VectorFunctionizeBridge{T} <: AbstractBridge end |
| 30 | + |
| 31 | +const VectorFunctionize{T,OT<:MOI.ModelLike} = |
| 32 | + SingleBridgeOptimizer{VectorFunctionizeBridge{T},OT} |
| 33 | + |
| 34 | +function bridge_objective( |
| 35 | + ::Type{VectorFunctionizeBridge{T}}, |
| 36 | + model::MOI.ModelLike, |
| 37 | + f::MOI.VectorOfVariables, |
| 38 | +) where {T} |
| 39 | + F = MOI.VectorAffineFunction{T} |
| 40 | + MOI.set(model, MOI.ObjectiveFunction{F}(), convert(F, f)) |
| 41 | + return VectorFunctionizeBridge{T}() |
| 42 | +end |
| 43 | + |
| 44 | +function supports_objective_function( |
| 45 | + ::Type{<:VectorFunctionizeBridge}, |
| 46 | + ::Type{MOI.VectorOfVariables}, |
| 47 | +) |
| 48 | + return true |
| 49 | +end |
| 50 | + |
| 51 | +function MOI.Bridges.added_constrained_variable_types( |
| 52 | + ::Type{<:VectorFunctionizeBridge}, |
| 53 | +) |
| 54 | + return Tuple{Type}[] |
| 55 | +end |
| 56 | + |
| 57 | +function MOI.Bridges.added_constraint_types(::Type{<:VectorFunctionizeBridge}) |
| 58 | + return Tuple{Type,Type}[] |
| 59 | +end |
| 60 | + |
| 61 | +function MOI.Bridges.set_objective_function_type( |
| 62 | + ::Type{VectorFunctionizeBridge{T}}, |
| 63 | +) where {T} |
| 64 | + return MOI.VectorAffineFunction{T} |
| 65 | +end |
| 66 | + |
| 67 | +MOI.get(::VectorFunctionizeBridge, ::MOI.NumberOfVariables)::Int64 = 0 |
| 68 | + |
| 69 | +function MOI.get(::VectorFunctionizeBridge, ::MOI.ListOfVariableIndices) |
| 70 | + return MOI.VariableIndex[] |
| 71 | +end |
| 72 | + |
| 73 | +MOI.delete(::MOI.ModelLike, ::VectorFunctionizeBridge) = nothing |
| 74 | + |
| 75 | +function MOI.set( |
| 76 | + ::MOI.ModelLike, |
| 77 | + ::MOI.ObjectiveSense, |
| 78 | + ::VectorFunctionizeBridge, |
| 79 | + ::MOI.OptimizationSense, |
| 80 | +) |
| 81 | + # `VectorFunctionizeBridge` is sense agnostic, therefore, we don't need to |
| 82 | + # change anything. |
| 83 | + return |
| 84 | +end |
| 85 | + |
| 86 | +function MOI.get( |
| 87 | + model::MOI.ModelLike, |
| 88 | + attr::MOI.Bridges.ObjectiveFunctionValue{MOI.VectorOfVariables}, |
| 89 | + ::VectorFunctionizeBridge{T}, |
| 90 | +) where {T} |
| 91 | + F = MOI.VectorAffineFunction{T} |
| 92 | + attr_f = MOI.Bridges.ObjectiveFunctionValue{F}(attr.result_index) |
| 93 | + return MOI.get(model, attr_f) |
| 94 | +end |
| 95 | + |
| 96 | +function MOI.get( |
| 97 | + model::MOI.ModelLike, |
| 98 | + ::MOI.ObjectiveFunction{MOI.VectorOfVariables}, |
| 99 | + ::VectorFunctionizeBridge{T}, |
| 100 | +) where {T} |
| 101 | + f = MOI.get(model, MOI.ObjectiveFunction{MOI.VectorAffineFunction{T}}()) |
| 102 | + return convert(MOI.VectorOfVariables, f) |
| 103 | +end |
0 commit comments