Skip to content

Commit 3410853

Browse files
author
Alexey Stukalov
committed
SemOptimizer: cleanup docstrings
1 parent efd4911 commit 3410853

5 files changed

Lines changed: 79 additions & 88 deletions

File tree

ext/SEMNLOptExt/NLopt.jl

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@
44

55
const NLoptConstraint = Pair{Any, Number}
66

7-
"""
8-
Uses *NLopt.jl* as the optimization engine.
9-
Only available if *NLopt.jl* is loaded in the current Julia session!
7+
struct SemOptimizerNLopt <: SemOptimizer{:NLopt}
8+
algorithm::Symbol
9+
local_algorithm::Union{Symbol, Nothing}
10+
options::Dict{Symbol, Any}
11+
local_options::Dict{Symbol, Any}
12+
equality_constraints::Vector{NLoptConstraint}
13+
inequality_constraints::Vector{NLoptConstraint}
14+
end
15+
16+
SEM.sem_optimizer_subtype(::Val{:NLopt}) = SemOptimizerNLopt
1017

11-
# Constructor
18+
############################################################################################
19+
### Constructor
20+
############################################################################################
1221

22+
"""
1323
SemOptimizer(;
1424
engine = :NLopt,
1525
algorithm = :LD_LBFGS,
@@ -21,6 +31,10 @@ Only available if *NLopt.jl* is loaded in the current Julia session!
2131
constraint_tol::Number = 0.0,
2232
kwargs...)
2333
34+
Uses *NLopt.jl* as the optimization engine. For more information on the available algorithms
35+
and options, see the [*NLopt.jl*](https://github.com/JuliaOpt/NLopt.jl) package and
36+
the [NLopt docs](https://nlopt.readthedocs.io/en/latest/).
37+
2438
# Arguments
2539
- `algorithm`: optimization algorithm.
2640
- `options::Dict{Symbol, Any}`: options for the optimization algorithm
@@ -38,8 +52,10 @@ Each constraint could be a function or any other callable object that
3852
takes the two input arguments:
3953
- the vector of the model parameters;
4054
- the array for the in-place calculation of the constraint gradient.
41-
To override the default tolerance, the constraint could be specified
55+
To override the default tolerance, the constraint can be specified
4256
as a pair of the function and its tolerance: `constraint_func => tol`.
57+
For information on how to use inequality and equality constraints,
58+
see [Constrained optimization](@ref) in our online documentation.
4359
4460
# Example
4561
```julia
@@ -55,42 +71,14 @@ my_constrained_optimizer = SemOptimizer(;
5571
)
5672
```
5773
58-
# Usage
59-
All algorithms and options from the *NLopt* library are available, for more information see
60-
the [*NLopt.jl*](https://github.com/JuliaOpt/NLopt.jl) package and the
61-
[NLopt docs](https://nlopt.readthedocs.io/en/latest/).
62-
For information on how to use inequality and equality constraints,
63-
see [Constrained optimization](@ref) in our online documentation.
64-
65-
# Extended help
66-
67-
## Interfaces
74+
# Interfaces
6875
- `algorithm(::SemOptimizerNLopt)`
6976
- `local_algorithm(::SemOptimizerNLopt)`
7077
- `options(::SemOptimizerNLopt)`
7178
- `local_options(::SemOptimizerNLopt)`
7279
- `equality_constraints(::SemOptimizerNLopt)`
7380
- `inequality_constraints(::SemOptimizerNLopt)`
74-
75-
## Implementation
76-
77-
Subtype of `SemOptimizer`.
7881
"""
79-
struct SemOptimizerNLopt <: SemOptimizer{:NLopt}
80-
algorithm::Symbol
81-
local_algorithm::Union{Symbol, Nothing}
82-
options::Dict{Symbol, Any}
83-
local_options::Dict{Symbol, Any}
84-
equality_constraints::Vector{NLoptConstraint}
85-
inequality_constraints::Vector{NLoptConstraint}
86-
end
87-
88-
SEM.sem_optimizer_subtype(::Val{:NLopt}) = SemOptimizerNLopt
89-
90-
############################################################################################
91-
### Constructor
92-
############################################################################################
93-
9482
function SemOptimizerNLopt(;
9583
algorithm = :LD_LBFGS,
9684
local_algorithm = nothing,

ext/SEMProximalOptExt/ProximalAlgorithms.jl

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
############################################################################################
22
### Types
33
############################################################################################
4-
"""
5-
Connects to `ProximalAlgorithms.jl` as the optimization backend.
6-
7-
Can be used for regularized SEM, for a tutorial see the online docs on [Regularization](@ref).
8-
9-
# Constructor
4+
mutable struct SemOptimizerProximal{A, B, C} <: SemOptimizer{:Proximal}
5+
algorithm::A
6+
operator_g::B
7+
operator_h::C
8+
end
109

10+
"""
1111
SemOptimizerProximal(;
1212
algorithm = ProximalAlgorithms.PANOC(),
1313
operator_g,
1414
operator_h = nothing,
1515
kwargs...,
16+
)
17+
18+
Connects to `ProximalAlgorithms.jl` as the optimization backend. For more information on
19+
the available algorithms and options, see the online docs on [Regularization](@ref) and
20+
the documentation of [*ProximalAlgorithms.jl*](https://github.com/JuliaFirstOrder/ProximalAlgorithms.jl) /
21+
[ProximalOperators.jl](https://github.com/JuliaFirstOrder/ProximalOperators.jl).
1622
1723
# Arguments
18-
- `algorithm`: optimization algorithm.
24+
- `algorithm`: proximal optimization algorithm.
1925
- `operator_g`: proximal operator (e.g., regularization penalty)
2026
- `operator_h`: optional second proximal operator
21-
22-
# Usage
23-
All algorithms and operators from `ProximalAlgorithms.jl` are available,
24-
for more information see the online docs on [Regularization](@ref) and
25-
the documentation of `ProximalAlgorithms.jl` / `ProximalOperators.jl`.
2627
"""
27-
mutable struct SemOptimizerProximal{A, B, C} <: SemOptimizer{:Proximal}
28-
algorithm::A
29-
operator_g::B
30-
operator_h::C
31-
end
32-
3328
SemOptimizerProximal(;
3429
algorithm = ProximalAlgorithms.PANOC(),
3530
operator_g,

src/optimizer/Empty.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
############################################################################################
22
### Types
33
############################################################################################
4-
"""
5-
Empty placeholder for models that don't need
6-
an optimizer part.
74

8-
# Constructor
5+
"""
6+
SemOptimizer(engine = :Empty)
97
10-
SemOptimizerEmpty()
8+
Constructs a dummy placeholder optimizer for models that don't need it.
119
"""
1210
struct SemOptimizerEmpty <: SemOptimizer{:Empty} end
1311

src/optimizer/abstract.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ sem_optimizer_subtype(engine::Symbol) = sem_optimizer_subtype(Val(engine))
2121
# fallback method for unsupported engines
2222
sem_optimizer_subtype(::Val{E}) where {E} = throw_engine_error(E)
2323

24+
"""
25+
SemOptimizer(args...; engine::Symbol = :Optim, kwargs...)
26+
27+
Constructs a `SemOptimizer` object that can be passed to [`fit`](@ref) for specifying aspects
28+
of the numerical optimization involved in fitting a SEM.
29+
30+
The keyword `engine` controlls which Julia package is used, with `:Optim` being the default.
31+
- [`optimizer_engines()`](@ref optimizer_engines) prints a list of currently available engines.
32+
- [`optimizer_engine_doc(EngineName)`](@ref optimizer_engine_doc) prints information on the usage of a specific engine.
33+
34+
More engines become available if specific packages are loaded, for example
35+
[*NLopt.jl*](https://github.com/JuliaOpt/NLopt.jl) (also see [Constrained optimization](@ref)
36+
in the online documentation) or
37+
[*ProximalAlgorithms.jl*](https://github.com/JuliaFirstOrder/ProximalAlgorithms.jl)
38+
(also see [Regularization](@ref) in the online documentation).
39+
40+
The arguments `args...` and `kwargs...` are engine-specific and control further
41+
aspects of the optimization process, such as the algorithm, convergence criteria or constraints.
42+
Information on those can be accessed with [`optimizer_engine_doc`](@ref).
43+
44+
[Custom optimizer types](@ref) shows how to connect the *SEM.jl* package to a completely new optimization engine.
45+
"""
46+
SemOptimizer
47+
2448
# default constructor that dispatches to the engine-specific type
2549
SemOptimizer(::Val{E}, args...; kwargs...) where {E} =
2650
sem_optimizer_subtype(E)(args...; kwargs...)

src/optimizer/optim.jl

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,52 @@
33
############################################################################################
44
### Types and Constructor
55
############################################################################################
6-
"""
7-
SemOptimizerOptim{A, B} <: SemOptimizer{:Optim}
8-
9-
Connects to `Optim.jl` as the optimization backend.
106

11-
# Constructor
7+
# SemOptimizer for the Optim.jl
8+
mutable struct SemOptimizerOptim{A, B} <: SemOptimizer{:Optim}
9+
algorithm::A
10+
options::B
11+
end
1212

13-
SemOptimizerOptim(;
13+
"""
14+
SemOptimizer(;
15+
engine = :Optim,
1416
algorithm = LBFGS(),
1517
options = Optim.Options(;f_reltol = 1e-10, x_abstol = 1.5e-8),
1618
kwargs...)
1719
20+
Connects to *Optim.jl* as the optimization engine.
21+
22+
For more information on the available algorithms and options,
23+
see the [*Optim.jl* docs](https://julianlsolvers.github.io/Optim.jl/stable/).
24+
1825
# Arguments
19-
- `algorithm`: optimization algorithm from `Optim.jl`
26+
- `algorithm`: optimization algorithm from *Optim.jl*
2027
- `options::Optim.Options`: options for the optimization algorithm
2128
22-
# Usage
23-
All algorithms and options from the Optim.jl library are available, for more information see
24-
the Optim.jl online documentation.
25-
2629
# Examples
2730
```julia
28-
my_optimizer = SemOptimizerOptim()
29-
3031
# hessian based optimization with backtracking linesearch and modified initial step size
3132
using Optim, LineSearches
3233
33-
my_newton_optimizer = SemOptimizerOptim(
34+
my_newton_optimizer = SemOptimizer(
35+
engine = :Optim,
3436
algorithm = Newton(
3537
;linesearch = BackTracking(order=3),
3638
alphaguess = InitialHagerZhang()
3739
)
3840
)
3941
```
4042
41-
# Extended help
42-
43-
## Constrained optimization
44-
43+
# Constrained optimization
4544
When using the `Fminbox` or `SAMIN` constrained optimization algorithms,
4645
the vector or dictionary of lower and upper bounds for each model parameter can be specified
4746
via `lower_bounds` and `upper_bounds` keyword arguments.
4847
Alternatively, the `lower_bound` and `upper_bound` keyword arguments can be used to specify
4948
the default bound for all non-variance model parameters,
5049
and the `variance_lower_bound` and `variance_upper_bound` keyword --
5150
for the variance parameters (the diagonal of the *S* matrix).
52-
53-
## Interfaces
54-
- `algorithm(::SemOptimizerOptim)`
55-
- `options(::SemOptimizerOptim)`
56-
57-
## Implementation
58-
59-
Subtype of `SemOptimizer`.
6051
"""
61-
mutable struct SemOptimizerOptim{A, B} <: SemOptimizer{:Optim}
62-
algorithm::A
63-
options::B
64-
end
65-
6652
SemOptimizerOptim(;
6753
algorithm = LBFGS(),
6854
options = Optim.Options(; f_reltol = 1e-10, x_abstol = 1.5e-8),

0 commit comments

Comments
 (0)