
    (^i?"                        d dl mZ d dlmZ d dlmZ d dlm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 d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl m"Z" erd dl#m$Z$  ed       G d de             Z%y)    )annotations)Callable)Sequence)Any)TYPE_CHECKINGN)experimental_class)BaseDistribution)BaseGASampler)LazyRandomState)'NSGAIIIElitePopulationSelectionStrategy)RandomSampler)NSGAIIAfterTrialStrategy)NSGAIIChildGenerationStrategy)BaseCrossover)UniformCrossover)IntersectionSearchSpace)FrozenTrial)
TrialState)Studyz3.2.0c                       e Zd ZdZddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZdd	Z	 	 	 	 	 	 dd
ZddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZ	ddZ
	 	 	 	 	 	 	 	 	 	 ddZ xZS )NSGAIIISamplera  Multi-objective sampler using the NSGA-III algorithm.

    NSGA-III stands for "Nondominated Sorting Genetic Algorithm III",
    which is a modified version of NSGA-II for many objective optimization problem.

    For further information about NSGA-III, please refer to the following papers:

    - `An Evolutionary Many-Objective Optimization Algorithm Using Reference-Point-Based
      Nondominated Sorting Approach, Part I: Solving Problems With Box Constraints
      <https://doi.org/10.1109/TEVC.2013.2281535>`__
    - `An Evolutionary Many-Objective Optimization Algorithm Using Reference-Point-Based
      Nondominated Sorting Approach, Part II: Handling Constraints and Extending to an Adaptive
      Approach
      <https://doi.org/10.1109/TEVC.2013.2281534>`__

    Args:
        reference_points:
            A 2 dimension ``numpy.ndarray`` with objective dimension columns. Represents
            a list of reference points which is used to determine who to survive.
            After non-dominated sort, who out of borderline front are going to survived is
            determined according to how sparse the closest reference point of each individual is.
            In the default setting the algorithm uses `uniformly` spread points to diversify the
            result. It is also possible to reflect your `preferences` by giving an arbitrary set of
            `target` points since the algorithm prioritizes individuals around reference points.

        dividing_parameter:
            A parameter to determine the density of default reference points. This parameter
            determines how many divisions are made between reference points on each axis. The
            smaller this value is, the less reference points you have. The default value is 3.
            Note that this parameter is not used when ``reference_points`` is not :obj:`None`.

    .. note::
        Other parameters than ``reference_points`` and ``dividing_parameter`` are the same as
        :class:`~optuna.samplers.NSGAIISampler`.

    2   Ng?g      ?   )population_sizemutation_prob	crossovercrossover_probswapping_probseedconstraints_funcreference_pointsdividing_parameter#elite_population_selection_strategychild_generation_strategyafter_trial_strategyc          	         |dk  rt        d      |t        |      }t        |t              st        d| d      ||j                  k  rt        d| d|j                   d| d      t
        |   |	       t        |
      | _        t        |      | _
        || _        t               | _        |
xs t        ||||	| j                        | _        |xs t!        |||||| j                        | _        |xs t%        |      | _        y )N   z5`population_size` must be greater than or equal to 2.'zu' is not a valid crossover. For valid crossovers see https://optuna.readthedocs.io/en/stable/reference/samplers.html.zUsing z9, the population size should be greater than or equal to z%. The specified `population_size` is .)r   )r   )r   r    r!   r"   rng)r   r   r   r   r    r*   )r    )
ValueErrorr   
isinstancer   	n_parentssuper__init__r   _random_samplerr   _rng_constraints_funcr   _search_spacer   $_elite_population_selection_strategyr   _child_generation_strategyr   _after_trial_strategy)selfr   r   r   r   r   r   r    r!   r"   r#   r$   r%   	__class__s                b/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/samplers/_nsgaiii/_sampler.pyr/   zNSGAIIISampler.__init__F   sR   2 QTUU(7I)]3I; T T  Y000 $KKTK^K^J_ `77F6GqJ  	9,$7#D)	!146 0 6 /!1!1#5II 	1 & ,-++#!1II 	' &: &
=U->
"    c                    | j                   j                          | j                  j                  j	                          y N)r0   
reseed_rngr1   r*   r   )r7   s    r9   r=   zNSGAIIISampler.reseed_rng   s(    '')		r:   c                    i }| j                   j                  |      j                         D ]  \  }}|j                         r|||<    |S r<   )r3   	calculateitemssingle)r7   studytrialsearch_spacenamedistributions         r9   infer_relative_search_spacez*NSGAIIISampler.infer_relative_search_space   sZ     57"&"4"4">">u"E"K"K"M 	.D,""$
 !-L	. r:   c           	     x    | j                  || j                  ||dz
        | j                  ||dz
        z         S )N   )r4   get_populationget_parent_population)r7   rB   
generations      r9   select_parentzNSGAIIISampler.select_parent   sF    88zA~6((
Q?@
 	
r:   c                    | j                  ||      }| j                  ||      }t        |      dk(  ri S | j                  |||      S )Nr   )get_trial_generationrK   lenr5   )r7   rB   rC   rD   rL   parent_populations         r9   sample_relativezNSGAIIISampler.sample_relative   sR     ..ue<
 66ujI !Q&I..ulDUVVr:   c                >    | j                   j                  ||||      S r<   )r0   sample_independent)r7   rB   rC   
param_nameparam_distributions        r9   rT   z!NSGAIIISampler.sample_independent   s'     ##665*&8
 	
r:   c                <    | j                   j                  ||       y r<   )r0   before_trial)r7   rB   rC   s      r9   rX   zNSGAIIISampler.before_trial   s    ))%7r:   c                    |t         j                  t         j                  t         j                  fv sJ | j	                  ||||       | j
                  j                  ||||       y r<   )r   COMPLETEFAILPRUNEDr6   r0   after_trial)r7   rB   rC   statevaluess        r9   r]   zNSGAIIISampler.after_trial   sW     ,,jooz?P?PQQQQ""5%?((ufEr:   )r   intr   zfloat | Noner   zBaseCrossover | Noner   floatr   ra   r   z
int | Noner    z/Callable[[FrozenTrial], Sequence[float]] | Noner!   znp.ndarray | Noner"   r`   r#   z>Callable[[Study, list[FrozenTrial]], list[FrozenTrial]] | Noner$   zXCallable[[Study, dict[str, BaseDistribution], list[FrozenTrial]], dict[str, Any]] | Noner%   zOCallable[[Study, FrozenTrial, TrialState, Sequence[float] | None], None] | NonereturnNone)rb   rc   )rB   r   rC   r   rb   dict[str, BaseDistribution])rB   r   rL   r`   rb   zlist[FrozenTrial])rB   r   rC   r   rD   rd   rb   zdict[str, Any])
rB   r   rC   r   rU   strrV   r	   rb   r   )rB   r   rC   r   rb   rc   )
rB   r   rC   r   r^   r   r_   zSequence[float] | Nonerb   rc   )__name__
__module____qualname____doc__r/   r=   rG   rM   rR   rT   rX   r]   __classcell__)r8   s   @r9   r   r      s   #P  "&**. #"LP.2"#   +J
 J
 $	J

 (J
 J
 J
 J
 JJ
 ,J
  J
 KJ
 !J
( \)J
, 
-J
X#.	$

W
W 
W 2	
W
 

W

 
 	

 -
 

 8	F	F 	F 		F
 '	F 
	Fr:   r   )&
__future__r   collections.abcr   r   typingr   r   numpynpoptuna._experimentalr   optuna.distributionsr	   optuna.samplers._gar
   "optuna.samplers._lazy_random_stater   =optuna.samplers._nsgaiii._elite_population_selection_strategyr   optuna.samplers._randomr   ,optuna.samplers.nsgaii._after_trial_strategyr   1optuna.samplers.nsgaii._child_generation_strategyr   (optuna.samplers.nsgaii._crossovers._baser   +optuna.samplers.nsgaii._crossovers._uniformr   optuna.search_spacer   optuna.trialr   r   optuna.studyr   r    r:   r9   <module>r~      sp    " $ $     3 1 - > 2 Q [ B H 7 $ # " GsF] sF sFr:   