Skip to content

Commit d407801

Browse files
committed
Simplify
1 parent ff5b32c commit d407801

2 files changed

Lines changed: 41 additions & 45 deletions

File tree

src/Bridges/Variable/map.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,20 @@ function Base.empty!(map::Map)
134134
return map
135135
end
136136

137-
"""
138-
is_variable_mapping_active(map::Map)::Bool
139-
140-
Return `true` once at least one variable bridge has been added (and hence
141-
the outer/inner translation has been materialized).
142-
"""
143-
is_variable_mapping_active(map::Map) = map.next_outer_variable != 0
144-
145137
"""
146138
is_constraint_mapping_active(map::Map, ::Type{F}, ::Type{S})::Bool
147139
148140
Return `true` once at least one `CI{F, S}` has been force-bridged at this
149141
layer (and hence the outer/inner translation for `(F, S)` has been
150142
materialized).
143+
144+
Note that this is strictly stronger than [`has_bridges`](@ref): the latter
145+
is `true` as soon as any variable bridge exists, while constraint mapping
146+
only activates for the specific `(F, S)` pairs whose outer and inner
147+
namespaces have diverged because a `VariableIndex`/`VectorOfVariables`
148+
constraint was force-bridged. Regular `F`-in-`S` constraints are bridged
149+
per *type*, never per *instance*, so their namespaces never diverge and
150+
this stays `false` for them.
151151
"""
152152
function is_constraint_mapping_active(
153153
map::Map,
@@ -168,7 +168,12 @@ namespaces independently. No-op if the mapping is already active.
168168
`b.model` of the enclosing `AbstractBridgeOptimizer`).
169169
"""
170170
function activate_variable_mapping!(map::Map, model::MOI.ModelLike)
171-
if is_variable_mapping_active(map)
171+
# `has_bridges` flips to `true` only inside `add_key_for_bridge` (which
172+
# pushes to `map.info`), and that always runs *after* this function in
173+
# `add_constrained_variable`. So on the first variable bridge this is
174+
# still `false` and we populate; any nested re-entry (a variable bridge
175+
# that itself adds constrained variables) sees `true` and is a no-op.
176+
if has_bridges(map)
172177
return
173178
end
174179
max_value = Int64(0)
@@ -223,7 +228,7 @@ Return a fresh `Int64` value to use as a `VariableIndex.value` in the outer
223228
namespace and advance the internal counter.
224229
"""
225230
function next_outer_variable!(map::Map)
226-
@assert is_variable_mapping_active(map)
231+
@assert has_bridges(map)
227232
value = map.next_outer_variable
228233
map.next_outer_variable = value + 1
229234
return value

src/Bridges/bridge_optimizer.jl

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,12 @@ Translate an outer index (in `b`'s user-facing namespace) to its inner
471471
counterpart (`b.model`'s namespace). Returns `idx` unchanged when no
472472
translation is in effect at this layer:
473473
474-
* For a `VariableIndex`, the variable mapping must be active
475-
(`Variable.is_variable_mapping_active(map)`) AND `idx` must not refer to
476-
an outer-only bridged variable (`idx.value < 0`).
477-
* For a `ConstraintIndex{F, S}`, the constraint mapping for `(F, S)` must
478-
be active AND `idx.value >= 0`.
474+
* For a `VariableIndex`, only when variable bridges are used
475+
(`Variable.has_bridges(map)`). The caller is responsible for having
476+
already established that `idx` is not bridged at `b`'s layer.
477+
* For a `ConstraintIndex{F, S}`, only when the constraint mapping for
478+
`(F, S)` has been activated (see
479+
[`Variable.is_constraint_mapping_active`](@ref)).
479480
480481
This is the canonical "if variable bridges are used, use the index map;
481482
else return the index unchanged" helper.
@@ -485,9 +486,7 @@ function outer_to_inner(
485486
vi::MOI.VariableIndex,
486487
)
487488
map = Variable.bridges(b)
488-
if map isa Variable.Map &&
489-
vi.value > 0 &&
490-
Variable.is_variable_mapping_active(map)
489+
if Variable.has_bridges(map)
491490
return map.outer_to_inner[vi]
492491
end
493492
return vi
@@ -498,8 +497,7 @@ function outer_to_inner(
498497
ci::MOI.ConstraintIndex{F,S},
499498
) where {F,S}
500499
map = Variable.bridges(b)
501-
if map isa Variable.Map &&
502-
ci.value > 0 &&
500+
if Variable.has_bridges(map) &&
503501
Variable.is_constraint_mapping_active(map, F, S)
504502
return map.outer_to_inner[ci]
505503
end
@@ -529,7 +527,7 @@ function inner_to_outer(
529527
vi::MOI.VariableIndex,
530528
)
531529
map = Variable.bridges(b)
532-
if map isa Variable.Map && Variable.is_variable_mapping_active(map)
530+
if Variable.has_bridges(map)
533531
return map.inner_to_outer[vi]
534532
end
535533
return vi
@@ -540,7 +538,8 @@ function inner_to_outer(
540538
ci::MOI.ConstraintIndex{F,S},
541539
) where {F,S}
542540
map = Variable.bridges(b)
543-
if map isa Variable.Map && Variable.is_constraint_mapping_active(map, F, S)
541+
if Variable.has_bridges(map) &&
542+
Variable.is_constraint_mapping_active(map, F, S)
544543
return map.inner_to_outer[ci]
545544
end
546545
return ci
@@ -625,10 +624,7 @@ namespace. When `recursive_model(b) !== b` (for example,
625624
(see [`bridged_variable_function`](@ref)) so this is the identity.
626625
"""
627626
function _to_inner_value(b::AbstractBridgeOptimizer, value)
628-
map = Variable.bridges(b)
629-
if map isa Variable.Map &&
630-
Variable.is_variable_mapping_active(map) &&
631-
recursive_model(b) === b
627+
if Variable.has_bridges(Variable.bridges(b)) && recursive_model(b) === b
632628
return MOI.Utilities.map_indices(_OuterToInner(b), value)
633629
end
634630
return value
@@ -644,8 +640,7 @@ unchanged; the caller is expected to substitute them out via
644640
[`unbridged_function`](@ref).
645641
"""
646642
function _from_inner_value(b::AbstractBridgeOptimizer, value)
647-
map = Variable.bridges(b)
648-
if map isa Variable.Map && Variable.is_variable_mapping_active(map)
643+
if Variable.has_bridges(Variable.bridges(b))
649644
return MOI.Utilities.map_indices(_TotalInnerToOuter(b), value)
650645
end
651646
return value
@@ -688,8 +683,7 @@ is active, regardless of `recursive_model(b)`, because such functions are
688683
never rewritten by [`bridged_function`](@ref).
689684
"""
690685
function _to_inner_index_function(b::AbstractBridgeOptimizer, f)
691-
map = Variable.bridges(b)
692-
if map isa Variable.Map && Variable.is_variable_mapping_active(map)
686+
if Variable.has_bridges(Variable.bridges(b))
693687
return MOI.Utilities.map_indices(_OuterToInner(b), f)
694688
end
695689
return f
@@ -711,7 +705,7 @@ function _record_inner_constraint!(
711705
inner_ci::MOI.ConstraintIndex{F,S},
712706
) where {F,S}
713707
map = Variable.bridges(b)
714-
if map isa Variable.Map &&
708+
if Variable.has_bridges(map) &&
715709
Variable.is_constraint_mapping_active(map, F, S)
716710
outer_ci =
717711
MOI.ConstraintIndex{F,S}(Variable.next_outer_constraint!(map, F, S))
@@ -734,7 +728,7 @@ function MOI.is_valid(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
734728
return haskey(Variable.bridges(b), vi)
735729
end
736730
map = Variable.bridges(b)
737-
if map isa Variable.Map && Variable.is_variable_mapping_active(map)
731+
if Variable.has_bridges(map)
738732
# Outer/inner translation is in effect. Outer `vi` must be in
739733
# `outer_to_inner` to be valid; the entry then points to the inner
740734
# index to forward.
@@ -801,9 +795,7 @@ function _inner_index_or_nothing(
801795
vi::MOI.VariableIndex,
802796
)
803797
map = Variable.bridges(b)
804-
if map isa Variable.Map &&
805-
vi.value > 0 &&
806-
Variable.is_variable_mapping_active(map)
798+
if Variable.has_bridges(map)
807799
return get(map.outer_to_inner.var_map, vi, nothing)
808800
end
809801
return vi
@@ -814,8 +806,7 @@ function _inner_index_or_nothing(
814806
ci::MOI.ConstraintIndex{F,S},
815807
) where {F,S}
816808
map = Variable.bridges(b)
817-
if map isa Variable.Map &&
818-
ci.value > 0 &&
809+
if Variable.has_bridges(map) &&
819810
Variable.is_constraint_mapping_active(map, F, S)
820811
if haskey(map.outer_to_inner.con_map, ci)
821812
return map.outer_to_inner.con_map[ci]
@@ -1076,7 +1067,7 @@ function _remove_inner_variable_mapping!(
10761067
vi::MOI.VariableIndex,
10771068
)
10781069
map = Variable.bridges(b)
1079-
if !(map isa Variable.Map) || !Variable.is_variable_mapping_active(map)
1070+
if !Variable.has_bridges(map)
10801071
return
10811072
end
10821073
inner_vi = get(map.outer_to_inner.var_map, vi, nothing)
@@ -1239,8 +1230,7 @@ function _get_all_including_bridged(
12391230
key = inner_variable
12401231
if !haskey(bridge_var_map, key)
12411232
map_b = Variable.bridges(b)
1242-
if map_b isa Variable.Map &&
1243-
Variable.is_variable_mapping_active(map_b) &&
1233+
if Variable.has_bridges(map_b) &&
12441234
haskey(map_b.inner_to_outer, key)
12451235
key = map_b.inner_to_outer[key]
12461236
end
@@ -2363,7 +2353,7 @@ function _outer_variable_for_name_lookup(
23632353
vi::MOI.VariableIndex,
23642354
)
23652355
map = Variable.bridges(b)
2366-
if !(map isa Variable.Map) || !Variable.is_variable_mapping_active(map)
2356+
if !Variable.has_bridges(map)
23672357
return vi
23682358
end
23692359
if haskey(map.inner_to_outer.var_map, vi)
@@ -2861,16 +2851,17 @@ end
28612851
"""
28622852
_record_inner_variable!(b::AbstractBridgeOptimizer, inner_vi::MOI.VariableIndex)
28632853
2864-
If variable mapping is active in `b`, allocate a fresh outer `VariableIndex`
2865-
value and record the bidirectional mapping. Otherwise (identity mode, or `b`
2866-
does not own a `Variable.Map` at all), return `inner_vi` unchanged.
2854+
If `b` uses variable bridges (`Variable.has_bridges`), allocate a fresh
2855+
outer `VariableIndex` value and record the bidirectional mapping.
2856+
Otherwise (identity mode, or `b` does not own a `Variable.Map` at all),
2857+
return `inner_vi` unchanged.
28672858
"""
28682859
function _record_inner_variable!(
28692860
b::AbstractBridgeOptimizer,
28702861
inner_vi::MOI.VariableIndex,
28712862
)
28722863
map = Variable.bridges(b)
2873-
if !(map isa Variable.Map) || !Variable.is_variable_mapping_active(map)
2864+
if !Variable.has_bridges(map)
28742865
return inner_vi
28752866
end
28762867
outer_vi = MOI.VariableIndex(Variable.next_outer_variable!(map))

0 commit comments

Comments
 (0)