From 9d56a95b284c8663656ccf5489fb2a58bcc47297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 21 Sep 2026 09:33:22 +0200 Subject: [PATCH] Preserve nonlinear generator reconstruction across JuMP versions The quadrotor regression checks the multiset of objective generators because older supported JuMP versions can reorder operands in flatten!. Keep this version-compatible example assertion with the nonlinear reconstruction fix and its regression coverage, rather than treating term ordering as a separate behavior change. --- examples/quadrotor/runtests.jl | 3 ++- src/JuMP_wrapper.jl | 2 +- test/JuMP_wrapper.jl | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/examples/quadrotor/runtests.jl b/examples/quadrotor/runtests.jl index dbf9afc..a8fd87e 100644 --- a/examples/quadrotor/runtests.jl +++ b/examples/quadrotor/runtests.jl @@ -84,7 +84,8 @@ end @test all(a -> a isa SumGenerator, obj.args) terms = [prod(it -> length(it.values), a.iterators) for a in obj.args] # `N * p` control terms, `N * n` stage terms and `n` terminal terms. - @test terms == [N * 4, N * n, n] + # Older JuMP versions may reorder terms when flattening the objective sum. + @test sort(terms) == sort([N * 4, N * n, n]) end end # module TestQuadrotor diff --git a/src/JuMP_wrapper.jl b/src/JuMP_wrapper.jl index 5bb6130..1ddf753 100644 --- a/src/JuMP_wrapper.jl +++ b/src/JuMP_wrapper.jl @@ -153,7 +153,7 @@ end function index_iterators(func::JuMP.GenericNonlinearExpr, values) args = map(Base.Fix2(index_iterators, values), func.args) if any(JuMP._has_variable_ref_type, args) - return JuMP.GenericNonlinearExpr(func.head, args) + return JuMP.GenericNonlinearExpr(func.head, args...) elseif func.head == :getindex # `JuMP.jump_function` converts every number to `Float64` when it # converts a `MOI` function back, so the indices need to be converted diff --git a/test/JuMP_wrapper.jl b/test/JuMP_wrapper.jl index ff759e4..7dfab03 100644 --- a/test/JuMP_wrapper.jl +++ b/test/JuMP_wrapper.jl @@ -61,6 +61,40 @@ function test_container() @test_broken JuMP.isequal_canonical(con_expr, expr) end +function test_index_iterators_scalar_arguments() + model = Model() + @variable(model, x) + i = GenOpt.iterator([1, 2]) + expr = (x + i)^2 + expanded = GenOpt.index_iterators(expr.expr, ((2,),)) + @test expanded isa GenericNonlinearExpr{VariableRef} + @test expanded.head == :^ + @test length(expanded.args) == 2 + @test expanded.args[2] == 2 + inner = expanded.args[1] + @test inner isa GenericNonlinearExpr{VariableRef} + @test inner.head == :+ + @test length(inner.args) == 2 + @test inner.args[1] === x + @test inner.args[2] == 2 + # Unlike the mixed expression/number arguments above, these reconstructions + # produce a homogeneous vector of expressions. It is an argument list, not + # one vector-valued argument to the nonlinear operator. + for (head, expr, count) in + ((:sin, sin(x + i), 1), (:*, (x + i) * (x + i), 2)) + expanded = GenOpt.index_iterators(expr.expr, ((2,),)) + @test expanded isa GenericNonlinearExpr{VariableRef} + @test expanded.head == head + @test length(expanded.args) == count + for inner in expanded.args + @test inner isa GenericNonlinearExpr{VariableRef} + @test inner.head == :+ + @test inner.args == Any[x, 2] + end + end + return +end + function test_ind2sub() # `bridge.jl` expands a generator with `CartesianIndices` so `_ind2sub` # must agree with it, otherwise `getindex` and the bridge would disagree