class_docstr: .. include:: /global.rst
  
  Please refer to |pso| for details on this algorithm.
class_docstr_tok: ["..","include",":",":","\/global.rst","Please","refer","to","|pso|","for","details","on","this","algorithm","."]
docstr: Initializes a PSO solver.
  
  :param num_particles: number of particles to use
  :type num_particles: int
  :param num_generations: number of generations to use
  :type num_generations: int
  :param max_speed: maximum velocity of each particle
  :type max_speed: float or None
  :param phi1: parameter used in updating position based on local best
  :type phi1: float
  :param phi2: parameter used in updating position based on global best
  :type phi2: float
  :param kwargs: box constraints for each hyperparameter
  :type kwargs: {'name': [lb, ub], ...}
  
  The number of function evaluations it will perform is `num_particles`*`num_generations`.
  The search space is rescaled to the unit hypercube before the solving process begins.
  
  >>> solver = ParticleSwarm(num_particles=10, num_generations=5, x=[-1, 1], y=[0, 2])
  >>> solver.bounds['x']
  [-1, 1]
  >>> solver.bounds['y']
  [0, 2]
  >>> solver.num_particles
  10
  >>> solver.num_generations
  5
  
  .. warning:: |warning-unconstrained|
docstr_tok: ["Initializes","a","PSO","solver",".",":","param","num_particles",":","number","of","particles","to","use",":","type","num_particles",":","int",":","param","num_generations",":","number","of","generations","to","use",":","type","num_generations",":","int",":","param","max_speed",":","maximum","velocity","of","each","particle",":","type","max_speed",":","float","or","None",":","param","phi1",":","parameter","used","in","updating","position","based","on","local","best",":","type","phi1",":","float",":","param","phi2",":","parameter","used","in","updating","position","based","on","global","best",":","type","phi2",":","float",":","param","kwargs",":","box","constraints","for","each","hyperparameter",":","type","kwargs",":","{","'name","'",":","[","lb",",","ub","]",",","...","}","The","number","of","function","evaluations","it","will","perform","is","`","num_particles","`","*","`","num_generations","`",".","The","search","space","is","rescaled","to","the","unit","hypercube","before","the","solving","process","begins",".",">",">",">","solver","=","ParticleSwarm","(","num_particles=10",",","num_generations=5",",","x=","[","-1",",","1","]",",","y=","[","0",",","2","]",")",">",">",">","solver.bounds","[","'","x","'","]","[","-1",",","1","]",">",">",">","solver.bounds","[","'","y","'","]","[","0",",","2","]",">",">",">","solver.num_particles","10",">",">",">","solver.num_generations","5","..","warning",":",":","|warning-unconstrained|"]
code: def __init__(
      self,
      num_particles,
      num_generations,
      max_speed=None,
      phi1=1.5,
      phi2=2.0,
      **kwargs,
  ):
      """
      Initializes a PSO solver.
  
      :param num_particles: number of particles to use
      :type num_particles: int
      :param num_generations: number of generations to use
      :type num_generations: int
      :param max_speed: maximum velocity of each particle
      :type max_speed: float or None
      :param phi1: parameter used in updating position based on local best
      :type phi1: float
      :param phi2: parameter used in updating position based on global best
      :type phi2: float
      :param kwargs: box constraints for each hyperparameter
      :type kwargs: {'name': [lb, ub], ...}
  
      The number of function evaluations it will perform is `num_particles`*`num_generations`.
      The search space is rescaled to the unit hypercube before the solving process begins.
  
      >>> solver = ParticleSwarm(num_particles=10, num_generations=5, x=[-1, 1], y=[0, 2])
      >>> solver.bounds['x']
      [-1, 1]
      >>> solver.bounds['y']
      [0, 2]
      >>> solver.num_particles
      10
      >>> solver.num_generations
      5
  
      .. warning:: |warning-unconstrained|
  
      """
  
      assert all(
          [len(v) == 2 and v[0] <= v[1] for v in kwargs.values()]
      ), "kwargs.values() are not [lb, ub] pairs"
      self._bounds = kwargs
      self._num_particles = num_particles
      self._num_generations = num_generations
  
      self._sobolseed = random.randint(100, 2000)
  
      if max_speed is None:
          max_speed = 0.7 / num_generations
      #            max_speed = 0.2 / math.sqrt(num_generations)
      self._max_speed = max_speed
      self._smax = [
          self.max_speed * (b[1] - b[0]) for _, b in self.bounds.items()
      ]
      self._smin = list(map(op.neg, self.smax))
  
      self._phi1 = phi1
      self._phi2 = phi2
  
imports: import math
  import operator
  import random
  import array
  import functools
  from .solver_registry import register_solver
  from .util import Solver, _copydoc, uniform_in_bounds
  from .None import util
  from .Sobol import Sobol
label_desc: Use image node_id 1 to create a new ParticleSwarm object from inherited base classes: Solver with example: obj = ParticleSwarm(num_particles, num_generations, max_speed, phi1, phi2)