Skip to content

Commit 8b905d0

Browse files
Merge pull request #330 from SciML/constut
Add constraints tutorial
2 parents 94699cd + f4c150b commit 8b905d0

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

docs/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
[deps]
2+
AmplNLWriter = "7c4d4715-977e-5154-bfe0-e096adeac482"
23
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
34
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
45
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
6+
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
7+
Ipopt_jll = "9cc047cb-c261-5740-88fc-0cf96f7bdcc7"
58
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
69
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
10+
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
711
OptimizationBBO = "3e6eede4-6085-4f62-9a71-46d9bc1eb92b"
812
OptimizationCMAEvolutionStrategy = "bd407f91-200f-4536-9381-e4ba712f53f8"
913
OptimizationEvolutionary = "cb963754-43f6-435e-8d4b-99009ff27753"
14+
OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1"
1015
OptimizationNLopt = "4e6fcdb7-1186-4e1f-a706-475e75c168bb"
1116
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
1217
OptimizationOptimisers = "42dfb2eb-d2b4-4451-abcd-913932933ac1"

docs/pages.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pages = [
55
"tutorials/intro.md",
66
"tutorials/rosenbrock.md",
77
"tutorials/minibatch.md",
8-
"tutorials/symbolic.md"
8+
"tutorials/symbolic.md",
9+
"tutorials/constraints.md",
910
],
1011

1112
"API" => [
@@ -30,4 +31,4 @@ pages = [
3031
"Optimisers.jl" => "optimization_packages/optimisers.md",
3132
"QuadDIRECT.jl" => "optimization_packages/quaddirect.md"
3233
],
33-
]
34+
]

docs/src/tutorials/constraints.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,99 @@
1-
# [Using Equality and Inequality Constraints](@id constraints)
1+
# [Using Equality and Inequality Constraints](@id constraints)
2+
3+
Multiple optmization packages available with the MathOptInterface and Optim's `IPNewton` solver can handle non-linear constraints.
4+
Optimization.jl provides a simple interface to define the constraint as a julia function and then specify the bounds for the output
5+
in `OptimizationFunction` to indicate if it's an equality or inequality constraint.
6+
7+
Let's define the rosenbrock function as our objective function and consider the below inequalities as our constraints.
8+
9+
```math
10+
\begin{aligned}
11+
12+
x_1^2 + x_2^2 \leq 0.8 \\
13+
14+
0.0 \leq x_1 * x_2 \leq 5.0
15+
\end{aligned}
16+
```
17+
18+
```@example constraints
19+
using Optimization, OptimizationMOI, OptimizationOptimJL, ForwardDiff, ModelingToolkit
20+
21+
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
22+
x0 = zeros(2)
23+
_p = [1.0, 1.0]
24+
```
25+
26+
Next we define the sum of squares and the product of the optimization variables as our constraint functions.
27+
28+
```@example constraints
29+
cons(res, x, p) = (res .= [x[1]^2+x[2]^2, x[1]*x[2]])
30+
```
31+
32+
We'll use the `IPNewton` solver from Optim to solve the problem.
33+
34+
```@example constraints
35+
optprob = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff(), cons = cons)
36+
prob = OptimizationProblem(optprob, x0, _p, lcons = [-Inf, -1.0], ucons = [0.8, 2.0])
37+
sol = solve(prob, IPNewton())
38+
```
39+
40+
Let's check that the constraints are satisfied and the objective is lower than at initial values to be sure.
41+
42+
```@example constraints
43+
res = zeros(2)
44+
cons(res, sol.u, _p)
45+
res
46+
```
47+
48+
```@example constraints
49+
prob.f(sol.u, _p)
50+
```
51+
52+
We can also use the Ipopt library with the OptimizationMOI package.
53+
54+
```@example constraints
55+
sol = solve(prob, Ipopt.Optimizer())
56+
```
57+
58+
```@example constraints
59+
res = zeros(2)
60+
cons(res, sol.u, _p)
61+
res
62+
```
63+
64+
```@example constraints
65+
prob.f(sol.u, _p)
66+
```
67+
68+
We can also use ModelingToolkit as our AD backend and generate symbolic derivatives and expression graph for the objective and constraints.
69+
70+
Let's modify the bounds to use the function as an equality constraint. The constraint now becomes -
71+
72+
```math
73+
\begin{aligned}
74+
75+
x_1^2 + x_2^2 = 1.0 \\
76+
77+
x_1 * x_2 = 0.5
78+
\end{aligned}
79+
```
80+
81+
```@example constraints
82+
optprob = OptimizationFunction(rosenbrock, Optimization.AutoModelingToolkit(), cons = cons)
83+
prob = OptimizationProblem(optprob, x0, _p, lcons = [1.0, 0.5], ucons = [1.0, 0.5])
84+
```
85+
86+
Below the AmplNLWriter.jl package is used with to use the Ipopt library to solve the problem.
87+
88+
```@example constraints
89+
using AmplNLWriter, Ipopt_jll
90+
sol = solve(prob, AmplNLWriter.Optimizer(Ipopt_jll.amplexe))
91+
```
92+
93+
The constraints evaluate to 1.0 and 0.5 respectively as expected.
94+
95+
```@example constraints
96+
res = zeros(2)
97+
cons(res, sol.u, _p)
98+
println(res)
99+
```

0 commit comments

Comments
 (0)