a
    ¬J5dz  ã                   @   s|  U d Z ddlmZ ddlmZmZmZmZ ee eee ee df dœdd„Z	ee eee ee df dœdd	„Z
ee eee ee df dœd
d„Zee eee ee df dœdd„Zee eee ee df dœdd„Zee eee ee df dœdd„Zee eee ee df dœdd„Ze	e
eeeeedœZeeeee geee ee df f f ed< dS )az  Perturbation schemes used in local search-based algorithms.
The functions here should receive a permutation list with numbers from 0 to `n`
and return a generator with all neighbors of this permutation.
The neighbors should preferably be randomized to be used in Simulated
Annealing, which samples a single neighbor at a time.

References
----------
.. [1] Goulart, Fillipe, et al. "Permutation-based optimization for the load
    restoration problem with improved time estimation of maneuvers."
    International Journal of Electrical Power & Energy Systems 101 (2018):
    339-355.
.. [2] 2-opt: https://en.wikipedia.org/wiki/2-opt
é    )Úsample)ÚCallableÚDictÚ	GeneratorÚListN)ÚxÚreturnc                 c   s^   t | ƒ}td|d ƒ}t|t |ƒƒD ]4}|  ¡ }||d  ||  ||< ||d < |V  q$dS )zbPS1 perturbation scheme: Swap two adjacent terms [1]
    This scheme has at most n - 1 swaps.
    é   N©ÚlenÚranger   Úcopy)r   ÚnÚi_rangeÚiÚxn© r   úf/var/www/html/django/DPS/env/lib/python3.9/site-packages/python_tsp/heuristics/perturbation_schemes.pyÚps1_gen   s    "r   c                 c   sx   t | ƒ}td|d ƒ}t|t |ƒƒD ]N}t|d |ƒ}t|t |ƒƒD ],}|  ¡ }|| ||  ||< ||< |V  qDq$dS )zbPS2 perturbation scheme: Swap any two elements [1]
    This scheme has n * (n - 1) / 2 swaps.
    r	   Nr
   ©r   r   r   r   Új_rangeÚjr   r   r   r   Úps2_gen    s    r   c                 #   sz   t | ƒ}td|ƒ}t|t |ƒƒD ]T‰ ‡ fdd„td|ƒD ƒ}t|t |ƒƒD ](}|  ¡ }| ˆ ¡}| ||¡ |V  qJq dS )z_PS3 perturbation scheme: A single term is moved [1]
    This scheme has n * (n - 1) swaps.
    r	   c                    s   g | ]}|ˆ kr|‘qS r   r   )Ú.0r   ©r   r   r   Ú
<listcomp>7   ó    zps3_gen.<locals>.<listcomp>N)r   r   r   r   ÚpopÚinsert)r   r   r   r   r   r   Únoder   r   r   Úps3_gen/   s    

r    c                 #   sú   t | ƒ}td|ƒ}t|t |ƒƒD ]Ô‰ tˆ d |d ƒ}t|t |ƒƒD ]®‰‡ ‡fdd„td|d ƒD ƒ}t|t |ƒƒD ]|}|  ¡ }|ˆ k rº|d|… |ˆ ˆ…  ||ˆ …  |ˆd…  }n0|dˆ … |ˆ|…  |ˆ ˆ…  ||d…  }|V  qtqDq dS )z3PS4 perturbation scheme: A subsequence is moved [1]r	   c                    s"   g | ]}|t ˆ ˆd  ƒvr|‘qS ©r	   ©r   ©r   Úk©r   r   r   r   r   G   r   zps4_gen.<locals>.<listcomp>Nr
   ©r   r   r   r   Zk_ranger$   r   r   r%   r   Úps4_gen?   s    
20r'   c                 c   sŠ   t | ƒ}td|ƒ}t|t |ƒƒD ]d}t|d |d ƒ}t|t |ƒƒD ]>}|  ¡ }|d|… tt|||… ƒƒ ||d…  }|V  qDq dS )z6PS5 perturbation scheme: A subsequence is reversed [1]r	   é   N©r   r   r   r   ÚlistÚreversedr   r   r   r   Úps5_genQ   s    
,r,   c              	   #   s
  t | ƒ}td|ƒ}t|t |ƒƒD ]ä‰ tˆ d |d ƒ}t|t |ƒƒD ]¾‰‡ ‡fdd„td|d ƒD ƒ}t|t |ƒƒD ]Œ}|  ¡ }|ˆ k rÂ|d|… tt|ˆ ˆ… ƒƒ ||ˆ …  |ˆd…  }n8|dˆ … |ˆ|…  tt|ˆ ˆ… ƒƒ ||d…  }|V  qtqDq dS )z@PS6 perturbation scheme: A subsequence is reversed and moved [1]r	   c                    s"   g | ]}|t ˆ ˆd  ƒvr|‘qS r!   r"   r#   r%   r   r   r   f   r   zps6_gen.<locals>.<listcomp>Nr)   r&   r   r%   r   Úps6_gen^   s    
:8r-   c                 c   s’   t | ƒ}td|ƒ}t|t |ƒƒD ]l}t|d |d ƒ}t|t |ƒƒD ]F}|  ¡ }|d|d … tt||d |… ƒƒ ||d…  }|V  qDq dS )z2-opt perturbation scheme [2]r(   r	   Nr)   r   r   r   r   Útwo_opt_genp   s    
4r.   )Úps1Zps2Zps3Zps4Zps5Zps6Ztwo_optÚneighborhood_gen)Ú__doc__Úrandomr   Útypingr   r   r   r   Úintr   r   r    r'   r,   r-   r.   r0   ÚstrÚ__annotations__r   r   r   r   Ú<module>   s(   &&&&&&&ùþ&ÿ