def __init__(self, model=None, heuristic_method=inspyred.ec.GA, objective_function=None, seed=None, termination=inspyred.ec.terminators.evaluation_termination, plot=True, progress=True, *args, **kwargs): super(HeuristicOptimization, self).__init__(*args, **kwargs) logger.debug("Seed: %s" % seed) self.plot = plot self.progress = progress if seed is None: seed = int(time.time()) self.seed = seed self.observers = [] self.random = Random(seed=seed) self.model = model self.termination = termination self._objective_function = objective_function self._heuristic_method = None self.heuristic_method = heuristic_method self.heuristic_method.terminator = termination self._generator = None
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_set_indel(self): individual = OrderedSet([1, 3, 5, 9, 10]) representation = list(range(10)) args = { "representation": representation, "indel_rate": 1 } new_individuals = set_indel(Random(SEED), [individual], args) self.assertEqual(new_individuals[0], [5, 3, 9, 1])
def test_set_indel(self): individual = [1, 3, 5, 9, 10] representation = list(range(10)) args = { "representation": representation, "indel_rate": 1 } new_individuals = set_indel(Random(SEED), [individual], args) self.assertNotEqual(len(new_individuals[0]), len(individual)) self.assertEqual(new_individuals[0], [1, 3, 5, 6, 9, 10])
def test_do_set_n_point_crossover(self): representation = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"] int_representation = [representation.index(v) for v in representation] mom = OrderedSet([representation.index(v) for v in ["A", "B", "E", "K", "L", "M"]]) dad = OrderedSet([representation.index(v) for v in ["A", "C", "I", "J", "K", "L"]]) points = [4] children = _do_set_n_point_crossover(int_representation, mom, dad, points, Random(), len(mom)) bro = OrderedSet([0, 1, 8, 9, 10, 11]) sis = OrderedSet([0, 2, 4, 10, 11, 12]) self.assertEqual(children[0], bro) self.assertEqual(children[1], sis)
def test_set_mutation(self): individual = OrderedSet([1, 3, 5, 9, 10]) representation = list(range(10)) args = { "representation": representation, "mutation_rate": 1 } new_individuals = set_mutation(Random(SEED), [individual], args) self.assertEqual(len(new_individuals[0]), len(individual)) self.assertNotEqual(new_individuals[0], individual) self.assertEqual(new_individuals[0], [0, 2, 4, 6, 7])
def test_multiple_chromossome_set_generator(self): random = Random(SEED) args = dict(keys=["test_key_1", "test_key_2"], test_key_1_representation=["a1", "a2", "a3", "a4", "a5"], test_key_2_representation=["b1", "b2", "b3", "b4", "b5", "b6", "b7"], test_key_1_max_size=3, test_key_2_max_size=5, variable_size=False) candidate = multiple_chromosome_set_generator(random, args) self.assertEqual(len(candidate['test_key_1']), 3) self.assertEqual(len(candidate['test_key_2']), 5)
def test_set_n_point_crossover(self): mom = OrderedSet([1, 3, 5, 9, 10]) dad = OrderedSet([2, 3, 7, 8]) args = { "crossover_rate": 1.0, "num_crossover_points": 1, "candidate_size": 10 } children = set_n_point_crossover(Random(SEED), [mom, dad], args) bro = OrderedSet([1, 3, 5, 8]) sis = OrderedSet([2, 3, 7, 9, 10]) self.assertEqual(bro, children[0]) self.assertEqual(sis, children[1])
def test_multiple_chromosome_set_mutation(self): genome = MultipleChromosomeGenome(["A", "B"]) genome["A"] = [1, 2, 3, 4] genome["B"] = [1, 5, 7, 10] representation = list(range(10)) args = { "A_representation": representation, "B_representation": representation, "A_mutation_rate": 1, "B_mutation_rate": 1 } new_individuals = multiple_chromosome_set_mutation(Random(SEED), [genome], args) self.assertEqual(new_individuals[0]["A"], OrderedSet([0, 6, 7, 8])) self.assertEqual(new_individuals[0]["B"], OrderedSet([0, 6, 8, 9]))
def test_set_generator(self): random = Random(SEED) representation = ["a", "b", "c", "d", "e", "f"] max_size = 5 variable_size = False expected = [[0, 1, 2, 4, 5], [0, 2, 3, 4, 5], [0, 1, 2, 3, 5], [1, 2, 3, 4, 5], [0, 2, 3, 4, 5]] for i in range(len(expected)): candidate = set_generator(random, dict(representation=representation, max_size=max_size, variable_size=variable_size)) self.assertEqual(candidate, expected[i])
def test_multiple_chromosome_set_indel(self): genome = MultipleChromosomeGenome(["A", "B"]) genome["A"] = [1, 2, 3, 4] genome["B"] = [1, 5, 7, 10] representation = list(range(10)) args = { "A_representation": representation, "B_representation": representation, "A_indel_rate": 1, "B_indel_rate": 1 } random = Random(SEED) new_individuals = multiple_chromosome_set_indel(random, [genome for _ in range(5)], args) self.assertEqual(new_individuals[0]["A"], OrderedSet([1, 2, 3, 4, 7])) self.assertEqual(new_individuals[0]["B"], OrderedSet([1, 5, 10])) self.assertEqual(new_individuals[1]["A"], OrderedSet([2, 3, 4])) self.assertEqual(new_individuals[1]["B"], OrderedSet([1, 5, 7, 8, 10])) self.assertEqual(new_individuals[2]["A"], OrderedSet([1, 2, 3, 4, 6])) self.assertEqual(new_individuals[2]["B"], OrderedSet([1, 5, 7])) self.assertEqual(new_individuals[3]["A"], OrderedSet([1, 2, 3, 4, 8])) self.assertEqual(new_individuals[3]["B"], OrderedSet([0, 1, 5, 7, 10])) self.assertEqual(new_individuals[4]["A"], OrderedSet([1, 2, 3, 4, 7])) self.assertEqual(new_individuals[4]["B"], OrderedSet([1, 5, 7, 8, 10]))
def random(self): if self._random is None: self._random = Random() return self._random
def setUp(self): self.population = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] self.random = Random(SEED)
def setUp(self): self.model = TEST_MODEL self.args = {} self.args.setdefault('representation', [r.id for r in self.model.reactions]) self.random = Random()