ó
ŸÙrZc           @  sˆ   d  d l  m Z m Z d  d l m Z d  d l m Z d e ƒ  k rN e Z	 n  d „  Z
 d „  Z d „  Z d „  Z d	 e d d
 „ Z d S(   iÿÿÿÿ(   t   print_functiont   division(   t   islice(   t   arrayt   xrangec           sÅ  t  | ƒ } t | | ƒ ‰ ‡  ‡ f d †  } d } d } xt | d ƒ D]m} | d } xZt | d | d ƒ D]A}	 |	 d }
 | | | ƒ | |	 |
 ƒ | | |	 ƒ | | |
 ƒ } | d k rr | | 7} | d 7} | ˆ | j ˆ | ƒ | ˆ | j ˆ |	 ƒ | ˆ | j ˆ | ƒ | ˆ | j ˆ |
 ƒ | ˆ |	 j ˆ |
 ƒ | ˆ |	 j ˆ | ƒ | ˆ |
 j ˆ |	 ƒ | ˆ |
 j ˆ | ƒ t | | ƒ ˆ (qr qr WqJ W| | f S(   s9   Tries to optimize solution, found by the greedy algorithmc           s<   ˆ |  } ˆ | } | | k  r, ˆ  | | Sˆ  | | Sd  S(   N(    (   t   it   jt   pit   pj(   t	   distancest   path(    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   ds   s
    

g        i    i   i   (   t   lent   restore_pathR   t   removet   append(   R	   t   connectionst	   endpointst   NR   t   d_totalt   optimizationst   at   bt   ct   dt   delta_d(    (   R	   R
   sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   optimize_solution   s.    

6

c         C  sÕ   | d k rL g  t |  ƒ D]$ \ } } t | ƒ d k r | ^ q \ } } n | \ } } | g } d } | } xa t rÐ g  |  | D] }	 |	 | k r |	 ^ q }
 |
 s© Pn  |
 d } | j | ƒ | | } } qp W| S(   sá   Takes array of connections and returns a path.
    Connections is array of lists with 1 or 2 elements.
    These elements are indices of teh vertices, connected to this vertex
    Guarantees that first index < last index
    i   i    N(   t   Nonet	   enumerateR   t   TrueR   (   R   R   t   idxt   connt   startt   endR
   t
   prev_pointt	   cur_pointt   pntt   next_pointst
   next_point(    (    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyR   3   s"    '		 
c         C  sN   xG t  |  ƒ D]9 \ } } t | ƒ | k  r t d j t ƒ    ƒ ‚ q q Wd S(   s4   Ensure that matrix is left-triangular at least.
    sX   Distance matrix must be left-triangular at least. Row {row} must have at least {i} itemsN(   R   R   t
   ValueErrort   formatt   locals(   R	   R   t   row(    (    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   _assert_triangularM   s     c           sd   g  } x> t  |  ƒ D]0 } x' t  | ƒ D] } | j | | f ƒ q& Wq W| j d ‡  f d †  ƒ | S(   sL   returns list of coordinate pairs (i,j), sorted by distances; such that i < jt   keyc           s   ˆ  |  d |  d S(   Ni    i   (    (   t   ij(   R	   (    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   <lambda>\   s    (   R   R   t   sort(   R   R	   t   indicesR   R   (    (   R	   sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   pairs_by_distT   s    i   c   	        s8  t  |  ƒ ‰  ˆ  d k r g  Sˆ  d k r/ d g St |  ƒ t d d g ƒ ˆ  ‰ ˆ d k	 r™ ˆ \ ‰ ‰ ˆ ˆ k r‚ t d ƒ ‚ n  d ˆ ˆ <d ˆ ˆ <n  g  t ˆ  ƒ D] } g  ^ q¦ ‰ ‡  ‡ ‡ ‡ ‡ ‡ f d †  } | | ˆ  |  ƒ ƒ x< t | ƒ D]. } t |  ˆ ˆ ƒ \ } } | d k rö Pqö qö Wt ˆ d ˆ ƒS(	   sö  Given a distance matrix, finds a solution for the TSP problem.
    Returns list of vertex indices. 
    Guarantees that the first index is lower than the last

    :arg: distances : left-triangular matrix of distances. array of arrays
    :arg: optim_steps (int) number of additional optimization steps, allows to improve solution but costly.
    :arg: pairs_by_dist (function) an implementtion of the pairs_by_dist function. for optimization purposes.
    :arg: endpoinds : None or pair (int,int)
    i    i   R   i   s   start=end is not supportedc           s×   g  t  ˆ ƒ D] } | g ^ q ‰  ‡ ‡  ‡ f d †  } ‡ ‡ ‡  f d †  } ‡ ‡  ‡ f d †  } ˆ d } xe | ƒ  D]Z \ } } ˆ r¨ | d k r¨ | | | ƒ r¨ qu n  | | | ƒ | d 8} | d k ru Pqu qu Wd  S(   Nc          3  sQ   xJ ˆ D]B }  |  \ } } ˆ  | r ˆ  | r ˆ | ˆ | k	 r |  Vq q Wd  S(   N(    (   R-   R   R   (   t   node_valencyt   segmentst   sorted_pairs(    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   possible_edges   s
    c           s·   ˆ |  c d 8<ˆ | c d 8<ˆ  |  j  | ƒ ˆ  | j  |  ƒ ˆ |  } ˆ | } t | ƒ t | ƒ k r‹ | | } } | |  }  } n  x | D] } | ˆ | <q’ W| j | ƒ d  S(   Ni   (   R   R   t   extend(   R   R   t   seg_it   seg_jt   node_idx(   R   R2   R3   (    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   connect_verticesŠ   s    

c           sX   ˆ |  ˆ | } } ˆ ˆ ˆ ˆ  } } | | k rB | | k pW | | k oW | | k S(   N(    (   R   R   t   sit   sjt   sst   se(   R!   R3   R    (    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   edge_connects_endpoint_segments™   s    i   i    (   R   (   R4   R   R5   R:   R?   t
   edges_leftR   (   R   R   R!   R   R2   R    (   R3   R4   sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   join_segments{   s    "
!
R   N(	   R   R+   t   pyarrayR   R'   R   t   rangeR   R   (	   R	   t   optim_stepsR1   R   R   RA   t   passnt   noptt   dtotal(    (   R   R   R!   R   R2   R    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt	   solve_tsp_   s*    
  
 
5N(   t
   __future__R    R   t	   itertoolsR   R   RB   t   globalsRC   R   R   R   R+   R1   R   RH   (    (    (    sI   /home/guillaumes/Documents/class/ing2/s4/ml/opti/tsp/tsp_solver/greedy.pyt   <module>   s   		#			