Skip to content

Commit 02eb7dc

Browse files
committed
tidy sorted_variable_sets_by_cost
1 parent 5ec7cba commit 02eb7dc

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

src/Utilities/copy.jl

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -386,26 +386,39 @@ _is_variable_function(::Any) = false
386386

387387
function _cost_of_bridging(
388388
dest::MOI.ModelLike,
389-
::Type{F},
390389
::Type{S},
391-
) where {F,S}
390+
) where {S<:MOI.AbstractScalarSet}
391+
return (
392+
MOI.get(dest, MOI.VariableBridgingCost{S}()) -
393+
MOI.get(dest, MOI.ConstraintBridgingCost{MOI.SingleVariable,S}()),
394+
# In case of ties, we give priority to vector sets. See issue #987.
395+
false,
396+
)
397+
end
398+
399+
function _cost_of_bridging(
400+
dest::MOI.ModelLike,
401+
::Type{S},
402+
) where {S<:MOI.AbstractVectorSet}
392403
return (
393404
MOI.get(dest, MOI.VariableBridgingCost{S}()) -
394-
MOI.get(dest, MOI.ConstraintBridgingCost{F,S}()),
395-
# In case of ties, we give priority to vector sets.
396-
# See issue #987
397-
F === MOI.SingleVariable,
405+
MOI.get(dest, MOI.ConstraintBridgingCost{MOI.VectorOfVariables,S}()),
406+
# In case of ties, we give priority to vector sets. See issue #987
407+
true,
398408
)
399409
end
400410

401-
_cost_of_bridging(arg::Tuple) = _cost_of_bridging(arg[1], arg[2], arg[3])
411+
"""
412+
sorted_variable_sets_by_cost(dest::MOI.ModelLike, src::MOI.ModelLike)
402413
403-
function _sorted_variable_sets_by_cost(dest::MOI.ModelLike, src::MOI.ModelLike)
414+
Returns a `Vector{Type}` of the set types corresponding to `SingleVariable` and
415+
`VectorOfVariables` constraints in the order in which they should be added.
416+
"""
417+
function sorted_variable_sets_by_cost(dest::MOI.ModelLike, src::MOI.ModelLike)
404418
constraint_types = MOI.get(src, MOI.ListOfConstraintTypesPresent())
405-
single_or_vector_variables_types = Any[
406-
(dest, F, S) for (F, S) in constraint_types if _is_variable_function(F)
407-
]
408-
return sort!(single_or_vector_variables_types; by = _cost_of_bridging)
419+
sets = Type[S for (F, S) in constraint_types if _is_variable_function(F)]
420+
sort!(sets; by = S::Type -> _cost_of_bridging(dest, S))
421+
return sets
409422
end
410423

411424
"""
@@ -481,7 +494,7 @@ function default_copy_to(
481494
else
482495
Any[
483496
_try_constrain_variables_on_creation(dest, src, index_map, S)
484-
for (_, F, S) in _sorted_variable_sets_by_cost(dest, src)
497+
for S in sorted_variable_sets_by_cost(dest, src)
485498
]
486499
end
487500
_copy_free_variables(dest, index_map, vis_src)

test/Utilities/copy.jl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,8 @@ function test_create_variables_using_supports_add_constrained_variable()
464464

465465
dest = OrderConstrainedVariablesModel()
466466
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
467-
@test MOIU._sorted_variable_sets_by_cost(bridged_dest, src) == Any[
468-
(bridged_dest, MOI.VectorOfVariables, MOI.Zeros),
469-
(bridged_dest, MOI.VectorOfVariables, MOI.Nonnegatives),
470-
(bridged_dest, MOI.VectorOfVariables, MOI.Nonpositives),
471-
]
467+
@test MOIU.sorted_variable_sets_by_cost(bridged_dest, src) ==
468+
Type[MOI.Zeros, MOI.Nonnegatives, MOI.Nonpositives]
472469
@test MOI.supports_add_constrained_variables(bridged_dest, MOI.Nonnegatives)
473470
@test MOI.get(bridged_dest, MOI.VariableBridgingCost{MOI.Nonnegatives}()) ==
474471
0.0
@@ -509,11 +506,8 @@ function test_create_variables_using_supports_add_constrained_variable()
509506

510507
dest = ReverseOrderConstrainedVariablesModel()
511508
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
512-
@test MOIU._sorted_variable_sets_by_cost(bridged_dest, src) == Any[
513-
(bridged_dest, MOI.VectorOfVariables, MOI.Zeros),
514-
(bridged_dest, MOI.VectorOfVariables, MOI.Nonpositives),
515-
(bridged_dest, MOI.VectorOfVariables, MOI.Nonnegatives),
516-
]
509+
@test MOIU.sorted_variable_sets_by_cost(bridged_dest, src) ==
510+
Type[MOI.Zeros, MOI.Nonpositives, MOI.Nonnegatives]
517511
@test MOI.supports_add_constrained_variables(bridged_dest, MOI.Nonnegatives)
518512
@test MOI.get(bridged_dest, MOI.VariableBridgingCost{MOI.Nonnegatives}()) ==
519513
2.0

0 commit comments

Comments
 (0)