a
    J5d                  
   @   s   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	Z
ddlmZ ddlmZ de
jeee  eee ee eeeef d
ddZde
jee eee ef dddZdS )zSimple local search solver    )sample)default_timer)ListOptionalTupleN)compute_permutation_distance)neighborhood_gentwo_optF)distance_matrixx0perturbation_schememax_processing_timelog_fileverbosereturnc                 C   s   t | |\}}|ptj}|r*t|ddd}t }	d}
d}|r|
sd}tt| |D ]\}}t |	 |krd}|r~t||d |rt| d}
 q8t| |}d| d	| }|rt||d |rt| ||k rTd}|| }} q8qTq8||fS )
a  Solve a TSP problem with a local search heuristic

    Parameters
    ----------
    distance_matrix
        Distance matrix of shape (n x n) with the (i, j) entry indicating the
        distance from node i to j

    x0
        Initial permutation. If not provided, it starts with a random path

    perturbation_scheme {"ps1", "ps2", "ps3", "ps4", "ps5", "ps6", ["two_opt"]}
        Mechanism used to generate new solutions. Defaults to "two_opt"

    max_processing_time {None}
        Maximum processing time in seconds. If not provided, the method stops
        only when a local minimum is obtained

    log_file
        If not `None`, creates a log file with details about the whole
        execution

    verbose
        If true, prints algorithm status every iteration

    Returns
    -------
    A permutation of nodes from 0 to n - 1 that produces the least total
    distance obtained (not necessarily optimal).

    The total distance the returned permutation produces.

    Notes
    -----
    Here are the steps of the algorithm:
        1. Let `x`, `fx` be a initial solution permutation and its objective
        value;
        2. Perform a neighborhood search in `x`:
            2.1 For each `x'` neighbor of `x`, if `fx'` < `fx`, set `x` <- `x'`
            and stop;
        3. Repeat step 2 until all neighbors of `x` are tried and there is no
        improvement. Return `x`, `fx` as solution.
    wzutf-8)encodingFTz/WARNING: Stopping early due to time constraints)filezCurrent value: z; Neighbor: )	setupnpinfopenr   	enumerater   printr   )r
   r   r   r   r   r   xZfxZlog_file_handlerZticZ
stop_earlyZimprovementZn_indexZxnZwarning_msgfnmsg r   ^/var/www/html/django/DPS/env/lib/python3.9/site-packages/python_tsp/heuristics/local_search.pysolve_tsp_local_search   s:    3


r   )r
   r   r   c                 C   s:   |s(| j d }dgttd||d  }t| |}||fS )a  Return initial solution and its objective value

    Parameters
    ----------
    distance_matrix
        Distance matrix of shape (n x n) with the (i, j) entry indicating the
        distance from node i to j

    x0
        Permutation of nodes from 0 to n - 1 indicating the starting solution.
        If not provided, a random list is created.

    Returns
    -------
    x0
        Permutation with initial solution. If ``x0`` was provided, it is the
        same list

    fx0
        Objective value of x0
    r      )shaper   ranger   )r
   r   nZfx0r   r   r   r   c   s
    

r   )Nr	   NNF)N)__doc__randomr   Ztimeitr   typingr   r   r   numpyr   Zpython_tsp.utilsr   Z*python_tsp.heuristics.perturbation_schemesr   Zndarrayintstrfloatboolr   r   r   r   r   r   <module>   s2        

X 
