def test_uniqueness_of_solutions(self): size = 2 pool = BestSolutionArchive() pool.add(SOLUTIONS[1][0], SOLUTIONS[1][1], None, True, size) pool.add(SOLUTIONS[1][0], SOLUTIONS[1][1], None, True, size) self.assertEqual(pool.length(), 1, "Added repeated solution")
class TestKnockoutOptimizationResult(unittest.TestCase): def setUp(self): self.model = TEST_MODEL self.representation = [r.id for r in self.model.reactions] random = Random(SEED) args = {"representation": self.representation} self.solutions = BestSolutionArchive() for _ in range(10000): self.solutions.add(set_generator(random, args), random.random(), None, True, 100) self.decoder = ReactionKnockoutDecoder(self.representation, self.model) def test_reaction_result(self): result = KnockoutOptimizationResult( model=self.model, heuristic_method=None, simulation_method=fba, simulation_kwargs=None, solutions=self.solutions, objective_function=None, ko_type="reaction", decoder=self.decoder, seed=SEED) self.assertEqual(result.ko_type, "reaction") individuals = [] for row in result: encoded = set(self.representation.index(v) for v in row[0]) individual = Individual(encoded, row[2]) self.assertNotIn(individual, individuals, msg="%s is repeated on result") individuals.append(individual) self.assertIn(individual, self.solutions.archive) self.assertEqual(self.solutions.archive.count(individual), 1, msg="%s is unique in archive" % individual)
class TestOptimizationResult(unittest.TestCase): def setUp(self): self.model = TEST_MODEL self.representation = [r.id for r in self.model.reactions] random = Random(SEED) args = {"representation": self.representation} self.solutions = BestSolutionArchive() for _ in range(10000): self.solutions.add(set_generator(random, args), random.random(), None, True, 100) self.decoder = ReactionSetDecoder(self.representation, self.model) def test_reaction_result(self): result = TargetOptimizationResult( model=self.model, heuristic_method=None, simulation_method=fba, simulation_kwargs=None, solutions=self.solutions, objective_function=None, target_type="reaction", decoder=self.decoder, seed=SEED) self.assertEqual(result.target_type, "reaction") individuals = [] for row in result: encoded = set(self.representation.index(v) for v in row[0]) individual = Individual(encoded, row[1]) self.assertNotIn(individual, individuals, msg="%s is repeated on result") individuals.append(individual) self.assertIn(individual, self.solutions.archive) self.assertEqual(self.solutions.archive.count(individual), 1, msg="%s is unique in archive" % individual)
def setUp(self): self.model = TEST_MODEL self.representation = [r.id for r in self.model.reactions] random = Random(SEED) args = {"representation": self.representation} self.solutions = BestSolutionArchive() for _ in range(10000): self.solutions.add(set_generator(random, args), random.random(), None, True, 100) self.decoder = ReactionSetDecoder(self.representation, self.model)
def test_add_smaller_solution_with_same_fitness(self): size = 1 pool = BestSolutionArchive() pool.add(SOLUTIONS[1][0], SOLUTIONS[1][1], None, True, size) pool.add(SOLUTIONS[0][0], SOLUTIONS[0][1], None, True, size) self.assertEqual(pool.length(), 1, msg="Pool must keep one solution (length=%s)" % pool.length()) solution = set(SOLUTIONS[0][0]) fitness = SOLUTIONS[0][1] sol = pool.get(0) self.assertEqual(sol.candidate, solution, msg="Best solution must be the first (%s)" % sol.candidate) self.assertEqual(sol.fitness, fitness, msg="Best fitness must be the first (%s)" % sol.fitness)
def test_callable_pool(self): pool = BestSolutionArchive() size = 3 args = {} args['max_archive_size'] = size population = [Individual(SOLUTIONS[0][0], SOLUTIONS[0][1]), Individual(SOLUTIONS[1][0], SOLUTIONS[1][1]), Individual(SOLUTIONS[2][0], SOLUTIONS[2][1]), Individual(SOLUTIONS[3][0], SOLUTIONS[3][1]), Individual(SOLUTIONS[4][0], SOLUTIONS[4][1]), Individual(SOLUTIONS[5][0], SOLUTIONS[5][1]), Individual(SOLUTIONS[6][0], SOLUTIONS[6][1])] archive = pool(None, population, [], args) self.assertEqual(pool.length(), size) for sol in pool: self.assertTrue(sol in archive)
def setUp(self): self.model = TEST_MODEL self.representation = [r.id for r in self.model.reactions] random = Random(SEED) args = {"representation": self.representation} self.solutions = BestSolutionArchive() for _ in range(10000): self.solutions.add(set_generator(random, args), random.random(), None, True, 100) self.decoder = ReactionKnockoutDecoder(self.representation, self.model)
def test_pool_size_limit(self): size = 1 pool = BestSolutionArchive() pool.add(SOLUTIONS[0][0], SOLUTIONS[0][1], None, True, size) pool.add(SOLUTIONS[1][0], SOLUTIONS[1][1], None, True, size) pool.add(SOLUTIONS[2][0], SOLUTIONS[2][1], None, True, size) pool.add(SOLUTIONS[3][0], SOLUTIONS[3][1], None, True, size) pool.add(SOLUTIONS[4][0], SOLUTIONS[4][1], None, True, size) pool.add(SOLUTIONS[5][0], SOLUTIONS[5][1], None, True, size) pool.add(SOLUTIONS[6][0], SOLUTIONS[6][1], None, True, size) pool.add(SOLUTIONS[7][0], SOLUTIONS[7][1], None, True, size) pool.add(SOLUTIONS[8][0], SOLUTIONS[8][1], None, True, size) pool.add(SOLUTIONS[9][0], SOLUTIONS[9][1], None, True, size) self.assertLessEqual(pool.length(), 1, msg="Pool must keep one solution (length=%s)" % pool.length()) size = 2 pool = BestSolutionArchive() pool.add(SOLUTIONS[0][0], SOLUTIONS[0][1], None, True, size) pool.add(SOLUTIONS[1][0], SOLUTIONS[1][1], None, True, size) pool.add(SOLUTIONS[2][0], SOLUTIONS[2][1], None, True, size) pool.add(SOLUTIONS[3][0], SOLUTIONS[3][1], None, True, size) pool.add(SOLUTIONS[4][0], SOLUTIONS[4][1], None, True, size) pool.add(SOLUTIONS[5][0], SOLUTIONS[5][1], None, True, size) pool.add(SOLUTIONS[6][0], SOLUTIONS[6][1], None, True, size) pool.add(SOLUTIONS[7][0], SOLUTIONS[7][1], None, True, size) pool.add(SOLUTIONS[8][0], SOLUTIONS[8][1], None, True, size) pool.add(SOLUTIONS[9][0], SOLUTIONS[9][1], None, True, size) self.assertLessEqual(pool.length(), 2, msg="Pool must keep one solution (length=%s)" % pool.length()) size = 3 pool = BestSolutionArchive() pool.add(SOLUTIONS[0][0], SOLUTIONS[0][1], None, True, size) pool.add(SOLUTIONS[1][0], SOLUTIONS[1][1], None, True, size) pool.add(SOLUTIONS[2][0], SOLUTIONS[2][1], None, True, size) pool.add(SOLUTIONS[3][0], SOLUTIONS[3][1], None, True, size) pool.add(SOLUTIONS[4][0], SOLUTIONS[4][1], None, True, size) pool.add(SOLUTIONS[5][0], SOLUTIONS[5][1], None, True, size) pool.add(SOLUTIONS[6][0], SOLUTIONS[6][1], None, True, size) pool.add(SOLUTIONS[7][0], SOLUTIONS[7][1], None, True, size) pool.add(SOLUTIONS[8][0], SOLUTIONS[8][1], None, True, size) pool.add(SOLUTIONS[9][0], SOLUTIONS[9][1], None, True, size) self.assertLessEqual(pool.length(), 3, msg="Pool must keep one solution (length=%s)" % pool.length()) size = 4 pool = BestSolutionArchive() pool.add(SOLUTIONS[0][0], SOLUTIONS[0][1], None, True, size) pool.add(SOLUTIONS[1][0], SOLUTIONS[1][1], None, True, size) pool.add(SOLUTIONS[2][0], SOLUTIONS[2][1], None, True, size) pool.add(SOLUTIONS[3][0], SOLUTIONS[3][1], None, True, size) pool.add(SOLUTIONS[4][0], SOLUTIONS[4][1], None, True, size) pool.add(SOLUTIONS[5][0], SOLUTIONS[5][1], None, True, size) pool.add(SOLUTIONS[6][0], SOLUTIONS[6][1], None, True, size) pool.add(SOLUTIONS[7][0], SOLUTIONS[7][1], None, True, size) pool.add(SOLUTIONS[8][0], SOLUTIONS[8][1], None, True, size) pool.add(SOLUTIONS[9][0], SOLUTIONS[9][1], None, True, size) self.assertLessEqual(pool.length(), 4, msg="Pool must keep one solution (length=%s)" % pool.length())