Source code for pycif.plugins.minimizers.m1qn3.minimize
import numpy as np
from logging import info
[docs]
def minimize(self, finit, gradinit, chi0, **kwargs):
"""Run the M1QN3 L-BFGS minimisation and return the optimal iterate.
Args:
self (Plugin): M1QN3 minimizer plugin instance.
finit (float): initial cost function value :math:`J(\chi_0)`.
gradinit (np.ndarray): initial gradient :math:`\\nabla J(\chi_0)`,
shape ``(n,)``.
chi0 (np.ndarray): initial iterate :math:`\chi_0`, shape ``(n,)``.
**kwargs: forwarded to the simulator.
Returns:
np.ndarray: optimal iterate :math:`\chi_\mathrm{opt}`, shape ``(n,)``.
"""
# Initializing options (and filling missing values with default)
self = self.check_options(chi0, finit, **kwargs)
# Running M1QN3
xopt, fopt, gradopt, niter, nsim, epsg, mode = self.m1qn3(
finit, gradinit, chi0, **kwargs
)
# Final verbose and output
towrite = """
M1QN3:
output mode is {}
number of iterations: {}
number of simulations: {}
realized relative precision on g: {}
""".format(
mode, niter, nsim, epsg
)
info(towrite)
r1 = np.sqrt(np.dot(xopt, xopt))
r2 = np.sqrt(np.dot(gradopt, gradopt))
info("norm of x = " + str(r1))
info("f = " + str(fopt))
info("norm of g = " + str(r2))
return xopt