Skip to content

Commit 0b30c1e

Browse files
committed
Clearer error message with constrained variables in VectorOfVariables
1 parent 9bddcb7 commit 0b30c1e

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/Utilities/functions.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,29 @@ function substitute_variables(
466466
return substitute_variables.(variable_map, x)
467467
end
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+
469492
function substitute_variables(
470493
variable_map::F,
471494
term::MOI.ScalarAffineTerm{T},

test/Utilities/test_functions.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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
333346
end
334347

0 commit comments

Comments
 (0)