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
84 changes: 18 additions & 66 deletions html/DiffEqUncertainty/01-expectation_introduction.html

Large diffs are not rendered by default.

69 changes: 25 additions & 44 deletions html/DiffEqUncertainty/02-AD_and_optimization.html

Large diffs are not rendered by default.

99 changes: 12 additions & 87 deletions markdown/DiffEqUncertainty/01-expectation_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@
author: "Adam Gerlach"
title: "An Intro to Expectations via DiffEqUncertainty.jl"
---
<!--
using Pkg, SciMLTutorials
cd(joinpath(dirname(pathof(SciMLTutorials)), ".."))
Pkg.pkg"activate ."
Pkg.pkg"instantiate"
SciMLTutorials.weave_file("DiffEqUncertainty","01-expectation_introduction.jmd",(:github,))
SciMLTutorials.weave_file("DiffEqUncertainty","01-expectation_introduction.jmd")
-->



## System Model
Expand Down Expand Up @@ -120,7 +111,7 @@ mean([g(sol) for sol in ensemblesol])

````
1-element Array{Float64,1}:
-0.003406754846664461
0.005073268080398201
````


Expand All @@ -137,7 +128,7 @@ expectation(g, prob, u0_dist, p, MonteCarlo(), Tsit5(); trajectories=100000)

````
1-element Array{Float64,1}:
0.007225918038175185
0.005772652448278111
````


Expand Down Expand Up @@ -191,9 +182,9 @@ We see that for this case the `Koopman()` algorithm produces a more accurate sol


````
2.078746 seconds (79.60 M allocations: 7.168 GiB, 71.29% gc time)
2.215703 seconds (79.60 M allocations: 7.168 GiB, 73.13% gc time)
1-element Array{Float64,1}:
0.0002649264693847175
0.00412338430030202
````


Expand All @@ -204,7 +195,7 @@ We see that for this case the `Koopman()` algorithm produces a more accurate sol


````
0.000824 seconds (12.28 k allocations: 1.112 MiB)
0.000801 seconds (12.28 k allocations: 1.112 MiB)
u: 1-element Array{Float64,1}:
0.0
````
Expand All @@ -222,9 +213,9 @@ u0_dist = [Uniform(0.0,10.0)]


````
1.082617 seconds (79.59 M allocations: 7.168 GiB, 45.45% gc time)
0.653305 seconds (79.60 M allocations: 7.168 GiB)
1-element Array{Float64,1}:
1.5058180695041825
1.504424534047735
````


Expand All @@ -237,7 +228,7 @@ and


````
0.004236 seconds (14.04 k allocations: 1.221 MiB)
0.004519 seconds (14.04 k allocations: 1.221 MiB)
1.5059722133001539
````

Expand Down Expand Up @@ -596,9 +587,8 @@ using Quadrature, Cuba


````
Error: ArgumentError: Package Quadrature not found in current path:
- Run `import Pkg; Pkg.add("Quadrature")` to install the Quadrature package
.
Error: ArgumentError: Package Cuba not found in current path:
- Run `import Pkg; Pkg.add("Cuba")` to install the Cuba package.
````


Expand Down Expand Up @@ -847,74 +837,9 @@ Status `/builds/JuliaGPU/DiffEqTutorials.jl/tutorials/DiffEqUncertainty/Project.
[ef61062a-5684-51dc-bb67-a0fcdec5c97d] DiffEqUncertainty 1.5.0
[0c46a032-eb83-5123-abaf-570d42b7fbaa] DifferentialEquations 6.15.0
[31c24e10-a181-5473-b8eb-7969acd0382f] Distributions 0.23.8
[f6369f11-7733-5829-9624-2563aa707210] ForwardDiff 0.10.12
[76087f3c-5699-56af-9a33-bf431cd00edd] NLopt 0.6.0
[1dea7af3-3e70-54e6-95c3-0bf5283fa5ed] OrdinaryDiffEq 5.42.1
[91a5bcdd-55d7-5caf-9e0b-520d859cae80] Plots 1.5.8
[67601950-bd08-11e9-3c89-fd23fb4432d2] Quadrature 1.3.0
```




<!--
## Batch + nout
````julia
using DiffEqGPU

function f(du, u,p,t)
@inbounds begin
du[1] = p[1]*u[1];
end
nothing
end

u0 = Float32[10.0]
p = Float32[-0.3]
tspan = (0.0f0,10.0f0)
prob = ODEProblem(f,u0,tspan,p)

g(sol) = [sol(4.0)[1], sol(6.0)[1]]

u0_dist = [truncated(Normal(3.0f0,2.0f0),-5f0,11f0)]
p_dist = [truncated(Normal(-.7f0, .1f0), -1f0,0f0)]

expectation(g, prob, u0_dist, p_dist, Koopman(), Tsit5(), EnsembleGPUArray();
quadalg = CubaSUAVE(), batch=1000, nout=2)[1]
````


````
Error: MethodError: no method matching __solvebp_call(::DiffEqBase.Quadratu
reProblem{true,Array{Float32,1},DiffEqUncertainty.var"#13#26"{DiffEqUncerta
inty.var"#20#33",DiffEqUncertainty.var"#21#34",Base.Iterators.Pairs{Union{}
,Union{},Tuple{},NamedTuple{(),Tuple{}}},typeof(Main.##WeaveSandBox#496.g),
DiffEqBase.ODEProblem{Array{Float32,1},Tuple{Float32,Float32},true,Array{Fl
oat32,1},DiffEqBase.ODEFunction{true,typeof(Main.##WeaveSandBox#496.f),Line
arAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Noth
ing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{U
nion{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProble
m},Tuple{OrdinaryDiffEq.Tsit5,DiffEqGPU.EnsembleGPUArray},Int64,Array{Bool,
1},Array{Distributions.Truncated{Distributions.Normal{Float32},Distribution
s.Continuous,Float32},1},Array{Float32,1}},Array{Float32,1},Array{Float32,1
},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}}, ::
Quadrature.CubaSUAVE, ::Quadrature.ReCallVJP{Quadrature.ZygoteVJP}, ::Array
{Float32,1}, ::Array{Float32,1}, ::Array{Float32,1}; reltol=0.01, abstol=0.
01, maxiters=1000000)
Closest candidates are:
__solvebp_call(::DiffEqBase.QuadratureProblem, !Matched::Quadrature.QuadG
KJL, ::Any, ::Any, ::Any, ::Any, !Matched::Any...; reltol, abstol, maxiters
, kwargs...) at /builds/JuliaGPU/DiffEqTutorials.jl/.julia/packages/Quadrat
ure/L8aMP/src/Quadrature.jl:70
__solvebp_call(::DiffEqBase.QuadratureProblem, !Matched::Quadrature.HCuba
tureJL, ::Any, ::Any, ::Any, ::Any, !Matched::Any...; reltol, abstol, maxit
ers, kwargs...) at /builds/JuliaGPU/DiffEqTutorials.jl/.julia/packages/Quad
rature/L8aMP/src/Quadrature.jl:87
__solvebp_call(::DiffEqBase.QuadratureProblem, !Matched::Quadrature.VEGAS
, ::Any, ::Any, ::Any, ::Any, !Matched::Any...; reltol, abstol, maxiters, k
wargs...) at /builds/JuliaGPU/DiffEqTutorials.jl/.julia/packages/Quadrature
/L8aMP/src/Quadrature.jl:113
````




-->
52 changes: 14 additions & 38 deletions markdown/DiffEqUncertainty/02-AD_and_optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
author: "Adam Gerlach"
title: "Optimization Under Uncertainty with DiffEqUncertainty.jl"
---
<!--
using Pkg, SciMLTutorials
cd(joinpath(dirname(pathof(SciMLTutorials)), ".."))
Pkg.pkg"activate ."
Pkg.pkg"instantiate"
SciMLTutorials.weave_file("DiffEqUncertainty","02-AD_and_optimization.jmd",(:github,))
SciMLTutorials.weave_file("DiffEqUncertainty","02-AD_and_optimization.jmd")

-->



This tutorial gives and overview of how to leverage the efficient Koopman expectation method from DiffEqUncertainty to perform optimization under uncertainty. We demonstrate this by using a bouncing ball model with an uncertain model parameter. We also demonstrate its application to problems with probabilistic constraints, in particular a special class of constraints called chance constraints.
Expand Down Expand Up @@ -138,7 +128,7 @@ mean_ensemble = mean([obs(sol) for sol in ensemblesol])


````
35.986155065108754
35.70585036601521
````


Expand Down Expand Up @@ -171,18 +161,6 @@ First, we load the required packages and define our loss function

````julia
using NLopt, DiffEqSensitivity, ForwardDiff
````


````
Error: ArgumentError: Package ForwardDiff not found in current path:
- Run `import Pkg; Pkg.add("ForwardDiff")` to install the ForwardDiff packa
ge.
````



````julia

make_u0(θ) = [θ[1],θ[2],θ[3], 0.0]

Expand Down Expand Up @@ -233,7 +211,8 @@ opt.min_objective = 𝔼_loss_nlopt


````
(0.0, [-1.0, 2.0, 50.0], :FORCED_STOP)
(0.0838151729557601, [-0.05713109086715892, 2.4366742783682063, 49.99835957
072762], :XTOL_REACHED)
````


Expand Down Expand Up @@ -274,8 +253,9 @@ using BenchmarkTools


````
119.826 μs (8 allocations: 448 bytes)
(0.0, [-1.0, 2.0, 50.0], :FORCED_STOP)
129.798 ms (851126 allocations: 63.07 MiB)
(0.0838151729557601, [-0.05713109086715892, 2.4366742783682063, 49.99835957
072762], :XTOL_REACHED)
````


Expand Down Expand Up @@ -363,7 +343,7 @@ expectation(constraint_obs, prob, make_u0(minx), p_uncertain, Koopman(), Tsit5()


````
0.0028823461332289553
0.9910305871387488
````


Expand Down Expand Up @@ -410,7 +390,8 @@ inequality_constraint!(opt,𝔼_constraint_nlopt, 1e-5)


````
(0.0, [-1.0, 2.0, 50.0], :FORCED_STOP)
(40.28996781875263, [-0.8176321801341233, 2.000952669391841, 50.0], :XTOL_R
EACHED)
````


Expand All @@ -425,20 +406,20 @@ The probability of impacting the wall is now


````
0.0028823461332289553
0.010004498174330818
````




We can check if this is within tolerance by
````julia
isapprox(λ, 0.01, atol=1e-5)
λ - 0.01 <= 1e-5
````


````
false
true
````


Expand Down Expand Up @@ -471,13 +452,6 @@ end
![](figures/02-AD_and_optimization_21_1.png)





## Conclusion



## Appendix

This tutorial is part of the SciMLTutorials.jl repository, found at: <https://github.com/SciML/SciMLTutorials.jl>.
Expand Down Expand Up @@ -517,7 +491,9 @@ Status `/builds/JuliaGPU/DiffEqTutorials.jl/tutorials/DiffEqUncertainty/Project.
[ef61062a-5684-51dc-bb67-a0fcdec5c97d] DiffEqUncertainty 1.5.0
[0c46a032-eb83-5123-abaf-570d42b7fbaa] DifferentialEquations 6.15.0
[31c24e10-a181-5473-b8eb-7969acd0382f] Distributions 0.23.8
[f6369f11-7733-5829-9624-2563aa707210] ForwardDiff 0.10.12
[76087f3c-5699-56af-9a33-bf431cd00edd] NLopt 0.6.0
[1dea7af3-3e70-54e6-95c3-0bf5283fa5ed] OrdinaryDiffEq 5.42.1
[91a5bcdd-55d7-5caf-9e0b-520d859cae80] Plots 1.5.8
[67601950-bd08-11e9-3c89-fd23fb4432d2] Quadrature 1.3.0
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 1 addition & 24 deletions notebook/DiffEqUncertainty/01-expectation_introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"cell_type": "markdown",
"source": [
"<!-- \nusing Pkg, SciMLTutorials\ncd(joinpath(dirname(pathof(SciMLTutorials)), \"..\"))\nPkg.pkg\"activate .\"\nPkg.pkg\"instantiate\"\nSciMLTutorials.weave_file(\"DiffEqUncertainty\",\"01-expectation_introduction.jmd\",(:github,))\nSciMLTutorials.weave_file(\"DiffEqUncertainty\",\"01-expectation_introduction.jmd\") \n-->\n\n\n\n## System Model\n\nFirst, lets consider the following linear model.\n\n$$u' = p u$$"
"## System Model\n\nFirst, lets consider the following linear model.\n\n$$u' = p u$$"
],
"metadata": {}
},
Expand Down Expand Up @@ -552,29 +552,6 @@
"The performance gains realized by leveraging batch GPU processing is problem dependent. In this case, the number of batch evaluations required to overcome the overhead of using the GPU exceeds the number of simulations required to converge to the quadrature solution."
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"<!--\n## Batch + nout"
],
"metadata": {}
},
{
"outputs": [],
"cell_type": "code",
"source": [
"using DiffEqGPU\n\nfunction f(du, u,p,t) \n @inbounds begin\n du[1] = p[1]*u[1];\n end\n nothing\nend\n\nu0 = Float32[10.0]\np = Float32[-0.3]\ntspan = (0.0f0,10.0f0)\nprob = ODEProblem(f,u0,tspan,p)\n\ng(sol) = [sol(4.0)[1], sol(6.0)[1]]\n\nu0_dist = [truncated(Normal(3.0f0,2.0f0),-5f0,11f0)]\np_dist = [truncated(Normal(-.7f0, .1f0), -1f0,0f0)]\n\nexpectation(g, prob, u0_dist, p_dist, Koopman(), Tsit5(), EnsembleGPUArray(); \n quadalg = CubaSUAVE(), batch=1000, nout=2)[1]"
],
"metadata": {},
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"-->"
],
"metadata": {}
}
],
"nbformat_minor": 2,
Expand Down
11 changes: 2 additions & 9 deletions notebook/DiffEqUncertainty/02-AD_and_optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"cell_type": "markdown",
"source": [
"<!-- \nusing Pkg, SciMLTutorials\ncd(joinpath(dirname(pathof(SciMLTutorials)), \"..\"))\nPkg.pkg\"activate .\"\nPkg.pkg\"instantiate\"\nSciMLTutorials.weave_file(\"DiffEqUncertainty\",\"02-AD_and_optimization.jmd\",(:github,))\nSciMLTutorials.weave_file(\"DiffEqUncertainty\",\"02-AD_and_optimization.jmd\") \n\n-->\n\n\n\nThis tutorial gives and overview of how to leverage the efficient Koopman expectation method from DiffEqUncertainty to perform optimization under uncertainty. We demonstrate this by using a bouncing ball model with an uncertain model parameter. We also demonstrate its application to problems with probabilistic constraints, in particular a special class of constraints called chance constraints. \n\n## System Model\nFirst lets consider a 2D bouncing ball, where the states are the horizontal position $x$, horizontal velocity $\\dot{x}$, vertical position $y$, and vertical velocity $\\dot{y}$. This model has two system parameters, acceleration due to gravity and coefficient of restitution (models energy loss when the ball impacts the ground). We can simulate such a system using `ContinuousCallback` as"
"This tutorial gives and overview of how to leverage the efficient Koopman expectation method from DiffEqUncertainty to perform optimization under uncertainty. We demonstrate this by using a bouncing ball model with an uncertain model parameter. We also demonstrate its application to problems with probabilistic constraints, in particular a special class of constraints called chance constraints. \n\n## System Model\nFirst lets consider a 2D bouncing ball, where the states are the horizontal position $x$, horizontal velocity $\\dot{x}$, vertical position $y$, and vertical velocity $\\dot{y}$. This model has two system parameters, acceleration due to gravity and coefficient of restitution (models energy loss when the ball impacts the ground). We can simulate such a system using `ContinuousCallback` as"
],
"metadata": {}
},
Expand Down Expand Up @@ -315,7 +315,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"isapprox(λ, 0.01, atol=1e-5)"
"λ - 0.01 <= 1e-5"
],
"metadata": {},
"execution_count": null
Expand All @@ -335,13 +335,6 @@
],
"metadata": {},
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Conclusion"
],
"metadata": {}
}
],
"nbformat_minor": 2,
Expand Down
Binary file modified pdf/DiffEqUncertainty/01-expectation_introduction.pdf
Binary file not shown.
Binary file modified pdf/DiffEqUncertainty/02-AD_and_optimization.pdf
Binary file not shown.
23 changes: 0 additions & 23 deletions script/DiffEqUncertainty/01-expectation_introduction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,3 @@ p_dist = [truncated(Normal(-.7f0, .1f0), -1f0,0f0)]
using SciMLTutorials
SciMLTutorials.tutorial_footer(WEAVE_ARGS[:folder],WEAVE_ARGS[:file])


using DiffEqGPU

function f(du, u,p,t)
@inbounds begin
du[1] = p[1]*u[1];
end
nothing
end

u0 = Float32[10.0]
p = Float32[-0.3]
tspan = (0.0f0,10.0f0)
prob = ODEProblem(f,u0,tspan,p)

g(sol) = [sol(4.0)[1], sol(6.0)[1]]

u0_dist = [truncated(Normal(3.0f0,2.0f0),-5f0,11f0)]
p_dist = [truncated(Normal(-.7f0, .1f0), -1f0,0f0)]

expectation(g, prob, u0_dist, p_dist, Koopman(), Tsit5(), EnsembleGPUArray();
quadalg = CubaSUAVE(), batch=1000, nout=2)[1]

Loading