
    ,YHhq                    |    d dl mZ d dlZd dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ  G d deej                        Zy)	    )annotationsN)Any)BaseSampler)FrozenTrial)
TrialStatec                       e Zd ZdZdZdZd fdZedd       Zedd       Z	ddZ
edd       Zej                  dd	       Zej                  dd
       ZddZddZddZ xZS )BaseGASampleraz  Base class for Genetic Algorithm (GA) samplers.

    Genetic Algorithm samplers generate new trials by mimicking natural selection, using
    generations and populations to iteratively improve solutions. This base class defines the
    interface for GA samplers in Optuna and provides utility methods for managing generations and
    populations.

    The selection process is handled by :meth:`~BaseGASampler.select_parent`, which must be
    implemented by subclasses to define the parent selection strategy.

    Generation and population management is facilitated by methods like
    :meth:`~BaseGASampler.get_generation` and :meth:`~BaseGASampler.get_population`, ensuring
    consistent tracking and selection.

    Note:
        This class should be extended by subclasses that define specific GA sampling strategies,
        including parent selection and crossover operations.
    zBaseGASampler:generationzBaseGASampler:parent:c                t    t        |   di | | j                   d| _        | j                   d| _        y )Nz:generationz:parent: )super__init_subclass____name___GENERATION_KEY_PARENT_CACHE_KEY_PREFIX)clskwargs	__class__s     R/var/www/html/planif/env/lib/python3.12/site-packages/optuna/samplers/_ga/_base.pyr   zBaseGASampler.__init_subclass__$   s9    !+F+!$k:*-,,x'@$    c                    | j                   S N)r   r   s    r   _get_generation_keyz!BaseGASampler._get_generation_key)   s    """r   c                    | j                   S r   )r   r   s    r   _get_parent_cache_key_prefixz*BaseGASampler._get_parent_cache_key_prefix-   s    +++r   c                    || _         y r   _population_size)selfpopulation_sizes     r   __init__zBaseGASampler.__init__1   s
     /r   c                    | j                   S r   r   )r   s    r   r    zBaseGASampler.population_size4   s    $$$r   c                    || _         y r   r   )r   values     r   r    zBaseGASampler.population_size8   s
     %r   c                    t         )a9  Select parent trials from the population for the given generation.

        This method is called once per generation to select parents from
        the population of the current generation.

        Output of this function is cached in the study system attributes.

        This method must be implemented in a subclass to define the specific selection strategy.

        Args:
            study:
                Target study object.
            generation:
                Target generation number.

        Returns:
            List of parent frozen trials.
        )NotImplementedError)r   study
generations      r   select_parentzBaseGASampler.select_parent<   s
    ( "!r   c                   |j                   j                  | j                         d      }||S |j                  dt        j
                  gd      }d\  }}t        |      D ]A  }|j                   j                  | j                         d      }||k  r3||kD  r|}d}=|dz  }C | j                  J d       || j                  k  r|}n|dz   }|j                  j                  |j                  | j                         |       |S )	ab  Get the generation number of the given trial.

        This method returns the generation number of the specified trial. If the generation number
        is not set in the trial's system attributes, it will calculate and set the generation
        number.

        The current generation number depends on the maximum generation number of all completed
        trials.

        Args:
            study:
                Study object which trial belongs to.
            trial:
                Trial object to get the generation number.

        Returns:
            Generation number of the given trial.
        NFTdeepcopystates	use_cache)r   r      zPopulation size must be set.)system_attrsgetr   _get_trialsr   COMPLETEreversedr   _storageset_trial_system_attr	_trial_id)r   r'   trialr(   trialsmax_generationmax_generation_countts           r   get_trial_generationz"BaseGASampler.get_trial_generationR   s   & ''++D,D,D,FM
!""E:;N;N:O[_"`/3,,&! 		*A++D,D,D,FKJN*n,!+'($$)$		* $$0P2PP0$"7"77'J'!+J,,OOT557	
 r   c                    |j                  dt        j                  gd      D cg c]1  }|j                  j	                  | j                         d      |k(  r|3 c}S c c}w )a
  Get the population of the given generation.

        Args:
            study:
                Target study object.
            generation:
                Target generation number.

        Returns:
            List of frozen trials in the given generation.
        FTr+   N)r3   r   r4   r1   r2   r   )r   r'   r(   r9   s       r   get_populationzBaseGASampler.get_population   sj     **
(;(;'< + 
 !!%%d&>&>&@$G:U	 
 	
 
s   6Ac                   |dk(  rg S |j                   j                  |j                        }|j                  | j	                         t        |      z   d      }|%|j                  d      }|D cg c]  }||   	 c}S | j                  ||      }|j                   j                  |j                  | j	                         t        |      z   |D cg c]  }|j                   c}       |S c c}w c c}w )a  Get the parent population of the given generation.

        This method caches the parent population in the study's system attributes.

        Args:
            study:
                Target study object.
            generation:
                Target generation number.

        Returns:
            List of parent frozen trials. If `generation == 0`, returns an empty list.
        r   NF)r,   )
r6   get_study_system_attrs	_study_idr2   r   strr3   r)   set_study_system_attrr8   )	r   r'   r(   study_system_attrscached_parent_population_idsr:   trial_idparent_populationr9   s	            r   get_parent_populationz#BaseGASampler.get_parent_population   s     ?I"^^BB5??S'9'='=--/#j/A4(
$ (3&&&6F5QRF8$RR $ 2 25* ENN00113c*oE.?@U@
 %$ S As   1C-C2)r   r   )returnrD   )r    
int | None)rK   rL   )r$   intrK   None)r'   optuna.Studyr(   rM   rK   zlist[FrozenTrial])r'   rO   r9   r   rK   rM   )r   
__module____qualname____doc__r   r   r   classmethodr   r   r!   propertyr    setterabcabstractmethodr)   r>   r@   rJ   __classcell__)r   s   @r   r	   r	      s    & 1O6A
 # # , ,0 % % & & 	" "*.`
( %r   r	   )
__future__r   rV   typingr   optunaoptuna.samplers._baser   optuna.trial._frozenr   optuna.trial._stater   ABCr	   r   r   r   <module>r`      s.    " 
   - , *i%K i%r   