Skip to content

Commit 4bb680d

Browse files
improve basic example with standard rosenbrock function
1 parent 45ff78b commit 4bb680d

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

docs/src/examples/basic.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,41 @@
22

33
In this tutorial, we will show how to model and solve the nonconvex nonsmooth optimization problem
44
```math
5-
\min_{x \in \mathbb{R}^2} x_1^2 + 100(x_2 - x_1^2 - 2x_1)^2 + |x_1| + |x_2|.
5+
\min_{x \in \mathbb{R}^2} (1 - x_1)^2 + 100(x_2 - x_1^2)^2 + |x_1| + |x_2|,
66
```
7+
which can be seen as a $$\ell_1$$ regularization of the Rosenbrock function.
8+
It can be shown that the solution to the problem is
9+
```math
10+
x^* = \begin{pmatrix}
11+
0.25\\
12+
0.0575
13+
\end{pmatrix}
14+
```
15+
716

817
## Modelling the problem
918
We first formulate the objective function as the sum of a smooth function $f$ and a nonsmooth regularizer $h$:
1019
```math
11-
x_1^2 + 100(x_2 - x_1^2 - 2x_1)^2 + |x_1| + |x_2| = f(x_1, x_2) + h(x_1, x_2),
20+
(1 - x_1)^2 + 100(x_2 - x_1^2)^2 + |x_1| + |x_2| = f(x_1, x_2) + h(x_1, x_2),
1221
```
1322
where
1423
```math
1524
\begin{align*}
16-
f(x_1, x_2) &:= x_1^2 + 100(x_2 - x_1^2 - 2x_1)^2,\\
25+
f(x_1, x_2) &:= (1 - x_1)^2 + 100(x_2 - x_1^2)^2,\\
1726
h(x_1, x_2) &:= \|x\|_1.
1827
\end{align*}
1928
```
2029
To model $f$, we are going to use [ADNLPModels.jl](https://github.com/JuliaSmoothOptimizers/ADNLPModels.jl).
2130
For the nonsmooth regularizer, we observe that $h$ is actually readily available in [ProximalOperators.jl](https://github.com/JuliaFirstOrder/ProximalOperators.jl), you can refer to [this section](@ref regularizers) for a list of readily available regularizers.
2231
We then wrap the smooth function and the regularizer in a `RegularizedNLPModel`
2332

24-
```@example
33+
```@example basic
2534
using ADNLPModels
2635
using ProximalOperators
2736
using RegularizedProblems
2837
2938
# Model the function
30-
f_fun = x -> x[1]^2 + 100*(x[2] - x[1]^2 - 2*x[1])^2
39+
f_fun = x -> (1 - x[1])^2 + 100*(x[2] - x[1]^2)^2
3140
3241
# Choose a starting point for the optimization process, for the sake of this example, we choose
3342
x0 = [-1.0, 2.0]
@@ -47,57 +56,48 @@ We can now choose one of the solvers presented [here](@ref algorithms) to solve
4756
Please refer to other sections of this documentation to make the wisest choice for your particular problem.
4857
Depending on the problem structure and on requirements from the user, some solvers are more appropriate than others.
4958
The following tries to give a quick overview of what choices one can make.
50-
```@example
51-
using ADNLPModels
52-
using ProximalOperators
53-
using RegularizedProblems
5459

55-
f_fun = x -> x[1]^2 + 100*(x[2] - x[1]^2 - 2*x[1])^2
56-
x0 = [-1.0, 2.0]
57-
58-
f_model = ADNLPModel(f_fun, x0, name = "AD model of f")
59-
h = NormL1(1.0)
60-
regularized_pb = RegularizedNLPModel(f_model, h)
60+
Suppose for example that we don't want to use a quasi-Newton approach and that we don't have access to the Hessian of f, or that we don't want to incur the cost of computing it.
61+
In this case, the most appropriate solver would be R2.
62+
For this example, we also choose a relatively small tolerance by specifying the keyword arguments `atol` and `rtol` across all solvers.
6163

64+
```@example basic
6265
using RegularizedOptimization
63-
64-
# Suppose for example that we don't want to use a quasi-Newton approach
65-
# and that we don't have access to the Hessian of f, or that we don't want to incur the cost of computing it
66-
# In this case, the most appropriate solver would be R2.
67-
# For this example, we also choose a relatively small tolerance by specifying the keyword argument atol across all solvers.
68-
out = R2(regularized_pb, verbose = 10, atol = 1e-3)
66+
67+
out = R2(regularized_pb, verbose = 10, atol = 1e-3, rtol = 1e-3)
6968
println("R2 converged after $(out.iter) iterations to the solution x = $(out.solution)")
70-
println("--------------------------------------------------------------------------------------")
69+
```
7170

72-
# Now, on this example, we can actually use second information on f.
73-
# To do so, we are going to use TR, a trust-region solver that can exploit second order information.
74-
out = TR(regularized_pb, verbose = 10, atol = 1e-3)
75-
println("TR converged after $(out.iter) iterations to the solution x = $(out.solution)")
76-
println("--------------------------------------------------------------------------------------")
71+
Now, we can actually use second information on f.
72+
To do so, we are going to use TR, a trust-region solver that can exploit second order information.
73+
```@example basic
7774
78-
# Suppose for some reason we can not compute the Hessian.
79-
# In this case, we can try to switch to a quasi-Newton approximation, this can be done with NLPModelsModifiers.jl
80-
# We could choose to use TR again but for the sake of this tutorial we run it with R2N
75+
out = TR(regularized_pb, verbose = 10, atol = 1e-3, rtol = 1e-3)
76+
println("TR converged after $(out.iter) iterations to the solution x = $(out.solution)")
77+
```
8178

79+
Suppose for some reason we can not compute the Hessian.
80+
In this case, we can try to switch to a quasi-Newton approximation, this can be done with NLPModelsModifiers.jl
81+
We could choose to use TR again but for the sake of this tutorial we run it with R2N
82+
```@example basic
8283
using NLPModelsModifiers
8384
8485
# Switch the model of the smooth function to a quasi-Newton approximation
8586
f_model_lsr1 = LSR1Model(f_model)
8687
regularized_pb_lsr1 = RegularizedNLPModel(f_model_lsr1, h)
8788
8889
# Solve with R2N
89-
out = R2N(regularized_pb_lsr1, verbose = 10, atol = 1e-3)
90+
out = R2N(regularized_pb_lsr1, verbose = 10, atol = 1e-3, rtol = 1e-3)
9091
println("R2N converged after $(out.iter) iterations to the solution x = $(out.solution)")
91-
println("--------------------------------------------------------------------------------------")
92+
```
93+
94+
Finally, TRDH and R2DH are specialized for diagonal quasi-Newton approximations, and should be used instead of TR and R2N, respectively.
95+
```@example basic
9296
93-
# Finally, TRDH and R2DH are specialized for diagonal quasi-Newton approximations,
94-
# and should be used instead of TR and R2N, respectively.
9597
f_model_sg = SpectralGradientModel(f_model)
9698
regularized_pb_sg = RegularizedNLPModel(f_model_sg, h)
9799
98100
# Solve with R2DH
99-
out = R2DH(regularized_pb_sg, verbose = 10, atol = 1e-3)
101+
out = R2DH(regularized_pb_sg, verbose = 10, atol = 1e-3, rtol = 1e-3)
100102
println("R2DH converged after $(out.iter) iterations to the solution x = $(out.solution)")
101-
println("--------------------------------------------------------------------------------------")
102-
103103
```

0 commit comments

Comments
 (0)