docstr: Set the particle to the boundary
  
  This method resets particles that exceed the bounds to the intersection
  of its previous velocity and the boundary. This can be imagined as shrinking
  the previous velocity until the particle is back in the valid search space.
  Let :math:`\sigma_{i, t, d}` be the :math:`d` th shrinking value of the
  :math:`i` th particle at the time :math:`t` and :math:`v_{i, t}` the velocity
  of the :math:`i` th particle at the time :math:`t`. Then the new position is
  computed by the following equation:
  
  .. math::
      :nowrap:
  
      \begin{gather*}
      \mathbf{x}_{i, t} = \mathbf{x}_{i, t-1} + \sigma_{i, t} \mathbf{v}_{i, t} \\
      \\
      \text{with} \\
      \\
      \sigma_{i, t, d} = \begin{cases}
                          \frac{lb_d-x_{i, t-1, d}}{v_{i, t, d}} & \quad \text{if } x_{i, t, d} < lb_d \\
                          \frac{ub_d-x_{i, t-1, d}}{v_{i, t, d}} & \quad \text{if } x_{i, t, d} > ub_d \\
                          1 & \quad \text{otherwise}
                    \end{cases} \\
      \\
      \text{and} \\
      \\
      \sigma_{i, t} = \min_{d=1...n} \sigma_{i, t, d}
      \\
      \end{gather*}
docstr_tok: ["Set","the","particle","to","the","boundary","This","method","resets","particles","that","exceed","the","bounds","to","the","intersection","of","its","previous","velocity","and","the","boundary",".","This","can","be","imagined","as","shrinking","the","previous","velocity","until","the","particle","is","back","in","the","valid","search","space",".","Let",":","math",":","`","\\sigma_","{","i",",","t",",","d","}","`","be","the",":","math",":","`","d","`","th","shrinking","value","of","the",":","math",":","`","i","`","th","particle","at","the","time",":","math",":","`","t","`","and",":","math",":","`","v_","{","i",",","t","}","`","the","velocity","of","the",":","math",":","`","i","`","th","particle","at","the","time",":","math",":","`","t","`",".","Then","the","new","position","is","computed","by","the","following","equation",":","..","math",":",":",":","nowrap",":","\\begin","{","gather","*","}","\\mathbf","{","x","}","_","{","i",",","t","}","=","\\mathbf","{","x","}","_","{","i",",","t-1","}","+","\\sigma_","{","i",",","t","}","\\mathbf","{","v","}","_","{","i",",","t","}","\\\\","\\\\","\\text","{","with","}","\\\\","\\\\","\\sigma_","{","i",",","t",",","d","}","=","\\begin","{","cases","}","\\frac","{","lb_d-x_","{","i",",","t-1",",","d","}","}","{","v_","{","i",",","t",",","d","}","}","&","\\quad","\\text","{","if","}","x_","{","i",",","t",",","d","}","<","lb_d","\\\\","\\frac","{","ub_d-x_","{","i",",","t-1",",","d","}","}","{","v_","{","i",",","t",",","d","}","}","&","\\quad","\\text","{","if","}","x_","{","i",",","t",",","d","}",">","ub_d","\\\\","1","&","\\quad","\\text","{","otherwise","}","\\end","{","cases","}","\\\\","\\\\","\\text","{","and","}","\\\\","\\\\","\\sigma_","{","i",",","t","}","=","\\min_","{","d=1","...","n","}","\\sigma_","{","i",",","t",",","d","}","\\\\","\\end","{","gather","*","}"]
code: def shrink(self, position, bounds, **kwargs):
      r"""Set the particle to the boundary
  
          This method resets particles that exceed the bounds to the intersection
          of its previous velocity and the boundary. This can be imagined as shrinking
          the previous velocity until the particle is back in the valid search space.
          Let :math:`\sigma_{i, t, d}` be the :math:`d` th shrinking value of the
          :math:`i` th particle at the time :math:`t` and :math:`v_{i, t}` the velocity
          of the :math:`i` th particle at the time :math:`t`. Then the new position is
          computed by the following equation:
  
          .. math::
              :nowrap:
  
              \begin{gather*}
              \mathbf{x}_{i, t} = \mathbf{x}_{i, t-1} + \sigma_{i, t} \mathbf{v}_{i, t} \\
              \\
              \text{with} \\
              \\
              \sigma_{i, t, d} = \begin{cases}
                                  \frac{lb_d-x_{i, t-1, d}}{v_{i, t, d}} & \quad \text{if } x_{i, t, d} < lb_d \\
                                  \frac{ub_d-x_{i, t-1, d}}{v_{i, t, d}} & \quad \text{if } x_{i, t, d} > ub_d \\
                                  1 & \quad \text{otherwise}
                            \end{cases} \\
              \\
              \text{and} \\
              \\
              \sigma_{i, t} = \min_{d=1...n} \sigma_{i, t, d}
              \\
              \end{gather*}
  
          """
      if self.memory is None:
          new_pos = position
          self.memory = new_pos
      else:
          lb, ub = bounds
          lower_than_bound, greater_than_bound = self._out_of_bounds(
              position, bounds
          )
          velocity = position - self.memory
          # Create a coefficient matrix
          sigma = np.tile(1.0, position.shape)
          sigma[lower_than_bound] = (
              lb[lower_than_bound[1]] - self.memory[lower_than_bound]
          ) / velocity[lower_than_bound]
          sigma[greater_than_bound] = (
              ub[greater_than_bound[1]]
              - self.memory[greater_than_bound]
          ) / velocity[greater_than_bound]
          min_sigma = np.amin(sigma, axis=1)
          new_pos = position
          new_pos[lower_than_bound[0]] = (
              self.memory[lower_than_bound[0]]
              + np.multiply(
                  min_sigma[lower_than_bound[0]],
                  velocity[lower_than_bound[0]].T,
              ).T
          )
          new_pos[greater_than_bound[0]] = (
              self.memory[greater_than_bound[0]]
              + np.multiply(
                  min_sigma[greater_than_bound[0]],
                  velocity[greater_than_bound[0]].T,
              ).T
          )
          self.memory = new_pos
      return new_pos
  
imports: import inspect
  import logging
  import numpy
  import math
  from copy import copy
  from ..utils.reporter import Reporter
label_desc: Use image node_id 5 for calling the BoundaryHandler obj's underlying member method code with example usage: obj.shrink(position, bounds) and returns: new_pos