L idZddlmZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z mZddlmZddlmZddlmZdd lmZdd lmZd gZ dd d dZGddZGddZGddZy)z`). Alternatively supply a map-like callable, such as `multiprocessing.Pool.map` for parallel evaluation. This evaluation is carried out as ``workers(func, iterable)``. Requires that `func` be pickleable. .. versionadded:: 1.11.0 Returns ------- res : OptimizeResult The optimization result represented as a `OptimizeResult` object. Important attributes are: ``x`` the solution array corresponding to the global minimum, ``fun`` the function output at the global solution, ``xl`` an ordered list of local minima solutions, ``funl`` the function output at the corresponding local solutions, ``success`` a Boolean flag indicating if the optimizer exited successfully, ``message`` which describes the cause of the termination, ``nfev`` the total number of objective function evaluations including the sampling calls, ``nlfev`` the total number of objective function evaluations culminating from all local search optimizations, ``nit`` number of iterations performed by the global routine. Notes ----- Global optimization using simplicial homology global optimization [1]_. Appropriate for solving general purpose NLP and blackbox optimization problems to global optimality (low-dimensional problems). In general, the optimization problems are of the form:: minimize f(x) subject to g_i(x) >= 0, i = 1,...,m h_j(x) = 0, j = 1,...,p where x is a vector of one or more variables. ``f(x)`` is the objective function ``R^n -> R``, ``g_i(x)`` are the inequality constraints, and ``h_j(x)`` are the equality constraints. Optionally, the lower and upper bounds for each element in x can also be specified using the `bounds` argument. While most of the theoretical advantages of SHGO are only proven for when ``f(x)`` is a Lipschitz smooth function, the algorithm is also proven to converge to the global optimum for the more general case where ``f(x)`` is non-continuous, non-convex and non-smooth, if the default sampling method is used [1]_. The local search method may be specified using the ``minimizer_kwargs`` parameter which is passed on to ``scipy.optimize.minimize``. By default, the ``SLSQP`` method is used. In general, it is recommended to use the ``SLSQP``, ``COBYLA``, or ``COBYQA`` local minimization if inequality constraints are defined for the problem since the other methods do not use constraints. The ``halton`` and ``sobol`` method points are generated using `scipy.stats.qmc`. Any other QMC method could be used. References ---------- .. [1] Endres, SC, Sandrock, C, Focke, WW (2018) "A simplicial homology algorithm for lipschitz optimisation", Journal of Global Optimization. .. [2] Joe, SW and Kuo, FY (2008) "Constructing Sobol' sequences with better two-dimensional projections", SIAM J. Sci. Comput. 30, 2635-2654. .. [3] Hock, W and Schittkowski, K (1981) "Test examples for nonlinear programming codes", Lecture Notes in Economics and Mathematical Systems, 187. Springer-Verlag, New York. http://www.ai7.uni-bayreuth.de/test_problem_coll.pdf .. [4] Wales, DJ (2015) "Perspective: Insight into reaction coordinates and dynamics from the potential energy landscape", Journal of Chemical Physics, 142(13), 2015. .. [5] https://docs.scipy.org/doc/scipy/tutorial/optimize.html#constrained-minimization-of-multivariate-scalar-functions-minimize Examples -------- First consider the problem of minimizing the Rosenbrock function, `rosen`: >>> from scipy.optimize import rosen, shgo >>> bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)] >>> result = shgo(rosen, bounds) >>> result.x, result.fun (array([1., 1., 1., 1., 1.]), 2.920392374190081e-18) Note that bounds determine the dimensionality of the objective function and is therefore a required input, however you can specify empty bounds using ``None`` or objects like ``np.inf`` which will be converted to large float numbers. >>> bounds = [(None, None), ]*4 >>> result = shgo(rosen, bounds) >>> result.x array([0.99999851, 0.99999704, 0.99999411, 0.9999882 ]) Next, we consider the Eggholder function, a problem with several local minima and one global minimum. We will demonstrate the use of arguments and the capabilities of `shgo`. (https://en.wikipedia.org/wiki/Test_functions_for_optimization) >>> import numpy as np >>> def eggholder(x): ... return (-(x[1] + 47.0) ... * np.sin(np.sqrt(abs(x[0]/2.0 + (x[1] + 47.0)))) ... - x[0] * np.sin(np.sqrt(abs(x[0] - (x[1] + 47.0)))) ... ) ... >>> bounds = [(-512, 512), (-512, 512)] `shgo` has built-in low discrepancy sampling sequences. First, we will input 64 initial sampling points of the *Sobol'* sequence: >>> result = shgo(eggholder, bounds, n=64, sampling_method='sobol') >>> result.x, result.fun (array([512. , 404.23180824]), -959.6406627208397) `shgo` also has a return for any other local minima that was found, these can be called using: >>> result.xl array([[ 512. , 404.23180824], [ 283.0759062 , -487.12565635], [-294.66820039, -462.01964031], [-105.87688911, 423.15323845], [-242.97926 , 274.38030925], [-506.25823477, 6.3131022 ], [-408.71980731, -156.10116949], [ 150.23207937, 301.31376595], [ 91.00920901, -391.283763 ], [ 202.89662724, -269.38043241], [ 361.66623976, -106.96493868], [-219.40612786, -244.06020508]]) >>> result.funl array([-959.64066272, -718.16745962, -704.80659592, -565.99778097, -559.78685655, -557.36868733, -507.87385942, -493.9605115 , -426.48799655, -421.15571437, -419.31194957, -410.98477763]) These results are useful in applications where there are many global minima and the values of other global minima are desired or where the local minima can provide insight into the system (for example morphologies in physical chemistry [4]_). If we want to find a larger number of local minima, we can increase the number of sampling points or the number of iterations. We'll increase the number of sampling points to 64 and the number of iterations from the default of 1 to 3. Using ``simplicial`` this would have given us 64 x 3 = 192 initial sampling points. >>> result_2 = shgo(eggholder, ... bounds, n=64, iters=3, sampling_method='sobol') >>> len(result.xl), len(result_2.xl) (12, 23) Note the difference between, e.g., ``n=192, iters=1`` and ``n=64, iters=3``. In the first case the promising points contained in the minimiser pool are processed only once. In the latter case it is processed every 64 sampling points for a total of 3 times. To demonstrate solving problems with non-linear constraints consider the following example from Hock and Schittkowski problem 73 (cattle-feed) [3]_:: minimize: f = 24.55 * x_1 + 26.75 * x_2 + 39 * x_3 + 40.50 * x_4 subject to: 2.3 * x_1 + 5.6 * x_2 + 11.1 * x_3 + 1.3 * x_4 - 5 >= 0, 12 * x_1 + 11.9 * x_2 + 41.8 * x_3 + 52.1 * x_4 - 21 -1.645 * sqrt(0.28 * x_1**2 + 0.19 * x_2**2 + 20.5 * x_3**2 + 0.62 * x_4**2) >= 0, x_1 + x_2 + x_3 + x_4 - 1 == 0, 1 >= x_i >= 0 for all i The approximate answer given in [3]_ is:: f([0.6355216, -0.12e-11, 0.3127019, 0.05177655]) = 29.894378 >>> def f(x): # (cattle-feed) ... return 24.55*x[0] + 26.75*x[1] + 39*x[2] + 40.50*x[3] ... >>> def g1(x): ... return 2.3*x[0] + 5.6*x[1] + 11.1*x[2] + 1.3*x[3] - 5 # >=0 ... >>> def g2(x): ... return (12*x[0] + 11.9*x[1] +41.8*x[2] + 52.1*x[3] - 21 ... - 1.645 * np.sqrt(0.28*x[0]**2 + 0.19*x[1]**2 ... + 20.5*x[2]**2 + 0.62*x[3]**2) ... ) # >=0 ... >>> def h1(x): ... return x[0] + x[1] + x[2] + x[3] - 1 # == 0 ... >>> cons = ({'type': 'ineq', 'fun': g1}, ... {'type': 'ineq', 'fun': g2}, ... {'type': 'eq', 'fun': h1}) >>> bounds = [(0, 1.0),]*4 >>> res = shgo(f, bounds, n=150, constraints=cons) >>> res message: Optimization terminated successfully. success: True fun: 29.894378159142136 funl: [ 2.989e+01] x: [ 6.355e-01 1.137e-13 3.127e-01 5.178e-02] # may vary xl: [[ 6.355e-01 1.137e-13 3.127e-01 5.178e-02]] # may vary nit: 1 nfev: 142 # may vary nlfev: 35 # may vary nljev: 5 nlhev: 0 >>> g1(res.x), g2(res.x), h1(res.x) (-5.062616992290714e-14, -2.9594104944408173e-12, 0.0) ) args constraintsniterscallbackminimizer_kwargsoptionssampling_methodrNz/Successfully completed construction of complex.rTzCFailed to find a feasible minimizer point. Lowest sampling point = )mesz%Optimization terminated successfully.) isinstancerr lbublenSHGO iterate_all break_routinedisplogginginfoLMCxl_mapsfind_lowest_vertex fail_routinef_lowestresfunx_lowestxfnnfev n_sampledtnevmessagesuccess) funcboundsrrrrrrrrrshcs Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_shgo.pyr r sUN &&!"699fiiVYYH dF;!X 0  $'      88 LLJ K 377??q     88; ~G Hll LL vv }}     A 77NEs F!!F+ceZdZ d"dZdZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZd#dZdZdZdZdZd$dZd$dZdZd%dZd#dZdZdZ d Z!d&d!Z"y)'rNc `ddlm} gd} t| tr-| | vr)t dj dj | | .| jdddur|i}d|d<| jd |ddurFt|ds8t|_ jj}||d<j}n|_ t||_ |_|_|_t'j(|t*}t'j,|d_t'j0|}d||dddfdf<d ||ddd fd f<|dddf|ddd fkD}|j3r%t d dj d |Dd |_|_||_g_g_t=|t'j>j.t*d_j4D]G}|ddvs j8jA|d j:jA|dItCj8_tCj:_nd_d_dj ij$d_"|jDjG|nddijDd<jDdjIdvr|d|vr| j8j6jDd<| jK| nTd_&d_'d_(d_)d_*d_+d_&d_,d_-d_.d_/d_0gd_1idgdd gd!gd"dgd#dgd$gd%d&dd'gd(dd'gd)dd*gd+gd,d-gd.d/dd0gd1gd%d2gd%d3dd0gd4gd5}jDd}xjb||jIz c_1d6}|jDjb|jDdjbdgzd_2d_3|_4d_5|_6d_7d_8d_9d_:d_;d_<jl/jh#| d7k(rd8j.zd z_6d_7jhd _4jl| d7k(sd9x_6_6d_7jld9k(r| d7k(rd8j.zd z_6jP0jR$jTjX jLd_4t{j.j jdjZj4| :_>| d7k(rj~_@| _An| d;vst| tsމj_@| d;vr| d_A| jj.dd=_Gfd?} nd@_Aj_J| _Kd_Ld_Mg_Nt_Pt_Rdj_Sdj_Tdj_Udj_Vy#ttf$r |_ YwxYw#t$rj:jAdYwxYw)ANr)qmc)haltonsobol simplicialz4Unknown sampling_method specified. Valid methods: {}z, jacTgd~Qgd~QJrzError: lb > ub in bounds c32K|]}t|ywN)str).0bs r6 z SHGO.__init__..s)AQ#a&)As.oldtypeineqr*rSLSQP)methodr4rrftolg-q=rrJ)slsqpcobylacobyqa trust-constrrF)r*x0rrrrJ_custom)r=hesshesspr4rz nelder-meadpowellcgbfgsz newton-cgr=rRrSzl-bfgs-br4tncrMcatolrN)r4rfeasibility_tolrL)r=r4rdoglegrRz trust-ncgz trust-krylovz trust-exactrO)r=rRrSrcdt|}|t|z D]}|j|dy)z8Remove keys from dictionary if not in goodkeys - inplaceN)setpop) dictionarygoodkeys existingkeyskeys r6_restrict_to_keysz(SHGO.__init__.._restrict_to_keyss1z?L#c(m3 *sD) *r<d)dimdomainsfield sfield_argssymmetryrr)r:r;r;)dscrambleseedr:c:jj|Sr?) qmc_enginerandom)rrlselfs r6rz&SHGO.__init__..sampling_methods??11!44rdcustom)W scipy.statsr9rr@ ValueErrorformatjoingetr^callablerr3 derivative TypeErrorKeyErrorr r4rrnparrayfloatshapergisfiniteanyrmin_consg_consg_argsr emptyappendtuplerupdatelower init_options f_min_trueminimize_every_itermaxitermaxfevmaxevmaxtimeminhgrdrkinfty_cons_sampl local_iterr!min_solver_args stop_globalr r iters_donerncn_prcr/r-hgrqhull_incrementalr HCiterate_hypercubeiterate_complexriterate_delaunayintceillog2SobolrpHaltonsampling_customsamplingsampling_function stop_l_iterstop_complex_iterminimizer_pool LMapCacher$rr)r.nlfevnljevnlhev)rrr3r4rrrrrrrrrr9methodsr=aboundinfindbnderrcons solver_argsrJrcs` r6__init__z SHGO.__init__s $3 os +w0N34:F499W;M4NP P  7;;ud#;t#C'#% &* U # KK  !%(D0!"25"9:&t, ii***- 'yy  %T40     &%(88F#A&++f%%"'vad|Q"&vad|Q1q!t , ::<8 $ )A&)A AB!EF F '  "'DMDKDK 75) D  (( /!>?DF DG+2D(&)ii$((U56'0'8DO,4D(&)jj488d67'1'9DO5 (0$ 00DM%4D "!!&!;"# ]8$ DI b$/ **2./s%:A](4^(^^$^-,^-c|jdj|dD]?}||jdvs|jdj||j|<A|jdd|_|jdd|_|jdd|_|jdd|_tj|_ |jd d|_ d |vr"|d |_ |jd d |_ nd|_ |jd d|_ |jdd|_|jrdgt|j z|_nd|_|jdd|_|jdd|_|jdd|_y)z Initiates the options. Can also be useful to change parameters after class initiation. Parameters ---------- options : dict Returns ------- None rrWrTrNrrrf_minf_tolg-C6?rrkFrrinfty_constraintsr!)rrr^rxrrrrtimeinitrrrrrkrr4rrr!)rrropts r6rzSHGO.init_optionss" i(//8, ?Cd++I66)))488=%%c* ? $+;;/Dd#K {{9d3 kk(D1 [[$/ IIK {{9d3 g %g.DO Wd3DJ"DO{{9d3  J6 ==E#dkk"22DM DM"++lE: ' ,? FKK. rdc|Sr?rHrrs r6 __enter__zSHGO.__enter__3s rdc\|jjjj|Sr?)rV _mapwrapper__exit__)rrrs r6rz SHGO.__exit__6s#-twwyy$$--t44rdc|jrtjd|js:|jrn-|j |j |js:|js|js|j|j|j_ |jjj|_y)z Construct for `iters` iterations. If uniform sampling is used, every iteration adds 'n' sampling points. Iterations if a stopping criteria (e.g., sampling points or processing time) has been met. zSplitting first generationN)r!r"r#rr iteratestopping_criteriar find_minimarr)nitrrr.r-rs r6rzSHGO.iterate_all;s 99 LL5 6""!! LLN  " " $ ""''%%  " ''))..rdc|jrtjd|jt |j dk7rb|j |j|j|jj|_ |jj|_ n|j|jr#tjd|j yy)z Construct the minimizer pool, map the minimizers to local minima and sort the results into a global return object. zSearching for minimizer pool...rzMinimiser pool = SHGO.X_min = N)r!r"r# minimizersrX_min minimise_poolr sort_resultr)r*r(r,r+r&rs r6rzSHGO.find_minimaYs 99 LL: ;  tzz?a    t /    !HHLLDM HHJJDM  # # % 99 LL9$**F G rdcXtj|_|jjj D]}|jj|j |jks4|jr9tjd|jj|j |jj|j |_|jj|j|_ |jj D]e}|j|j|jks*|j|j|_|j|j|_ g|jtjk(rd|_d|_ yy)Nzself.HC.V[x].f = )r}infr(rrcachefr!r"r#x_ar+r$rx_l)rrr,lmcs r6r&zSHGO.find_lowest_vertexts$  1Awwyy|~~ -99LL#4TWWYYq\^^4D!EF $ !  $ ! 0 0  1 88>> 2Cxx}""T]]2 $ 3 3 $ 1 1  2 ==BFF " DM DM #rdc|td|j|jfD}|jr%t j d|j d||j |j |jk\rd|_|j |j |jk\rd|_|jS)Nc3&K|] }|| ywr?rH)rAr,s r6rCz)SHGO.finite_iterations..sHq!-HszIterations done =  / T)minrrr!r"r#rr)rrmis r6finite_iterationszSHGO.finite_iterationss HTZZ6H H 99 LL-doo->c"F G :: !4::.#' << #4<<0#' rdc|jr/tjd|jd|j|j|jk\rd|_|j S)NzFunction evaluations done = rT)r!r"r#r-rrrs r6 finite_fevzSHGO.finite_fevsO 99 LL7yDKK=Q R 77dkk !#D rdc|jr/tjd|jd|j|j|jk\rd|_yy)NzSampling evaluations done = rT)r!r"r#r/rrrs r6 finite_evzSHGO.finite_evsR 99 LL77GH"jj\+ , >>TZZ '#D  (rdc|jrDtjdtj|jz d|j tj|jz |j k\rd|_yy)NzTime elapsed = rT)r!r"r#rrrrrs r6 finite_timezSHGO.finite_timesg 99 LL?499;+B*CD"ll^- . IIK$)) # 4#D  5rdc|j|jrDtjd|jtjd|j |j |j S|j dk(r,|j|jkrd|_|j S|j|j z t|j z }|j|j krSd|_t|d|jzk\r1tjd|j d|jd ||jkrd|_|j S) z Stop the algorithm if the final function value is known Specify in options (with ``self.f_min_true = options['f_min']``) and the tolerance with ``f_tol = options['f_tol']`` zLowest function evaluation = zSpecified minimum = Trez&A much lower value than expected f* = z was found f_lowest = ) stacklevel) r&r!r"r#r(rrrabswarningswarn)rrpes r6finite_precisionzSHGO.finite_precisions2 ! 99 LL8H I LL//@A B == ## # ??c !}} *#' --$//1S5IIB}}/#' r7a$**n,MM@@QR004 @#$ TZZ#' rdc|jjdk(ry|jj|jz |_|jj|_|j|jkrd|_|j r0tjd|jd|jd|j S)zV Stop the algorithm if homology group rank did not grow in iteration. rNTzCurrent homology growth = z (minimum growth = )) r$sizerhgrdrrr!r"r#rs r6finite_homology_growthzSHGO.finite_homology_growths 88==A  HHMMDHH, 88== 99 $#D  99 LL5dii[A//3||nA? @rdc|j|j|j|j|j|j |j |j |j|j|j|j|j|j|jS)zt Various stopping criteria ran every iteration Returns ------- stop : bool )rrrrrrrrrrrrrrrs r6rzSHGO.stopping_criterias << #  " " $ :: !  " " $ ;; " OO  :: ! NN  << #     ?? &  ! ! # << #  ' ' )rdc|j|jr|js|j|xjdz c_yNr)rrr rrrs r6rz SHGO.iterates>   # #%%  " 1rdc|jrtjd|jD|jj |jj j|_nD|jj|j|xj|jz c_|jrtjdt|jjdkDrp|jjD]W}|jj |}|j}|jD]}|j!|j}Y|jj j#|jrtjd|jj j$|_y)z Iterate a subdivision of the complex Note: called with ``self.iterate_complex()`` after class initiation z GG   !WWYY^^-DN GGNN466 " NNdff $N 99 LL: ; txx 1 $hhnn 0GGIIbM0A#\\!$$/F0 0  ! 99 LL1 2''))..rdc|xj|jz c_|j|j|jrYt j d|jt j d|jt j d|jdkrtj|jd|_ |jj|_ g}t|jD]/\}}|dkDs |j|j|dz |dz1tj|}t!d d d g|j||_i|_nc|jj&d|jdzkDr|j)|j* |jj&d|_|jrt j d t-|d rD|j.j1|j"j$|j"j2|jrt j d |j.j4j7|jrt j d|j.j4j8|_|j|_y)z Build a complex of Delaunay triangulated points Note: called with ``self.iterate_complex()`` after class initiation )rz self.n = z self.nc = zRConstructing and refining simplicial complex graph structure from sampling points.reraxisrTripoints simplices)rrrN)rrsampled_surfacerr!r"r#rgr}argsortC Ind_sortedflatten enumeraterr~rrrrdelaunay_triangulationrhasattrrvf_to_vvrrrr.r-r/)rrtrisindind_ss r6rzSHGO.iterate_delaunay2s" 466 d.C.CD 99 LL9TVVH- . LL:dggY/ 0 LL; < 88a< jja8DO"oo557DOD'8 B U7KKaa @A B88D>DAz%(K)@A$&&$ODHDKvv||AA-++$**+=aDJ 99 LLF G 4  GG  TXX__dhh.@.@ A 99 LL: ;  ! 99 LL1 2''))..rdcg|_|jjjD]S}d}t |j j dkDr\|j j D]C}tjtj|tj|k(sBd}E|r|jj|js|jrtjdtjd|jj|jdtjd|jj|jdtjd|jj||jvr2|jj!|jj||jstjd tjd|jj|j"D]1}tjd |j$d |j3tjdVg|_g|_i|_|jD]x}|j(j!|j|j&j!|j|j$|j*t-|j<ztj|j&|_tj|j(|_|j/|j(S) z7 Returns the indexes of all minimizers FrTz<============================================================zv.x = z is minimizerzv.f = z==============================z Neighbors:zx = z || f = )rrrrrr$r%r}allr~ minimiserr!r"r#rrrrr,minimizer_pool_Fr X_min_cacher sort_min_pool)rrr,in_LMCxlmivnrs r6rzSHGO.minimizersksu! +AF488##$q( HH,,&DvvbhhqkRXXd^;)> ?XXdjj)  zzrdcv|j|jd|jd}|jd|js|j |r|dz}|dk(rd|_ d|_yt j|jddk(rd|_ d|_y|j|j|j|jdddf}|j|jdddf|jd}|j||jsd|_y)a; This processing method can optionally minimise only the best candidate solutions in the minimiser pool Parameters ---------- force_iter : int Number of starting minimizers to process (can be specified globally or locally) rrrTNF) rrr trim_min_poolrrr}r g_topographr,ZSs)rr force_iter lres_f_min ind_xmin_ls r6rzSHGO.minimise_pools+]]4::a=d6I6I!6L]M  1""  " " $a ?'+D$(!'xx #A&!+#'  !   Z\\4:: 62Jtwwr1u~t7J7J27NOJ   z *5"":!rdctj|j|_tj|j |j|_tj|j|j|_yr?)r}rr ind_f_minr~rrs r6rzSHGO.sort_min_pools^D$9$9: hht':':;DNNK ")>)> ? NN!rdctj|j|d|_tj|j||_tj|j||_y)Nrr)r}deleterrr)rrtrim_inds r6rzSHGO.trim_min_poolsOYYtzz8!< " $*?*? J ii(;(;XFrdcptj|g}tjj ||d|_tj |j d|_||jd|_|j|j|_ |jd|_ |jS)a Returns the topographical vector stemming from the specified value ``x_min`` for the current feasible set ``X_min`` with True boolean values indicating positive entries and False values indicating negative entries. euclideanrrr) r}r~rdistancecdistYrrrr)rrx_minrs r6rzSHGO.g_topographs%!!!''ukBDFF,-""11$&&9"11!4wwrdc|jDcgc] }|d|dg}}|jD]k}t|jD]Q\}}||j|kr|||dkDr|||d<||j|kDs>|||dksJ|||d<Sm|jr:t j d|jt j d||Scc}w)aM Construct locally (approximately) convex bounds Parameters ---------- v_min : Vertex object The minimizer vertex Returns ------- cbounds : list of lists List of size dimension with length-2 list of bounds for each dimension. rrzcbounds found for v_min.x_a = z cbounds = )r4rr rr!r"r#)rrv_minx_b_icboundsrix_is r6construct_lcb_simplicialzSHGO.construct_lcb_simplicials 6:[[AEE!HeAh'AA(( (B#BFF+ (3%))A,&S71:a=-@$'GAJqM%))A,&S71:a=-@$'GAJqM ( ( 99 LL9%))E F LL:gY/ 0!BsC$cR|jDcgc] }|d|dg}}|Scc}w)aL Construct locally (approximately) convex bounds Parameters ---------- v_min : Vertex object The minimizer vertex Returns ------- cbounds : list of lists List of size dimension with length-2 list of bounds for each dimension. rrr4)rrr/rr0r1s r6construct_lcb_delaunayzSHGO.construct_lcb_delaunays36:[[AEE!HeAh'AABs$c|jr,tjd|jj|j|j Htjd|j|j |j|j S|j tjd|d|jrtjd|d|jdk(rt|}|jt|}t|}|j|jj|}d|jvr||jd<tj|jdnR|j|| }d|jvr1||jd<tj|jd|jrEd|jvr7tjd tj|jdt!|j"|fi|j}|jrtjd ||j$xj&|j(z c_d |vr)|j$xj*|j,z c_d |vr)|j$xj.|j0z c_ |j2d|_|j||jj9||||S#t4t6f$r|j2YLwxYw)a This function is used to calculate the local minima using the specified sampling point as a starting value. Parameters ---------- x_min : vector of floats Current starting point to minimize. Returns ------- lres : OptimizeResult The local optimization result represented as a `OptimizeResult` object. zVertex minimiser maps = zFound self.LMC[x_min].lres = z#Callback for minimizer starting at :zStarting minimization at z...r<r4rzbounds in kwarg:zlres = njevnhevrr6)r!r"r#r$v_mapslresrrrrr4rrrrr7rr3r)rr.rr:rr;r* IndexErrorr{add_res)rrr-rx_min_t x_min_t_normg_boundsr=s r6rz SHGO.minimize*s" 99 LL3DHHOO3DE F 88E?   + LL8 HHUO0013 488E?'' ' == $ LL>ugQG H 99 LL4UG3? @   < /ElG++E'N;L .L44TWWYY|5LMH4///2:%%h/ T228<=225c2BH4///2:%%h/ T228<= 99T%:%:: LL+ , LL..x8 9 5BD,A,AB 99 LL74&) * $))# T> HHNNdii 'N T> HHNNdii 'N xx{DH  X6 I&  HH s5L88MMcR|jj}|d|j_|d|j_|d|j_|d|j_|j|jjz|j_ |jS)A Sort results and build the global return object rfunlr,r*) r$sort_cache_resultr)rrEr,r*r-rr.)rrresultss r6rzSHGO.sort_resultvsy ((,,.dm  S\ u~ $((..0 xxrdcfd|_d|j_dg|_||j_y)NTF)r r)r2rr1)rrrs r6r'zSHGO.fail_routines+! V rdc|jrtjd|j|j|j t |jjdkDrMtj|jtj|jjf|_ |s|j|j|j|j|_y)a Sample the function surface. There are 2 modes, if ``infty_cons_sampl`` is True then the sampled points that are generated outside the feasible domain will be assigned an ``inf`` value in accordance with SHGO rules. This guarantees convergence and usually requires less objective function evaluations at the computational costs of more Delaunay triangulation points. If ``infty_cons_sampl`` is False, then the infeasible points are discarded and only a subspace of the sampled points are used. This comes at the cost of the loss of guaranteed convergence and usually requires more objective function evaluations. zGenerating sampling pointsrN)r!r"r#rrrgrr$r%r}vstackrr~rsampling_subspacesorted_samplesr/)rrrs r6rzSHGO.sampled_surfaces" 99 LL5 6 dggtxx( txx 1 $YY1A1A(BCDDF{{&&&( rdc|jdk(r|j|||_n|j|||_tt |j D]_}|jdd|f|j |d|j |dz z|j |dz|jdd|f<a|jS)zu Generates uniform sampling points in a hypercube and scales the points to the bound limits. rNr)r/rrrangerr4)rrrrgr2s r6rzSHGO.sampling_customs >>Q ++As3DF++As3DFs4;;'( 1A FF1a4L![[^A.Q1BBD"kk!nQ/0DFF1a4L 1vv rdc t|jD]\}}tj|jDcgc].}tj ||g|j |dk\0c}t}|j||_|jjdk(sd|j_ |jstj|jjycc}w)z7Find subspace of feasible points from g_func definitionr)dtyperzJNo sampling point found within the feasible set. Increasing sampling size.N)r rr}r~rrrboolrr)r1r!r"r#)rrrgx_Cfeasibles r6rKzSHGO.sampling_subspaces  , 3FC xxEIVVLc#1 C 01S89LHVVH%DFvv{{a%. 99LL!1!12# 3 Ms3C1 ctj|jd|_|j|j|_|j|jfS)z*Find indexes of the sorted sampling pointsrr)r}rrrXsrs r6rLzSHGO.sorted_sampless?**TVV!4&&)''rdcPt|drF|jr:|jj|j|dddf|jS t j |j|j|_|jS#t j$rttjddddk(rMtjdd|_t j |j|j|_nY|jSwxYw)Nr) incrementalrQH6239aQH6239 Qhull precision error detected, this usually occurs when no bounds are specified, Qhull can only run with handling cocircular/cospherical points and in this case incremental mode is switched off. The performance of shgo will be reduced in this mode.F) r rr add_pointsrrDelaunay QhullErrorr@sysexc_infor"warning)rrrs r6r zSHGO.delaunay_triangulations 4 D$:$: HH  uvqy 1 2,xx) "++DFF8<8N8N.&xx!%% s||~a()"1-9OO%DE.3D*&//040F0F HDH  xx! s0BBD%$D%) rHNNNNNNr<r)Fr?)zFailed to converge)r)#__name__ __module__ __qualname__rrrrrrr&rrrrrrrrrrrrrrrr4r7rrr'rrrKrLr rHrdr6rrsBF=AEFHV=/~5 !<H6!(   $$" H  0 -^6r0h2h & D(IX" !B$3,( rdrceZdZdZy)LMapcJ||_d|_d|_d|_g|_yr?)rrr=rlbounds)rrrs r6rz LMap.__init__s%   rdN)rarbrcrrHrdr6reresrdrec&eZdZdZdZddZdZy)rcvi|_g|_g|_t|_g|_g|_d|_y)Nr)rr<r%r] xl_maps_setf_maps lbound_mapsrrs r6rzLMapCache.__init__s9   5  rdc tjj|}t |} |j |S#t$rY&wxYw#t $r,t|}||j |<|j |cYSwxYwr?)r}ndarraytolistr{rrr|re)rrrxvals r6 __getitem__zLMapCache.__getitem__s  !!!$A !H !::a=      !7D DJJqM::a=  !s <A AA 2B?BNctjj|}t|}|j|j |_||j |_|j|j |_ ||j |_ |xjdz c_ |jj||jj|j|jj!t|j|j"j|j|j$j|yr)r}rnrorr,rrr=r*rrgrr<rr%rjaddrkrl)rrrr=r4s r6r?zLMapCache.add_ress JJ  a  !H FF 1 ! 1 "hh 1  & 1  Q  1 DFF# U466]+ 488$ 'rdci}tj|j|_tj|j|_tj|j}|j||d<tj|j|_|j||d<|dj |d<|j|d|d<|j|d|d<tj j|j|_tj j|j|_|S)rDrrErr,r*)r}r~r%rkrTrnro)rrrG ind_sorteds r6rFzLMapCache.sort_cache_result.sxx - hht{{+ ZZ ,  Z0 hht{{+ ++j1!&/++||JqM2 Z]3zz((6 jj'' 4 rdr?)rarbrcrrqr?rFrHrdr6rrs  !($rdr)rHNrfrNNNr<)__doc__ collectionsrrr"rr^numpyr}scipyrscipy.optimizerrrscipy.optimize._optimizerscipy.optimize._constraintsr scipy.optimize._minimizer scipy._lib._utilr !scipy.optimize._shgo_lib._complexr __all__r rrerrHrdr6rsxB"  ;;/9<-5 (GK9EO OdNNb DDrd