ó
"ˆIc           @   sŸ  d  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 d	 l m Z d d
 l m Z d d l m Z d e f d „  ƒ  YZ d „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d d! d „  ƒ  YZ d e j f d „  ƒ  YZ e d k r›e j d d ƒ Z e j  d  e ƒ n  d S("   s5   Tests different Genetic Algorithm crossover classes.
iÿÿÿÿN(   t
   MutableSeq(   t   SingleLetterAlphabet(   t   Organism(   t   SafeFitnessCrossover(   t   GeneralPointCrossover(   t   InterleaveCrossover(   t   TwoPointCrossover(   t   SinglePointCrossover(   t   UniformCrossovert   TestAlphabetc           B   s&   e  Z d  Z d d d g Z d „  Z RS(   s   Simple test alphabet.
    t   1t   2t   3c         C   s   d S(   Ni   (    (   t   selft   oalpha(    (    s   test_GACrossover.pyt   contains   s    (   t   __name__t
   __module__t   __doc__t   lettersR   (    (    (    s   test_GACrossover.pyR	      s   c         C   s   |  j  ƒ  } t | j ƒ S(   s,   Simple class for calculating fitnesses.
    (   t   toseqt   intt   data(   t   genomet
   seq_genome(    (    s   test_GACrossover.pyt   test_fitness   s    t   SinglePointTestc           B   s    e  Z d  Z d „  Z d „  Z RS(   s"   Test simple point crossovers.
    c         C   sg   t  ƒ  |  _ t d |  j ƒ } t | t ƒ |  _ t d |  j ƒ } t | t ƒ |  _ t d ƒ |  _ d  S(   Nt   11111t   22222g      ð?(	   R	   t   alphabetR    R   R   t   org_1t   org_2R   t	   crossover(   R   t   genome_1t   genome_2(    (    s   test_GACrossover.pyt   setUp%   s    c         C   s“   |  j  j } |  j j } |  j j |  j  |  j ƒ \ } } | j | k rY | j | k se t d ƒ ‚ | |  j  k rƒ | |  j k s t d ƒ ‚ d S(   s2   Test basic point crossover functionality.
        s*   Did not perform a crossover when expected.s0   Returned an exact copy of the original organism.N(   R   R   R   R    t   do_crossovert   AssertionError(   R   t   start_genome_1t   start_genome_2t	   new_org_1t	   new_org_2(    (    s   test_GACrossover.pyt   test_basic_crossover/   s    	!(   R   R   R   R#   R*   (    (    (    s   test_GACrossover.pyR   "   s   	
t   UniformTestc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s"   Test simple point crossovers.
    c         C   sŽ   t  ƒ  |  _ t d |  j ƒ } t | t ƒ |  _ t d |  j ƒ } t | t ƒ |  _ t d |  j ƒ } t | t ƒ |  _ t d d ƒ |  _	 d  S(   Nt   11111111t   22222222t   333g      ð?gš™™™™™é?(
   R	   R   R    R   R   R   R   t   org_3R   R    (   R   R!   R"   t   genome_3(    (    s   test_GACrossover.pyR#   B   s    c         C   s“   |  j  j } |  j j } |  j j |  j  |  j ƒ \ } } | j | k rY | j | k se t d ƒ ‚ | |  j  k rƒ | |  j k s t d ƒ ‚ d S(   s4   Test basic uniform crossover functionality.
        s*   Did not perform a crossover when expected.s0   Returned an exact copy of the original organism.N(   R   R   R   R    R$   R%   (   R   R&   R'   R(   R)   (    (    s   test_GACrossover.pyR*   O   s    	!	c         C   sÆ   |  j  j |  j |  j ƒ \ } } t | j ƒ t | j ƒ k sK t d ƒ ‚ | j j ƒ  j d ƒ | j j ƒ  j d ƒ k s‡ t d ƒ ‚ |  j j t | j ƒ | j t | j ƒ k sÂ t d ƒ ‚ d S(   sA   Test properties of differing genome length, uniform crossovers.
	s3   Strings are of wrong sizes after uniform crossover.R
   R   s9   There should be equal distributions of the smaller strings;   Uniform should not touch non-overlapping elements of genomeN(	   R    R$   R   R/   t   lenR   R%   t   tostringt   count(   R   R(   R)   (    (    s   test_GACrossover.pyt   test_ds_prop_uniform_crossover`   s    !			c         C   s»   |  j  j |  j |  j ƒ \ } } t | j ƒ t | j ƒ k sK t d ƒ ‚ | j j ƒ  j d ƒ | j j ƒ  j d ƒ k r« | j j ƒ  j d ƒ | j j ƒ  j d ƒ k s· t d ƒ ‚ d S(   s=   Test properties of equal genome length, uniform crossovers.
	s9   Strings are of different sizes after symmetric crossover.R
   R   s,   There should be equal, inverse distributionsN(	   R    R$   R   R   R1   R   R%   R2   R3   (   R   R(   R)   (    (    s   test_GACrossover.pyt   test_ss_prop_uniform_crossoverr   s    !		(   R   R   R   R#   R*   R4   R5   (    (    (    s   test_GACrossover.pyR+   ?   s
   			t   InterleaveTestc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s&   Test 'simple' 4-point crossovers.
    c         C   s‹   t  ƒ  |  _ t d |  j ƒ } t | t ƒ |  _ t d |  j ƒ } t | t ƒ |  _ t d |  j ƒ } t | t ƒ |  _ t d ƒ |  _	 d  S(   NR   R   t	   333333333g      ð?(
   R	   R   R    R   R   R   R   R/   R   t
   _crossover(   R   R!   R"   R0   (    (    s   test_GACrossover.pyR#   †   s    c         C   s“   |  j  j } |  j j } |  j j |  j  |  j ƒ \ } } | j | k rY | j | k se t d ƒ ‚ | |  j  k rƒ | |  j k s t d ƒ ‚ d S(   s7   Test basic interleave crossover functionality.
        s*   Did not perform a crossover when expected.s0   Returned an exact copy of the original organism.N(   R   R   R   R8   R$   R%   (   R   R&   R'   R(   R)   (    (    s   test_GACrossover.pyR*   “   s    	!	c         C   sñ   |  j  j |  j |  j ƒ \ } } t | j ƒ t | j ƒ k sK t d ƒ ‚ | j j ƒ  j d ƒ | j j ƒ  j d ƒ k r« | j j ƒ  j d ƒ | j j ƒ  j d ƒ k s· t d ƒ ‚ | j j ƒ  d k rá | j j ƒ  d k sí t d ƒ ‚ d S(	   s0   Test properties of interleave point crossover.
	s9   Strings are of different sizes after symmetric crossover.R
   R   s,   There should be equal, inverse distributionst   12121t   21212s   Did not interleave.N(	   R8   R$   R   R   R1   R   R%   R2   R3   (   R   R(   R)   (    (    s   test_GACrossover.pyt   test_prop_sym_crossover¤   s    !			c         C   sÉ   |  j  j } |  j j } |  j j |  j  |  j ƒ \ } } | j | k rY | j | k se t d ƒ ‚ | |  j  k rƒ | |  j k s t d ƒ ‚ | j j ƒ  d k r¹ | j j ƒ  d k sÅ t d ƒ ‚ d S(   s:   Test basic interleave crossover with asymmetric genomes.
	s*   Did not perform a crossover when expected.s0   Returned an exact copy of the original organism.t   13131t   31313333s   Did not interleave with growth.N(   R   R   R/   R8   R$   R%   R2   (   R   R&   t   start_genome_3R(   t	   new_org_3(    (    s   test_GACrossover.pyt   test_prop_asym_crossover¹   s    	!		(   R   R   R   R#   R*   R;   R@   (    (    (    s   test_GACrossover.pyR6   ƒ   s
   			t   FourPointTestc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s&   Test 'simple' 4-point crossovers.
    c         C   s|   t  ƒ  |  _ t d |  j ƒ } t | t ƒ |  _ t d |  j ƒ } t | t ƒ |  _ t d d ƒ |  _ t d d ƒ |  _	 d  S(   NR   R   i   g      ð?i   (
   R	   R   R    R   R   R   R   R   t   sym_crossovert   asym_crossover(   R   R!   R"   (    (    s   test_GACrossover.pyR#   Ñ   s    c         C   s“   |  j  j } |  j j } |  j j |  j  |  j ƒ \ } } | j | k rY | j | k se t d ƒ ‚ | |  j  k rƒ | |  j k s t d ƒ ‚ d S(   s4   Test basic 4-point crossover functionality.
        s*   Did not perform a crossover when expected.s0   Returned an exact copy of the original organism.N(   R   R   R   RB   R$   R%   (   R   R&   R'   R(   R)   (    (    s   test_GACrossover.pyR*   Ü   s    	!	c         C   s»   |  j  j |  j |  j ƒ \ } } t | j ƒ t | j ƒ k sK t d ƒ ‚ | j j ƒ  j d ƒ | j j ƒ  j d ƒ k r« | j j ƒ  j d ƒ | j j ƒ  j d ƒ k s· t d ƒ ‚ d S(   s1   Test properties of symmetric 4-point crossover.
	s9   Strings are of different sizes after symmetric crossover.R
   R   s,   There should be equal, inverse distributionsN(	   RB   R$   R   R   R1   R   R%   R2   R3   (   R   R(   R)   (    (    s   test_GACrossover.pyR;   í   s    !		c         C   s“   |  j  j } |  j j } |  j j |  j  |  j ƒ \ } } | j | k rY | j | k se t d ƒ ‚ | |  j  k rƒ | |  j k s t d ƒ ‚ d S(   s?   Test basic asymmetric 2-point crossover functionality.
        s*   Did not perform a crossover when expected.s0   Returned an exact copy of the original organism.N(   R   R   R   RC   R$   R%   (   R   R&   R'   R(   R)   (    (    s   test_GACrossover.pyt   test_basic_asym_crossoverÿ   s    	!	(   R   R   R   R#   R*   R;   RD   (    (    (    s   test_GACrossover.pyRA   Î   s
   			t   TwoPointTestc           B   s    e  Z d  Z d „  Z d „  Z RS(   s$   Test simple 2-point crossovers.
    c         C   sg   t  ƒ  |  _ t d |  j ƒ } t | t ƒ |  _ t d |  j ƒ } t | t ƒ |  _ t d ƒ |  _ d  S(   NR,   R-   g      ð?(	   R	   R   R    R   R   R   R   R   RC   (   R   R!   R"   (    (    s   test_GACrossover.pyR#     s    c         C   s“   |  j  j } |  j j } |  j j |  j  |  j ƒ \ } } | j | k rY | j | k se t d ƒ ‚ | |  j  k rƒ | |  j k s t d ƒ ‚ d S(   s?   Test basic asymmetric 2-point crossover functionality.
        s*   Did not perform a crossover when expected.s0   Returned an exact copy of the original organism.N(   R   R   R   RC   R$   R%   (   R   R&   R'   R(   R)   (    (    s   test_GACrossover.pyRD     s    	!	(   R   R   R   R#   RD   (    (    (    s   test_GACrossover.pyRE     s   	
t   TestCrossoverc           B   s    e  Z d  Z d „  Z d „  Z RS(   sC   Provide basic crossover functionality for testing SafeFitness.
    c         C   s   d |  _  d  S(   Nt   lower(   t   type(   R   (    (    s   test_GACrossover.pyt   __init__4  s    c         C   sZ  | j  j ƒ  } | j  j ƒ  } | j } | j } | j ƒ  } | j ƒ  } |  j d k ra | | f S|  j d k rÏ t t | ƒ d ƒ }	 t t | ƒ d ƒ }
 t |	 | j  j ƒ | _  t |
 | j  j ƒ | _  nQ |  j d k rt t | ƒ d ƒ }	 t t | ƒ d ƒ }
 n t	 d |  j ƒ ‚ t |	 | j  j ƒ | _  t |
 | j  j ƒ | _  | | f S(   Nt   sameRG   i   t   highers   Got type %s(
   R   R   R   t   copyRH   t   strR   R    R   t
   ValueError(   R   R   R   t   seq_org1t   seq_org2t   org1_genomet   org2_genomeR(   R)   t   new_org1_genomet   new_org2_genome(    (    s   test_GACrossover.pyR$   9  s0    		
(   R   R   R   RI   R$   (    (    (    s   test_GACrossover.pyRF   1  s   	t   SafeFitnessTestc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s6   Tests for crossovers which do not reduce fitness.
    c         C   sd   t  ƒ  |  _ t d |  j ƒ } t | t ƒ |  _ t d |  j ƒ } t | t ƒ |  _ t ƒ  |  _ d  S(   NR   (	   R	   R   R    R   R   R   R   RF   t   test_crossover(   R   R!   R"   (    (    s   test_GACrossover.pyR#   \  s    c         C   s  t  |  j ƒ } d |  j _ | j |  j |  j ƒ \ } } | |  j k rW | |  j k sc t d ƒ ‚ d |  j _ | j |  j |  j ƒ \ } } | |  j k r« | |  j k s· t d ƒ ‚ d |  j _ | j |  j |  j ƒ \ } } | j |  j j k r| j |  j j k st d ƒ ‚ d S(   s@   Make sure we always keep higher fitness when specified.
        RJ   s)   Did not retain organism for same fitness.RG   s9   Did not retain organism when crossover had lower fitness.RK   s4   Did not get new organism when it had higher fitness.N(   R   RV   RH   R$   R   R   R%   t   fitness(   R   R    R(   R)   (    (    s   test_GACrossover.pyt   test_keep_higherf  s    !	!	c         C   s  t  |  j d ƒ } d |  j _ | j |  j |  j ƒ \ } } | |  j k rZ | |  j k sf t d ƒ ‚ d |  j _ | j |  j |  j ƒ \ } } | |  j k r® | |  j k sº t d ƒ ‚ d |  j _ | j |  j |  j ƒ \ } } | j |  j j k r| j |  j j k st d ƒ ‚ d S(	   sG   Make sure we do normal crossover functionality when specified.
        g      ð?RJ   s)   Did not retain organism for same fitness.RG   s3   Did not retain lower fitness organism in crossover.RK   s9   Did not get new organism under higher fitness conditions.N(   R   RV   RH   R$   R   R   R%   RW   (   R   R    R(   R)   (    (    s   test_GACrossover.pyt   test_keep_lower~  s    !	!	(   R   R   R   R#   RX   RY   (    (    (    s   test_GACrossover.pyRU   Y  s   	
	t   __main__t	   verbosityi   t
   testRunner(    (!   R   t   unittestt   Bio.SeqR    t   Bio.AlphabetR   t   Bio.GA.OrganismR   t   Bio.GA.Crossover.GeneralR   t   Bio.GA.Crossover.GeneralPointR   R   t   Bio.GA.Crossover.TwoPointR   t   Bio.GA.Crossover.PointR   t   Bio.GA.Crossover.UniformR   R	   R   t   TestCaseR   R+   R6   RA   RE   RF   RU   R   t   TextTestRunnert   runnert   main(    (    (    s   test_GACrossover.pyt   <module>   s,   	DKD(=