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

add another hybrid example #466

Merged
merged 3 commits into from
Dec 21, 2020
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 Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
GalacticOptim = "a75be94c-b780-496d-a8a9-0878b188d577"
GeometricFlux = "7e08b658-56d3-11e9-2997-919d5b31e4ea"
NLopt = "76087f3c-5699-56af-9a33-bf431cd00edd"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -66,4 +67,4 @@ StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["DelayDiffEq", "Distances", "DataInterpolations", "DiffEqCallbacks", "Distributed", "GalacticOptim", "OrdinaryDiffEq", "NLopt", "Pkg", "Random", "SafeTestsets", "Statistics", "StochasticDiffEq", "Test", "GeometricFlux", "ReverseDiff"]
test = ["DelayDiffEq", "Distances", "DataInterpolations", "DiffEqCallbacks", "Distributed", "GalacticOptim", "OrdinaryDiffEq", "NLopt", "Optim", "Pkg", "Random", "SafeTestsets", "Statistics", "StochasticDiffEq", "Test", "GeometricFlux", "ReverseDiff"]
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ makedocs(
],
"Hybrid and Jump Tutorials" => Any[
"examples/hybrid_diffeq.md",
"examples/bouncing_ball.md"
"examples/jump.md",
],
"Optimal and Model Predictive Control Tutorials" => Any[
Expand Down
109 changes: 109 additions & 0 deletions docs/src/examples/bouncing_ball.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Bouncing Ball Hybrid ODE Optimization

The bouncing ball is a classic hybrid ODE which can be represented in
the [DifferentialEquations.jl event handling system](https://diffeq.sciml.ai/stable/features/callback_functions/). This can be applied to ODEs, SDEs, DAEs, DDEs,
and more. Let's now add the DiffEqFlux machinery to this
problem in order to optimize the friction that's required to match
data. Assume we have data for the ball's height after 15 seconds. Let's
first start by implementing the ODE:

```julia
using DiffEqFlux, Optim, OrdinaryDiffEq, DiffEqSensitivity

function f(du,u,p,t)
du[1] = u[2]
du[2] = -p[1]
end

function condition(u,t,integrator) # Event when event_f(u,t) == 0
u[1]
end

function affect!(integrator)
integrator.u[2] = -integrator.p[2]*integrator.u[2]
end

cb = ContinuousCallback(condition,affect!)
u0 = [50.0,0.0]
tspan = (0.0,15.0)
p = [9.8, 0.8]
prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob,Tsit5(),callback=cb)
```

Here we have a friction coefficient of `0.8`. We want to refine this
coefficient to find the value so that the predicted height of the ball
at the endpoint is 20. We do this by minimizing a loss function against
the value 20:

```julia
function loss(θ)
sol = solve(prob,Tsit5(),p=[9.8,θ[1]],callback=cb,sensealg=ForwardDiffSensitivity())
target = 20.0
abs2(sol[end][1] - target)
end

loss([0.8])
res = DiffEqFlux.sciml_train(loss,[0.8],BFGS())
```

```julia
* Status: success

* Candidate solution
Final objective value: 3.995469e-01

* Found with
Algorithm: BFGS

* Convergence measures
|x - x'| = 4.93e-07 ≰ 0.0e+00
|x - x'|/|x'| = 5.69e-07 ≰ 0.0e+00
|f(x) - f(x')| = 8.12e-10 ≰ 0.0e+00
|f(x) - f(x')|/|f(x')| = 2.03e-09 ≰ 0.0e+00
|g(x)| = 7.71e-12 ≤ 1.0e-08

* Work counters
Seconds run: 0 (vs limit Inf)
Iterations: 5
f(x) calls: 16
∇f(x) calls: 16
```

Finding an optimal friction coefficient of approximately `0.866`. In
that version we showcased forward sensitivity analysis, but adjoints
can be utilized as well:

```julia
function loss(θ)
sol = solve(prob,Tsit5(),p=[9.8,θ[1]],callback=cb,sensealg=ReverseDiffAdjoint())
target = 20.0
abs2(sol[end][1] - target)
end

loss([0.8])
res = DiffEqFlux.sciml_train(loss,[0.8],BFGS())
```

```julia
* Status: success

* Candidate solution
Final objective value: 3.995469e-01

* Found with
Algorithm: BFGS

* Convergence measures
|x - x'| = 4.93e-07 ≰ 0.0e+00
|x - x'|/|x'| = 5.69e-07 ≰ 0.0e+00
|f(x) - f(x')| = 8.12e-10 ≰ 0.0e+00
|f(x) - f(x')|/|f(x')| = 2.03e-09 ≰ 0.0e+00
|g(x)| = 7.71e-12 ≤ 1.0e-08

* Work counters
Seconds run: 0 (vs limit Inf)
Iterations: 5
f(x) calls: 16
∇f(x) calls: 16
```
39 changes: 39 additions & 0 deletions test/event_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using DiffEqFlux, Optim, OrdinaryDiffEq, DiffEqSensitivity, Test

function f(du,u,p,t)
du[1] = u[2]
du[2] = -p[1]
end

function condition(u,t,integrator) # Event when event_f(u,t) == 0
u[1]
end

function affect!(integrator)
integrator.u[2] = -integrator.p[2]*integrator.u[2]
end

cb = ContinuousCallback(condition,affect!)
u0 = [50.0,0.0]
tspan = (0.0,15.0)
p = [9.8, 0.8]
prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob,Tsit5(),callback=cb)

function loss(θ)
sol = solve(prob,Tsit5(),p=[9.8,θ[1]],callback=cb,sensealg=ForwardDiffSensitivity())
target = 20.0
abs2(sol[end][1] - target)
end

res = DiffEqFlux.sciml_train(loss,[0.8],BFGS())
@test loss(res.minimizer) < 1

function loss(θ)
sol = solve(prob,Tsit5(),p=[9.8,θ[1]],callback=cb,sensealg=ReverseDiffAdjoint())
target = 20.0
abs2(sol[end][1] - target)
end

res = DiffEqFlux.sciml_train(loss,[0.8],BFGS())
@test loss(res.minimizer) < 1
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ end

if GROUP == "All" || GROUP == "Integration"
@safetestset "Ensemble Tests" begin include("ensembles.jl") end
@safetestset "Event Tests" begin include("event_tests.jl") end
@safetestset "Partial Neural Tests" begin include("partial_neural.jl") end
@safetestset "Size Handling in Adjoint Tests" begin include("size_handling_adjoint.jl") end
@safetestset "odenet" begin include("odenet.jl") end
Expand Down