11import unittest
2+ from tests .test_helpers import TestHelper
3+
4+ from MLC .mlc_parameters .mlc_parameters import saved , Config
5+ from MLC .Population .Population import Population
6+ from MLC .db .mlc_repository import MLCRepository
7+ from MLC .Population .Creation .IndividualSelection import IndividualSelection
8+ from MLC .individual .Individual import Individual
29
3- from MLC .matlab_engine import MatlabEngine
410
511class PopulationTest (unittest .TestCase ):
6- def test_pass (self ):
7- self .assertTrue (True )
12+ @classmethod
13+ def setUpClass (cls ):
14+ TestHelper .load_default_configuration ()
15+
16+ def test_add_one_individual (self ):
17+ creator = IndividualSelection ({Individual ("1+1" ): [0 , 1 , 2 , 3 , 4 ]})
18+
19+ self .__fill_and_assert (fill_creator = creator ,
20+ expected_pop_indexes = [1 , 1 , 1 , 1 , 1 ],
21+ expected_individuals = {1 : Individual ("1+1" )})
22+
23+ def test_add_one_individual_incomplete_population (self ):
24+ creator = IndividualSelection ({Individual ("1+1" ): [0 ]})
25+
26+ self .__fill_and_assert (fill_creator = creator ,
27+ expected_pop_indexes = [1 , 1 , 1 , 1 , 1 ],
28+ expected_individuals = {1 : Individual ("1+1" )})
29+
30+ def test_add_multiple_individuals (self ):
31+ creator = IndividualSelection ({Individual ("1+1" ): [0 , 1 , 2 ],
32+ Individual ("2+2" ): [3 , 4 ]})
33+
34+ self .__fill_and_assert (fill_creator = creator ,
35+ expected_pop_indexes = [1 , 1 , 1 , 2 , 2 ],
36+ expected_individuals = {1 : Individual ("1+1" ),
37+ 2 : Individual ("2+2" )})
38+
39+ def test_add_more_individuals_than_the_gensize (self ):
40+ creator = IndividualSelection ({Individual ("1+1" ): [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ]})
41+
42+ self .__fill_and_assert (fill_creator = creator ,
43+ expected_pop_indexes = [1 , 1 , 1 , 1 , 1 ],
44+ expected_individuals = {1 : Individual ("1+1" )})
45+
46+ def test_add_individuals_fill_empty_spaces (self ):
47+ creator = IndividualSelection ({Individual ("1+1" ): [0 , 4 ],
48+ Individual ("2+2" ): [2 ]})
49+
50+ self .__fill_and_assert (fill_creator = creator ,
51+ expected_pop_indexes = [1 , 1 , 2 , 2 , 1 ],
52+ expected_individuals = {1 : Individual ("1+1" ),
53+ 2 : Individual ("2+2" )})
54+
55+ def test_add_individuals_out_of_gen_not_inserted (self ):
56+ creator = IndividualSelection ({Individual ("1+1" ): [0 , 1 , 2 , 3 , 4 ],
57+ Individual ("2+2" ): [5 , 6 , 7 ]})
58+
59+ self .__fill_and_assert (fill_creator = creator ,
60+ expected_pop_indexes = [1 , 1 , 1 , 1 , 1 ],
61+ expected_individuals = {1 : Individual ("1+1" )})
62+
63+ def __fill_and_assert (self , fill_creator , expected_pop_indexes , expected_individuals ):
64+ with saved (Config .get_instance ()) as config :
65+ Config .get_instance ().set ("POPULATION" , "size" , "5" )
66+ Config .get_instance ().set ("BEHAVIOUR" , "save" , "false" )
67+
68+ population = Population (5 , 0 , Config .get_instance (), MLCRepository .make ("" ))
69+ population .fill (fill_creator )
70+ MLCRepository .get_instance ().add_population (population )
71+
72+ # Assert that one Population was added
73+ self .assertEqual (MLCRepository .get_instance ().count_population (), 1 )
74+
75+ # Assert that all expected individuals were created
76+ self .assertEqual (MLCRepository .get_instance ().count_individual (), len (expected_individuals ))
77+
78+ # Assert that the individuals are in the expected position inside the Population
79+ for position , i in enumerate (expected_pop_indexes ):
80+ expected_individual = expected_individuals [i ]
81+ inserted_individual_id = population .get_individuals ()[position ]
82+ inserted_individual = MLCRepository .get_instance ().get_individual (inserted_individual_id )
83+
84+ self .assertEqual (expected_individual .get_value (), inserted_individual .get_value ())
85+
86+ # Assert all individuals are created
87+ created_individuals = [MLCRepository .get_instance ().get_individual (i ).get_value () for i in range (1 , len (expected_individuals )+ 1 )]
88+ for _ , individual in expected_individuals .items ():
89+ self .assertIn (individual .get_value (), created_individuals )
0 commit comments