<s> """<STR_LIT>""" <EOL> import unittest <EOL> from foox . species . third import ( Genome , create_population , is_parallel , <EOL> make_fitness_function , make_generate_function , make_halt_function , <EOL> MAX_REWARD , REWARD_STEPWISE_MOTION ) <EOL> CANTUS_FIRMUS = [ <NUM_LIT:5> , <NUM_LIT:7> , <NUM_LIT:6> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT:8> , <NUM_LIT:7> , <NUM_LIT:6> , <NUM_LIT:5> ] <EOL> class TestCreatePopulation ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_returns_valid_genomes ( self ) : <EOL> """<STR_LIT>""" <EOL> result = create_population ( <NUM_LIT:1> , CANTUS_FIRMUS ) <EOL> self . assertEqual ( Genome , type ( result [ <NUM_LIT:0> ] ) ) <EOL> def test_returns_correct_number_of_genomes ( self ) : <EOL> """<STR_LIT>""" <EOL> result = create_population ( <NUM_LIT:100> , CANTUS_FIRMUS ) <EOL> self . assertEqual ( <NUM_LIT:100> , len ( result ) ) <EOL> def test_uses_only_valid_intervals ( self ) : <EOL> """<STR_LIT>""" <EOL> valid_even_beat_intervals = [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT:11> ] <EOL> valid_odd_beat_intervals = valid_even_beat_intervals + [ <NUM_LIT:3> , <NUM_LIT:6> , <NUM_LIT:8> , <NUM_LIT:10> ] <EOL> result = create_population ( <NUM_LIT:20> , CANTUS_FIRMUS ) <EOL> for genome in result : <EOL> for i in range ( len ( genome . chromosome ) ) : <EOL> contrapunctus_note = genome . chromosome [ i ] <EOL> cantus_firmus_note = CANTUS_FIRMUS [ i / <NUM_LIT:4> ] <EOL> interval = contrapunctus_note - cantus_firmus_note <EOL> if i % <NUM_LIT:2> : <EOL> self . assertIn ( interval , valid_odd_beat_intervals ) <EOL> else : <EOL> self . assertIn ( interval , valid_even_beat_intervals ) <EOL> def test_solutions_have_correct_number_of_notes ( self ) : <EOL> """<STR_LIT>""" <EOL> result = create_population ( <NUM_LIT:20> , CANTUS_FIRMUS ) <EOL> expected_length = ( len ( CANTUS_FIRMUS ) * <NUM_LIT:4> ) - <NUM_LIT:3> <EOL> for genome in result : <EOL> self . assertEqual ( expected_length , len ( genome . chromosome ) ) <EOL> class TestFitnessFunction ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_make_fitness_function_returns_callable ( self ) : <EOL> """<STR_LIT>""" <EOL> result = make_fitness_function ( CANTUS_FIRMUS ) <EOL> self . assertTrue ( callable ( result ) ) <EOL> def test_fitness_function_returns_float ( self ) : <EOL> """<STR_LIT>""" <EOL> fitness_function = make_fitness_function ( CANTUS_FIRMUS ) <EOL> genome = Genome ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> result = fitness_function ( genome ) <EOL> self . assertTrue ( float , type ( result ) ) <EOL> def test_fitness_function_sets_fitness_on_genome ( self ) : <EOL> """<STR_LIT>""" <EOL> fitness_function = make_fitness_function ( CANTUS_FIRMUS ) <EOL> genome = Genome ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( None , genome . fitness ) <EOL> result = fitness_function ( genome ) <EOL> self . assertNotEqual ( None , genome . fitness ) <EOL> self . assertEqual ( result , genome . fitness ) <EOL> def test_fitness_function_uses_cached_genome_fitness ( self ) : <EOL> """<STR_LIT>""" <EOL> fitness_function = make_fitness_function ( CANTUS_FIRMUS ) <EOL> genome = Genome ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> genome . fitness = <NUM_LIT> <EOL> result = fitness_function ( genome ) <EOL> self . assertEqual ( <NUM_LIT> , result ) <EOL> class TestHalt ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_halt_expected ( self ) : <EOL> """<STR_LIT>""" <EOL> halt = make_halt_function ( [ <NUM_LIT:6> , <NUM_LIT:5> ] ) <EOL> g1 = Genome ( [ <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:5> ] ) <EOL> g1 . fitness = MAX_REWARD <EOL> population = [ g1 , ] <EOL> result = halt ( population , <NUM_LIT:1> ) <EOL> self . assertTrue ( result ) <EOL> def test_halt_checks_dissonance_count ( self ) : <EOL> """<STR_LIT>""" <EOL> halt = make_halt_function ( [ <NUM_LIT:6> , <NUM_LIT:5> ] ) <EOL> g1 = Genome ( [ <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:9> , <NUM_LIT:11> , <NUM_LIT:12> ] ) <EOL> g1 . fitness = MAX_REWARD + REWARD_STEPWISE_MOTION <EOL> population = [ g1 , ] <EOL> result = halt ( population , <NUM_LIT:1> ) <EOL> self . assertFalse ( result ) <EOL> g1 . fitness = MAX_REWARD + ( REWARD_STEPWISE_MOTION * <NUM_LIT:2> ) <EOL> population = [ g1 , ] <EOL> result = halt ( population , <NUM_LIT:1> ) <EOL> self . assertTrue ( result ) <EOL> def test_halt_not ( self ) : <EOL> """<STR_LIT>""" <EOL> halt = make_halt_function ( [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> g1 = Genome ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> g1 . fitness = MAX_REWARD - <NUM_LIT:0.1> <EOL> g2 = Genome ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> g2 . fitness = <NUM_LIT:3> <EOL> g3 = Genome ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> g3 . fitness = <NUM_LIT:2> <EOL> population = [ g1 , g2 , g3 ] <EOL> result = halt ( population , <NUM_LIT:1> ) <EOL> self . assertFalse ( result ) <EOL> class TestGenome ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_mutate_is_implemented ( self ) : <EOL> """<STR_LIT>""" <EOL> genome = Genome ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertNotEqual ( NotImplemented , genome . mutate ( <NUM_LIT:2> , <NUM_LIT> , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> def test_mutate_bounded_by_arg_values ( self ) : <EOL> """<STR_LIT>""" <EOL> cantus_firmus = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> mutation_rate = <NUM_LIT:1> <EOL> mutation_range = <NUM_LIT:2> <EOL> genome = Genome ( [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] ) <EOL> genome . mutate ( mutation_range , mutation_rate , cantus_firmus ) <EOL> self . assertEqual ( [ <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ] , genome . chromosome ) </s>
