
    -ji                    |    S SK Jr  S SKrS SKJr  S SKrS SKJr  S SKJ	r	  S SK
Jr   " S S\\R                  5      rg)	    )annotationsN)Any)BaseSampler)FrozenTrial)
TrialStatec                     ^  \ rS rSrSrSrSrSU 4S jjr\SS j5       r	\SS j5       r
SS jr\SS	 j5       r\R                  SS
 j5       r\R                   SS j5       rSS jrSS jrSS jrSrU =r$ )BaseGASampler   aF  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                v   > [         TU ]  " S0 UD6  U R                   S3U l        U R                   S3U l        g )Nz:generationz:parent: )super__init_subclass____name___GENERATION_KEY_PARENT_CACHE_KEY_PREFIX)clskwargs	__class__s     S/home/james-whalen/.local/lib/python3.13/site-packages/optuna/samplers/_ga/_base.pyr   BaseGASampler.__init_subclass__$   s9    !+F+!$k:*-,,x'@$    c                    U R                   $ N)r   r   s    r   _get_generation_key!BaseGASampler._get_generation_key)   s    """r   c                    U R                   $ r   )r   r   s    r   _get_parent_cache_key_prefix*BaseGASampler._get_parent_cache_key_prefix-   s    +++r   c                    Xl         g r   _population_size)selfpopulation_sizes     r   __init__BaseGASampler.__init__1   s     /r   c                    U R                   $ r   r!   )r#   s    r   r$   BaseGASampler.population_size4   s    $$$r   c                    Xl         g r   r!   )r#   values     r   r$   r(   8   s     %r   c                    [         e)a  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_parentBaseGASampler.select_parent<   s
    ( "!r   c                   UR                   R                  U R                  5       S5      nUb  U$ UR                  S[        R
                  /SS9nSu  pV[        U5       HD  nUR                   R                  U R                  5       S5      nX5:  a  M4  X5:  a  UnSnM?  US-  nMF     U R                  c   S5       eX`R                  :  a  UnOUS-   nUR                  R                  UR                  U R                  5       U5        U$ )	a  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_generation"BaseGASampler.get_trial_generationR   s   & ''++D,D,D,FM
!""E:;N;N:O[_"`/3,&!A++D,D,D,FKJ*,!+'($$)$ " $$0P2PP0"7"77'J'!+J,,OOT557	
 r   c                    UR                  S[        R                  /SS9 Vs/ s H5  nUR                  R	                  U R                  5       S5      U:X  d  M3  UPM7     sn$ s  snf )zGet the population of the given generation.

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

Returns:
    List of frozen trials in the given generation.
FTr2   N)r:   r   r;   r8   r9   r   )r#   r-   r.   r@   s       r   get_populationBaseGASampler.get_population   ss     **
(;(;'< + 
 !!%%d&>&>&@$G:U	 
 	
 
s   2A"A"c                &   US:X  a  / $ UR                   R                  UR                  5      nUR                  U R	                  5       [        U5      -   S5      nUb>  UR                  SS9n[        U5      nU Vs/ s H  owR                  U;   d  M  UPM     sn$ U R                  X5      nUR                   R                  UR                  U R	                  5       [        U5      -   U Vs/ s H  owR                  PM     sn5        U$ s  snf s  snf )a9  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)r3   )r=   get_study_system_attrs	_study_idr9   r   strr:   setr?   r/   set_study_system_attr)	r#   r-   r.   study_system_attrscached_parent_population_idsrA   parent_population_idsr@   parent_populations	            r   get_parent_population#BaseGASampler.get_parent_population   s     ?I"^^BB5??S'9'='=--/#j/A4(
$ (3&&&6F$'(D$E!'-ZveDY1YEvZZ $ 2 25 ENN00113c*oE.?@.?U.?@
 %$ [ As   ;D	D	+Dr!   )r   r   )returnrM   )r$   
int | None)rV   rW   )r*   intrV   None)r-   optuna.Studyr.   rX   rV   zlist[FrozenTrial])r-   rZ   r@   r   rV   rX   )r   
__module____qualname____firstlineno____doc__r   r   r   classmethodr   r   r%   propertyr$   setterabcabstractmethodr/   rE   rH   rT   __static_attributes____classcell__)r   s   @r   r	   r	      s    & 1O6A
 # # , ,0 % % & & 	" "*.`
(!% !%r   r	   )
__future__r   rb   typingr   optunaoptuna.samplers._baser   optuna.trial._frozenr   optuna.trial._stater   ABCr	   r   r   r   <module>rm      s.    " 
   - , *j%K j%r   