Open baggepinnen opened 2 years ago
Actually, this error appears still on the latest versions of Symbolics and SymbolicUtils, but in a different context
using Symbolics, LinearAlgebra
using SparseArrays
nx = 2
ny = 1
nu = 1
N = 20
U0 = randn(nu, N)
#G = c2d(ss(tf(1, [1, 0.1, 1])), 0.1)
#res = lsim(G, U0)
#Y0 = res.y
Y0 = randn(ny, N)
nx = 2
ny = 1
nu = 1
@variables A[1:nx, 1:nx] B[1:nx, 1:nu] C[1:ny, 1:nx] D[1:ny, 1:nu] K[1:nx, 1:ny]
A,B,C,D,K = collect.((A,B,C,D,K))
##
next = []
w = []
x = @variables x1[1:nx]
x = collect(x[])
push!(w, x)
U = []
Y = []
i = 1
c = 0
for i = 1:N
global x, c
un = Symbol("u$i")
u = @variables $un[1:nu]
u = collect(u[])
yn = Symbol("y$i")
y = @variables $yn[1:ny]
y = collect(y[])
yh = C*x + D*u
e = y-yh
c += sum(abs2, e)
xp = A*x + B*u + K*e
xn = Symbol("x$(i+1)")
x = @variables $xn[1:nx]
x = collect(x[])
push!(next, xp)
push!(w, x)
push!(U, u)
push!(Y, y)
end
##
@variables λ[1:N*nx]
λ = collect(λ)
@variables σ
# vcollect(x) = reduce(vcat, collect.(x))
vcollect(x) = reduce(vcat, x)
next2 = vcollect(next)
w2 = vcollect(w)
g = next2 .- w2[nx+1:end]
p = vcollect(vec.((A,B,C,D,K)))
w3 = [p; w2]
wλ = [w3; λ]
lagrange = σ*c + λ'g
grad = Symbolics.gradient(c, w3);
jac = Symbolics.sparsejacobian(g, w3);
# hess = Symbolics.sparsehessian(c, w3) # errors, workaround below
laghess = Symbolics.sparsejacobian(Symbolics.gradient(lagrange, wλ), wλ)
laghess = tril(laghess)
w0 = randn(size(w3))
U2 = vcollect(U)
Y2 = vcollect(Y)
λ0 = randn(size(λ))
@time jacfun = build_function(jac, w3, U2, expression=Val{false}()) # this errors
julia> jacfun = build_function(jac, w3, U2, expression=Val{false}())
ERROR: MethodError: no method matching isconstant(::DynamicPolynomials.Polynomial{true, Int64})
Closest candidates are:
isconstant(::MultivariatePolynomials.AbstractVariable) at ~/.julia/packages/MultivariatePolynomials/0oEYT/src/monomial.jl:90
isconstant(::MultivariatePolynomials.AbstractTermLike) at ~/.julia/packages/MultivariatePolynomials/0oEYT/src/monomial.jl:89
Stacktrace:
[1] (PolyForm{Real})(p::DynamicPolynomials.Polynomial{true, Int64}, d1::Bijections.Bijection{Any, Any}, d2::Dict{SymbolicUtils.Sym, Any}, m::Nothing)
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/fgHzN/src/polyform.jl:39
[2] PolyForm(x::SymbolicUtils.Add{Real, Int64, Dict{Any, Number}, Nothing}, pvar2sym::Bijections.Bijection{Any, Any}, sym2term::Dict{SymbolicUtils.Sym, Any}, vtype::Type; Fs::Type, recurse::Bool, metadata::Nothing)
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/fgHzN/src/polyform.jl:169
[3] expand(expr::SymbolicUtils.Add{Real, Int64, Dict{Any, Number}, Nothing})
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/fgHzN/src/polyform.jl:232
[4] (::ComposedFunction{typeof(SymbolicUtils._iszero), typeof(expand)})(x::SymbolicUtils.Add{Real, Int64, Dict{Any, Number}, Nothing}; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ Base ./operators.jl:1085
[5] ComposedFunction
@ ./operators.jl:1085 [inlined]
[6] _any(f::ComposedFunction{typeof(SymbolicUtils._iszero), typeof(expand)}, itr::Vector{SymbolicUtils.Add{Real, Int64, Dict{Any, Number}, Nothing}}, #unused#::Colon)
@ Base ./reduce.jl:1110
[7] any(f::Function, a::Vector{SymbolicUtils.Add{Real, Int64, Dict{Any, Number}, Nothing}}; dims::Function)
@ Base ./reducedim.jl:899
[8] any(f::Function, a::Vector{SymbolicUtils.Add{Real, Int64, Dict{Any, Number}, Nothing}})
@ Base ./reducedim.jl:899
[9] fraction_iszero(x::SymbolicUtils.Add{Real, Int64, Dict{Any, Number}, Nothing})
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/fgHzN/src/polyform.jl:327
[10] iszero(x::Num)
@ Symbolics ~/.julia/packages/Symbolics/LTmGT/src/num.jl:91
[11] _iszero
@ ~/julia-1.7.0-rc3/share/julia/stdlib/v1.7/SparseArrays/src/higherorderfns.jl:207 [inlined]
[12] _map_zeropres!(f::Symbolics.var"#266#267"{Symbol}, C::SparseMatrixCSC{Num, Int64}, A::SparseMatrixCSC{Num, Int64})
@ SparseArrays.HigherOrderFns ~/julia-1.7.0-rc3/share/julia/stdlib/v1.7/SparseArrays/src/higherorderfns.jl:248
[13] _noshapecheck_map(::Symbolics.var"#266#267"{Symbol}, ::SparseMatrixCSC{Num, Int64})
@ SparseArrays.HigherOrderFns ~/julia-1.7.0-rc3/share/julia/stdlib/v1.7/SparseArrays/src/higherorderfns.jl:171
[14] map
@ ~/julia-1.7.0-rc3/share/julia/stdlib/v1.7/SparseArrays/src/higherorderfns.jl:1162 [inlined]
[15] _make_array
@ ~/.julia/packages/Symbolics/LTmGT/src/build_function.jl:269 [inlined]
[16] make_array(s::Symbolics.SerialForm, dargs::Vector{SymbolicUtils.Code.DestructuredArgs}, arr::SparseMatrixCSC{Num, Int64}, similarto::Symbol)
@ Symbolics ~/.julia/packages/Symbolics/LTmGT/src/build_function.jl:229
[17] _build_function(::Symbolics.JuliaTarget, ::SparseMatrixCSC{Num, Int64}, ::Vector{Num}, ::Vararg{Vector{Num}}; expression::Val{false}, expression_module::Module, checkbounds::Bool, postprocess_fbody::Symbolics.var"#257#261", linenumbers::Bool, outputidxs::Nothing, skipzeros::Bool, wrap_code::Tuple{Nothing, Nothing}, fillzeros::Bool, parallel::Symbolics.SerialForm, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ Symbolics ~/.julia/packages/Symbolics/LTmGT/src/build_function.jl:194
[18] build_function(::SparseMatrixCSC{Num, Int64}, ::Vararg{Any}; target::Symbolics.JuliaTarget, kwargs::Base.Pairs{Symbol, Val{false}, Tuple{Symbol}, NamedTuple{(:expression,), Tuple{Val{false}}}})
@ Symbolics ~/.julia/packages/Symbolics/LTmGT/src/build_function.jl:52
[19] top-level scope
@ ./timing.jl:220 [inlined]
[20] top-level scope
@ ./REPL[3]:0
I get a similar error with
MethodError: no method matching isconstant(::DynamicPolynomials.Polynomial{true, Float64})
However, I cannont reproduce it reliable. I occurs in the tests of my package but not when I run it directly from the repl. I tried your example as well and it worked fine from the repl but not from the environment of my package where I get
ERROR: MethodError: no method matching isconstant(::DynamicPolynomials.Polynomial{true, Int64})
But I was not able yet to reproducte this error in a new environment.
Update: I removed some unused dependencies from the package and updated the others. Now it seems to work. Sorry that I cannot be more specific
results in