ó
X<c           @   s<   d  Z  d d l Z d d l m Z d e f d     YZ d S(   s  Provide Tournament style selection.

This implements selection based on a tournament style. In this model of
selection, two individuals are randomly chosen from the population, and
the organism with the higher fitness is considered the 'winner' and moves
to the next generation.
iĸĸĸĸN(   t   AbstractSelectiont   TournamentSelectionc           B   s,   e  Z d  Z d d  Z d   Z d   Z RS(   s*   Implement tournament style selection.
    i   c         C   s>   t  j |  | | |  | d k  r1 t d   n  | |  _ d S(   sM  Initialize the tournament selector.

        Arguments:

        o num_competitors-- The number of individiuals that should be
        involved in a selection round. By default we just have two
        individuals (head to head!).

        See AbstractSelection for a description of the arguments to
        the initializer.
        i   s!   Must have at least 2 competitors!N(   R    t   __init__t
   ValueErrort   _num_competitors(   t   selft   mutatort	   crossovert   repairert   num_competitors(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Selection/Tournament.pyR      s    c         C   s   t  | j | j  S(   s{   Comparison function for comparing two organisms.

        This just allows us to easily sort organisms by fitness.
        (   t   cmpt   fitness(   R   t   org_1t   org_2(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Selection/Tournament.pyt   _fitness_cmp$   s    c   	      C   s  g  } xø t  |  t  |  k  r g  } x t d  D]t } g  } xD t  |  |  j k  r t j |  } | | k rC | j |  qC qC W| j |  j  | j | d  q4 Wt  |  d k sĘ t d   |  j	 | d | d  \ } } | j
 | | g  q	 W| S(   s=  Perform selection on the population using the Tournament model.

        Arguments:

        o population -- A population of organisms on which we will perform
        selection. The individuals are assumed to have fitness values which
        are due to their current genome (ie. the fitness is up to date).
        i   iĸĸĸĸs%   Expected two organisms to be selectedi    i   (   t   lent   rangeR   t   randomt   choicet   appendt   sortR   t   AssertionErrort   mutate_and_crossovert   extend(	   R   t
   populationt   new_populationt   new_orgst	   round_numt   competitorst   new_orgt	   new_org_1t	   new_org_2(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Selection/Tournament.pyt   select+   s     
(   t   __name__t
   __module__t   __doc__R   R   R    (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Selection/Tournament.pyR      s   	(   R#   R   t   AbstractR    R   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/GA/Selection/Tournament.pyt   <module>   s   