Skip to content

Commit 96f7ddd

Browse files
committed
#44 Individual Selection implemented to fill the first Population
1 parent e481e8c commit 96f7ddd

File tree

4 files changed

+129
-5
lines changed

4 files changed

+129
-5
lines changed

MLC/Population/Creation/BaseCreation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self):
1515
# A list of tuples (index, number)
1616
self._individuals = []
1717

18-
def create(self):
18+
def create(self, gen_size):
1919
raise NotImplementedError()
2020

2121
def individuals(self):
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from BaseCreation import BaseCreation
2+
from MLC.db.mlc_repository import MLCRepository
3+
4+
5+
class IndividualSelection(BaseCreation):
6+
"""
7+
Fill a Population with fixed Individuals.
8+
9+
selected_individuals: dictionary containing {Individual: positions inside
10+
the population}
11+
"""
12+
13+
def __init__(self, selected_individuals):
14+
BaseCreation.__init__(self)
15+
self.__selected_individuals = selected_individuals
16+
self.__individuals = []
17+
18+
def create(self, gen_size):
19+
self.__individuals = [-1]*gen_size
20+
21+
for individual, positions in self.__selected_individuals.items():
22+
for position in positions:
23+
if position < gen_size:
24+
individual_id, _ = MLCRepository.get_instance().add_individual(individual)
25+
print ">>> ADDING %s to %s -> %s" % (individual.get_value(), individual_id, individual_id)
26+
self.__individuals[position] = individual_id
27+
28+
for index, indiv_id in enumerate(self.__individuals):
29+
if indiv_id == -1:
30+
if index > 0:
31+
self.__individuals[index] = self.__individuals[index-1]
32+
else:
33+
self.__individuals[index] = self.__individuals[index + 1]
34+
35+
def individuals(self):
36+
return enumerate(self.__individuals)

MLC/individual/Individual.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,10 @@ def __simplify_and_sensors_tree(value, config):
415415
if config.getboolean('OPTIMIZATION', 'simplify'):
416416
return Lisp_Tree_Expr(value).get_simplified_tree_as_string()
417417

418-
return value
418+
return value
419+
420+
def __cmp__(self, other):
421+
self._value == other.get_value()
422+
423+
def __hash__(self):
424+
return hash(self._value)
Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,89 @@
11
import 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

511
class 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

Comments
 (0)