
    Ui6                     (   d dl Z d dlmZmZ d dlZd dlmZ  G d de          Z G d de          Z	 G d d	e          Z
 G d
 d          Z G d de          Z G d de          Z G d de          Z G d d          Z G d d          ZdS )    N)ABCabstractmethod)
MapWrapperc                   X    e Zd ZdZd	dZd Zd Zed             Zed             Z	d Z
dS )

VertexBasez"
    Base class for a vertex.
    Nc                     || _         t          | j                   | _        |t          |          | _        nt                      | _        || _        dS )a  
        Initiation of a vertex object.

        Parameters
        ----------
        x : tuple or vector
            The geometric location (domain).
        nn : list, optional
            Nearest neighbour list.
        index : int, optional
            Index of vertex.
        N)xhashsetnnindex)selfr	   r   r   s       x/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/scipy/optimize/_shgo_lib/_vertex.py__init__zVertexBase.__init__   sD     LL	>"ggDGGeeDG


    c                     | j         S N)r
   r   s    r   __hash__zVertexBase.__hash__$   s
    yr   c                     |dvr"t          t          |            d| d          |dk    r%t          j        | j                  | _        | j        S d S )N)x_az object has no attribute ''r   )AttributeErrortypenparrayr	   r   )r   items     r   __getattr__zVertexBase.__getattr__'   sm    w DJJ "- "-%)"- "- "- . . .5==x''DH8O =r   c                      t          d          NzKThis method is only implemented with an associated child of the base class.NotImplementedErrorr   vs     r   connectzVertexBase.connect/       ! #H I I 	Ir   c                      t          d          r    r!   r#   s     r   
disconnectzVertexBase.disconnect4   r&   r   c                 \    | j         | _        | j                            |            | j        S )a  Returns the star domain ``st(v)`` of the vertex.

        Parameters
        ----------
        v :
            The vertex ``v`` in ``st(v)``

        Returns
        -------
        st : set
            A set containing all the vertices in ``st(v)``
        )r   staddr   s    r   starzVertexBase.star9   s(     'Dwr   NN)__name__
__module____qualname____doc__r   r   r   r   r%   r(   r,    r   r   r   r   	   s            .     I I ^I I I ^I    r   r   c                   @     e Zd ZdZ	 	 d	 fd	Zd Zd Zd Zd Z xZ	S )
VertexScalarFieldz
    Add homology properties of a scalar field f: R^n --> R associated with
    the geometry built from the VertexBase class
    Nr2   c                 l    t                                          |||           d| _        d| _        dS )aY  
        Parameters
        ----------
        x : tuple,
            vector of vertex coordinates
        field : callable, optional
            a scalar field f: R^n --> R associated with the geometry
        nn : list, optional
            list of nearest neighbours
        index : int, optional
            index of the vertex
        field_args : tuple, optional
            additional arguments to be passed to field
        g_cons : callable, optional
            constraints on the vertex
        g_cons_args : tuple, optional
            additional arguments to be passed to g_cons

        r   r   TN)superr   	check_min	check_max)	r   r	   fieldr   r   
field_argsg_consg_cons_args	__class__s	           r   r   zVertexScalarField.__init__Q   s8    * 	r/// r   c                     || ur[|| j         vrT| j                             |           |j                             |            d| _        d| _        d|_        d|_        dS dS dS )zConnects self to another vertex object v.

        Parameters
        ----------
        v : VertexBase or VertexScalarField object
        TN)r   r+   r8   r9   r#   s     r   r%   zVertexScalarField.connectt   sj     D==Qdg--GKKNNNDHHTNNN "DN!DNAKAKKK =--r   c                     || j         v rR| j                             |           |j                             |            d| _        d| _        d|_        d|_        d S d S )NT)r   remover8   r9   r#   s     r   r(   zVertexScalarField.disconnect   s^    <<GNN1DKK "DN!DNAKAKKK <r   c                 x      j         r,t           fd j        D                        _        d _          j        S )zMCheck whether this vertex is strictly less than all its
           neighboursc              3   8   K   | ]}j         |j         k     V  d S r   f.0r$   r   s     r   	<genexpr>z.VertexScalarField.minimiser.<locals>.<genexpr>   +      ::QDFQSL::::::r   F)r8   allr   _minr   s   `r   	minimiserzVertexScalarField.minimiser   sD     > 	#::::$':::::DI"DNyr   c                 x      j         r,t           fd j        D                        _        d _          j        S )z`
        Check whether this vertex is strictly greater than all its
        neighbours.
        c              3   8   K   | ]}j         |j         k    V  d S r   rD   rF   s     r   rH   z.VertexScalarField.maximiser.<locals>.<genexpr>   rI   r   F)r9   rJ   r   _maxr   s   `r   	maximiserzVertexScalarField.maximiser   sD    
 > 	#::::$':::::DI"DNyr   )NNNr2   Nr2   )
r.   r/   r0   r1   r   r%   r(   rL   rP   __classcell__r>   s   @r   r4   r4   K   s         
 GI*,! ! ! ! ! !F  "	 	 	  	 	 	 	 	 	 	r   r4   c                   *     e Zd ZdZ	 	 	 d fd	Z xZS )VertexVectorFieldz
    Add homology properties of a scalar field f: R^n --> R^m associated with
    the geometry built from the VertexBase class.
    Nr2   c
                 j    t                                          |||	           t          d          )Nr6   z&This class is still a work in progress)r7   r   r"   )r   r	   sfieldvfieldr;   vfield_argsr<   r=   r   r   r>   s             r   r   zVertexVectorField.__init__   s3     	r///!"JKKKr   )NNr2   r2   Nr2   NN)r.   r/   r0   r1   r   rQ   rR   s   @r   rT   rT      s^         
 @B(,04L L L L L L L L L Lr   rT   c                   *    e Zd ZdZd Zd Zd Zd ZdS )VertexCacheBasez7Base class for a vertex cache for a simplicial complex.c                 R    t          j                    | _        d| _        d| _        d S )Nr   )collectionsOrderedDictcachenfevr   r   s    r   r   zVertexCacheBase.__init__   s$     ,..
	


r   c              #   <   K   | j         D ]}| j         |         V  d S r   )r_   r#   s     r   __iter__zVertexCacheBase.__iter__   s1       	  	 A*Q-r   c                     | j         dz   S )z%Returns the size of the vertex cache.   r   r   s    r   sizezVertexCacheBase.size   s    zA~r   c                 4   t          dt          | j                   d          }t          d|z             t          dt          | j                   d           t          d|z             | j        D ]!}| j        |                                          "d S )NzVertex cache of size: :=)lenr_   print	print_out)r   headlenr$   s      r   rl   zVertexCacheBase.print_out   s    As4:AAABBcGm9s4:999:::cGm 	& 	&AJqM##%%%%	& 	&r   N)r.   r/   r0   r1   r   rb   rf   rl   r2   r   r   rZ   rZ      sV        AA    
  & & & & &r   rZ   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )
VertexCubezVertex class to be used for a pure simplicial complex with no associated
    differential geometry (single level domain that exists in R^n)Nc                 P    t                                          |||           d S )Nr6   )r7   r   )r   r	   r   r   r>   s       r   r   zVertexCube.__init__   s(    r/////r   c                     || ur?|| j         vr8| j                             |           |j                             |            d S d S d S r   )r   r+   r#   s     r   r%   zVertexCube.connect   sJ    D==Qdg--GKKNNNDHHTNNNNN =--r   c                     || j         v r6| j                             |           |j                             |            d S d S r   )r   rA   r#   s     r   r(   zVertexCube.disconnect   sB    <<GNN1DKK <r   r-   )r.   r/   r0   r1   r   r%   r(   rQ   rR   s   @r   ro   ro      sg        F F0 0 0 0 0 0  
      r   ro   c                   &     e Zd Z fdZddZ xZS )VertexCacheIndexc                 `    t                                                       t          | _        dS )z
        Class for a vertex cache for a simplicial complex without an associated
        field. Useful only for building and visualising a domain complex.

        Parameters
        ----------
        N)r7   r   ro   Vertex)r   r>   s    r   r   zVertexCacheIndex.__init__   s&     	 r   Nc                     	 | j         |         S # t          $ rF | xj        dz  c_        |                     || j                  }|| j         |<   | j         |         cY S w xY w)Nrd   re   )r_   KeyErrorr   rv   r   r	   r   xvals       r   __getitem__zVertexCacheIndex.__getitem__   ss    		!:a=  	! 	! 	!JJ!OJJ;;q
;33D !DJqM:a=   	!s    AAAr   )r.   r/   r0   r   r{   rQ   rR   s   @r   rt   rt      sL        	! 	! 	! 	! 	!
! 
! 
! 
! 
! 
! 
! 
!r   rt   c                   n     e Zd Z	 	 d fd	ZddZd Zd Zd Zd	 Zd
 Z	d Z
d Zd Zd Zd Zd Z xZS )VertexCacheFieldNr2   rd   c                 :   t                                                       d| _        t          | _        || _        || _        t          ||          | _        || _	        || _
        t          ||          | _        t                      | _        t                      | _        d| _        || _        t%          |          | _        |dk    r*| j        | _        || j        | _        dS | j        | _        dS | j        | _        || j        | _        dS | j        | _        dS )a  
        Class for a vertex cache for a simplicial complex with an associated
        field.

        Parameters
        ----------
        field : callable
            Scalar or vector field callable.
        field_args : tuple, optional
            Any additional fixed parameters needed to completely specify the
            field function
        g_cons : dict or sequence of dict, optional
            Constraints definition.
            Function(s) ``R**n`` in the form::
        g_cons_args : tuple, optional
            Any additional fixed parameters needed to completely specify the
            constraint functions
        workers : int  optional
            Uses `multiprocessing.Pool <multiprocessing>`) to compute the field
             functions in parallel.

        r\   Frd   N)r7   r   r   r4   rv   r:   r;   FieldWrapperwfieldr<   r=   ConstraintWrapperwgconsr   gpoolfpoolsfc_lockworkersr   _mapwrapper
proc_gpoolprocess_gpoolproc_fpool_nogprocess_fpoolproc_fpool_gpproc_gpoolpproc_fpool_nogpproc_fpool_g)r   r:   r;   r<   r=   r   r>   s         r   r   zVertexCacheField.__init__   s
   0 	
'
$"5*55&'<<UU
 UU
%g..a<<!%D~%)%8"""%)%6"""!%!1D~%)%9"""%)%7"""r   c           
      ^   	 | j         |         S # t          $ r | xj        dz  c_        |                     || j        || j        | j        | j        | j                  }|| j         |<   | j        	                    |           | j
        	                    |           | j         |         cY S w xY w)Nrd   )r:   r   r   r;   r<   r=   )r_   rx   r   rv   r:   r;   r<   r=   r   r+   r   ry   s       r   r{   zVertexCacheField.__getitem__0  s    	!:a=  
	! 
	! 
	!JJ!OJJ;;q
r*./&*k+/+;  = =D
 !DJqMJNN4   JNN4   :a=   
	!s    BB,+B,c                 >    | j                                         }|d= |S )Npool)__dict__copy)r   	self_dicts     r   __getstate__zVertexCacheField.__getstate__?  s#    M&&((	fr   c                     | j         |                                  |                                  |                                  d S r   )r<   r   r   proc_minimisersr   s    r   process_poolszVertexCacheField.process_poolsD  sF    ;"   r   c                     d|_         t          | j        | j                  D ]D\  }}t	          j         ||j        g|R  dk               rt          j        |_        d|_          d S Ed S NTg        F)	feasiblezipr<   r=   r   anyr   infrE   )r   r$   gargss       r   feasibility_checkz"VertexCacheField.feasibility_checkJ  sx    
4;(899 	 	GAtvaaoooo+,, f"
	 	r   c                     	  | j         |j        g| j        R  |_        | xj        dz  c_        n!# t
          $ r t          j        |_        Y nw xY wt          j        |j                  rt          j        |_        dS dS )zCompute the scalar field values of a vertex object `v`.

        Parameters
        ----------
        v : VertexBase or VertexScalarField object
        rd   N)	r:   r   r;   rE   r`   r   r   r   isnanr#   s     r   compute_sfieldzVertexCacheField.compute_sfieldS  s    	$*QU5T_555ACIINIII 	 	 	&ACCC	 8AC== 	&ACCC	 	s   -0 AAc                 x    | j         | j        D ]}|                     |           t                      | _        dS )zProcess all constraints.N)r<   r   r   r   r#   s     r   r   zVertexCacheField.proc_gpoolc  sA    ;"Z * *&&q))))UU


r   c                     g }| j         D ]}|                    |j                   |                     | j        j        |          }t          | j         |          D ]\  }}||_        dS )z$Process all constraints in parallel.N)r   appendr   r   r   gconsr   r   )r   gpool_lr$   Gr   s        r   r   zVertexCacheField.pproc_gpoolk  s{     	" 	"ANN15!!!!T[.88
A&& 	 	DAqAJJ	 	r   c                 x    | j         D ]}|j        r|                     |           t                      | _         dS )z6Process all field functions with constraints supplied.N)r   r   r   r   r#   s     r   r   zVertexCacheField.proc_fpool_gu  sA     	' 	'Az '##A&&&UU


r   c                 j    | j         D ]}|                     |           t                      | _         dS )z9Process all field functions with no constraints supplied.N)r   r   r   r#   s     r   r   zVertexCacheField.proc_fpool_nog}  s9     	# 	#A""""UU


r   c                    | j         j         g }| j        D ]5}|j        r|                    |j                   $t          j        |_        6| 	                    | j         j        |          }t          ||          D ]1\  }}t          |          }|| |         _        | xj        dz  c_        2t                      | _        dS )zT
        Process all field functions with constraints supplied in parallel.
        rd   N)r   funcr   r   r   r   r   r   rE   r   r   tupler`   r   r   fpool_lr$   FvarE   vts          r   r   zVertexCacheField.pproc_fpool_g  s     	 	 	Az qu%%%%fT[-w77!__ 	 	EBrBDHJIINIIIUU


r   c                 T   | j         j         g }| j        D ]}|                    |j                   |                     | j         j        |          }t          ||          D ]1\  }}t          |          }|| |         _        | xj	        dz  c_	        2t                      | _        dS )zW
        Process all field functions with no constraints supplied in parallel.
        rd   N)r   r   r   r   r   r   r   r   rE   r`   r   r   s          r   r   z VertexCacheField.pproc_fpool_nog  s     	 	" 	"ANN15!!!!T[-w77!__ 	 	EBrBDHJIINIIIUU


r   c                 `    | D ]*}|                                  |                                 +dS )zCheck for minimisers.N)rL   rP   r#   s     r   r   z VertexCacheField.proc_minimisers  s7     	 	AKKMMMKKMMMM	 	r   )Nr2   Nr2   rd   r   )r.   r/   r0   r   r{   r   r   r   r   r   r   r   r   r   r   r   rQ   rR   s   @r   r}   r}      s        KM68 68 68 68 68 68p! ! ! !  
                 &         r   r}   c                       e Zd ZdZd Zd ZdS )r   z=Object to wrap constraints to pass to `multiprocessing.Pool`.c                 "    || _         || _        d S r   )r<   r=   )r   r<   r=   s      r   r   zConstraintWrapper.__init__  s    &r   c                     d}t          | j        | j                  D ](\  }}t          j         ||g|R  dk               rd} n)|S r   )r   r<   r=   r   r   )r   v_x_a	vfeasibler   r   s        r   r   zConstraintWrapper.gcons  se    	4;(899 	 	GAtvaaoooo+,, !	 r   N)r.   r/   r0   r1   r   r   r2   r   r   r   r     s8        GG' ' '    r   r   c                       e Zd ZdZd Zd ZdS )r   z7Object to wrap field to pass to `multiprocessing.Pool`.c                 "    || _         || _        d S r   )r:   r;   )r   r:   r;   s      r   r   zFieldWrapper.__init__  s    
$r   c                     	  | j         |g| j        R  }n# t          $ r t          j        }Y nw xY wt          j        |          rt          j        }|S r   )r:   r;   	Exceptionr   r   r   )r   r   v_fs      r   r   zFieldWrapper.func  sf    	$*U5T_555CC 	 	 	&CCC	8C== 	&C
s    //N)r.   r/   r0   r1   r   r   r2   r   r   r   r     s8        AA% % %    r   r   )r]   abcr   r   numpyr   scipy._lib._utilr   r   r4   rT   rZ   ro   rt   r}   r   r   r2   r   r   <module>r      s       # # # # # # # #     ' ' ' ' ' '? ? ? ? ? ? ? ?DW W W W W
 W W WtL L L L L
 L L L& & & & & & & &4       "! ! ! ! ! ! ! !2t t t t t t t tn                 r   