ó
BªVCc           @   sH   d  Z  d d l Z d d l Z d d l m Z d e f d „  ƒ  YZ d S(   sò   Implement Roulette Wheel selection on a population.

This implements Roulette Wheel selection in which individuals are
selected from a population randomly, with their proportion of selection
based on their relative fitness in the population.
iÿÿÿÿN(   t   AbstractSelectiont   RouletteWheelSelectionc           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   sç   Roulette wheel selection proportional to individuals fitness.

    The implements a roulette wheel selector that selects individuals
    from the population, and performs mutation and crossover on
    the selected individuals.
    c         C   s   t  j |  | | | ƒ d S(   sØ  Initialize the selector.

        Arguments:

        o mutator -- A Mutation object which will perform mutation
        on an individual.

        o crossover -- A Crossover object which will take two
        individuals and produce two new individuals which may
        have had crossover occur.

        o repairer -- A class which can do repair on rearranged genomes
        to eliminate infeasible individuals. If set at None, so repair
        will be done.
        N(   R    t   __init__(   t   selft   mutatort	   crossovert   repairer(    (    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/RouletteWheel.pyR      s    c         C   s6  |  j  | ƒ } | j ƒ  } | j ƒ  g  } xt t | ƒ d ƒ D]ì } t j ƒ  } t j ƒ  } d } d }	 d }
 x^ | D]V } | |
 k r¤ | | k r¤ | | } n  | |
 k rÉ | | k rÉ | | }	 n  | }
 qy W| d k	 së t d ƒ ‚ |	 d k	 st d ƒ ‚ |  j | |	 ƒ \ } } | j	 | | g ƒ qB W| S(   s  Perform selection on the population based using a Roulette 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.
        i   i    s   Didn't select organism ones   Didn't select organism twoN(
   t   _set_up_wheelt   keyst   sortt   ranget   lent   randomt   Nonet   AssertionErrort   mutate_and_crossovert   extend(   R   t
   populationt
   prob_wheelt   probst   new_populationt	   pair_spint   choice_num_1t   choice_num_2t   chosen_org_1t   chosen_org_2t	   prev_probt   cur_probt	   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/RouletteWheel.pyt   select'   s,    

	c         C   s   d } x | D] } | | j  7} q Wi  } d } xH | D]@ } t | j  ƒ t | ƒ } t j | ƒ | | | <| | 7} q7 W| S(   sò  Set up the roulette wheel based on the fitnesses.

        This creates a fitness proportional 'wheel' that will be used for
        selecting based on random numbers.

        Returns:
        
        o A dictionary where the keys are the 'high' value that an
        individual will be selected. The low value is determined by
        the previous key in a sorted list of keys. For instance, if we
        have a sorted list of keys like:

        [.1, .3, .7, 1]

        Then the individual whose key is .1 will be selected if a number
        between 0 and .1 is chosen, the individual whose key is .3 will
        be selected if the number is between .1 and .3, and so on.

        The values of the dictionary are the organism instances.
        i    (   t   fitnesst   floatt   copy(   R   R   t   total_fitnesst   orgt
   wheel_dictt   total_percentaget   org_percentage(    (    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/RouletteWheel.pyR   U   s    N(   t   __name__t
   __module__t   __doc__R   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/RouletteWheel.pyR      s   	.(   R)   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/RouletteWheel.pyt   <module>   s   