Module: Lbfgsb

Defined in:
lib/lbfgsb.rb,
lib/lbfgsb/version.rb,
ext/lbfgsb/lbfgsbext.c

Overview

Lbfgsb.rb is a Ruby binding for L-BFGS-B with Numo::NArray.

Constant Summary collapse

VERSION =

The version of Lbfgsb.rb you are using.

'0.5.1'
DBL_EPSILON =

The value of double epsilon used in the native extension.

DBL2NUM(DBL_EPSILON)
SZ_F77_INTEGER =

The bit size of fortran integer.

INT2NUM(32)

Class Method Summary collapse

Class Method Details

.minimize(fnc:, x_init:, jcb:, args: nil, bounds: nil, factr: 1e7, pgtol: 1e-5, maxcor: 10, maxiter: 15_000, verbose: nil) ⇒ Hash

Minimize a function using the L-BFGS-B algorithm.

Parameters:

  • fnc (Method/Proc)

    Method for calculating the function to be minimized.

  • x_init (Numo::DFloat)

    (shape: [n_elements]) Initial point.

  • jcb (Method/Proc/Boolean)

    Method for calculating the gradient vector. If true is given, fnc is assumed to return the function value and gardient vector as [f, g] array.

  • args (Object) (defaults to: nil)

    Arguments pass to the 'fnc' and 'jcb'.

  • bounds (Numo::DFloat/Nil) (defaults to: nil)

    (shape: [n_elements, 2]) [lower, upper] bounds for each element x. If nil is given, x is unbounded.

  • factr (Float) (defaults to: 1e7)

    The iteration will be stop when

    (f^k - f^k+1)/max{|f^k|,|f^{k+1}|,1} <= factr * Lbfgsb::DBL_EPSILON

    Typical values for factr: 1e12 for low accuracy; 1e7 for moderate accuracy; 1e1 for extremely high accuracy.

  • pgtol (Float) (defaults to: 1e-5)

    The iteration will be stop when

    max{|pg_i| i = 1, …, n} <= pgtol

    where pg_i is the ith component of the projected gradient.

  • maxcor (Integer) (defaults to: 10)

    The maximum number of variable metric corrections used to define the limited memory matrix.

  • maxiter (Integer) (defaults to: 15_000)

    The maximum number of iterations.

  • verbose (Integer/Nil) (defaults to: nil)

    If negative value or nil is given, no display output is generated.

Returns:

  • (Hash)

    Optimization results; { x:, n_fev:, n_jev:, n_iter:, fnc:, jcb:, task:, success: }

    • x [Numo::DFloat] Updated vector by optimization.

    • n_fev [Interger] Number of calls of the objective function.

    • n_jev [Integer] Number of calls of the jacobian.

    • n_iter [Integer] Number of iterations.

    • fnc [Float] Value of the objective function.

    • jcb [Numo::Narray] Values of the jacobian

    • task [String] Description of the cause of the termination.

    • success [Boolean] Whether or not the optimization exited successfully.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lbfgsb.rb', line 43

def minimize(fnc:, x_init:, jcb:, args: nil, bounds: nil, factr: 1e7, pgtol: 1e-5, maxcor: 10, maxiter: 15_000, verbose: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  n_elements = x_init.size
  l = Numo::DFloat.zeros(n_elements)
  u = Numo::DFloat.zeros(n_elements)
  nbd = SZ_F77_INTEGER == 64 ? Numo::Int64.zeros(n_elements) : Numo::Int32.zeros(n_elements)

  unless bounds.nil?
    n_elements.times do |n|
      lower = bounds[n, 0]
      upper = bounds[n, 1]
      l[n] = lower
      u[n] = upper
      if lower.finite? && !upper.finite?
        nbd[n] = 1
      elsif lower.finite? && upper.finite?
        nbd[n] = 2
      elsif !lower.finite? && upper.finite?
        nbd[n] = 3
      end
    end
  end

  min_l_bfgs_b(fnc, x_init, jcb, args, l, u, nbd, maxcor, factr, pgtol, maxiter, verbose)
end