code: def NormalizedCostTarget(fval=None, tolerance=1e-6, generations=30):
      """normalized absolute difference from given cost value is < tolerance:
      (if fval is not provided, then terminate when no improvement over g iterations)
  
      ``abs(cost[-1] - fval)/fval <= tolerance`` or ``(cost[-1] - cost[-g]) = 0``
      """
      # NOTE: modified from original behavior
      #  original --> if generations: then return cost[-g] - cost[-1] < 0
      #           --> else: return fval != 0 and abs((best - fval)/fval) < tol
      doc = "NormalizedCostTarget with %s" % {
          "fval": fval,
          "tolerance": tolerance,
          "generations": generations,
      }
  
      def _NormalizedCostTarget(inst, info=False):
          if info:
              info = lambda x: x
          else:
              info = bool
          hist = inst.energy_history
          lg = len(hist)
          if not lg:
              return info(null)
          if generations and fval is None:
              # XXX: throws error when hist is shorter than generations ?
              if lg > generations and (
                  (hist[-generations] - hist[-1]) <= 0
                  or (hist[-generations] == hist[-1])
              ):
                  return info(doc)
              return info(null)
          if not generations and fval is None:
              return info(doc)
          if abs(hist[-1] - fval) <= abs(tolerance * fval):
              return info(doc)
          return info(null)
  
      _NormalizedCostTarget.__doc__ = doc
      return _NormalizedCostTarget
  
imports: import numpy
  import mystic.collapse
  from mystic.math.distance import Lnorm
  from mystic._scipy060optimize import approx_fprime, _epsilon
label_desc: Use image node_id 8 for calling a global function with example usage: NormalizedCostTarget(fval, tolerance, generations) and returns: _NormalizedCostTarget, info, info, info, info, info, info