def test_solution_string(self): sol1 = SolutionTuple(SOLUTIONS[0][0], SOLUTIONS[0][1]) sol2 = SolutionTuple(SOLUTIONS[1][0], SOLUTIONS[1][1]) sol3 = SolutionTuple(SOLUTIONS[2][0], SOLUTIONS[2][1]) self.assertEqual(sol1.__str__(), "[1, 2, 3] - 0.1 sense: max") self.assertEqual(sol2.__str__(), "[1, 2, 3, 4] - 0.1 sense: max") self.assertEqual(sol3.__str__(), "[2, 3, 4] - 0.45 sense: max")
def test_callable_pool(self): pool = BestSolutionArchiver() size = 3 args = {} args.setdefault('max_archive_size', size) population = [SolutionTuple(SOLUTIONS[0][0], SOLUTIONS[0][1]), SolutionTuple(SOLUTIONS[1][0], SOLUTIONS[1][1]), SolutionTuple(SOLUTIONS[2][0], SOLUTIONS[2][1]), SolutionTuple(SOLUTIONS[3][0], SOLUTIONS[3][1]), SolutionTuple(SOLUTIONS[4][0], SOLUTIONS[4][1]), SolutionTuple(SOLUTIONS[5][0], SOLUTIONS[5][1]), SolutionTuple(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 test_result(self): result = KnockoutOptimizationResult( model=self.model, heuristic_method=None, simulation_method=fba, solutions=self.solutions, objective_function=None, ko_type="reaction", decoder=self.decoder, product="EX_ac_LPAREN_e_RPAREN_", biomass="Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2", seed=SEED, reference=None) self.assertEqual(result.ko_type, "reaction") individuals = [] for index, row in result.solutions.iterrows(): individual = SolutionTuple(set(self.representation.index(r) for r in row["Knockouts"]), row["Fitness"]) self.assertNotIn(individual, individuals, msg="%s is repeated on result") individuals.append(individual) self.assertIn(individual, self.solutions.archive) self.assertEqual(len(row["Knockouts"]), row["Size"]) self.assertEqual(self.solutions.archive.count(individual), 1, msg="%s is unique in archive" % individual)
def test_solution_comparison_maximization(self): sol1 = SolutionTuple(SOLUTIONS[0][0], SOLUTIONS[0][1]) sol2 = SolutionTuple(SOLUTIONS[1][0], SOLUTIONS[1][1]) sol3 = SolutionTuple(SOLUTIONS[2][0], SOLUTIONS[2][1]) # test ordering self.assertEqual(sol1.__cmp__(sol2), -1) self.assertEqual(sol1.__cmp__(sol1), 0) self.assertEqual(sol1.__cmp__(sol3), 1) self.assertTrue(sol1 < sol2) self.assertTrue(sol1 == sol1) self.assertTrue(sol1 > sol3) # test gt and lt self.assertTrue(sol1.__lt__(sol2)) self.assertTrue(sol1.__gt__(sol3)) self.assertFalse(sol1.__lt__(sol1)) self.assertFalse(sol1.__gt__(sol1)) self.assertFalse(sol2.__lt__(sol1)) self.assertFalse(sol3.__gt__(sol1)) # testing issubset self.assertTrue(sol1.issubset(sol2), msg="Solution 1 is subset of Solution 2") self.assertFalse(sol2.issubset(sol1), msg="Solution 2 is not subset of Solution 1") self.assertTrue(sol3.issubset(sol2), msg="Solution 3 is subset of Solution 2") self.assertFalse(sol2.issubset(sol3), msg="Solution 2 is not subset of Solution 3") self.assertFalse(sol1.issubset(sol3), msg="Solution 1 is subset of Solution 3") self.assertFalse(sol2.issubset(sol3), msg="Solution 3 is not subset of Solution 1") # test difference l = len(sol2.symmetric_difference(sol1)) self.assertEqual(l, 1, msg="Difference between Solution 2 and 1 is (%s)" % sol2.symmetric_difference(sol1)) l = len(sol3.symmetric_difference(sol2)) self.assertEqual(l, 1, msg="Difference between Solution 3 and 1 is (%s)" % sol3.symmetric_difference(sol2)) l = len(sol3.symmetric_difference(sol1)) self.assertEqual(l, 2, msg="Difference between Solution 1 and 3 is (%s)" % sol3.symmetric_difference(sol1)) self.assertTrue(sol1.improves(sol2), msg="Solution 1 is better than Solution 2") self.assertTrue(sol3.improves(sol2), msg="Solution 3 is better than Solution 2") self.assertFalse(sol3.improves(sol1), msg="Solution 3 does not improve Solution 1") self.assertFalse(sol2.improves(sol1), msg="Solution 2 does not improve Solution 1") self.assertFalse(sol2.improves(sol3), msg="Solution 2 does not improve Solution 3")