class macopt.macopt.Macopt(gradient, x_init, options={})

Conjugate gradient optimizer that uses only gradient information.

Parameters:
  • gradient (function) –

    The callable gradient must have the following signature:

    gradient(x) → (gradient, convergence)

    where x is an array_like that represents the variable vector, gradient is an array_like that represents the gradient at point x. The minimization terminates when convergence < tol.

  • x_init (array_like) – Initial guess.
  • options (dict, optional) –

    Dictionary of solver options.

    tol : float
    Tolerance criterion for the user-supplied convergence, see above. Default: 1e-10.
    max_iterations : int
    Maximum number of iterations. Default: 500000.
    callback : function
    Function that is called after each iteration, with signature callback(current_x) -> void. Default: None.
    normalize_function : function
    If supplied, the minimizer will keep the variable vector normalized at all times. This is a requirement for certain problems. Signature must be normalize_function(current_x) -> void, i.e. it must normalize in place. Default: None.
    logging_level : int
    Level of verbosity of output. logging.WARNING shows a warning when the minimizer failed to converge. logging.INFO shows the convergence every log_every steps (see directly below). logging.DEBUG shows updates about various stages in the line search. For more information about logging levels, see here. Default: logging.WARNING.
    log_every : int
    Frequency of logging, i.e. if log_every = n, log current convergence every n iterations. Default: 25.

    In addition, there are several minimizer-specific options. For more information, see David MacKay’s page.

    linmin_max_iterations : int
    Default: 40.
    linmin_g1 : float
    Default: 2.0.
    linmin_g2 : float
    Default: 1.25.
    linmin_g3 : float
    Default: 0.5.
    last_x_default : float
    Default: 0.0001.
    gam_bound : float
    Default: 4.0.
Returns:

result – Dictionary with the result.

x : array_like

Solution vector.

converged : bool

Whether the minimizer converged or not.

tol : float

Final convergence.

iterations : int

Number of iterations.

Return type:

dict

Example

import numpy as np
from scipy.optimize import rosen_der, rosen

# Minimize the Rosenbrock function in 2D (https://mathworld.wolfram.com/RosenbrockFunction.html).
def gradient(x):
    grad = rosen_der(x)
    convergence = np.linalg.norm(grad)
    return grad, convergence

x_init = np.random.rand(2)

minimizer = Macopt(gradient, x_init)
result =  minimizer.minimize()
# => {'x': array([1., 1.]), 'converged': True, 'tol': 8.109748258843716e-12, 'iterations': 15}

Indices and tables