Skip to content

Commit d2eace0

Browse files
committed
Update
1 parent 8536b2c commit d2eace0

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

src/Bridges/Variable/set_map.jl

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ function MOI.Bridges.added_constraint_types(::Type{<:SetMapBridge})
7272
return Tuple{Type,Type}[]
7373
end
7474

75-
# Get a primal function, going from bridged-space -> user-space
76-
# => use inverse_map_function
7775
function MOI.get(
7876
model::MOI.ModelLike,
7977
attr::Union{
@@ -83,27 +81,28 @@ function MOI.get(
8381
},
8482
bridge::SetMapBridge,
8583
)
86-
f = MOI.get(model, attr, bridge.constraint)
87-
return MOI.Bridges.inverse_map_function(bridge, f)
84+
value = MOI.get(model, attr, bridge.constraint)
85+
if value === nothing
86+
return
87+
end
88+
return MOI.Bridges.inverse_map_function(bridge, value)
8889
end
8990

90-
# Set a primal function, going from user-space -> bridged-space
91-
# => use map_function
9291
function MOI.set(
9392
model::MOI.ModelLike,
9493
attr::MOI.ConstraintPrimalStart,
9594
bridge::SetMapBridge,
9695
value,
9796
)
97+
if value === nothing
98+
MOI.set(model, attr, bridge.constraint, nothing)
99+
return
100+
end
98101
bridged_value = MOI.Bridges.map_function(bridge, value)
99102
MOI.set(model, attr, bridge.constraint, bridged_value)
100103
return
101104
end
102105

103-
# Get a primal set, going from bridged-space -> user-space
104-
# => well we should use inverse_map_set, but for some reason, the convention
105-
# is reversed in Variable.SetMapBridge and Constraint.SetMapBridge
106-
# => use map_set instead
107106
function MOI.get(
108107
model::MOI.ModelLike,
109108
attr::MOI.ConstraintSet,
@@ -113,40 +112,39 @@ function MOI.get(
113112
return MOI.Bridges.map_set(bridge, set)
114113
end
115114

116-
# Set a primal set, going from user-space -> bridged-space
117-
# => well we should use map_set, but for some reason, the convention is
118-
# reversed in Variable.SetMapBridge and Constraint.SetMapBridge
119-
# => use inverse_map_set instead
120115
function MOI.set(
121116
model::MOI.ModelLike,
122117
attr::MOI.ConstraintSet,
123118
bridge::SetMapBridge{T,S1},
124119
set::S1,
125120
) where {T,S1}
126-
mapped = MOI.Bridges.inverse_map_set(typeof(bridge), set)
121+
mapped = MOI.Bridges.inverse_map_set(bridge, set)
127122
MOI.set(model, attr, bridge.constraint, mapped)
128123
return
129124
end
130125

131-
# Get a dual function, going from bridged-space -> user-space
132-
# => use inverse_adjoint_map_function
133126
function MOI.get(
134127
model::MOI.ModelLike,
135128
attr::Union{MOI.ConstraintDual,MOI.ConstraintDualStart},
136129
bridge::SetMapBridge,
137130
)
138131
value = MOI.get(model, attr, bridge.constraint)
139-
return MOI.Bridges.inverse_adjoint_map_function(typeof(bridge), value)
132+
if value === nothing
133+
return
134+
end
135+
return MOI.Bridges.inverse_adjoint_map_function(bridge, value)
140136
end
141137

142-
# Set a dual function, going from user-space -> bridged-space
143-
# => use adjoint_map_function
144138
function MOI.set(
145139
model::MOI.ModelLike,
146140
attr::MOI.ConstraintDualStart,
147141
bridge::SetMapBridge,
148142
value,
149143
)
144+
if value === nothing
145+
MOI.set(model, attr, bridge.constraint, nothing)
146+
return
147+
end
150148
bridged_value = MOI.Bridges.adjoint_map_function(bridge, value)
151149
MOI.set(model, attr, bridge.constraint, bridged_value)
152150
return
@@ -221,6 +219,9 @@ function MOI.get(
221219
bridge::SetMapBridge,
222220
)
223221
value = MOI.get(model, attr, bridge.variable)
222+
if value === nothing
223+
return
224+
end
224225
return MOI.Bridges.map_function(bridge, value)
225226
end
226227

@@ -230,6 +231,10 @@ function MOI.set(
230231
bridge::SetMapBridge,
231232
value,
232233
)
234+
if value === nothing
235+
MOI.set(model, attr, bridge.variable, nothing)
236+
return
237+
end
233238
bridged_value = MOI.Bridges.inverse_map_function(bridge, value)
234239
MOI.set(model, attr, bridge.variable, bridged_value)
235240
return
@@ -287,7 +292,7 @@ function MOI.get(
287292
)
288293
value = MOI.get(model, attr, bridge.variables)
289294
if any(isnothing, value)
290-
return nothing
295+
return
291296
end
292297
return MOI.Bridges.map_function(bridge, value, i)
293298
end
@@ -299,6 +304,10 @@ function MOI.set(
299304
value,
300305
i::MOI.Bridges.IndexInVector,
301306
)
307+
if value === nothing
308+
MOI.set(model, attr, bridge.variables[i.value], nothing)
309+
return
310+
end
302311
bridged_value = MOI.Bridges.inverse_map_function(bridge, value)
303312
MOI.set(model, attr, bridge.variables[i.value], bridged_value)
304313
return

src/Bridges/set_map.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ function map_function(bridge::AbstractBridge, func)
6767
return map_function(typeof(bridge), func)
6868
end
6969

70-
map_function(::AbstractBridge, ::Nothing) = nothing
71-
7270
"""
7371
map_function(::Type{BT}, func, i::IndexInVector) where {BT}
7472
@@ -108,8 +106,6 @@ function inverse_map_function(bridge::AbstractBridge, func)
108106
return inverse_map_function(typeof(bridge), func)
109107
end
110108

111-
inverse_map_function(::AbstractBridge, ::Nothing) = nothing
112-
113109
"""
114110
adjoint_map_function(bridge::MOI.Bridges.AbstractBridge, func)
115111
adjoint_map_function(::Type{BT}, func) where {BT}
@@ -127,8 +123,6 @@ function adjoint_map_function(bridge::AbstractBridge, func)
127123
return adjoint_map_function(typeof(bridge), func)
128124
end
129125

130-
adjoint_map_function(::AbstractBridge, ::Nothing) = nothing
131-
132126
"""
133127
inverse_adjoint_map_function(bridge::MOI.Bridges.AbstractBridge, func)
134128
inverse_adjoint_map_function(::Type{BT}, func) where {BT}
@@ -148,5 +142,3 @@ interface is kept for backward compatibility.
148142
function inverse_adjoint_map_function(bridge::AbstractBridge, func)
149143
return inverse_adjoint_map_function(typeof(bridge), func)
150144
end
151-
152-
inverse_adjoint_map_function(::AbstractBridge, ::Nothing) = nothing

test/Bridges/Variable/ParameterToEqualToBridge.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ function test_list_of_constraint_indices()
8080
return
8181
end
8282

83+
function test_VariablePrimalStart()
84+
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
85+
model = MOI.Bridges.Variable.ParameterToEqualTo{Float64}(inner)
86+
x, ci = MOI.add_constrained_variable(model, MOI.Parameter(2.0))
87+
@test MOI.supports(model, MOI.VariablePrimalStart(), MOI.VariableIndex)
88+
MOI.set(model, MOI.VariablePrimalStart(), x, 2.0)
89+
@test MOI.get(model, MOI.VariablePrimalStart(), x) == 2.0
90+
MOI.set(model, MOI.VariablePrimalStart(), x, nothing)
91+
@test MOI.get(model, MOI.VariablePrimalStart(), x) === nothing
92+
return
93+
end
94+
8395
function test_ConstraintPrimalStart()
8496
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
8597
model = MOI.Bridges.Variable.ParameterToEqualTo{Float64}(inner)

0 commit comments

Comments
 (0)