Convert Figma logo to code with AI

JuliaNLSolvers logoOptim.jl

Optimization functions for Julia

1,168
233
1,168
107

Top Related Projects

2,331

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)

Quick Overview

The Optim.jl package is a Julia library for numerical optimization, providing a wide range of optimization algorithms and tools for solving various types of optimization problems. It is part of the JuliaNLSolvers organization, which focuses on developing high-performance numerical solvers for Julia.

Pros

  • Comprehensive Optimization Algorithms: Optim.jl offers a diverse set of optimization algorithms, including first-order methods (e.g., gradient descent, conjugate gradient), second-order methods (e.g., Newton's method, Quasi-Newton methods), and global optimization techniques (e.g., simulated annealing, genetic algorithms).
  • Flexible and Extensible: The library is designed to be flexible and extensible, allowing users to easily define their own objective functions, constraints, and optimization problems.
  • High Performance: Optim.jl is implemented in Julia, a high-performance programming language, which enables efficient numerical computations and optimization.
  • Active Development and Community: The project is actively maintained and developed by a community of contributors, ensuring regular updates, bug fixes, and new features.

Cons

  • Steep Learning Curve: The library can have a steep learning curve, especially for users new to numerical optimization or the Julia programming language.
  • Limited Documentation: While the documentation is generally good, some users may find it lacking in certain areas or specific use cases.
  • Dependency on Julia: Optim.jl is tightly coupled with the Julia programming language, which may be a limitation for users who prefer to work in other programming environments.
  • Performance Overhead: Depending on the complexity of the optimization problem, the performance of Optim.jl may not always be as efficient as specialized optimization libraries in other programming languages.

Code Examples

Here are a few code examples demonstrating the usage of Optim.jl:

Unconstrained Optimization:

using Optim

# Define the objective function
f(x) = (x[1] - 1)^2 + (x[2] - 2)^2

# Perform unconstrained optimization using the BFGS algorithm
result = optimize(f, [0.0, 0.0], BFGS())
println("Optimal solution: ", result.minimizer)
println("Optimal value: ", result.minimum)

Constrained Optimization:

using Optim

# Define the objective function and constraints
f(x) = (x[1] - 1)^2 + (x[2] - 2)^2
c1(x) = x[1] + x[2] - 3
c2(x) = x[1] - x[2]

# Perform constrained optimization using the SLSQP algorithm
result = optimize(f, [0.0, 0.0], COX(), constraints = [c1, c2])
println("Optimal solution: ", result.minimizer)
println("Optimal value: ", result.minimum)

Global Optimization:

using Optim

# Define the objective function
f(x) = (x[1] - 1)^2 + (x[2] - 2)^2

# Perform global optimization using the Simulated Annealing algorithm
result = optimize(f, [-10.0, -10.0], [10.0, 10.0], SimulatedAnnealing())
println("Optimal solution: ", result.minimizer)
println("Optimal value: ", result.minimum)

Multiobjective Optimization:

using Optim, MultivariateOptimization

# Define the objective functions
f1(x) = (x[1] - 1)^2
f2(x) = (x[2] - 2)^2

# Perform multiobjective optimization using the NSGA-II algorithm
result = optimize([f1, f2], [0.0, 0.0], NSGAII())
println("Pareto optimal solutions: ", result.minimizers)
println("Pareto optimal values: ", result.minimum)

Getting Started

To get started with Optim.jl, you can follow these steps:

  1. Install the Julia programming language on your system.

Competitor Comparisons

2,331

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)

Pros of JuMP

  • More comprehensive modeling capabilities for complex optimization problems
  • Supports a wider range of problem types, including mixed-integer programming
  • Integrates with multiple solvers, allowing users to switch easily

Cons of JuMP

  • Steeper learning curve due to its more extensive feature set
  • May be overkill for simpler optimization tasks
  • Potentially slower for basic unconstrained optimization problems

Code Comparison

JuMP example:

using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, x >= 0)
@objective(model, Min, x^2)
optimize!(model)

Optim example:

using Optim
f(x) = x^2
result = optimize(f, 0.0, BFGS())

JuMP offers a more declarative approach, allowing users to define variables, constraints, and objectives separately. Optim, on the other hand, focuses on providing a simpler interface for unconstrained optimization problems, where the objective function is defined directly.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Optim.jl

Build Status Codecov branch JOSS

Univariate and multivariate optimization in Julia.

Optim.jl is part of the JuliaNLSolvers family.

Help and support

For help and support, please post on the Optimization (Mathematical) section of the Julia discourse or the #math-optimization channel of the Julia slack.

Installation

Install Optim.jl using the Julia package manager:

import Pkg
Pkg.add("Optim")

Documentation

The online documentation is available at https://julianlsolvers.github.io/Optim.jl/stable.

Example

To minimize the Rosenbrock function, do:

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

To get information on the keywords used to construct method instances, use the Julia REPL help prompt (?)

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

Use with JuMP

You can use Optim.jl with JuMP.jl as follows:

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

If you use Optim.jl in your work, please cite the following:

@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}
}