U
    -eS                     @   sl   d dl Zd dlZG dd dZG dd dZG dd deZG dd	 d	eZG d
d dZG dd dZdS )    Nc                   @   sz   e Zd ZdddZdd Zddd	Zd
d Zdd Zdd Zdd Z	dd Z
dd Zdd Zd ddZdd Zdd ZdS )!Complex FNc                 C   s   || _ || _|| _d| _d| _g | _t|||||| _| j||d |rPd| _	n| 
  | jg  | jd | j | j | _d| _|   g | _| jd | jd d S )Nr   )symmetry   )dimboundsr   genZ
perm_cycleHVertexCacheVn_cubegeneration_cycleadd_centroidappendC0homology_group_rankhgrZhgrd	graph_mapZperformance)selfr   func	func_argsr   r   g_consZg_argsr   r   g/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/scipy/optimize/_shgo_lib/triangulation.py__init__   s&    zComplex.__init__c                 C   s   | j S N)r	   r   r   r   r   __call__0   s    zComplex.__call__c                 C   s   t tj|td}|| _t tj|td}|| _t|}t|}|g}|rtddd| j	| _
| j
| j|  d}	| |	|| | j
| j|  nFtdd||| _
| j
| j|  | j
| j|  g }
| |
|| |rtd | 
 D ]}|  qdS )zm
        Generate the simplicial triangulation of the N-D hypercube
        containing 2**n vertices
        Zdtyper   zInitial hyper cube:N)listnpzerosintoriginZonessupremumtupleSimplexr   r   
add_vertexr   perm_symmetryCellpermprint	print_out)r   r   r   printoutr"   r#   ZorigintupleZsupremumtuple	x_parentsi_s	i_parentsvr   r   r   r   3   s,    zComplex.n_cubec                    s   t |} fddt| jD }|D ]}t }|| t|}d||< t |}	| j| j|	  | j|	 | j|  |D ]}
| j|	 | j|
  qt|}|| | 	||| q$d S )Nc                    s   g | ]}| kr|qS r   r   ).0xr/   r   r   
<listcomp>\   s      z Complex.perm.<locals>.<listcomp>r   )
r$   ranger   copyr   r   r&   r   connectr)   )r   r/   r-   xixi_tZ
iter_rangeiZ
i2_parentsxi2xi2_tx_ip
x_parents2r   r3   r   r)   W   s    




zComplex.permc           	      C   s   t |}t|}d||< t |}| j| j|  | j| | j|  |D ]}| j| | j|  qNt|}|| |d7 }|| jkrd S | ||| d S Nr   )	r$   r6   r   r&   r   r7   r   r   r'   )	r   r.   r-   r8   r9   r;   r<   r=   r>   r   r   r   r'   u   s    



zComplex.perm_symmetryc                 C   s   t t| jt| j d | _| j| jt	| j  | j| j_| jt	| j 
| jt	| j  |  D ]&}| jt	| j | jt	|j  qnd| _dS )znSplit the central edge between the origin and supremum of
        a cell and add the new vertex to the complex       @TN)r   r   arrayr"   r#   centroidr   r&   r   r$   
disconnectr7   r2   centroid_addedr   r0   r   r   r   r      s    
"$zComplex.add_centroidc                 C   s   | j r0tjd| j d d| j d gtd| _n tjd| j d| j gtd| _| j D ]"}|jD ]}d| j|j	|j	f< qdqZd S )N   r   r   )
rD   r   r    r   r!   Z	structureZHCr   nnindex)r   r0   v2r   r   r   	incidence   s    

zComplex.incidencec                 C   s   dd |   D | _dS )z Make a list of size 2**n + 1 where an entry is a vertex
        incidence, each list element contains a list of indexes
        corresponding to that entries neighborsc                 S   s   g | ]}d d |j D qS )c                 S   s   g | ]
}|j qS r   )rH   )r1   rI   r   r   r   r4      s     z0Complex.graph_map.<locals>.<listcomp>.<listcomp>)rG   r1   r0   r   r   r   r4      s     z%Complex.graph_map.<locals>.<listcomp>N)r   graphr   r   r   r   r      s    zComplex.graph_mapc              	   C   s   t |j}t| d }z| j|  W n  tk
rF   | jg  Y nX g }t| dd D ]*\}}t |j}|| ||||j	 q^t| j
D ]4\}}	||kr q|	D ]}
| | | |
  qq|| jk	r~|S )zSSubgenerate a cell `C_i` of generation `gen` and
        homology group rank `hgr`.r   N)r$   rB   lenr	   
IndexErrorr   	enumerater2   construct_hypercubehg_nrL   rC   r   )r   ZC_ir   Z
origin_newZcentroid_indexZH_newr:   r0   r#   connectionsjr   r   r   sub_generate_cell   s(    


zComplex.sub_generate_cellc                 C   sr   d}zB| j | j D ]0}| jr0| || jd  q| || jd  qW n tk
r^   d}Y nX |  jd7  _|S )zV
        Run sub_generate_cell for every cell in the current complex self.gen
        Fr   T)r	   r   r   split_simplex_symmetryrU   rO   )r   Z	no_splitscr   r   r   split_generation   s    
zComplex.split_generationc                 C   sN  t |}t |}t||||}t|| d |_g }	t|  dd D ]R\}
}t |j}|||  }|| }|| }t|}|| j	|  |	
| qL|| j	|j  |	
|j t| jD ]0\}
}|D ]"}| j	|	|
  | j	|	|   qq|r:td td| td| | D ]}|  q*| j| 
| |S )z
        Build a hypercube with triangulations symmetric to C0.

        Parameters
        ----------
        origin : vec
        supremum : vec (tuple)
        gen : generation
        hgr : parent homology group rank
        g      ?NrM   zA sub hyper cube with:z
origin: {}zsupremum: {})r   rA   r(   r$   rB   rP   r   r2   r&   r   r   rL   r7   r*   formatr+   r	   )r   r"   r#   r   r   r,   Zv_oZv_sZC_newV_newr:   r0   Zv_xZsub_cell_t1Zsub_cell_t2ZvecrS   rT   r   r   r   rQ      s4    

"
zComplex.construct_hypercubec                 C   s  z| j |  W n  tk
r.   | j g  Y nX | }|d j}|d j}| jtt|t| d  }| j| | j|  |dd D ]}|	| j|j  qt
||j| j| j}||d  || |dd D ]}|| qt
||j|j| j}	|	||	jd   |dd D ]}|	| q"t|dd D ],\}
}|
|jkrd|	| n
|	| qD|	|d  | j | | | j | |	 dS )aI  
        Split a hypersimplex S into two sub simplices by building a hyperplane
        which connects to a new vertex on an edge (the longest edge in
        dim = {2, 3}) and every other vertex in the simplex that is not
        connected to the edge being split.

        This function utilizes the knowledge that the problem is specified
        with symmetric constraints

        The longest edge is tracked by an ordering of the
        vertices in every simplices, the edge between first and second
        vertex is the longest edge to be split in the next iteration.
        r   rM   r@   Nr   )r	   rO   r   r2   r   r$   r   rA   rC   r7   r%   rR   r   r   r&   rP   )r   Sr   sZfirstxZlastxrZ   r0   ZS_new_lZS_new_ukr   r   r   rV   ,  s<    

"
zComplex.split_simplex_symmetryc              	   C   sP  ddl m} | jdkr4|  | jD ]}|D ]t}| D ]f}| jdkr^tj|jt	d}nZtj|jt	d}t
t| jD ]:}|| | j| d | j| d   | j| d  ||< q|||d g|d gd g }g }|jD ]}	| jdkrtj|	jt	d}
n\tj|	jt	d}
t
t| jD ]<}|
| | j| d | j| d   | j| d  |
|< q ||
d  ||
d  ||d  ||d  q||| q<q0q&| jdkr|dd	g |dd	g nT|| jd d d
 | jd d d
 g || jd d d
 | jd d d
 g |  n| jdkrD| }|jddd}| jD ]}|D ]}| D ]}g }g }g }||jd  ||jd  ||jd  |jD ]f}	||	jd  ||	jd  ||	jd  ||jd  ||jd  ||jd  q|j|||dd qnqdq\|  ntd dS )z
             Here, C is the LIST of simplexes S in the
             2- or 3-D complex

             To plot a single simplex S in a set C, use e.g., [C[0]]
        r   )pyplotrF   Nr   r   og{Gzg)\(?g{Gz?   o   Z3d)Z
projectionZsimplex)labelz/dimension higher than 3 or wrong complex format)Z
matplotlibr^   r   Zfigurer	   r   r   rA   r2   floatr5   rN   ZplotrG   r   ZylimZxlimshowZadd_subplotr*   )r   r^   CrW   r0   x_ar:   ZxlinesZylinesZvnZxn_aZfigZaxr2   yzr   r   r   plot_complexm  s    



""



zComplex.plot_complex)r   FNNr   )FF)F)__name__
__module____qualname__r   r   r   r)   r'   r   rJ   r   rU   rX   rQ   rV   ri   r   r   r   r   r      s"       
*
$# 
7Ar   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )VertexGroupc                 C   s"   || _ || _d | _d | _g | _d S r   )p_genp_hgrrR   hg_dre   )r   rn   ro   r   r   r   r     s
    zVertexGroup.__init__c                 C   s   | j S r   )re   r   r   r   r   r     s    zVertexGroup.__call__c                 C   s   || j kr| j | d S r   )re   r   )r   r   r   r   r   r&     s    
zVertexGroup.add_vertexc                 C   s&   | j dkr tdd | jD | _ | j S )zF
        Returns the homology group order of the current cell
        Nc                 s   s   | ]}|  rd V  qdS )r   N)	minimiserrK   r   r   r   	<genexpr>  s      z2VertexGroup.homology_group_rank.<locals>.<genexpr>)rR   sumre   r   r   r   r   r     s    
zVertexGroup.homology_group_rankc                 C   s   | j dkr| j| j | _| jS )zt
        Returns the difference between the current homology group of the
        cell and its parent group
        N)rp   rR   ro   Zhgdr   r   r   r   homology_group_differential  s    
z'VertexGroup.homology_group_differentialc                 C   s   dS )z
        Returns the number of stationary points theoretically contained in the
        cell based information currently known about the cell
        Nr   r   r   r   r   polytopial_sperner_lemma  s    z$VertexGroup.polytopial_sperner_lemmac                 C   s   |  D ]}|   qdS )z3
        Print the current cell to console
        N)r+   rE   r   r   r   r+     s    
zVertexGroup.print_outN)
rj   rk   rl   r   r   r&   r   rt   ru   r+   r   r   r   r   rm     s   	
rm   c                       s    e Zd ZdZ fddZ  ZS )r(   zR
    Contains a cell that is symmetric to the initial hypercube triangulation
    c                    s$   t  || || _|| _d | _d S r   )superr   r"   r#   rB   )r   rn   ro   r"   r#   	__class__r   r   r      s    zCell.__init__rj   rk   rl   __doc__r   __classcell__r   r   rw   r   r(     s   r(   c                       s    e Zd ZdZ fddZ  ZS )r%   zq
    Contains a simplex that is symmetric to the initial symmetry constrained
    hypersimplex triangulation
    c                    s$   t  || |d |d  | _d S r?   )rv   r   r   )r   rn   ro   r   r   rw   r   r   r     s    zSimplex.__init__ry   r   r   rw   r   r%   	  s   r%   c                   @   s>   e Zd ZdddZdd Zdd Zd	d
 Zdd Zdd ZdS )VertexNr   c	                 C   s   || _ t|| _tj|td}	|d k	rTt|D ]$\}
\}}|	|
 ||  | |	|
< q.|	| _|d k	rd| _|d k	rt	||D ].\}}|| jf| dk rztj
| _d| _ qqz| jr||	f| | _|d k	r|| _nt | _d | _d| _|d k	r|| _d S )Nr   Tg        F)r2   rs   orderr   rA   rc   rP   rf   feasiblezipinffrG   setZfval	check_minrH   )r   r2   r   r   r   r   g_cons_argsrG   rH   rf   r:   ZlbZubgargsr   r   r   r     s0    
zVertex.__init__c                 C   s
   t | jS r   )hashr2   r   r   r   r   __hash__;  s    zVertex.__hash__c                 C   sN   || k	rJ|| j krJ| j | |j |  |  r>d|_d|_d| _d|_d S )NFT)rG   addrq   _minr   rE   r   r   r   r7   >  s    zVertex.connectc                 C   s2   || j kr.| j | |j |  d| _d|_d S )NT)rG   remover   rE   r   r   r   rC   K  s
    
zVertex.disconnectc                    s,    j r&t fdd jD  _d _  jS )zACheck whether this vertex is strictly less than all its neighborsc                 3   s   | ]} j |j k V  qd S r   )r   rK   r   r   r   rr   U  s     z#Vertex.minimiser.<locals>.<genexpr>F)r   allrG   r   r   r   r   r   rq   R  s    zVertex.minimiserc                 C   sL   t d| j d}| jD ]}|d|j7 }qt | t d| j d S )Nz
Vertex: {}zConnections: z{} z
Order = {})r*   rY   r2   rG   r}   )r   Zconstrvcr   r   r   r+   Z  s    
zVertex.print_out)NNr   Nr   NN)	rj   rk   rl   r   r   r7   rC   rq   r+   r   r   r   r   r|     s         
%r|   c                   @   s    e Zd ZdddZd	ddZdS )
r
   r   NTc                 C   s>   i | _ || _|| _|| _|| _|| _d| _d| _|r:d| _d S )Nr   rM   )	cacher   r   r   r   r   nfevsizerH   )r   r   r   r   r   r   indexedr   r   r   r   e  s    zVertexCache.__init__c                 C   s   z| j | W S  tk
r   |rR|  jd7  _t|| j| j| j| j| j| jd}nt|| j| j| j| j| jd}|| j |< | jd k	r| jd k	r|j	r|  j
d7  _
|  jd7  _q|  jd7  _n|  j
d7  _
|  jd7  _| j |  Y S X d S )Nr   )r   r   r   r   r   rH   )r   r   r   r   r   )r   KeyErrorrH   r|   r   r   r   r   r   r~   r   r   )r   r2   r   Zxvalr   r   r   __getitem__t  s8      


zVertexCache.__getitem__)r   NNr   T)T)rj   rk   rl   r   r   r   r   r   r   r
   d  s
       
r
   )	numpyr   r6   r   rm   r(   r%   r|   r
   r   r   r   r   <module>   s      D5O