@@ -16,23 +16,18 @@ function function_to_moi(
1616 return Nonlinear (expr)
1717end
1818
19- function substitute_variables (expr:: Expr , variables :: Vector{MOI.VariableIndex} )
19+ function lift_variable_indices (expr:: Expr )
2020 if expr. head == :ref && length (expr. args) == 2 && expr. args[1 ] == :x
21- index = expr. args[2 ]
22- if ! (1 <= index <= length (variables))
23- error (" Oops! Your expression refers to x[$(index) ] but there are " *
24- " only $(length (variables)) variables." )
25- end
26- return variables[index]
21+ return expr. args[2 ]
2722 else
2823 for (index, arg) in enumerate (expr. args)
29- expr. args[index] = substitute_variables (arg, variables )
24+ expr. args[index] = lift_variable_indices (arg)
3025 end
3126 end
3227 return expr
3328end
34- # Recursion fallback.
35- substitute_variables (arg, variables :: Vector{MOI.VariableIndex} ) = arg
29+
30+ lift_variable_indices (arg) = arg # Recursion fallback.
3631
3732function extract_function_and_set (expr:: Expr )
3833 if expr. head == :call # One-sided constraint or foo-in-set.
@@ -71,7 +66,7 @@ function write_nlpblock(object::Object, model::Model,
7166 variables = MOI. get (model, MOI. ListOfVariableIndices ())
7267 if nlp_block. has_objective
7368 objective = MOI. objective_expr (nlp_block. evaluator)
74- objective = substitute_variables (objective, variables )
69+ objective = lift_variable_indices (objective)
7570 sense = MOI. get (model, MOI. ObjectiveSense ())
7671 object[" objective" ] = Object (
7772 " sense" => moi_to_object (sense),
@@ -81,7 +76,7 @@ function write_nlpblock(object::Object, model::Model,
8176 for (row, bounds) in enumerate (nlp_block. constraint_bounds)
8277 constraint = MOI. constraint_expr (nlp_block. evaluator, row)
8378 (func, set) = extract_function_and_set (constraint)
84- func = substitute_variables (func, variables )
79+ func = lift_variable_indices (func)
8580 push! (object[" constraints" ],
8681 Object (" function" => moi_to_object (Nonlinear (func), model, name_map),
8782 " set" => moi_to_object (set, model, name_map))
@@ -293,7 +288,7 @@ function convert_mof_to_expr(node::Object, node_list::Vector{Object},
293288end
294289
295290"""
296- convert_mof_to_expr (node::Object, node_list::Vector{Object},
291+ convert_expr_to_mof (node::Object, node_list::Vector{Object},
297292 name_map::Dict{MOI.VariableIndex, String})
298293
299294Convert a Julia expression into a MathOptFormat representation. Any intermediate
0 commit comments