|
1 | 1 | # Modeling Optimization Problems
|
2 | 2 |
|
3 |
| -```julia |
4 |
| -using ModelingToolkit, Optimization, OptimizationOptimJL |
| 3 | +## Rosenbrock Function in 2D |
| 4 | +Let's solve the classical _Rosenbrock function_ in two dimensions. |
5 | 5 |
|
6 |
| -@variables x y |
7 |
| -@parameters a b |
| 6 | +First, we need to make some imports. |
| 7 | +```@example rosenbrock_2d |
| 8 | +using ModelingToolkit, Optimization, OptimizationOptimJL |
| 9 | +``` |
| 10 | +Now we can define our optimization problem. |
| 11 | +```@example rosenbrock_2d |
| 12 | +@variables begin |
| 13 | + x, [bounds = (-2.0, 2.0)] |
| 14 | + y, [bounds = (-1.0, 3.0)] |
| 15 | +end |
| 16 | +@parameters a = 1 b = 1 |
8 | 17 | loss = (a - x)^2 + b * (y - x^2)^2
|
9 |
| -@named sys = OptimizationSystem(loss,[x,y],[a,b]) |
| 18 | +@named sys = OptimizationSystem(loss, [x, y], [a, b]) |
| 19 | +``` |
10 | 20 |
|
| 21 | +A visualization of the objective function is depicted below. |
| 22 | +```@eval |
| 23 | +using Plots |
| 24 | +x = -2:0.01:2 |
| 25 | +y = -1:0.01:3 |
| 26 | +contour(x, y, (x,y) -> (1 - x)^2 + 100 * (y - x^2)^2, fill=true, color=:viridis, ratio=:equal, xlims=(-2, 2)) |
| 27 | +``` |
| 28 | + |
| 29 | +### Explanation |
| 30 | +Every optimization problem consists of a set of _optimization variables_. In this case we create two variables. Additionally we assign _box constraints_ for each of them. In the next step we create two parameters for the problem with `@parameters`. While it is not needed to do this it makes it easier to `remake` the problem later with different values for the parameters. The _objective function_ is specified as well and finally everything is used to construct an `OptimizationSystem`. |
| 31 | + |
| 32 | +## Building and Solving the Optimization Problem |
| 33 | +Next the actual `OptimizationProblem` can be created. At this stage an initial guess `u0` for the optimization variables needs to be provided via map, using the symbols from before. Concrete values for the parameters of the system can also be provided or changed. However, if the parameters have default values assigned they are used automatically. |
| 34 | +```@example rosenbrock_2d |
11 | 35 | u0 = [
|
12 |
| - x=>1.0 |
13 |
| - y=>2.0 |
| 36 | + x => 1.0 |
| 37 | + y => 2.0 |
14 | 38 | ]
|
15 | 39 | p = [
|
16 |
| - a => 6.0 |
17 |
| - b => 7.0 |
| 40 | + a => 1.0 |
| 41 | + b => 100.0 |
| 42 | +] |
| 43 | +
|
| 44 | +prob = OptimizationProblem(sys, u0, p, grad=true, hess=true) |
| 45 | +solve(prob, GradientDescent()) |
| 46 | +``` |
| 47 | + |
| 48 | +## Rosenbrock Function with Constraints |
| 49 | +```@example rosenbrock_2d_cstr |
| 50 | +using ModelingToolkit, Optimization, OptimizationOptimJL |
| 51 | +
|
| 52 | +@variables begin |
| 53 | + x, [bounds = (-2.0, 2.0)] |
| 54 | + y, [bounds = (-1.0, 3.0)] |
| 55 | +end |
| 56 | +@parameters a = 1 b = 100 |
| 57 | +loss = (a - x)^2 + b * (y - x^2)^2 |
| 58 | +cons = [ |
| 59 | + x^2 + y^2 ≲ 1, |
18 | 60 | ]
|
| 61 | +@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons) |
| 62 | +u0 = [ |
| 63 | + x => 1.0 |
| 64 | + y => 2.0 |
| 65 | +] |
| 66 | +prob = OptimizationProblem(sys, u0, grad=true, hess=true) |
| 67 | +solve(prob, IPNewton()) |
| 68 | +``` |
19 | 69 |
|
20 |
| -prob = OptimizationProblem(sys,u0,p,grad=true,hess=true) |
21 |
| -solve(prob,Newton()) |
| 70 | +A visualization of the objective function and the inequality constraint is depicted below. |
| 71 | +```@eval |
| 72 | +using Plots |
| 73 | +x = -2:0.01:2 |
| 74 | +y = -1:0.01:3 |
| 75 | +contour(x, y, (x,y) -> (1 - x)^2 + 100 * (y - x^2)^2, fill=true, color=:viridis, ratio=:equal, xlims=(-2, 2)) |
| 76 | +contour!(x, y, (x, y) -> x^2 + y^2, levels=[1], color=:lightblue, line=4) |
22 | 77 | ```
|
23 | 78 |
|
| 79 | +### Explanation |
| 80 | +Equality and inequality constraints can be added to the `OptimizationSystem`. An equality constraint can be specified via and `Equation`, e.g., `x^2 + y^2 ~ 1`. While inequality constraints via an `Inequality`, e.g., `x^2 + y^2 ≲ 1`. The syntax is here `\lesssim` and `\gtrsim`. |
| 81 | + |
| 82 | +## Nested Systems |
24 | 83 | Needs more text but it's super cool and auto-parallelizes and sparsifies too.
|
25 | 84 | Plus you can hierarchically nest systems to have it generate huge
|
26 | 85 | optimization problems.
|
0 commit comments