
    (^i;6                       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
 erd dlmZ d dlZd dlmZ d dlmZ nd dlmZ  ed	      Z ed
      Z ed      Z e
e      Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddd	 	 	 	 	 	 	 	 	 ddZdddddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)     )annotationsN)TYPE_CHECKING)*single_blas_thread_if_scipy_v1_15_or_newer)
get_logger)batched_lbfgsb)BaseAcquisitionFunc)_LazyImportzscipy.optimizetorchzoptuna._gp.batched_lbfgsbc                    |j                   dk(  sJ t              dk(  r'||t        j                  t        |      t              fS 	 	 	 	 	 	 d	 fd}t               5  t        j                  ||ddf   z  |j                         D cg c]  }| c}fD cg c]	  }dd|z  f c}t        j                  |      d      \  }	}
}ddd       |j                         }	z  |ddf<   
 }||kD  dkD  z  }t        j                  |dddf   ||      t        j                  |||      |fS c c}w c c}w # 1 sw Y   sxY w)
a'  
    This function optimizes the acquisition function using preconditioning.
    Preconditioning equalizes the variances caused by each parameter and
    speeds up the convergence.

    In Optuna, acquisition functions use Matern 5/2 kernel, which is a function of `x / l`
    where `x` is `normalized_params` and `l` is the corresponding lengthscales.
    Then acquisition functions are a function of `x / l`, i.e. `f(x / l)`.
    As `l` has different values for each param, it makes the function ill-conditioned.
    By transforming `x / l` to `zl / l = z`, the function becomes `f(z)` and has
    equal variances w.r.t. `z`.
    So optimization w.r.t. `z` instead of `x` is the preconditioning here and
    speeds up the convergence.
    As the domain of `x` is [0, 1], that of `z` becomes [0, 1/l].
       r   dtypec                   t        j                  |      }| j                  dk(  r|j                  dk(  sJ | 	z  |d d f<   t        j                  |      j                  d      }j                  |       }|j                         j                          |j                  j                         j                         }t        j                  |j                         j                               }||d d f   	z  fS )Nr   T)nparrayndimr
   
from_numpyrequires_grad_	eval_acqfsumbackwardgraddetachnumpy
atleast_1d)
scaled_xfixed_paramsnext_paramsx_tensor	neg_fvalsgrads
neg_fvals_acqfcontinuous_indiceslengthscaless
          W/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/_gp/optim_mixed.pynegative_acqf_with_gradz9_gradient_ascent_batched.<locals>.negative_acqf_with_grad8   s     hh|,}}!k&6&6!&;;;-5-DA))*##K0??E^^H--	  "$$&,,.]]9#3#3#5#;#;#=>
 5$6!67,FFF    N      )func_and_grad
x0_batchedbatched_argsboundspgtol	max_iters)r   
np.ndarrayr   zlist[np.ndarray]returntuple[np.ndarray, np.ndarray])r   lenr   zerosboolr   r   copymathsqrtwhere)r#   initial_params_batchedinitial_fvalsr$   r%   tolr'   paramsscaled_cont_xs_optneg_fvals_optn_iterationsxs_opt	fvals_optis_updated_batchs   `  ``          r&   _gradient_ascent_batchedrF      sv   . "&&!+++
!#%}bhhs=?QY]6^^^GG,<G	&G" 
4	5 
:H:W:W1-a1C.CD|S.D.I.I.KLU5LN(451QAJ5))C.;
7M<
 $((*F$6$EF1  ! I!M1lQ6FG 	!!T'*F4JK
!9m<  M5
 
s*   ".E	D7E D<.!E7
EE
c                $   t        |      dk(  r||dfS ||||   k7     }t        j                  |d d d f   t        |      d      }||d d |f<   | j                  |      }t        j                  |      }||   |kD  r||d d f   ||   dfS ||dfS )Nr)   Fr   )axisT)r4   r   repeateval_acqf_no_gradargmax)	r#   initial_paramsinitial_fval	param_idxchoiceschoices_except_current
all_paramsfvalsbest_idxs	            r&   _exhaustive_searchrT   a   s     7|q|U22$Wy0I%IJ>$'2C8N4OVWXJ5Jq)|"":.EyyHX%(A+&h==<..r(   c                    t              dk(  r||dfS dfd} ||         }t        j                  |   |         sJ || i|j                         d fddfd}d}	t	        j
                  |d   |	z
  |   d   |	z   fd	|
      }
 ||
j                        } |       }||k7  r||kD  r|   <   |dfS ||dfS )Nr)   Fc           	         t        t        j                  t        j                  |       dt	              dz
              }t        | |dz
     z
        t        | |   z
        k  r|dz
  S |S )Nr)   )intr   clipsearchsortedr4   abs)xigridss     r&   find_nearest_indexz1_discrete_line_search.<locals>.find_nearest_index   sb    q11c%j1nEFAa!e,-AaL0AAq1uHqHr(   c                    j                  |       }||S |    <   t        j                               }|| <   |S )N)getfloatrJ   )r\   	cache_valnegvalr#   r]   negative_fval_cachenormalized_paramsrN   s      r&   negative_acqf_with_cachez7_discrete_line_search.<locals>.negative_acqf_with_cache   sY    '++A.	 ',Qx)$ ../@ABB!'Ar(   c           	     6   | d   k  s| d   kD  rt         j                  S t        t        j                  t        j                  |       dt              dz
              }|dz
  } |       |      }}|   | z
  |   |   z
  z  }d|z
  }||z  ||z  z   S )Nr   r)   g      ?)r   infrW   rX   rY   r4   )	r[   rightleftneg_acqf_leftneg_acqf_rightw_leftw_rightr]   rf   s	          r&   interpolated_negative_acqfz9_discrete_line_search.<locals>.interpolated_negative_acqf   s    uQx<1uRy=66MBGGBOOE15q#e*q.IJqy(@(FH`I
~ ,"uU|eDk'AB,%.(@@@r(   g-q=r   rh   brent)bracketmethodr=   T)r[   ra   r2   rW   )r\   rW   r2   ra   )r[   ra   r2   ra   )r4   r   iscloser7   sominimize_scalarr[   )r#   rL   rM   rN   r]   xtolr^   current_choice_irp   EPSresopt_idxfval_optrf   rd   re   s   `  ``        @@@r&   _discrete_line_searchr}   y   s    5zQ|U22I *.*CD::nY/7G1HIII+l];&++-
 

A C


" qC'7!8%)c/JC !'G(11H ""x,'>',W~)$ (D00<..r(   c                    d}| j                   j                  |   }|st        |      |k  rt        | ||||      S t	        | |||||      S )N   )search_spaceis_categoricalr4   rT   r}   )r#   rL   rM   rN   rO   rw    MAX_INT_EXHAUSTIVE_SEARCH_PARAMSr   s           r&   _local_search_discreter      sZ     (*$&&55i@NW)II!$iQXYY$T><T[]abbr(   c           	        |j                         }|j                         }t        j                  t        |      t              }t        |      D ]+  \  }	}
t        | |
||	   |||      \  }}}|||	<   |||	<   |||	<   - |||fS )Nr   )r7   r   r5   r4   r6   	enumerater   )r#   r;   r<   rN   rO   rw   best_normalized_params_batched
best_fvalsrE   batchre   best_normalized_params	best_fvalupdateds                 r&   _local_search_discrete_batchedr      s     &<%@%@%B"##%JxxM 2$?$-.D$E *  5K#Z%6	7D6
2	7 1G&u-%
5")* *:7GGGr(   g-C6?d   )r=   max_iterc          
        | j                   | j                  j                  x}   }| j                  j                  }| j                  j	                         }|D cg c]=  }t        j                  t        j                  |      t
        j                        dz  ? }	}| j                  |j                         x}
      }d}t        j                  t        |
      |t              }t        j                  t        |
            }t        |      D ]  }t!        | |
|   ||   |||      \  |
|<   ||<   }t        j"                  |||      }t%        |||	      D ]e  \  }}}|||k(  x}    }||    }|j&                  dk(  r|
|fc c S t)        | |
|   ||   |||      \  |
|<   ||<   }t        j"                  |||      }g |||k(  x}    }||    }|j&                  dk(  s|
|fc S  t*        j-                  d       |
|fS c c}w )N)initial   rh   r   r   z2local_search_mixed: Local search did not converge.)length_scalesr   r$   discrete_indicesget_choices_of_discrete_paramsr   mindiffri   rJ   r7   fullr4   rW   arangerangerF   r:   zipsizer   _loggerwarning)r#   xs0r=   r   	cont_indsr%   r   choices_of_discrete_paramsrO   discrete_xtolsbest_xsr   
CONTINUOUSlast_changed_dimsremaining_inds_r   r\   rw   is_convergeds                       r&   local_search_mixed_batchedr      sE    %%D4E4E4X4X'XyZL((99!%!2!2!Q!Q!S
 2	  	rwww014N  ''CHHJ)>@JJGjDYYs7|,N8_ NG_'.):n+EyR^`cH
DN!;W HHWj:KL #$46PR` a 
	HAw 1DUYZDZ4ZL2[ \+\M:N""a'
**.'.1:n3MqRY[_ IGN#Z%?
 !#!5F G
	H (:Kz:Y*Y,(Z[-|m<!#J&&+N. 	LMJEs   AG?i   
   )!warmstart_normalized_params_arrayn_preliminary_samplesn_local_searchr=   rngc                  |xs t         j                  j                         }|+t        j                  d| j                  j
                  f      }t        |      |dz
  k  sJ d       | j                  j                  ||      }| j                  |      }t        |t         j                        sJ t        j                  |      }t        j                  |||   z
        }	d|	|<   |	|	j                         z  }	t        t        j                  |	dkD              }
t!        |t        |      z
  dz
  |
      }||
k(  rt"        j%                  d       t        j&                  |g      }|dkD  r4|j)                  t        |      |d|	      }t        j*                  ||      }t        j,                  ||d d f   |g      }t/        | ||	      \  }}t        j                  |      j1                         }||   ||   fS )
Nr   r)   zPWe must choose at least 1 best sampled point + given_initial_xs as start points.)r   g        zBStudy already converged, so the number of local search is reduced.F)r   replacep)r=   )r   randomRandomStateemptyr   dimr4   sample_normalized_paramsrJ   
isinstancendarrayrK   expr   rW   count_nonzeror   r   r   r   choiceappendvstackr   item)r#   r   r   r   r=   r   
sampled_xsf_valsmax_iprobsn_non_zero_probs_improvementn_additional_warmstartchosen_idxsadditional_idxsx_warmstartsr   r   rS   s                     r&   optimize_acqf_mixedr     s    
(&&(C(0,.HHa9J9J9N9N5O,P) 	-..12DDZYZD "";;<QWZ;[J ##J/Ffbjj)))IIfE
 FF6F5M)*EE%L	UYY[E#&r'7'7'D#E  >??!CEa !==\]((E7#K!**
O"8%5 % 
 ii_=99ja8:[\]L4T<SQGZyy$))+H8j222r(   )r#   r   r;   r1   r<   r1   r$   r1   r%   r1   r=   ra   r2   )tuple[np.ndarray, np.ndarray, np.ndarray])r#   r   rL   r1   rM   ra   rN   rW   rO   r1   r2   tuple[np.ndarray, float, bool])r#   r   rL   r1   rM   ra   rN   rW   r]   r1   rw   ra   r2   r   )r#   r   rL   r1   rM   ra   rN   rW   rO   r1   rw   ra   r2   r   )r#   r   r;   r1   r<   r1   rN   rW   rO   r1   rw   ra   r2   r   )
r#   r   r   r1   r=   ra   r   rW   r2   r3   )r#   r   r   znp.ndarray | Noner   rW   r   rW   r=   ra   r   znp.random.RandomState | Noner2   ztuple[np.ndarray, float])
__future__r   r8   typingr   r   r   "optuna._gp.scipy_blas_thread_patchr   optuna.loggingr   scipy.optimizeoptimizeru   r
   
optuna._gpr   optuna._gp.acqfr   optunar	   __name__r   rF   rT   r}   r   r   r   r    r(   r&   <module>r      se   "     Y % )3"	%	&B E !<=N X
A
A&A A #	A
 A 
A /AH/
// / 	/
 / $/0@/
@/@/ @/ 	@/
 @/ @/ $@/Fc
cc c 	c
 c c $c(H
H&H H 	H
 H H /H2 AEVY-
-$.-8=-PS-"-f <@!%(,13
13 (913 	13
 13 
13 
&13 13r(   