L iydZddlZddlmZddlmZmZddlmZddl m Z m Z ddl m Z dgZGd d ZGd d ZGd dZGddZGddZe dd ddZy)z0 A Dual Annealing global optimization algorithm N)OptimizeResult)minimizeBounds)gammaln)check_random_state_transition_to_rng)new_bounds_to_olddual_annealingc*eZdZdZdZdZdZdZdZy)VisitingDistributiona@ Class used to generate new coordinates based on the distorted Cauchy-Lorentz distribution. Depending on the steps within the strategy chain, the class implements the strategy for generating new location changes. Parameters ---------- lb : array_like A 1-D NumPy ndarray containing lower bounds of the generated components. Neither NaN or inf are allowed. ub : array_like A 1-D NumPy ndarray containing upper bounds for the generated components. Neither NaN or inf are allowed. visiting_param : float Parameter for visiting distribution. Default value is 2.62. Higher values give the visiting distribution a heavier tail, this makes the algorithm jump to a more distant region. The value range is (1, 3]. Its value is fixed for the life of the object. rng_gen : {`~numpy.random.Generator`} A `~numpy.random.Generator` object for generating new locations. (can be a `~numpy.random.RandomState` object until SPEC007 transition is fully complete). gחA绽|=c||_||_||_||_||z |_t j d|jz t j|jdz z|_t j d|jz t jdz|jdz z |_ t jt j|jz|jd|jz zz |_ d|jdz z dz |_ d|jz |_t jd|jz zt jt jd|jz zz t j t!|jz |_y)Ng@?@@g?)_visiting_paramrng_genlowerupper bound_rangenpexplog_factor2_factor3sqrtpi _factor4_p_factor5_d1sinr_factor6)selflbubvisiting_paramrs d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_dual_annealing.py__init__zVisitingDistribution.__init__2sh .   7d&:&: :bff  3 &?( () d&:&: :bffSkI"&"6"6"< >? ''"%%.4==8DMM $&& &=()t33c9:S@ &t}}!45 EES4==( )9++-/VVGDHH4E-FG cF|j}||kr|j||}|jjd\}}|j|z|||jkD<|j |z|||j k<||z}||j z } t j| |j|jz} t j| |j|j z}|t j||j z |jkxxdz cc<|St j|}|j|dd} | |jkDr(|j|jjz} n8| |j kr(|j |jjz} ||z } | || z|| <|| |j | z } t j| |j| |j| z} t j| |j| |j | z|| <t j|| |j | z |jkr|| xx|jz cc<|S)z Based on the step in the strategy chain, new coordinates are generated by changing all components is the same time or only one of them, the new values are computed with visit_fn method sizer r) r-visit_fnruniform TAIL_LIMITrrfmodrfabsMIN_VISIT_BOUNDcopy) r#xstep temperaturedimvisits upper_sample lower_samplex_visitabvisitindexs r'visitingzVisitingDistribution.visitingJsm ff #:]];4F)-)=)=1)=)E &L,/3/MF6DOO+ ,150@<0OF6T__,, -qjG$**$A4++,t/?/??Agga!1!12TZZ?G BGG$**$&(,(<(<= >AG H >(ggajGMM+q1!4Et&$,,*>*>*@@$//))(4<<+?+?+AA3JE"QuX-GENE!22A4++E23d6F6Fu6MMAWWQ(8(8)**U+,GENwwwu~ )"223$"6"66r)cX|jj|dfj\}}tjtj ||j dz z }|j|z}|tj|j dz tj |j|z zd|j z z z}tj|j dz tj tj|zd|j z z }||z S)z- Formula Visita from p. 405 of reference [2] r+r,rr) rnormalTrrrrrr"r3)r#r8r9r6yfactor1factor4dens r'r/zVisitingDistribution.visit_fnps||""a"1331&& ,0D0Ds0JKL//G+ RVVd**S01BFF MMG #5%%(+d.B.B(BDE Effd**S0BFF2771:4FFD000233wr)N) __name__ __module__ __qualname____doc__r1r4r(rBr/r)r'r r s$4JOG0$Lr)r c0eZdZdZdZddZddZdZdZy) EnergyStatea! Class used to record the energy state. At any time, it knows what is the currently used coordinates and the most recent best location. Parameters ---------- lower : array_like A 1-D NumPy ndarray containing lower bounds for generating an initial random components in the `reset` method. upper : array_like A 1-D NumPy ndarray containing upper bounds for generating an initial random components in the `reset` method components. Neither NaN or inf are allowed. callback : callable, ``callback(x, f, context)``, optional A callback function which will be called for all minima found. ``x`` and ``f`` are the coordinates and function value of the latest minimum found, and `context` has value in [0, 1, 2] Ncfd|_d|_d|_d|_||_||_||_yN)ebestcurrent_energycurrent_locationxbestrrcallback)r#rrrXs r'r(zEnergyState.__init__s5 " $     r)c|A|j|j|jt|j|_nt j ||_d}d}|r|j|j|_|j tdt j|jsi|tjk\rd}d}t||j|j|j|jj|_|dz }nd}|jA|j5|j|_t j |j|_|ryy) z Initialize current location is the search domain. If `x0` is not provided, a random location within the bounds is generated. Nr,Trz$Objective function is returning NoneFznStopping algorithm because function create NaN or (+/-) infinity values even with trying new random parametersr.)r0rrlenrVrr5funrU ValueErrorisfiniterPMAX_REINIT_COUNTr-rTrW)r# func_wrapperrx0 init_errorreinit_countermessages r'resetzEnergyState.resets> :$+OODJJ 9C&&)T***,-C 8    , ,Q 8 1 1 B BCDI  D$=$= =Av**99DIIE --<< GGD$5$5$F$FG F >r)c|t|dzz |_|xjdz c_t|jj j dzD],}|dk(r|dk(rd|_nd|_|jj|jj ||}|jj|}||jjkri|jj||||jjkrG|jj||d}||r|cSd|_d|_n|j!||||jj"|jj$k\s-yy)Nr.r+rTFz8Maximum number of function call reached during annealing)floatrzrwrangersrVr-energy_state_improvedrurBr_r[rUrkrTrirnfevmaxfun)r#r7r8rr=rfrhs r'runzStrategyChain.runs_ +eD1Ho = "t((99>>BC ,AAv1915D.16D.oo..!!22A{DG!!%%g.A4$$333!!00G<t((...++777AFC#&J15D.,-D)""1a1  %%):):)A)AA+1 ,r)c|jr|jj|jj|jj \}}||jj krFd|_|jj||d}||r|S|jj|||jj|jjk\ryd}|jdt|jjzkrytj |j|jj |jj"z z|j$z }||j&j)k\rd}|j |j*k\rd}|r|jj|j,|j.\}}tj0||_||_d|_|jjj2|_||jj krS|jj|j.|j,d}||r|S|jj|||jj|jjk\ryyy) Nrr.z;Maximum number of function call reached during local searchFZTr+z=Maximum number of function call reached during dual annealing)rrv local_searchrsrWrTrwrirkr_rrr{rZrVrrrUrzryr0rxrrrpr5r-)r#rfr6rhdo_lsplss r'rzStrategyChain.local_search=sV  % %))66t7H7H7N7N7;7H7H7N7NPDAq4$$***()%''33Aq!<?" !!00A6  %%):):)A)AA. 66BT..??@@ @&&!!''$*;*;*J*JJL%%&'Cdnn,,..  D$=$= =E ))66tyy$))LDAq DIDI$%D !(,(9(9(J(J(O(OD %4$$***''33IItyy!-?" !!00A6  %%):):)A)AA0B r)N)rJrKrLrMr(rrrrNr)r'rmrms>:*H*,>.1r)rmceZdZddZdZy)ObjectiveFunWrappercX||_||_d|_d|_d|_||_y)Nr)funcargsrngevnhevr)r#rrrs r'r(zObjectiveFunWrapper.__init__ps,      r)cf|xjdz c_|j|g|jS)Nr.)rrr)r#r6s r'r[zObjectiveFunWrapper.fun{s) Q tyy'TYY''r)N)cA)rJrKrLr(r[rNr)r'rrns  (r)rc(eZdZdZdZdZdZdZdZy)LocalSearchWrapperz Class used to wrap around the minimizer used for local search Default local minimizer is SciPy minimizer L-BFGS-B rorQc*|_|_jjdd_jjdd_jjdd_jj ddt_tt|}tj|d_ tj|d_jstj}t!t#|j$zj&j(}djd<d |ijd <ttjjjd <yt+jrfd }|jd<t+jrfd } | jd<t+j rfd} | jd<yy)Njachesshessprrr.zL-BFGS-Bmethodmaxiteroptionsboundsc*j|gSrS)rr6rr#s r' wrapped_jacz0LocalSearchWrapper.__init__..wrapped_jacs#488A---r)c*j|gSrS)rrs r' wrapped_hessz1LocalSearchWrapper.__init__..wrapped_hesss$499Q...r)c,j||gSrS)r)r6prr#s r' wrapped_hesspz2LocalSearchWrapper.__init__..wrapped_hessps%4::a2T22r))r_kwargsgetrrrpopr minimizerlistziprrqrrrZminmaxLS_MAXITER_RATIOLS_MAXITER_MINLS_MAXITER_MAXcallable) r# search_boundsr_rr bounds_listn ls_max_iterrrrs ` ` r'r(zLocalSearchWrapper.__init__s( ;;??5$/KKOOFD1 [[__Wd3  %!3 ./ XXk!n- XXk!n- {{DJJAc!d&;&;";"&"5"57"113K%/DKK !;&DKK "%)TZZ)D$EDKK !!.%0 E" "/&2 F# #3'4 G$$r)ctj|}|j|jj|fi|j }d|vr)|jxj |jz c_d|vr)|jxj|jz c_tjtj|jxrtj|j}tj|j|jk\xr,tj|j|jk}|xr|}|r'|j|kr|j|jfS||fS)Nnjevr)rr5rr_r[rrrrallr]r6rr)r#r6rfx_tmpmres is_finite in_boundsis_valids r'rzLocalSearchWrapper.local_searchs t~~d//33QF$++F T>    " "dii / " T>    " "dii / "FF2;;tvv./IBKK4I FF466TZZ/0"RVV FFdjj 6" * 1 88TVV# #e8Or)N) rJrKrLrMrrrr(rrNr)r'rrs$ NN#5Jr)rseed ) position_numct|tr4t|j|jt |j}| "t | t |k(s t dtt|}tj|d}tj|d}|dks|dk\r t dtjtj|sxtjtj|sPtjtj|s(tjtj|r t dtj||ks t dt |t |k(s t d t|| g|}|xsi}t!||g|i|}t#| }t%||| }|j'||| ||z}t)||||}t+||||||}d }d}g}t-}d |_d|_tj2|dz tj4d zdz }|st7|D]}t9|d z}tj2|dz tj4|zdz }||z|z } ||k\r|j;d d }n| |kr|j'||ng|j=|| }!|!|j;|!d }d |_n7| s.|j?}!|!|j;|!d }d |_n|dz }|s|j@|_!|jD|_#||_$|jJ|_%|jL|_'|jP|_(||_)|S)a Find the global minimum of a function using Dual Annealing. Parameters ---------- func : callable The objective function to be minimized. Must be in the form ``f(x, *args)``, where ``x`` is the argument in the form of a 1-D array and ``args`` is a tuple of any additional fixed parameters needed to completely specify the function. bounds : sequence or `Bounds` Bounds for variables. There are two ways to specify the bounds: 1. Instance of `Bounds` class. 2. Sequence of ``(min, max)`` pairs for each element in `x`. args : tuple, optional Any additional fixed parameters needed to completely specify the objective function. maxiter : int, optional The maximum number of global search iterations. Default value is 1000. minimizer_kwargs : dict, optional Keyword arguments to be passed to the local minimizer (`minimize`). An important option could be ``method`` for the minimizer method to use. If no keyword arguments are provided, the local minimizer defaults to 'L-BFGS-B' and uses the already supplied bounds. If `minimizer_kwargs` is specified, then the dict must contain all parameters required to control the local minimization. `args` is ignored in this dict, as it is passed automatically. `bounds` is not automatically passed on to the local minimizer as the method may not support them. initial_temp : float, optional The initial temperature, use higher values to facilitates a wider search of the energy landscape, allowing dual_annealing to escape local minima that it is trapped in. Default value is 5230. Range is (0.01, 5.e4]. restart_temp_ratio : float, optional During the annealing process, temperature is decreasing, when it reaches ``initial_temp * restart_temp_ratio``, the reannealing process is triggered. Default value of the ratio is 2e-5. Range is (0, 1). visit : float, optional Parameter for visiting distribution. Default value is 2.62. Higher values give the visiting distribution a heavier tail, this makes the algorithm jump to a more distant region. The value range is (1, 3]. accept : float, optional Parameter for acceptance distribution. It is used to control the probability of acceptance. The lower the acceptance parameter, the smaller the probability of acceptance. Default value is -5.0 with a range (-1e4, -5]. maxfun : int, optional Soft limit for the number of objective function calls. If the algorithm is in the middle of a local search, this number will be exceeded, the algorithm will stop just after the local search is done. Default value is 1e7. rng : `numpy.random.Generator`, optional Pseudorandom number generator state. When `rng` is None, a new `numpy.random.Generator` is created using entropy from the operating system. Types other than `numpy.random.Generator` are passed to `numpy.random.default_rng` to instantiate a `Generator`. Specify `rng` for repeatable minimizations. The random numbers generated only affect the visiting distribution function and new coordinates generation. no_local_search : bool, optional If `no_local_search` is set to True, a traditional Generalized Simulated Annealing will be performed with no local search strategy applied. callback : callable, optional A callback function with signature ``callback(x, f, context)``, which will be called for all minima found. ``x`` and ``f`` are the coordinates and function value of the latest minimum found, and ``context`` has one of the following values: - ``0``: minimum detected in the annealing process. - ``1``: detection occurred in the local search process. - ``2``: detection done in the dual annealing process. If the callback implementation returns True, the algorithm will stop. x0 : ndarray, shape(n,), optional Coordinates of a single N-D starting point. Returns ------- res : OptimizeResult The optimization result represented as a `OptimizeResult` object. Important attributes are: ``x`` the solution array, ``fun`` the value of the function at the solution, and ``message`` which describes the cause of the termination. See `OptimizeResult` for a description of other attributes. Notes ----- This function implements the Dual Annealing optimization. This stochastic approach derived from [3]_ combines the generalization of CSA (Classical Simulated Annealing) and FSA (Fast Simulated Annealing) [1]_ [2]_ coupled to a strategy for applying a local search on accepted locations [4]_. An alternative implementation of this same algorithm is described in [5]_ and benchmarks are presented in [6]_. This approach introduces an advanced method to refine the solution found by the generalized annealing process. This algorithm uses a distorted Cauchy-Lorentz visiting distribution, with its shape controlled by the parameter :math:`q_{v}` .. math:: g_{q_{v}}(\Delta x(t)) \propto \frac{ \ \left[T_{q_{v}}(t) \right]^{-\frac{D}{3-q_{v}}}}{ \ \left[{1+(q_{v}-1)\frac{(\Delta x(t))^{2}} { \ \left[T_{q_{v}}(t)\right]^{\frac{2}{3-q_{v}}}}}\right]^{ \ \frac{1}{q_{v}-1}+\frac{D-1}{2}}} Where :math:`t` is the artificial time. This visiting distribution is used to generate a trial jump distance :math:`\Delta x(t)` of variable :math:`x(t)` under artificial temperature :math:`T_{q_{v}}(t)`. From the starting point, after calling the visiting distribution function, the acceptance probability is computed as follows: .. math:: p_{q_{a}} = \min{\{1,\left[1-(1-q_{a}) \beta \Delta E \right]^{ \ \frac{1}{1-q_{a}}}\}} Where :math:`q_{a}` is a acceptance parameter. For :math:`q_{a}<1`, zero acceptance probability is assigned to the cases where .. math:: [1-(1-q_{a}) \beta \Delta E] < 0 The artificial temperature :math:`T_{q_{v}}(t)` is decreased according to .. math:: T_{q_{v}}(t) = T_{q_{v}}(1) \frac{2^{q_{v}-1}-1}{\left( \ 1 + t\right)^{q_{v}-1}-1} Where :math:`q_{v}` is the visiting parameter. .. versionadded:: 1.2.0 References ---------- .. [1] Tsallis C. Possible generalization of Boltzmann-Gibbs statistics. Journal of Statistical Physics, 52, 479-487 (1988). .. [2] Tsallis C, Stariolo DA. Generalized Simulated Annealing. Physica A, 233, 395-406 (1996). .. [3] Xiang Y, Sun DY, Fan W, Gong XG. Generalized Simulated Annealing Algorithm and Its Application to the Thomson Model. Physics Letters A, 233, 216-220 (1997). .. [4] Xiang Y, Gong XG. Efficiency of Generalized Simulated Annealing. Physical Review E, 62, 4473 (2000). .. [5] Xiang Y, Gubian S, Suomela B, Hoeng J. Generalized Simulated Annealing for Efficient Global Optimization: the GenSA Package for R. The R Journal, Volume 5/1 (2013). .. [6] Mullen, K. Continuous Global Optimization in R. Journal of Statistical Software, 60(6), 1 - 45, (2014). :doi:`10.18637/jss.v060.i06` Examples -------- The following example is a 10-D problem, with many local minima. The function involved is called Rastrigin (https://en.wikipedia.org/wiki/Rastrigin_function) >>> import numpy as np >>> from scipy.optimize import dual_annealing >>> func = lambda x: np.sum(x*x - 10*np.cos(2*np.pi*x)) + 10*np.size(x) >>> lw = [-5.12] * 10 >>> up = [5.12] * 10 >>> ret = dual_annealing(func, bounds=list(zip(lw, up))) >>> ret.x array([-4.26437714e-09, -3.91699361e-09, -1.86149218e-09, -3.97165720e-09, -6.29151648e-09, -6.53145322e-09, -3.93616815e-09, -6.55623025e-09, -6.05775280e-09, -5.00668935e-09]) # random >>> ret.fun 0.000000 zBounds size does not match x0rr.r~rz3Restart temperature ratio has to be in range (0, 1)z/Some bounds values are inf values or nan valuesz#Bounds are not consistent min < maxz&Bounds do not have the same dimensionsFTrz#Maximum number of iteration reached)* isinstancerr r$r%rZr\rrrrqanyisinfisnanrrrrrPrdr rmrsuccessstatusrrrrappendrrrWr6rTr[nitrrrrrc)"rrrrminimizer_kwargs initial_temprestart_temp_ratior@acceptrrngno_local_searchrXr`lurrr_rvrrstemperature_restartrustrategy_chain need_to_stop iterationrc optimize_rest1ist2r8rhs" r'r r s{t&&!"699fiiVYYH ~c"gV4899 c6l B HHRUOE HHRUOER#5#;NOO rxx266"((5/#:bff HHUO? "rxx 7JKK 66%%- >?? u:U #ABB'tV;d;L(-2* 9#9'79!%GueX6L|Wb1');;%eUE7CJ"6:|#4g|MNLIG!#LLL bffSk) *S 0Bw Aa3AbffQi/036B&+b0KG#DE# 00""<9 $$Q 4Cs## ', $"$113?NN3'#'L+0L( NI9 @"''LN#))L L$))L$))L$))L"L r)) rNrQNgn@gh㈵>g(\@grNFNN)rMnumpyrscipy.optimizerrr scipy.specialrscipy._lib._utilrrscipy.optimize._constraintsr __all__r rPrmrrr rNr)r'rs )+!C9  jjZP+P+fW1W1t(($AAHF,267<@D9>%) W-Wr)