Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/quadrotor/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/JuMP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions test/JuMP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading