ó
X<c           @   sh   d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z d e f d     YZ	 d S(   sž   Select individuals into a new population trying to maintain diversity.

This selection mechanism seeks to try and get both high fitness levels
and high diversity levels in the population.
iĸĸĸĸN(   t
   MutableSeq(   t   AbstractSelection(   t   TournamentSelectiont   DiversitySelectionc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s  Implement diversity selection.

    Diversity selection is performed by trying to select individuals
    from the population that aren't already in the new_population. A group
    of selected individuals is then subjected to selection using
    a passed selection routine.

    If new individuals can not be selected, new individuals will be
    randomly generated and inserted into the population.
    c         C   s(   | |  _  | |  _ d |  _ d |  _ d S(   s  Initialize a diversity selector.

        Arguments:

        o internal_selector - A selection object that will be used to select
        individuals based on fitness, perform crossover, mutation and repair.

        o genome_generator - A function that, when called, will return a
        genome to be used for a new organism. The genome returned must
        be a MutableSeq() object.
        gđ?i
   N(   t   _internal_selectort   _genome_generatort   sub_pop_percentt   random_tries(   t   selft   internal_selectort   genome_generator(    (    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/Diversity.pyt   __init__   s    			c         C   s   d } d } xM | d k r[ | |  j k  r[ t j |  } | | k rN | } n  | d 7} q W| d k r | d j   } |  j   } | | _ | j   n  | S(   s5  Get a new organism from old_pop that isn't in new_pop.

        This attempts to select an organism from old_pop that isn't in
        new_pop. If we can't do this in the number of tries specified
        by the class attribute random_tries, we generate a new random
        organism and return that.
        i    i   N(   t   NoneR   t   randomt   choicet   copyR   t   genomet   recalculate_fitness(   R   t   new_popt   old_popt   new_orgt	   num_triest
   chance_orgt   random_genome(    (    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/Diversity.pyt   _get_new_organism.   s    			c         C   s°   g  } x t  |  t  |  k  rĄ t t j t  |  |  j   } g  } x3 t |  D]% } |  j | |  } | j |  qV W|  j j	 |  } | j
 |  q	 W| t  |   S(   sL   Perform selection on the current population, encouraging diversity.
        (   t   lent   intt   matht   ceilR   t   rangeR   t   appendR   t   selectt   extend(   R   t
   populationt   new_populationt   sub_pop_sizet   sub_popt
   individualR   t   new_sub_pop(    (    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/Diversity.pyR   J   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/Diversity.pyR      s   
		(
   R)   R   R   t   Bio.SeqR    t   AbstractR   t
   TournamentR   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/Diversity.pyt   <module>   s   