### Installation Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md Instructions on how to install the Optim.jl package using the Julia package manager. ```APIDOC ## Installation Install `Optim.jl` using the Julia package manager: ```julia import Pkg Pkg.add("Optim") ``` ``` -------------------------------- ### Basic Usage Example Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md An example demonstrating how to minimize the Rosenbrock function using the `optimize` function and the BFGS algorithm. ```APIDOC ## Example To minimize the [Rosenbrock function](https://en.wikipedia.org/wiki/Rosenbrock_function), do: ```julia julia> using Optim julia> rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 rosenbrock (generic function with 1 method) julia> result = optimize(rosenbrock, zeros(2), BFGS()) * Status: success * Candidate solution Final objective value: 5.471433e-17 * Found with Algorithm: BFGS * Convergence measures |x - x'| = 3.47e-07 ≰ 0.0e+00 |x - x'|/|x'| = 3.47e-07 ≰ 0.0e+00 |f(x) - f(x')| = 6.59e-14 ≰ 0.0e+00 |f(x) - f(x')|/|f(x')| = 1.20e+03 ≰ 0.0e+00 |g(x)| = 2.33e-09 ≤ 1.0e-08 * Work counters Seconds run: 0 (vs limit Inf) Iterations: 16 f(x) calls: 53 ∇f(x) calls: 53 julia> Optim.minimizer(result) 2-element Vector{Float64}: 0.9999999926033423 0.9999999926033423 julia> Optim.minimum(result) 5.471432670590216e-17 ``` ``` -------------------------------- ### Install Optim.jl Package Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md Use the Julia package manager to add Optim.jl to your project. This is the standard way to install Julia packages. ```julia import Pkg Pkg.add("Optim") ``` -------------------------------- ### Usage with JuMP Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md Example demonstrating how to use Optim.jl with the JuMP.jl modeling language to define and solve an optimization problem. ```APIDOC ## Use with JuMP You can use Optim.jl with [JuMP.jl](https://github.com/jump-dev/JuMP.jl) as follows: ```julia julia> using JuMP, Optim julia> model = Model(Optim.Optimizer) julia> set_optimizer_attribute(model, "method", BFGS()) julia> @variable(model, x[1:2]) julia> @objective(model, Min, (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2) (x[1]² - 2 x[1] + 1) + (100.0 * ((-x[1]² + x[2]) ^ 2.0)) julia> optimize!(model) julia> objective_value(model) 3.7218241804173566e-21 julia> value.(x) 2-element Vector{Float64}: 0.9999999999373603 0.99999999986862 ``` ``` -------------------------------- ### Define a New Solver Structure in Optim.jl Source: https://github.com/julianlsolvers/optim.jl/blob/master/docs/src/dev/contributing.md Example of defining a new solver structure, its state, initial state function, and update function for Optim.jl. Ensure all necessary fields and methods are implemented. ```julia struct Minim{IF, F<:Function, T} <: Optimizer alphaguess!::IF linesearch!::F minim_parameter::T end Minim(; alphaguess = LineSearches.InitialStatic(), linesearch = LineSearches.HagerZhang(), minim_parameter = 1.0) = Minim(linesearch, minim_parameter) mutable struct MinimState{T,N,G} x::AbstractArray{T,N} x_previous::AbstractArray{T,N} f_x_previous::T s::AbstractArray{T,N} @add_linesearch_fields() end function initial_state(method::Minim, options, d, initial_x) # prepare cache variables etc here end function update!{T}(d, state::MinimState{T}, method::Minim) # code for Minim here false # should the procedure force quit? end ``` -------------------------------- ### LBFGS Constructor and Description Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md Provides details on the LBFGS optimizer constructor, including its parameters like memory length (m) and initial Hessian approximation scaling. Explains its implementation as a limited-memory quasi-Newton method. ```julia help?> LBFGS search: LBFGS LBFGS ≡≡≡≡≡ Constructor =========== LBFGS(; m::Integer = 10, alphaguess = LineSearches.InitialStatic(), linesearch = LineSearches.HagerZhang(), P=nothing, precondprep = (P, x) -> nothing, manifold = Flat(), scaleinvH0::Bool = P === nothing) LBFGS has two special keywords; the memory length m, and the scaleinvH0 flag. The memory length determines how many previous Hessian approximations to store. When scaleinvH0 == true, then the initial guess in the two-loop recursion to approximate the inverse Hessian is the scaled identity, as can be found in Nocedal and Wright (2nd edition) (sec. 7.2). In addition, LBFGS supports preconditioning via the P and precondprep keywords. Description =========== The LBFGS method implements the limited-memory BFGS algorithm as described in Nocedal and Wright (sec. 7.2, 2006) and original paper by Liu & Nocedal (1989). It is a quasi-Newton method that updates an approximation to the Hessian using past approximations as well as the gradient. References ========== • Wright, S. J. and J. Nocedal (2006), Numerical optimization, 2nd edition. Springer • Liu, D. C. and Nocedal, J. (1989). "On the Limited Memory Method for Large Scale Optimization". Mathematical Programming B. 45 (3): 503–528 ``` -------------------------------- ### Minimize Rosenbrock Function with BFGS Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md Demonstrates minimizing the Rosenbrock function using the BFGS algorithm. Shows how to use the optimize function and retrieve the minimizer and minimum value. ```julia julia> using Optim julia> rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2 rosenbrock (generic function with 1 method) julia> result = optimize(rosenbrock, zeros(2), BFGS()) * Status: success * Candidate solution Final objective value: 5.471433e-17 * Found with Algorithm: BFGS * Convergence measures |x - x'| = 3.47e-07 ≰ 0.0e+00 |x - x'|/|x'| = 3.47e-07 ≰ 0.0e+00 |f(x) - f(x')| = 6.59e-14 ≰ 0.0e+00 |f(x) - f(x')|/|f(x')| = 1.20e+03 ≰ 0.0e+00 |g(x)| = 2.33e-09 ≤ 1.0e-08 * Work counters Seconds run: 0 (vs limit Inf) Iterations: 16 f(x) calls: 53 ∇f(x) calls: 53 julia> Optim.minimizer(result) 2-element Vector{Float64}: 0.9999999926033423 0.9999999852005355 julia> Optim.minimum(result) 5.471432670590216e-17 ``` -------------------------------- ### LBFGS Algorithm Details Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md Detailed information about the LBFGS (Limited-memory Broyden–Fletcher–Goldfarb–Shanno) algorithm, including its constructor, keywords, description, and references. ```APIDOC ## LBFGS Algorithm ### Constructor ```julia LBFGS(; m::Integer = 10, alphaguess = LineSearches.InitialStatic(), linesearch = LineSearches.HagerZhang(), P=nothing, precondprep = (P, x) -> nothing, manifold = Flat(), scaleinvH0::Bool = P === nothing) ``` LBFGS has two special keywords; the memory length `m`, and the `scaleinvH0` flag. The memory length determines how many previous Hessian approximations to store. When `scaleinvH0 == true`, then the initial guess in the two-loop recursion to approximate the inverse Hessian is the scaled identity, as can be found in Nocedal and Wright (2nd edition) (sec. 7.2). In addition, LBFGS supports preconditioning via the `P` and `precondprep` keywords. ### Description The LBFGS method implements the limited-memory BFGS algorithm as described in Nocedal and Wright (sec. 7.2, 2006) and original paper by Liu & Nocedal (1989). It is a quasi-Newton method that updates an approximation to the Hessian using past approximations as well as the gradient. ### References * Wright, S. J. and J. Nocedal (2006), Numerical optimization, 2nd edition. Springer * Liu, D. C. and Nocedal, J. (1989). "On the Limited Memory Method for Large Scale Optimization". Mathematical Programming B. 45 (3): 503–528 ``` -------------------------------- ### Minimize Rosenbrock Function with JuMP and BFGS Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md Integrates Optim.jl with JuMP.jl to define and solve an optimization problem. Sets the BFGS optimizer and demonstrates how to retrieve the objective value and variable values after optimization. ```julia julia> using JuMP, Optim julia> model = Model(Optim.Optimizer); julia> set_optimizer_attribute(model, "method", BFGS()) julia> @variable(model, x[1:2]); julia> @objective(model, Min, (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2) (x[1]² - 2 x[1] + 1) + (100.0 * ((-x[1]² + x[2]) ^ 2.0)) julia> optimize!(model) julia> objective_value(model) 3.7218241804173566e-21 julia> value.(x) 2-element Vector{Float64}: 0.9999999999373603 0.99999999986862 ``` -------------------------------- ### Citation Source: https://github.com/julianlsolvers/optim.jl/blob/master/README.md Information on how to cite Optim.jl in academic work. ```APIDOC ## Citation If you use `Optim.jl` in your work, please cite the following: ```tex @article{mogensen2018optim, author = {Mogensen, Patrick Kofod and Riseth, Asbj{\o}rn Nilsen}, title = {Optim: A mathematical optimization package for {Julia}}, journal = {Journal of Open Source Software}, year = {2018}, volume = {3}, number = {24}, pages = {615}, doi = {10.21105/joss.00615} } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.