class_docstr: A simple genetic algorithm implementing truncation selection.
class_docstr_tok: ["A","simple","genetic","algorithm","implementing","truncation","selection","."]
docstr: Initialization function.
  
  Args:
      param_size - Parameter size.
      pop_size - Population size.
      truncation_divisor - Number by which the population is truncated
                           every iteration.
      sigma - Variance of normal distribution for parameter perturbation
      seed - Random seed for parameters sampling.
      logger - Logger
docstr_tok: ["Initialization","function",".","Args",":","param_size","-","Parameter","size",".","pop_size","-","Population","size",".","truncation_divisor","-","Number","by","which","the","population","is","truncated","every","iteration",".","sigma","-","Variance","of","normal","distribution","for","parameter","perturbation","seed","-","Random","seed","for","parameters","sampling",".","logger","-","Logger"]
code: def __init__(
      self,
      param_size: int,
      pop_size: int,
      truncation_divisor: int = 2,
      sigma: float = 0.01,
      seed: int = 0,
      logger: logging.Logger = None,
  ):
      """Initialization function.
  
      Args:
          param_size - Parameter size.
          pop_size - Population size.
          truncation_divisor - Number by which the population is truncated
                               every iteration.
          sigma - Variance of normal distribution for parameter perturbation
          seed - Random seed for parameters sampling.
          logger - Logger
      """
  
      if logger is None:
          self.logger = create_logger(name="SimpleGA")
      else:
          self.logger = logger
  
      self.param_size = param_size
  
      self.pop_size = abs(pop_size)
      self.truncation_divisor = abs(truncation_divisor)
  
      if self.pop_size % 2 == 1:
          self.pop_size += 1
          self.logger.info(
              "Population size should be an even number, set to {}".format(
                  self.pop_size
              )
          )
  
      if self.pop_size % self.truncation_divisor != 0:
          self.truncation_divisor = 2
          self.logger.info(
              "Population size must be a multiple of truncation divisor, \
                   set to {}".format(
                  self.truncation_divisor
              )
          )
  
      self.truncation = self.pop_size // self.truncation_divisor
      self.sigma = sigma
  
      self.params = jnp.zeros((pop_size, param_size))
      self._best_params = None
  
      self.rand_key = jax.random.PRNGKey(seed=seed)
  
      self.jnp_array = jax.jit(jnp.array)
  
      def ask_fn(
          key: jnp.ndarray, params: Union[np.ndarray, jnp.ndarray]
      ) -> Tuple[jnp.ndarray, Union[np.ndarray, jnp.ndarray]]:
          next_key, sample_key = jax.random.split(key=key, num=2)
  
          perturbations = jax.random.normal(
              key=sample_key, shape=(self.pop_size, self.param_size)
          )
  
          return next_key, params + perturbations * self.sigma
  
      self.ask_fn = jax.jit(ask_fn)
  
      def tell_fn(
          fitness: Union[np.ndarray, jnp.ndarray],
          params: Union[np.ndarray, jnp.ndarray],
      ) -> Union[np.ndarray, jnp.ndarray]:
          params = params[fitness.argsort(axis=0)]
          params = params[-self.truncation :].repeat(
              self.truncation_divisor, axis=0
          )
          return params
  
      self.tell_fn = jax.jit(tell_fn)
  
file_path: evojax/evojax/algo/simple_ga.py
imports: import logging
  import numpy
  from typing import Union
  from typing import Tuple
  import jax
  import jax.numpy
  from evojax.algo.base import NEAlgorithm
  from evojax.util import create_logger
label_desc: Use image node_id 1 to create a new SimpleGA object from inherited base classes: NEAlgorithm with example: obj = SimpleGA(param_size, pop_size, truncation_divisor, sigma, seed, logger)