Source code for pycif.plugins.minimizers.congrad.minimize
import copy
import numpy as np
from logging import info
[docs]
def minimize(self, finit, gradinit, chi0, **kwargs):
"""Run the CONGRAD Lanczos minimisation and return the optimal iterate.
Args:
self (Plugin): CONGRAD 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, **kwargs)
# Running CONGRAD
lanczvect0 = copy.deepcopy(gradinit)
xopt, gradopt, preduc, pevecs, iiter = self.congrad(
chi0, gradinit, lanczvect0, **kwargs
)
# Save pevecs as uncertainty reductions
if self.save_uncertainties:
self.pevecs = pevecs
# Final verbose and output
towrite = """
CONGRAD:
number of iterations: {}
achieved relative reduction of the gradient: {}
""".format(
iiter, preduc
)
info(towrite)
r1 = np.sqrt(np.dot(xopt, xopt))
r2 = np.sqrt(np.dot(gradopt, gradopt))
info("norm of x = " + str(r1))
info("norm of g = " + str(r2))
return xopt