ó
"ˆIc           @   s{  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 d l	 m
 Z
 d d l m Z d d l m Z d	 e f d
 „  ƒ  YZ d „  Z d d  d „  ƒ  YZ d d! d „  ƒ  YZ d d" d „  ƒ  YZ d d# d „  ƒ  YZ d „  Z d „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ e d k rwe j d d ƒ Z e j d e ƒ n  d S($   sI   Tests for Genetic Algorithm classes that provide selection capabilities.
iÿÿÿÿN(   t
   MutableSeq(   t   SingleLetterAlphabet(   t   Organism(   t   DiversitySelection(   t   TournamentSelection(   t   RouletteWheelSelectiont   TestAlphabetc           B   s    e  Z d  Z d d d d g Z RS(   s   Simple test alphabet.
    t   0t   1t   2t   3(   t   __name__t
   __module__t   __doc__t   letters(    (    (    s   test_GASelection.pyR      s   c         C   s   |  j  ƒ  } t | j ƒ S(   s,   Simple class for calculating fitnesses.
    (   t   toseqt   intt   data(   t   genomet
   genome_seq(    (    s   test_GASelection.pyt   test_fitness   s    t   NoSelectionc           B   s   e  Z d  Z d „  Z RS(   sK   A simple 'selection' class that just returns the generated population.
    c         C   s   | S(   N(    (   t   selft
   population(    (    s   test_GASelection.pyt   select#   s    (   R   R   R   R   (    (    (    s   test_GASelection.pyR       s   t
   NoMutationc           B   s   e  Z d  Z d „  Z RS(   s6   Simple 'mutation' class that doesn't do anything.
    c         C   s
   | j  ƒ  S(   N(   t   copy(   R   t   org(    (    s   test_GASelection.pyt   mutate)   s    (   R   R   R   R   (    (    (    s   test_GASelection.pyR   &   s   t   NoCrossoverc           B   s   e  Z d  Z d „  Z RS(   s7   Simple 'crossover' class that doesn't do anything.
    c         C   s   | j  ƒ  | j  ƒ  f S(   N(   R   (   R   t   org_1t   org_2(    (    s   test_GASelection.pyt   do_crossover/   s    (   R   R   R   R    (    (    (    s   test_GASelection.pyR   ,   s   t   NoRepairc           B   s   e  Z d  Z d „  Z RS(   s4   Simple 'repair' class that doesn't do anything.
    c         C   s
   | j  ƒ  S(   N(   R   (   R   R   (    (    s   test_GASelection.pyt   repair5   s    (   R   R   R   R"   (    (    (    s   test_GASelection.pyR!   2   s   c          C   sI   t  ƒ  }  d } x* t d ƒ D] } | t j |  j ƒ 7} q Wt | |  ƒ S(   s#   Return a random genome string.
    t    i   (   R   t   ranget   randomt   choiceR   R    (   t   alphabett
   new_genomet   letter(    (    s   test_GASelection.pyt   random_genome8   s
    	c          C   s   t  ƒ  }  t |  t ƒ S(   s    Generate a random organism.
    (   R*   R   R   (   R   (    (    s   test_GASelection.pyt   random_organismC   s    	t   DiversitySelectionTestc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s1   Test selection trying to maximize diversity.
    c         C   s   t  t ƒ  t ƒ |  _ d  S(   N(   R   R   R*   t   selector(   R   (    (    s   test_GASelection.pyt   setUpN   s    c         C   sM   t  ƒ  } | g } g  } |  j j | | ƒ } | | k sI t d | ƒ ‚ d S(   s:   Getting a new organism not in the new population.
        s   Got an unexpected organism %sN(   R+   R-   t   _get_new_organismt   AssertionError(   R   R   t   old_popt   new_popt   new_org(    (    s   test_GASelection.pyt   test_get_new_organismQ   s
    		c         C   s4   t  ƒ  } | g } | g } |  j j | | ƒ } d S(   sD   Test not getting an organism already in the new population.
        N(   R+   R-   R/   (   R   R   R1   R2   R3   (    (    s   test_GASelection.pyt   test_no_retrieve_organism[   s    			c         C   s\   g  t  d ƒ D] } t ƒ  ^ q } |  j j | ƒ } t | ƒ t | ƒ k sX t d ƒ ‚ d S(   s4   Test basic selection on a small population.
        i2   s!   Did not maintain population size.N(   R$   R+   R-   R   t   lenR0   (   R   t   org_numt   popR2   (    (    s   test_GASelection.pyt   test_selectione   s    "(   R   R   R   R.   R4   R5   R9   (    (    (    s   test_GASelection.pyR,   K   s
   		
	
t   TournamentSelectionTestc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s7   Test selection based on a tournament style scheme.
    c         C   s%   t  t ƒ  t ƒ  t ƒ  d ƒ |  _ d  S(   Ni   (   R   R   R   R!   R-   (   R   (    (    s   test_GASelection.pyR.   q   s    c         C   s  t  ƒ  } x) t r4 t  ƒ  } | j | j k r Pq q W| j | j k rW | | } } n  | j | j k so t ‚ | | g } |  j j | ƒ } x& | D] } | | k s” t d ƒ ‚ q” W| | g } |  j j | ƒ } x& | D] } | | k sÛ t d ƒ ‚ qÛ Wd S(   sD   Ensure selection of the best organism in a population of 2.
        s   Got a worse organism selected.N(   R+   t   Truet   fitnessR0   R-   R   (   R   R   R   R8   R2   R   (    (    s   test_GASelection.pyt   test_select_bestu   s     			c         C   s\   g  t  d ƒ D] } t ƒ  ^ q } |  j j | ƒ } t | ƒ t | ƒ k sX t d ƒ ‚ d S(   s4   Test basic selection on a small population.
        i2   s!   Did not maintain population size.N(   R$   R+   R-   R   R6   R0   (   R   R7   R8   R2   (    (    s   test_GASelection.pyR9      s    "(   R   R   R   R.   R=   R9   (    (    (    s   test_GASelection.pyR:   n   s   		t   RouletteWheelSelectionTestc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s<   Test selection using a roulette wheel selection scheme.
    c         C   s"   t  t ƒ  t ƒ  t ƒ  ƒ |  _ d  S(   N(   R   R   R   R!   R-   (   R   (    (    s   test_GASelection.pyR.   ›   s    c         C   s‡   t  d t ƒ  ƒ } t | t ƒ } t  d t ƒ  ƒ } t | t ƒ } |  j j | | g ƒ } x& | D] } | | k sa t d ƒ ‚ qa Wd S(   sB   Ensure selection of a best organism in a population of 2.
        R   R   s#   Worse organism unexpectly selected.N(   R    R   R   R   R-   R   R0   (   R   t   worst_genomet	   worst_orgt   better_genomet
   better_orgR2   R   (    (    s   test_GASelection.pyR=   Ÿ   s    c         C   s\   g  t  d ƒ D] } t ƒ  ^ q } |  j j | ƒ } t | ƒ t | ƒ k sX t d ƒ ‚ d S(   s4   Test basic selection on a small population.
        i2   s!   Did not maintain population size.N(   R$   R+   R-   R   R6   R0   (   R   R7   R8   R2   (    (    s   test_GASelection.pyR9   ¬   s    "(   R   R   R   R.   R=   R9   (    (    (    s   test_GASelection.pyR>   ˜   s   		t   __main__t	   verbosityi   t
   testRunner(    (    (    (    (   R   R%   t   unittestt   Bio.SeqR    t   Bio.AlphabetR   t   Bio.GA.OrganismR   t   Bio.GA.Selection.DiversityR   t   Bio.GA.Selection.TournamentR   t   Bio.GA.Selection.RouletteWheelR   R   R   R   R   R   R!   R*   R+   t   TestCaseR,   R:   R>   R   t   TextTestRunnert   runnert   main(    (    (    s   test_GASelection.pyt   <module>   s,   			#*