Source code for pycif.plugins.simulators.dummy.simul
import numpy as np
from logging import info
[docs]
def simul(self, chi, grad=True, run_id=-1, **kwargs):
r"""Evaluate the analytical quadratic-bowl cost function and its gradient.
Computes
.. math::
J(\boldsymbol{\chi}) = \sum_{i=0}^{n-1}(\chi_i - i)^2,
\qquad
\nabla J(\boldsymbol{\chi}) = 2\,(\boldsymbol{\chi} - \mathbf{i})
with the global minimum at :math:`\chi^*_i = i`. No obs-operator or
observation vector is required.
Args:
self (Plugin): dummy simulator plugin instance.
chi (np.ndarray): current iterate :math:`\boldsymbol{\chi}`,
shape ``(n,)``.
grad (bool): if ``True`` (default), return both cost and gradient.
run_id (int): unused; accepted for interface consistency.
**kwargs: unused; accepted for interface consistency.
Returns:
tuple or float: ``(zcost, zgrad)`` when ``grad=True``;
``zcost`` when ``grad=False``.
"""
# Various variables
datei = self.datei
datef = self.datef
workdir = self.workdir
# # Get the observation operator from extra arguments
# if not hasattr(self, "obsoperator"):
# raise Exception(
# "Observation operator is missing to compute the "
# "simulator. Please check your setup files"
# )
# obsoper = self.obsoperator
controlvect = self.controlvect
# Saving chi to the control vector for later
controlvect.chi = chi
zcost = np.sum((chi - np.arange(len(chi))) ** 2)
zgrad = 2 * (chi - np.arange(len(chi)))
# Verbose the norms
znorm_grad_b = np.dot(zgrad, zgrad) ** 0.5
info(
"In Simulator:\n"
" grad(Jb) = {}\n"
" Jb = {}\n".format(znorm_grad_b, zcost)
)
return zcost, zgrad