code: def CollapseCost(clip=False, limit=1.0, samples=50, mask=None):
      """cost(x) - min(cost) is >= limit for all samples within an interval,
      where if clip is True, then clip beyond the space sampled the optimizer,
      and mask is a dict of {index:bounds} where bounds are provided as an
      interval (min,max), or a list of intervals:
  
      ``bool(collapse_cost(monitor, **kwds))``"""
      kwds = {
          "limit": limit,
          "samples": samples,
          "clip": clip,
          "mask": mask,
      }
      doc = "CollapseCost with %s" % kwds
  
      def _CollapseCost(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 lg <= samples:
              return info(null)
          # XXX: mask = interval_overlap(mask, solver_bounds(inst))?
          # XXX: might want to log/utilize *where* collapse happens...
          #       if collapse_cost(inst._stepmon, **kwds): return info(doc)
          collapsed = ct.collapse_cost(inst._stepmon, **kwds)
          if collapsed:
              return info(doc + " at %s" % str(collapsed))
          # otherwise bail out
          return info(null)
  
      _CollapseCost.__doc__ = doc
      return _CollapseCost
  
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 18 for calling a global function with example usage: CollapseCost(clip, limit, samples, mask) and returns: _CollapseCost, info, info, info, info