|
1 |
| -struct SplitZeroBridge{T, F<:MOI.Utilities.TypedLike{T}, G<:MOI.Utilities.TypedLike{Complex{T}}} <: MOI.Bridges.Constraint.AbstractBridge |
| 1 | +struct SplitZeroBridge{ |
| 2 | + T, |
| 3 | + F<:MOI.Utilities.TypedLike{T}, |
| 4 | + G<:MOI.Utilities.TypedLike{Complex{T}}, |
| 5 | +} <: MOI.Bridges.Constraint.AbstractBridge |
2 | 6 | dimension::Int
|
3 |
| - constraint::MOI.ConstraintIndex{F, MOI.Zeros} |
| 7 | + constraint::MOI.ConstraintIndex{F,MOI.Zeros} |
4 | 8 | real_indices::Vector{Int}
|
5 | 9 | imag_indices::Vector{Int}
|
6 | 10 | end
|
7 | 11 | function _nonzero_indices(func::MOI.AbstractVectorFunction)
|
8 |
| - return [i for (i, scalar_func) in enumerate(MOIU.scalarize(func)) if !iszero(scalar_func)] |
| 12 | + return [ |
| 13 | + i for (i, scalar_func) in enumerate(MOIU.scalarize(func)) if !iszero(scalar_func) |
| 14 | + ] |
9 | 15 | end
|
10 | 16 | function MOI.Bridges.Constraint.bridge_constraint(
|
11 |
| - ::Type{SplitZeroBridge{T, F, G}}, model::MOI.ModelLike, |
| 17 | + ::Type{SplitZeroBridge{T,F,G}}, |
| 18 | + model::MOI.ModelLike, |
12 | 19 | f::G,
|
13 |
| - set::MOI.Zeros |
14 |
| -) where {T, F, G} |
| 20 | + set::MOI.Zeros, |
| 21 | +) where {T,F,G} |
15 | 22 | real_part = real(f)
|
16 | 23 | imag_part = MOI.Utilities.operate(imag, T, f)
|
17 | 24 | real_indices = _nonzero_indices(real_part)
|
18 | 25 | imag_indices = _nonzero_indices(imag_part)
|
19 | 26 | func = MOIU.operate(
|
20 |
| - vcat, T, |
| 27 | + vcat, |
| 28 | + T, |
21 | 29 | MOIU.eachscalar(real_part)[real_indices],
|
22 |
| - MOIU.eachscalar(imag_part)[imag_indices] |
| 30 | + MOIU.eachscalar(imag_part)[imag_indices], |
| 31 | + ) |
| 32 | + constraint = MOI.add_constraint( |
| 33 | + model, |
| 34 | + func, |
| 35 | + MOI.Zeros(length(real_indices) + length(imag_indices)), |
| 36 | + ) |
| 37 | + return SplitZeroBridge{T,F,G}( |
| 38 | + MOI.dimension(set), |
| 39 | + constraint, |
| 40 | + real_indices, |
| 41 | + imag_indices, |
23 | 42 | )
|
24 |
| - constraint = MOI.add_constraint(model, func, MOI.Zeros(length(real_indices) + length(imag_indices))) |
25 |
| - return SplitZeroBridge{T, F, G}(MOI.dimension(set), constraint, real_indices, imag_indices) |
26 | 43 | end
|
27 | 44 |
|
28 | 45 | # We don't support `MOI.VectorOfVariables` as it would be a self-loop in the bridge graph
|
29 | 46 | function MOI.supports_constraint(
|
30 |
| - ::Type{SplitZeroBridge{T}}, ::Type{<:MOI.Utilities.TypedLike{Complex{T}}}, |
31 |
| - ::Type{MOI.Zeros}) where T |
| 47 | + ::Type{SplitZeroBridge{T}}, |
| 48 | + ::Type{<:MOI.Utilities.TypedLike{Complex{T}}}, |
| 49 | + ::Type{MOI.Zeros}, |
| 50 | +) where {T} |
32 | 51 | return true
|
33 | 52 | end
|
34 | 53 | MOIB.added_constrained_variable_types(::Type{<:SplitZeroBridge}) = Tuple{DataType}[]
|
35 |
| -function MOIB.added_constraint_types(::Type{SplitZeroBridge{T, F, G}}) where {T, F, G} |
36 |
| - return Tuple{DataType, DataType}[(F, MOI.Zeros)] |
| 54 | +function MOIB.added_constraint_types(::Type{SplitZeroBridge{T,F,G}}) where {T,F,G} |
| 55 | + return Tuple{DataType,DataType}[(F, MOI.Zeros)] |
37 | 56 | end
|
38 | 57 | function MOI.Bridges.Constraint.concrete_bridge_type(
|
39 |
| - ::Type{<:SplitZeroBridge{T}}, G::Type{<:MOI.Utilities.TypedLike}, |
40 |
| - ::Type{MOI.Zeros}) where T |
| 58 | + ::Type{<:SplitZeroBridge{T}}, |
| 59 | + G::Type{<:MOI.Utilities.TypedLike}, |
| 60 | + ::Type{MOI.Zeros}, |
| 61 | +) where {T} |
41 | 62 | F = MA.promote_operation(imag, G)
|
42 |
| - return SplitZeroBridge{T, F, G} |
| 63 | + return SplitZeroBridge{T,F,G} |
43 | 64 | end
|
44 | 65 |
|
45 | 66 | # Attributes, Bridge acting as a model
|
46 |
| -function MOI.get(::SplitZeroBridge{T, F}, |
47 |
| - ::MOI.NumberOfConstraints{F, MOI.Zeros}) where {T, F} |
| 67 | +function MOI.get(::SplitZeroBridge{T,F}, ::MOI.NumberOfConstraints{F,MOI.Zeros}) where {T,F} |
48 | 68 | return 1
|
49 | 69 | end
|
50 |
| -function MOI.get(bridge::SplitZeroBridge{T, F}, |
51 |
| - ::MOI.ListOfConstraintIndices{F, MOI.Zeros}) where {T, F} |
| 70 | +function MOI.get( |
| 71 | + bridge::SplitZeroBridge{T,F}, |
| 72 | + ::MOI.ListOfConstraintIndices{F,MOI.Zeros}, |
| 73 | +) where {T,F} |
52 | 74 | return [bridge.constraint]
|
53 | 75 | end
|
54 | 76 |
|
|
60 | 82 | # Attributes, Bridge acting as a constraint
|
61 | 83 | function MOI.supports(
|
62 | 84 | ::MOI.ModelLike,
|
63 |
| - ::Union{MOI.ConstraintPrimalStart, MOI.ConstraintDualStart}, |
64 |
| - ::Type{<:SplitZeroBridge}) |
| 85 | + ::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart}, |
| 86 | + ::Type{<:SplitZeroBridge}, |
| 87 | +) |
65 | 88 |
|
66 | 89 | return true
|
67 | 90 | end
|
68 |
| -function MOI.get(model::MOI.ModelLike, attr::Union{MOI.ConstraintPrimal, MOI.ConstraintPrimalStart, MOI.ConstraintDual, MOI.ConstraintDualStart}, |
69 |
| - bridge::SplitZeroBridge) |
| 91 | +function MOI.get( |
| 92 | + model::MOI.ModelLike, |
| 93 | + attr::Union{ |
| 94 | + MOI.ConstraintPrimal, |
| 95 | + MOI.ConstraintPrimalStart, |
| 96 | + MOI.ConstraintDual, |
| 97 | + MOI.ConstraintDualStart, |
| 98 | + }, |
| 99 | + bridge::SplitZeroBridge, |
| 100 | +) |
70 | 101 | values = MOI.get(model, attr, bridge.constraint)
|
71 | 102 | output = zeros(Complex{eltype(values)}, bridge.dimension)
|
72 | 103 | for (i, idx) in enumerate(bridge.real_indices)
|
73 | 104 | output[idx] = values[i]
|
74 | 105 | end
|
75 | 106 | for (i, idx) in enumerate(bridge.imag_indices)
|
76 |
| - output[idx] = values[length(bridge.real_indices) + i] * im |
| 107 | + output[idx] = values[length(bridge.real_indices)+i] * im |
77 | 108 | end
|
78 | 109 | return output
|
79 | 110 | end
|
80 |
| -function MOI.set(model::MOI.ModelLike, attr::Union{MOI.ConstraintPrimalStart, MOI.ConstraintDualStart}, |
81 |
| - bridge::SplitZeroBridge{T}, value) where T |
| 111 | +function MOI.set( |
| 112 | + model::MOI.ModelLike, |
| 113 | + attr::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart}, |
| 114 | + bridge::SplitZeroBridge{T}, |
| 115 | + value, |
| 116 | +) where {T} |
82 | 117 | input = Vector{T}(undef, length(bridge.real_indices) + length(bridge.imag_indices))
|
83 | 118 | for (i, idx) in enumerate(bridge.real_indices)
|
84 | 119 | input[i] = real(value[idx])
|
85 | 120 | end
|
86 | 121 | for (i, idx) in enumerate(bridge.imag_indices)
|
87 |
| - input[length(bridge.real_indices) + i] = imag(value[idx]) |
| 122 | + input[length(bridge.real_indices)+i] = imag(value[idx]) |
88 | 123 | end
|
89 | 124 | MOI.set(model, attr, bridge.constraint, input)
|
90 | 125 | end
|
0 commit comments