Skip to content

Commit b811893

Browse files
committed
#44 add Individual Selection
1 parent a5f2aab commit b811893

File tree

2 files changed

+17
-55
lines changed

2 files changed

+17
-55
lines changed

MLC/Population/Creation/IndividualSelection.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,27 @@ class IndividualSelection(BaseCreation):
99
selected_individuals: dictionary containing {Individual: positions inside
1010
the first population}
1111
12+
fill_creator: creator used to fill empty positions.
13+
1214
Empty positions inside the Population will be completed using the neighbor individual,
1315
"""
1416

15-
def __init__(self, selected_individuals):
17+
def __init__(self, selected_individuals, fill_creator):
1618
BaseCreation.__init__(self)
19+
self.__fill_creator = fill_creator
1720
self.__selected_individuals = selected_individuals
1821
self.__individuals = []
1922

2023
def create(self, gen_size):
21-
self.__individuals = [-1] * gen_size
24+
self.__fill_creator.create(gen_size)
25+
self.__individuals = self.__fill_creator.individuals()
2226

2327
# Add Individuals
2428
for individual, positions in self.__selected_individuals.items():
2529
for position in positions:
2630
if position < gen_size:
2731
individual_id, _ = MLCRepository.get_instance().add_individual(individual)
28-
self.__individuals[position] = individual_id
29-
30-
# Fill empty spaces
31-
for index, indiv_id in enumerate(self.__individuals):
32-
if indiv_id == -1:
33-
if index > 0:
34-
self.__individuals[index] = self.__individuals[index - 1]
35-
else:
36-
self.__individuals[index] = self.__individuals[index + 1]
32+
self.__individuals[position] = (position, individual_id)
3733

3834
def individuals(self):
39-
return enumerate(self.__individuals)
35+
return self.__individuals

tests/mlc/population/test_population.py

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from MLC.Population.Population import Population
66
from MLC.db.mlc_repository import MLCRepository
77
from MLC.Population.Creation.IndividualSelection import IndividualSelection
8+
from MLC.Population.Creation.MixedRampedGauss import MixedRampedGauss
89
from MLC.individual.Individual import Individual
910

1011

@@ -14,56 +15,28 @@ def setUpClass(cls):
1415
TestHelper.load_default_configuration()
1516

1617
def test_add_one_individual(self):
17-
creator = IndividualSelection({Individual("1+1"): [0, 1, 2, 3, 4]})
1818

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]})
19+
base_creator = MixedRampedGauss()
20+
creator = IndividualSelection({Individual("1+1"): [0, 1, 2, 3, 4]}, base_creator)
2521

2622
self.__fill_and_assert(fill_creator=creator,
2723
expected_pop_indexes=[1, 1, 1, 1, 1],
2824
expected_individuals={1: Individual("1+1")})
2925

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]})
26+
def test_add_one_individual_incomplete_population(self):
27+
base_creator = MixedRampedGauss()
28+
creator = IndividualSelection({Individual("1+1"): [0]}, base_creator)
5829

5930
self.__fill_and_assert(fill_creator=creator,
60-
expected_pop_indexes=[1, 1, 1, 1, 1],
31+
expected_pop_indexes=[1],
6132
expected_individuals={1: Individual("1+1")})
6233

6334
def __fill_and_assert(self, fill_creator, expected_pop_indexes, expected_individuals):
6435
with saved(Config.get_instance()) as config:
6536
Config.get_instance().set("POPULATION", "size", "5")
6637
Config.get_instance().set("BEHAVIOUR", "save", "false")
38+
from MLC.Log.log import set_logger
39+
set_logger('testing')
6740

6841
population = Population(5, 0, Config.get_instance(), MLCRepository.make(""))
6942
population.fill(fill_creator)
@@ -72,9 +45,6 @@ def __fill_and_assert(self, fill_creator, expected_pop_indexes, expected_individ
7245
# Assert that one Population was added
7346
self.assertEqual(MLCRepository.get_instance().count_population(), 1)
7447

75-
# Assert that all expected individuals were created
76-
self.assertEqual(MLCRepository.get_instance().count_individual(), len(expected_individuals))
77-
7848
# Assert that the individuals are in the expected position inside the Population
7949
for position, i in enumerate(expected_pop_indexes):
8050
expected_individual = expected_individuals[i]
@@ -83,7 +53,3 @@ def __fill_and_assert(self, fill_creator, expected_pop_indexes, expected_individ
8353

8454
self.assertEqual(expected_individual.get_value(), inserted_individual.get_value())
8555

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

Comments
 (0)