SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.43k stars 209 forks source link

Optimization tutorial fails #1781

Closed lamorton closed 9 months ago

lamorton commented 2 years ago

Copied from here

using ModelingToolkit, Optimization, OptimizationOptimJL

@variables x y
@parameters a b
loss = (a - x)^2 + b * (y - x^2)^2
@named sys = OptimizationSystem(loss,[x,y],[a,b])

u0 = [
    x=>1.0
    y=>2.0
]
p = [
    a => 6.0
    b => 7.0
]

prob = OptimizationProblem(sys,u0,p,grad=true,hess=true)
solve(prob,Newton())
ERROR: Optimization algorithm not found. Either the chosen algorithm is not a valid solver
choice for the `OptimizationProblem`, or the Optimization solver library is not loaded.
Make sure that you have loaded an appropriate Optimization.jl solver library, for example,
`solve(prob,Optim.BFGS())` requires `using OptimizationOptimJL` and
`solve(prob,Adam())` requires `using OptimizationOptimisers`.

For more information, see the Optimization.jl documentation: optimization.sciml.ai/dev.

Chosen Optimizer: Newton{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}}(LineSearches.InitialStatic{Float64}
  alpha: Float64 1.0
  scaled: Bool false
, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}
  delta: Float64 0.1
  sigma: Float64 0.9
  alphamax: Float64 Inf
  rho: Float64 5.0
  epsilon: Float64 1.0e-6
  gamma: Float64 0.66
  linesearchmax: Int64 50
  psi3: Float64 0.1
  display: Int64 0
  mayterminate: Base.RefValue{Bool}
)
Stacktrace:
 [1] __solve(::OptimizationProblem{true, OptimizationFunction{true, SciMLBase.NoAD, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xf9a137a4, 0x99f84ec7, 0xb287b28c, 0xeeeed133, 0x5f38201d)}, ModelingToolkit.var"#_grad#653"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0x20cda633, 0x005eeaa1, 0x5801d211, 0x9c6bb0d3, 0x2c4421d2)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xa25c733a, 0xf32fefc6, 0x7ccaa012, 0xaeae0d06, 0x3b028eeb)}}, ModelingToolkit.var"#_hess#654"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xfd327544, 0xb084d2ed, 0x80008c2d, 0xf6b542ee, 0xc2790519)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0x206d05ac, 0x618c01b5, 0xb23c9753, 0xd9171152, 0xf4896b97)}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Expr, Nothing, OptimizationSystem}, Vector{Float64}, Vector{Float64}, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, ::Newton{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ SciMLBase C:\Users\lucas\.julia\packages\SciMLBase\UY87O\src\solve.jl:95
 [2] __solve(::OptimizationProblem{true, OptimizationFunction{true, SciMLBase.NoAD, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xf9a137a4, 0x99f84ec7, 0xb287b28c, 0xeeeed133, 0x5f38201d)}, ModelingToolkit.var"#_grad#653"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0x20cda633, 0x005eeaa1, 0x5801d211, 0x9c6bb0d3, 0x2c4421d2)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xa25c733a, 0xf32fefc6, 0x7ccaa012, 0xaeae0d06, 0x3b028eeb)}}, ModelingToolkit.var"#_hess#654"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xfd327544, 0xb084d2ed, 0x80008c2d, 0xf6b542ee, 0xc2790519)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0x206d05ac, 0x618c01b5, 0xb23c9753, 0xd9171152, 0xf4896b97)}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Expr, Nothing, OptimizationSystem}, Vector{Float64}, Vector{Float64}, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, ::Newton{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}})
   @ SciMLBase C:\Users\lucas\.julia\packages\SciMLBase\UY87O\src\solve.jl:94
 [3] solve(::OptimizationProblem{true, OptimizationFunction{true, SciMLBase.NoAD, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), 
Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xf9a137a4, 0x99f84ec7, 0xb287b28c, 0xeeeed133, 0x5f38201d)}, ModelingToolkit.var"#_grad#653"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0x20cda633, 0x005eeaa1, 0x5801d211, 0x9c6bb0d3, 0x2c4421d2)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xa25c733a, 0xf32fefc6, 0x7ccaa012, 0xaeae0d06, 0x3b028eeb)}}, ModelingToolkit.var"#_hess#654"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xfd327544, 0xb084d2ed, 0x80008c2d, 0xf6b542ee, 0xc2790519)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0x206d05ac, 0x618c01b5, 0xb23c9753, 0xd9171152, 0xf4896b97)}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Expr, Nothing, OptimizationSystem}, Vector{Float64}, Vector{Float64}, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, ::Newton{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ SciMLBase C:\Users\lucas\.julia\packages\SciMLBase\UY87O\src\solve.jl:71
 [4] solve(::OptimizationProblem{true, OptimizationFunction{true, SciMLBase.NoAD, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), 
Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xf9a137a4, 0x99f84ec7, 0xb287b28c, 0xeeeed133, 0x5f38201d)}, ModelingToolkit.var"#_grad#653"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0x20cda633, 0x005eeaa1, 0x5801d211, 0x9c6bb0d3, 0x2c4421d2)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xa25c733a, 0xf32fefc6, 0x7ccaa012, 0xaeae0d06, 0x3b028eeb)}}, ModelingToolkit.var"#_hess#654"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0xfd327544, 0xb084d2ed, 0x80008c2d, 0xf6b542ee, 0xc2790519)}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), Symbolics.var"#_RGF_ModTag", Symbolics.var"#_RGF_ModTag", (0x206d05ac, 0x618c01b5, 0xb23c9753, 0xd9171152, 0xf4896b97)}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Expr, Nothing, OptimizationSystem}, Vector{Float64}, Vector{Float64}, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, ::Newton{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}})
   @ SciMLBase C:\Users\lucas\.julia\packages\SciMLBase\UY87O\src\solve.jl:70
 [5] top-level scope
   @ Untitled-1:44
Newton{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}}(LineSearches.InitialStatic{Float64}
  alpha: Float64 1.0
  scaled: Bool false
, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}
  delta: Float64 0.1
  sigma: Float64 0.9
  alphamax: Float64 Inf
  rho: Float64 5.0
  epsilon: Float64 1.0e-6
  gamma: Float64 0.66
  linesearchmax: Int64 50
  psi3: Float64 0.1
  display: Int64 0
  mayterminate: Base.RefValue{Bool}
)

I'm on Julia 1.8.0 with:

(modeling) pkg> st
Status `C:\Users\lucas\Documents\Code\Julia\modeling\Project.toml`
  [1520ce14] AbstractTrees v0.4.2
  [5b8099bc] DomainSets v0.5.13
  [a75be94c] GalacticOptim v3.4.0
  [961ee093] ModelingToolkit v8.20.0 `C:\Users\lucas\Documents\Code\Jdev\ModelingToolkit`
  [16a59e39] ModelingToolkitStandardLibrary v1.5.0
  [429524aa] Optim v1.7.2
  [7f7a1694] Optimization v3.8.2
  [36348300] OptimizationOptimJL v0.1.2
  [1dea7af3] OrdinaryDiffEq v6.24.2
  [91a5bcdd] Plots v1.31.7
  [295af30f] Revise v3.4.0
  [0c5d862f] Symbolics v4.10.4
  [1986cc42] Unitful v1.11.0
ChrisRackauckas commented 9 months ago

The tutorial was fixed