Skip to content

Commit 9c17234

Browse files
Merge pull request #110 from SciML/mtk
Further integrate and document ModelingToolkit
2 parents 3cf9f0b + 159fd4f commit 9c17234

File tree

8 files changed

+96
-14
lines changed

8 files changed

+96
-14
lines changed

Project.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GalacticOptim"
22
uuid = "a75be94c-b780-496d-a8a9-0878b188d577"
33
authors = ["Vaibhavdixit02 <[email protected]>"]
4-
version = "0.4.6"
4+
version = "0.4.7"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
@@ -13,6 +13,7 @@ Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
1313
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1414
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1515
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
16+
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1617
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
1718
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1819
ProgressLogging = "33c8b6b6-d38a-422a-b730-caa89a2f386c"
@@ -32,6 +33,7 @@ FiniteDiff = "2.5"
3233
Flux = "0.11"
3334
ForwardDiff = "0.10"
3435
LoggingExtras = "0.4"
36+
ModelingToolkit = "5.2.1"
3537
Optim = "1"
3638
ProgressLogging = "0.1"
3739
Reexport = "0.2, 1.0"
@@ -43,7 +45,6 @@ Zygote = "0.5, 0.6"
4345
julia = "1.5"
4446

4547
[extras]
46-
BlackBoxOptim = "a134a8b2-14d6-55f6-9291-3336d3ab0209"
4748
CMAEvolutionStrategy = "8d3b24bd-414e-49e0-94fb-163cc3a3e411"
4849
DiffEqFlux = "aae7a2af-3d4f-5e19-a356-7da93b79d9d0"
4950
Evolutionary = "86b6b26d-c046-49b6-aa0b-5f0f74682bd6"
@@ -56,4 +57,4 @@ SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
5657
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5758

5859
[targets]
59-
test = ["BlackBoxOptim", "Evolutionary", "DiffEqFlux", "IterTools", "OrdinaryDiffEq", "NLopt", "CMAEvolutionStrategy", "Pkg", "Random", "SafeTestsets", "Test"]
60+
test = ["Evolutionary", "DiffEqFlux", "IterTools", "OrdinaryDiffEq", "NLopt", "CMAEvolutionStrategy", "Pkg", "Random", "SafeTestsets", "Test"]

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
33

44
[compat]
5-
Documenter = "0.25.3"
5+
Documenter = "0.26"

docs/make.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ makedocs(
2222
"API" => [
2323
"OptimizationProblem" => "API/optimization_problem.md",
2424
"OptimizationFunction" => "API/optimization_function.md",
25-
"solve" => "API/solve.md"
25+
"solve" => "API/solve.md",
26+
"ModelingToolkit Integration" => "API/modelingtoolkit.md"
2627
],
2728

2829
"Local Optimizers" => [
@@ -31,7 +32,7 @@ makedocs(
3132
"local_optimizers/local_hessian.md",
3233
"local_optimizers/local_hessian_free.md"
3334
],
34-
35+
3536
"Global Optimizers" => [
3637
"global_optimizers/global.md",
3738
"global_optimizers/global_constrained.md"

docs/src/API/modelingtoolkit.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ModelingToolkit Integration
2+
3+
GalacticOptim.jl is heavily integrated with the ModelingToolkit.jl
4+
symbolic system for symbolic-numeric optimizations. It provides a
5+
front-end for automating the construction, parallelization, and
6+
optimization of code. Optimizers can better interface with the extra
7+
symbolic information provided by the system.
8+
9+
There are two ways that the user interacts with ModelingToolkit.jl.
10+
One can use `OptimizationFunction` with `AutoModelingToolkit` for
11+
automatically transforming numerical codes into symbolic codes. See
12+
the [OptimizationFunction documentation](@id optfunction) for more
13+
details.
14+
15+
Secondly, one can generate `OptimizationProblem`s for use in
16+
GalacticOptim.jl from purely a symbolic front-end. This is the form
17+
users will encounter when using ModelingToolkit.jl directly, and its
18+
also the form supplied by domain-specific languages. For more information,
19+
see the [OptimizationSystem documentation](https://mtk.sciml.ai/dev/systems/OptimizationSystem/).

docs/src/API/optimization_function.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,31 @@ This uses the [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl)
126126
symbolic system for automatically converting the `f` function into
127127
a symbolic equation and uses symbolic differentiation in order to generate
128128
a fast derivative code. Note that this will also compile a new version
129-
of your `f` function that is automatically optimized. In this choice,
130-
it defaults to `grad=false` and `hess=false`, and one must change these
131-
to `true` in order to enable the symbolic derivation. Future updates
132-
will enable automatic parallelization and sparsity in the derived
133-
functions. This can be the fastest for many systems, especially when
134-
parallelization and sparsity are required, but can take the longest
135-
to generate.
129+
of your `f` function that is automatically optimized. Because of the
130+
required symbolic analysis, the state and parameters are required in
131+
the function definition, i.e.:
132+
133+
```julia
134+
OptimizationFunction(f,AutoModelingToolkit(),x0,p,
135+
grad = false, hess = false, sparse = false,
136+
checkbounds = false,
137+
linenumbers = true,
138+
parallel=SerialForm(),
139+
kwargs...)
140+
```
141+
142+
The special keyword arguments are as follows:
143+
144+
- `grad`: whether to symbolically generate the gradient function.
145+
- `hess`: whether to symbolically generate the Hessian function.
146+
- `sparse`: whether to use sparsity detection in the Hessian.
147+
- `checkbounds`: whether to perform bounds checks in the generated code.
148+
- `linenumbers`: whether to include line numbers in the generated code.
149+
- `parallel`: whether to automatically parallelize the calculations.
150+
151+
For more information, see the [ModelingToolkit.jl `OptimizationSystem` documentation](https://mtk.sciml.ai/dev/systems/OptimizationSystem/)
152+
153+
Summary:
136154

137155
- Not compatible with GPUs
138156
- Compatible with Hessian-based optimization

docs/src/tutorials/symbolic.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Symbolic Problem Building with ModelingToolkit
2+
3+
```julia
4+
using ModelingToolkit, GalacticOptim
5+
6+
@variables x y
7+
@parameters a b
8+
loss = (a - x)^2 + b * (y - x^2)^2
9+
sys = OptimizationSystem(loss,[x,y],[a,b])
10+
11+
u0 = [
12+
x=>1.0
13+
y=>2.0
14+
]
15+
p = [
16+
a => 6.0
17+
b => 7.0
18+
]
19+
20+
prob = OptimizationProblem(sys,u0,p,grad=true,hess=true)
21+
solve(prob,Newton())
22+
```
23+
24+
Needs text but it's super cool and auto-parallelizes and sparsifies too.
25+
Plus you can hierarchically nest systems to have it generate huge
26+
optimization problems. Check out the
27+
[ModelingToolkit.jl OptimizationSystem documentation](https://mtk.sciml.ai/dev/systems/OptimizationSystem/)
28+
for more information.

src/GalacticOptim.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ using ArrayInterface, Base.Iterators
1111
using ForwardDiff: DEFAULT_CHUNK_THRESHOLD
1212
import DiffEqBase: OptimizationProblem, OptimizationFunction, AbstractADType
1313

14+
import ModelingToolkit
15+
import ModelingToolkit: AutoModelingToolkit
16+
export AutoModelingToolkit
17+
1418
include("solve.jl")
1519
include("function.jl")
1620

test/rosenbrock.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,22 @@ using Evolutionary
110110
sol = solve(prob, CMAES=40 , λ = 100),abstol=1e-15)
111111
@test 10*sol.minimum < l1
112112

113+
#=
113114
using BlackBoxOptim
114115
prob = GalacticOptim.OptimizationProblem(optprob, x0, lb=[-1.0, -1.0], ub=[0.8, 0.8])
115116
sol = solve(prob, BBO())
116117
@test 10*sol.minimum < l1
117118
118119
prob_multi = EnsembleOptimizationProblem([prob, prob])
119120
sol_multi = solve(prob_multi, BBO(:borg_moea), FitnessScheme=ParetoFitnessScheme{2}(is_minimizing=true), ϵ=0.05, MaxSteps=500000)
120-
@test 10*sum(sol_multi.minimum) < l1
121+
@test 10*sum(sol_multi.minimum) < l1
122+
=#
123+
124+
using ModelingToolkit
125+
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
126+
x0 = zeros(2)
127+
_p = [1.0, 100.0]
128+
129+
f = OptimizationFunction(rosenbrock,ModelingToolkit.AutoModelingToolkit(),x0,_p,grad=true,hess=true)
130+
prob = OptimizationProblem(f,x0,_p)
131+
sol = solve(prob,Optim.Newton())

0 commit comments

Comments
 (0)