|
1 | | -# Custom regularizers |
| 1 | +# Custom regularizers |
| 2 | + |
| 3 | +In the case where the regularizer for your application is not already implemented you can still implement one yourself and use our solvers. |
| 4 | +In this tutorial we are going to model the (nonsmooth) function |
| 5 | +```math |
| 6 | +h : \mathbb{R}^n \mapsto \mathbb{R} \cup \{\infty\} : x \mapsto \chi(x; \mathbb{B}_\infty) |
| 7 | +``` |
| 8 | +where $\chi(\cdot; A)$ is the indicator function on the set $A$ and $\mathbb{B}_\infty$ is the unit ball in the infinity norm, i.e, |
| 9 | +```math |
| 10 | +\chi(x; \mathbb{B}_\infty) = |
| 11 | +\begin{cases} |
| 12 | +0 & \text{if } \|x\|_\infty \leq 1,\\ |
| 13 | +\infty & \text{otherwise}. |
| 14 | +\end{cases} |
| 15 | +``` |
| 16 | + |
| 17 | +```@example |
| 18 | +using LinearAlgebra |
| 19 | +using ShiftedProximalOperators |
| 20 | +using RegularizedProblems |
| 21 | +using RegularizedOptimization |
| 22 | +
|
| 23 | +# First we create a type for our indicator function |
| 24 | +struct SimpleIndicator end |
| 25 | +
|
| 26 | +# Then, we add a evaluation function |
| 27 | +function (h::SimpleIndicator)(x) |
| 28 | + return norm(x, Inf) <= 1 ? 0.0 : Inf |
| 29 | +end |
| 30 | +
|
| 31 | +# Now, we add a type that represents the function t -> h(shift + t) |
| 32 | +mutable struct ShiftedSimpleIndicator{V} <: ShiftedProximableFunction |
| 33 | + shift::V |
| 34 | + temp::V |
| 35 | +end |
| 36 | +
|
| 37 | +# Add the evaluation function for the shifted type |
| 38 | +function (ψ::ShiftedSimpleIndicator)(x) |
| 39 | + @. ψ.temp = ψ.shift + x |
| 40 | + return norm(ψ.temp, Inf) <= 1 ? 0.0 : Inf |
| 41 | +end |
| 42 | +
|
| 43 | +# Add the shifted function |
| 44 | +function ShiftedProximalOperators.shifted(h::SimpleIndicator, xk::Vector{T}) where{T} |
| 45 | + return ShiftedSimpleIndicator(xk, similar(xk)) |
| 46 | +end |
| 47 | +
|
| 48 | +# Add the shift! function |
| 49 | +function ShiftedProximalOperators.shift!(ψ::ShiftedSimpleIndicator{Vector{T}}, xk::Vector{T}) where{T} |
| 50 | + ψ.shift .= xk |
| 51 | +end |
| 52 | +
|
| 53 | +# Add the prox! function for both the unshifted and shifted types, we can solve the proximal problem analytically by hand. |
| 54 | +function ShiftedProximalOperators.prox!( |
| 55 | + y::AbstractVector{T}, |
| 56 | + h::SimpleIndicator, |
| 57 | + q::AbstractVector{T}, |
| 58 | + σ::T, |
| 59 | +) where{T} |
| 60 | + for i ∈ eachindex(y) |
| 61 | + y[i] = min(max(q[i], -1), 1) |
| 62 | + end |
| 63 | +end |
| 64 | +
|
| 65 | +function ShiftedProximalOperators.prox!( |
| 66 | + y::AbstractVector{T}, |
| 67 | + ψ::ShiftedSimpleIndicator{Vector{T}}, |
| 68 | + q::AbstractVector{T}, |
| 69 | + σ::T, |
| 70 | +) where{T} |
| 71 | + for i ∈ eachindex(y) |
| 72 | + y[i] = min(max(q[i], -1 - ψ.shift[i]), 1 - ψ.shift[i]) |
| 73 | + end |
| 74 | +end |
| 75 | +
|
| 76 | +# We can try to use this new regularizer with R2 : |
| 77 | +
|
| 78 | +# define a small smooth problem |
| 79 | +using ADNLPModels |
| 80 | +f_model = ADNLPModel(x -> (x[1] - 4)^2, 3*ones(1), name = "Simple quadratic model") |
| 81 | +h = SimpleIndicator() |
| 82 | +regularized_nlp = RegularizedNLPModel(f_model, h) |
| 83 | +
|
| 84 | +out = R2(regularized_nlp, verbose = 1) |
| 85 | +println("R2 converged after $(out.iter) iterations to the solution x = $(out.solution)") |
| 86 | +``` |
0 commit comments