
    4[g]7                     j    d dl Z d dlZddlmZ  ej
                  e      j                  Zd Z	d Z
d Zy)    N   )get_arrays_tolc           	      L   |rNt        | t              sJ t        |t        j                        r|j                  dk(  sJ t        j                        j                  |      sJ t        |t        j                        r|j                  |j                  k(  sJ t        |t        j                        r|j                  |j                  k(  sJ t        |t              sJ t        |t              sJ t        ||      }t        j                  ||k        sJ t        j                  || k\        sJ t        j                  |      r|dkD  sJ t        j                  |d      }t        j                  |d      }t        | |||||      \  }}	t        |  | fd||||      \  }
}t!        |	      t!        |      k\  r|n|
}|r[t        j                  ||k        sJ t        j                  ||k        sJ t        j"                  j%                  |      d|z  k  sJ |S )a'  
    Maximize approximately the absolute value of a quadratic function subject
    to bound constraints in a trust region.

    This function solves approximately

    .. math::

        \max_{s \in \mathbb{R}^n} \quad \bigg\lvert c + g^{\mathsf{T}} s +
        \frac{1}{2} s^{\mathsf{T}} H s \bigg\rvert \quad \text{s.t.} \quad
        \left\{ \begin{array}{l}
            l \le s \le u,\\
            \lVert s \rVert \le \Delta,
        \end{array} \right.

    by maximizing the objective function along the constrained Cauchy
    direction.

    Parameters
    ----------
    const : float
        Constant :math:`c` as shown above.
    grad : `numpy.ndarray`, shape (n,)
        Gradient :math:`g` as shown above.
    curv : callable
        Curvature of :math:`H` along any vector.

            ``curv(s) -> float``

        returns :math:`s^{\mathsf{T}} H s`.
    xl : `numpy.ndarray`, shape (n,)
        Lower bounds :math:`l` as shown above.
    xu : `numpy.ndarray`, shape (n,)
        Upper bounds :math:`u` as shown above.
    delta : float
        Trust-region radius :math:`\Delta` as shown above.
    debug : bool
        Whether to make debugging tests during the execution.

    Returns
    -------
    `numpy.ndarray`, shape (n,)
        Approximate solution :math:`s`.

    Notes
    -----
    This function is described as the first alternative in Section 6.5 of [1]_.
    It is assumed that the origin is feasible with respect to the bound
    constraints and that `delta` is finite and positive.

    References
    ----------
    .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
       and Software*. PhD thesis, Department of Applied Mathematics, The Hong
       Kong Polytechnic University, Hong Kong, China, 2022. URL:
       https://theses.lib.polyu.edu.hk/handle/200/12294.
               c                      |        S )N )xcurvs    `/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.py<lambda>z!cauchy_geometry.<locals>.<lambda>[   s    47(    皙?)
isinstancefloatnpndarrayndiminspect	signaturebindshapeboolr   allisfiniteminimummaximum_cauchy_geomabslinalgnorm)constgradr   xlxudeltadebugtolstep1q_val1step2q_val2steps     `          r   cauchy_geometryr.      s   t %'''$

+		Q>>  &++D111"bjj)bhh$**.DDD"bjj)bhh$**.DDD%'''%&&&R$vvbCi   vvbSDj!!!{{5!eck11	B	B	B	B
 !dBE5IME6 		

ME6 K3v;.5EDvvbDj!!!vvdbj!!!yy~~d#cEk111Kr   c                    |rt        | t              sJ t        |t        j                        r|j                  dk(  sJ t        j                  |      j                  |      sJ t        |t        j                        r+|j                  dk(  r|j                  d   |j                  k(  sJ t        |t        j                        r|j                  |j                  k(  sJ t        |t        j                        r|j                  |j                  k(  sJ t        |t              sJ t        |t              sJ t        ||      }t        j                  ||k        sJ t        j                  || k\        sJ t        j                  |      r|dkD  sJ t        j                  |d      }t        j                  |d      }t        j                   |      }	| }
t        j"                  j%                  |d      }|t        j&                   kD  |j(                  t*         |z  kD  z  }|t        j&                   kD  |j(                  t*        |z  k  z  }|t        j&                  k  |j(                  t*        |z  kD  z  }|t        j&                  k  |j(                  t*         |z  k  z  }t        j,                  t        j.                  ||j                        |   |j(                  |   z        }t        j0                  |dt        j&                         }t        j.                  t        j2                  |      |j                  d         }t        j,                  t        j.                  ||j                        |   |j(                  |   z        }t        j0                  |dt        j&                        }t        j.                  t        j2                  |      |j                  d         }t        j,                  t        j.                  ||j                        |   |j(                  |   z        }t        j0                  |dt        j&                         }t        j.                  t        j2                  |      |j                  d         }t        j,                  t        j.                  ||j                        |   |j(                  |   z        }t        j0                  |dt        j&                        }t        j.                  t        j2                  |      |j                  d         }t5        |j                  d         D ]S  }||   t*        |z  kD  rt1        |||   z  d      }n't1        t7        ||   ||         d      }t7        t1        ||   ||         d      }||dd|f   z  } ||dd|f         }|dk\  r|t*         |z  k  s|dk  r|t*         |z  kD  rt1        | |z  d      }nt        j&                  }|dk\  r|t*        |z  kD  s|dk  r|t*        |z  k  rt7        | |z  d      }nt        j&                   }t7        ||      }t1        | |      }| ||z  z   d|d	z  z  |z  z   }| ||z  z   d|d	z  z  |z  z   }||k  r/| ||z  z   d|d	z  z  |z  z   } t9        |       t9        |      kD  r|}| }||kD  r/| ||z  z   d|d	z  z  |z  z   }!t9        |!      t9        |      kD  r|}|!}t9        |      t9        |      k\  r<t9        |      t9        |
      kD  r%t        j:                  ||dd|f   z  ||      }	|}
t9        |      t9        |      kD  st9        |      t9        |
      kD  s1t        j:                  ||dd|f   z  ||      }	|}
V |r[t        j                  ||	k        sJ t        j                  |	|k        sJ t        j"                  j%                  |	      d
|z  k  sJ |	S )a  
    Maximize approximately the absolute value of a quadratic function subject
    to bound constraints in a trust region.

    This function solves approximately

    .. math::

        \max_{s \in \mathbb{R}^n} \quad \bigg\lvert c + g^{\mathsf{T}} s +
        \frac{1}{2} s^{\mathsf{T}} H s \bigg\rvert \quad \text{s.t.} \quad
        \left\{ \begin{array}{l}
            l \le s \le u,\\
            \lVert s \rVert \le \Delta,
        \end{array} \right.

    by maximizing the objective function along given straight lines.

    Parameters
    ----------
    const : float
        Constant :math:`c` as shown above.
    grad : `numpy.ndarray`, shape (n,)
        Gradient :math:`g` as shown above.
    curv : callable
        Curvature of :math:`H` along any vector.

            ``curv(s) -> float``

        returns :math:`s^{\mathsf{T}} H s`.
    xpt : `numpy.ndarray`, shape (n, npt)
        Points defining the straight lines. The straight lines considered are
        the ones passing through the origin and the points in `xpt`.
    xl : `numpy.ndarray`, shape (n,)
        Lower bounds :math:`l` as shown above.
    xu : `numpy.ndarray`, shape (n,)
        Upper bounds :math:`u` as shown above.
    delta : float
        Trust-region radius :math:`\Delta` as shown above.
    debug : bool
        Whether to make debugging tests during the execution.

    Returns
    -------
    `numpy.ndarray`, shape (n,)
        Approximate solution :math:`s`.

    Notes
    -----
    This function is described as the second alternative in Section 6.5 of
    [1]_. It is assumed that the origin is feasible with respect to the bound
    constraints and that `delta` is finite and positive.

    References
    ----------
    .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
       and Software*. PhD thesis, Department of Applied Mathematics, The Hong
       Kong Polytechnic University, Hong Kong, China, 2022. URL:
       https://theses.lib.polyu.edu.hk/handle/200/12294.
    r   r   r   r   )axis)r0   initialN      ?       @r   )r   r   r   r   r   r   r   r   r   sizer   r   r   r   r   r   
zeros_liker    r!   infTTINY
atleast_2dbroadcast_tomax
atleast_1drangeminr   clip)"r"   r#   r   xptr$   r%   r&   r'   r(   r-   q_vals_normi_xl_posi_xl_negi_xu_posi_xu_negalpha_xl_posalpha_xl_negalpha_xu_negalpha_xu_poskalpha_tralpha_bd_posalpha_bd_neg	grad_step	curv_stepalpha_quad_posalpha_quad_neg	alpha_pos	alpha_neg	q_val_pos	q_val_negq_val_quad_posq_val_quad_negs"                                     r   spider_geometryrY   j   s   x %'''$

+		Q>>  &++D111sBJJ'A		!		)	
* "bjj)bhh$**.DDD"bjj)bhh$**.DDD%'''%&&&R$vvbCi   vvbSDj!!!{{5!eck11	B	B	B	B ==DEYY^^Ca^(F bffW$!34HbffW!23HRVVr	 12HRVV
 23H ==
HNN+H5hGL 66,Q@L??2==#>		!ML==
HNN+H5hGL 66,Q?L??2==#>		!ML==
HNN+H5hGL 66,Q@L??2==#>		!ML==
HNN+H5hGL 66,Q?L??2==#>		!ML399Q< !9te|#56!9,c2H 3|AQ@#F3|AQ@#F 3q!t9$	QTO	TEI--CTEI-- )i!7=NVVND9,,CD9,, )i!7=N ffWN ,/		<0	I	))C)S.,@9,LL 	 I	))C)S.,@9,LL 	 I% 9,-++i78 
 >"S^3*	*	I% 9,-++i78 
 >"S^3*	*	y>S^+IU0K779s1a4y0"b9DE^c)n,Y#e*1L779s1a4y0"b9DEU !X vvbDj!!!vvdbj!!!yy~~d#cEk111Kr   c                    |dk  |dkD  z  }|dkD  |dk  z  }t        j                  |      }	||   |	|<   ||   |	|<   t         j                  j                  |	      |kD  r||z  }
	 t         j                  j                  ||
         }t        j                  |dz  |	|
    |	|
    z  z
        }|t
        t        |      z  kD  rt        ||z  d      }nn`|||
   z  |	|
<   |
|	|k  z  }|
|	|kD  z  }t        j                  |      st        j                  |      sn||   |	|<   ||   |	|<   |
||z   z  }
||	z  }|dk\  rXt         j                  j                  |	      }|t
        |z  kD  rt        ||z  d      }nd} ||	      }|t
         |z  k  rt        | |z  d      }nt         j                  }|t         j                   kD  |	t
        |z  k  z  }|t         j                  k  |	t
        |z  kD  z  }t        j                  ||   |	|   z  t         j                        }t        j                  ||   |	|   z  t         j                        }t        ||      }t        |||      }t        j                  ||	z  ||      }| ||z  z   d|dz  z  |z  z   }nt        j                  |      }| }|r[t        j                  ||k        sJ t        j                  ||k        sJ t         j                  j                  |      d|z  k  sJ ||fS )zM
    Same as `bound_constrained_cauchy_step` without the absolute value.
    r   r3   )r1   r2   r   )r   r5   r    r!   sqrtr8   r   r;   anyr6   r>   r?   r   )r"   r#   r   r$   r%   r&   r'   fixed_xlfixed_xucauchy_stepworkingg_normdelta_reducedmurO   rB   rL   rP   
alpha_quadi_xli_xualpha_xlalpha_xualpha_bdalphar-   rA   s                              r   r   r   8  s!   
 STCZ(HSTCZ(H --%KxLKxLK	yy~~k"U*X%YY^^DM2FGGs
['2['5JJJM s=111/5#%W#5K  +"23H+"23H66(#BFF8,<$&xLK!$&xLK!(X"5 66G) . {"IC,D5L 56>3/H H %	uy((iZ)3S9JJ bffWtby!89RVVdRi 7866"T([%66G66"T([%66Gx* Hj(3wwu{*B3	))C%*,<y,HH }}T"vvbDj!!!vvdbj!!!yy~~d#cEk111;r   )r   numpyr   utilsr   finfor   tinyr8   r.   rY   r   r	   r   r   <module>ro      s9      " rxx\~K\Kr   