File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -466,6 +466,29 @@ function substitute_variables(
466466 return substitute_variables .(variable_map, x)
467467end
468468
469+ function substitute_variables (
470+ variable_map:: F ,
471+ f:: MOI.VectorOfVariables ,
472+ ) where {F<: Function }
473+ variables = MOI. VariableIndex[]
474+ for variable in f. variables
475+ mapped_variable = variable_map (variable)
476+ try
477+ push! (variables, convert (MOI. VariableIndex, mapped_variable))
478+ catch err
479+ @assert err isa InexactError
480+ error (
481+ " Cannot substitute `VectorOfVariables`: variable `$variable ` is mapped " *
482+ " to `$mapped_variable `, not directly to another variable. Variables " *
483+ " constrained on creation can be used in a `VectorOfVariables` function " *
484+ " only when their bridge maps each one directly to another variable, " *
485+ " without a transformation." ,
486+ )
487+ end
488+ end
489+ return MOI. VectorOfVariables (variables)
490+ end
491+
469492function substitute_variables (
470493 variable_map:: F ,
471494 term:: MOI.ScalarAffineTerm{T} ,
Original file line number Diff line number Diff line change @@ -329,6 +329,19 @@ function test_substitute_variables()
329329 int_quad = 3 * y * x
330330 @test MOI. Utilities. substitute_variables (vi -> true * y, int_quad) ≈
331331 3 * y * y
332+
333+ vector_of_variables = MOI. VectorOfVariables ([w, x])
334+ @test MOI. Utilities. substitute_variables (
335+ vi -> Dict (w => y, x => z)[vi],
336+ vector_of_variables,
337+ ) == MOI. VectorOfVariables ([y, z])
338+ err = try
339+ MOI. Utilities. substitute_variables (vi -> 2.0 * vi, vector_of_variables)
340+ catch err
341+ err
342+ end
343+ @test err isa ErrorException
344+ @test occursin (" Variables constrained on creation" , sprint (showerror, err))
332345 return
333346end
334347
You can’t perform that action at this time.
0 commit comments