Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code restrictions for merit function not documented #298

Closed
DavidSagan opened this issue Dec 2, 2023 · 4 comments · Fixed by #302
Closed

Code restrictions for merit function not documented #298

DavidSagan opened this issue Dec 2, 2023 · 4 comments · Fixed by #302
Labels
bug Something isn't working

Comments

@DavidSagan
Copy link

DavidSagan commented Dec 2, 2023

[Side note: You might want to consider a documentation problem category for issues.]

The following code fails:

using NonlinearSolve, LinearSolve, LinearAlgebra, Random, ForwardDiff, PyFormattedStrings

function fff(var, p)
  v_true = [1.0, 0.1, 2.0, 0.5]
  xx = [1.0, 2.0, 3.0, 4.0]
  println(f"{typeof(xx)}  {typeof(var)}  {typeof(v_true)}")
  xx[1] = var[1] - v_true[1]
  return var - v_true
end

v_true = [1.0, 0.1, 2.0, 0.5]
v_init = v_true .+ randn!(similar(v_true)) * 0.1

prob_oop = NonlinearLeastSquaresProblem{false}(fff, v_init)
sol = solve(prob_oop, LevenbergMarquardt(); maxiters = 10000, abstol = 1e-8)

The error generated is:

julia> include("z.jl")
Vector{Float64}  Vector{Float64}  Vector{Float64}
Vector{Float64}  Vector{Float64}  Vector{Float64}
Vector{Float64}  Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolve.NonlinearSolveTag, Float64}, Float64, 4}}  Vector{Float64}
ERROR: LoadError: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolve.NonlinearSolveTag, Float64}, Float64, 4})

Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
   @ Base rounding.jl:207
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:792
  (::Type{T})(::VectorizationBase.Double{T}) where T<:Union{Float16, Float32, Float64, VectorizationBase.Vec{<:Any, <:Union{Float16, Float32, Float64}}, VectorizationBase.VecUnroll{var"#s45", var"#s44", var"#s43", V} where {var"#s45", var"#s44", var"#s43"<:Union{Float16, Float32, Float64}, V<:Union{Bool, Float16, Float32, Float64, Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8, SIMDTypes.Bit, VectorizationBase.AbstractSIMD{var"#s44", var"#s43"}}}}
   @ VectorizationBase ~/.julia/packages/VectorizationBase/0dXyA/src/special/double.jl:111
  ...

Stacktrace:
  [1] convert(#unused#::Type{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolve.NonlinearSolveTag, Float64}, Float64, 4})
    @ Base ./number.jl:7
  [2] setindex!(A::Vector{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolve.NonlinearSolveTag, Float64}, Float64, 4}, i1::Int64)
    @ Base ./array.jl:969
  [3] fff(var::Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolve.NonlinearSolveTag, Float64}, Float64, 4}}, p::SciMLBase.NullParameters)
    @ Main ~/Bmad/turn_by_turn_sextupole/z.jl:7
  [4] (::NonlinearFunction{false, SciMLBase.FullSpecialize, typeof(fff), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing})(::Vector{ForwardDiff.Dual{ForwardDiff.Tag{NonlinearSolve.NonlinearSolveTag, Float64}, Float64, 4}}, ::Vararg{Any})
    @ SciMLBase ~/.julia/packages/SciMLBase/ynHlA/src/scimlfunctions.jl:2408
... etc...

The error is generated in the line xx[1] = var[1] - v_true[1] in the function fff. I believe I understand this to be due to a call to fff inside solve with the argument var not being a vector but instead a construct using ForwardDiff. This is not covered in the documentation and I'm not sure how to get around this. [It is true that this line is completely irrelevant to the test case but in my real code I need to understand exactly what is happening.]

@DavidSagan DavidSagan added the bug Something isn't working label Dec 2, 2023
@avik-pal
Copy link
Member

avik-pal commented Dec 2, 2023

@DavidSagan
Copy link
Author

@avik-pal Looking at the documentation for PreallocationTools it is not clear to me how to use this. Can you show me how to use this for implementing fff in the above example? That is, how to write xx[1] = var[1] - v_true[1] in a form that will not cause an error.

@ChrisRackauckas
Copy link
Member

xx = eltype(var)[1.0, 2.0, 3.0, 4.0]

@DavidSagan
Copy link
Author

@ChrisRackauckas Yes this works. Much Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants