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 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_randint(self): random = RandomGenerator() lower = 0 upper = 10 for _ in range(10000): self.assertGreaterEqual(random.randint(lower, upper), lower) self.assertLessEqual(random.randint(lower, upper), upper) lower = -10 upper = 100 for _ in range(10000): self.assertGreaterEqual(random.randint(lower, upper), lower) self.assertLessEqual(random.randint(lower, upper), upper) lower = 5 upper = 21 for _ in range(10000): self.assertGreaterEqual(random.randint(lower, upper), lower) self.assertLessEqual(random.randint(lower, upper), upper) lower = -5 upper = 5 for _ in range(10000): self.assertGreaterEqual(random.randint(lower, upper), lower) self.assertLessEqual(random.randint(lower, upper), upper)
def test_randint(self): random = RandomGenerator() lower = 0 upper = 10 for _ in range(10000): assert random.randint(lower, upper) >= lower assert random.randint(lower, upper) <= upper lower = -10 upper = 100 for _ in range(10000): assert random.randint(lower, upper) >= lower assert random.randint(lower, upper) <= upper lower = 5 upper = 21 for _ in range(10000): assert random.randint(lower, upper) >= lower assert random.randint(lower, upper) <= upper lower = -5 upper = 5 for _ in range(10000): assert random.randint(lower, upper) >= lower assert random.randint(lower, upper) <= upper
def test_seeded_methods(self): random = RandomGenerator() random.seed(SEED) value = random.random() random.seed(SEED) assert value == random.random() random.seed(SEED) value = random.randint(1, 10) random.seed(SEED) assert value == random.randint(1, 10) random.seed(SEED) population = [1, 2, 3, 4, 5] value = random.sample(population, 2) random.seed(SEED) assert value == random.sample(population, 2) random.seed(SEED) value = random.uniform() random.seed(SEED) assert value == random.uniform()
def test_random(self): random = RandomGenerator() for _ in range(1000): assert random.random() >= 0 assert random.random() <= 1
def test_seeded_methods(self): random = RandomGenerator() random.seed(self.seed) value = random.random() random.seed(self.seed) self.assertEqual(value, random.random()) random.seed(self.seed) value = random.randint(1, 10) random.seed(self.seed) self.assertEqual(value, random.randint(1, 10)) random.seed(self.seed) population = [1, 2, 3, 4, 5] value = random.sample(population, 2) random.seed(self.seed) self.assertEqual(value, random.sample(population, 2)) random.seed(self.seed) value = random.uniform() random.seed(self.seed) self.assertEqual(value, random.uniform())
def test_random(self): random = RandomGenerator() for _ in range(1000): self.assertGreaterEqual(random.random(), 0) self.assertLessEqual(random.random(), 1)
def setUp(self): self.model = TEST_MODEL self.args = {} self.args.setdefault('representation', [r.id for r in self.model.reactions]) self.random = Random()
class TestGenerators(unittest.TestCase): mockup_evolutionary_algorithm = namedtuple("EA", ["bounder"]) def setUp(self): self.model = TEST_MODEL self.args = {} self.args.setdefault('representation', [r.id for r in self.model.reactions]) self.random = Random() 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_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_fixed_size_set_generator(self): candidates_file = os.path.join(CURRENT_PATH, "data", "fix_size_candidates.pkl") self.random.seed(SEED) self.args.setdefault('variable_size', False) candidates = [] self.args['max_size'] = 10 for _ in range(1000): candidate = set_generator(self.random, self.args) self.assertEqual(len(candidate), 10) candidate = unique_set_generator(self.random, self.args) self.assertEqual(len(candidate), 10) candidates.append(candidate) with open(candidates_file, 'rb') as in_file: if six.PY3: expected_candidates = pickle.load(in_file, encoding="latin1") else: expected_candidates = pickle.load(in_file) self.assertEqual(candidates, expected_candidates) self.args['max_size'] = 20 for _ in range(1000): candidate = set_generator(self.random, self.args) self.assertEqual(len(candidate), 20) candidate = unique_set_generator(self.random, self.args) self.assertEqual(len(candidate), 20) def test_variable_size_set_generator(self): candidates_file = os.path.join(CURRENT_PATH, "data", "variable_size_candidates.pkl") self.args.setdefault('variable_size', True) self.random.seed(SEED) candidates = [] self.args['max_size'] = 10 for _ in range(1000): candidate = set_generator(self.random, self.args) self.assertLessEqual(len(candidate), 10) candidate = unique_set_generator(self.random, self.args) self.assertLessEqual(len(candidate), 10) candidates.append(candidate) with open(candidates_file, 'rb') as in_file: if six.PY3: expected_candidates = pickle.load(in_file, encoding="latin1") else: expected_candidates = pickle.load(in_file) self.assertEqual(candidates, expected_candidates) self.args['max_size'] = 20 for _ in range(1000): candidate = set_generator(self.random, self.args) self.assertLessEqual(len(candidate), 20) candidate = unique_set_generator(self.random, self.args) self.assertLessEqual(len(candidate), 20) def test_fixed_size_linear_set_generator(self): ec = self.mockup_evolutionary_algorithm(Bounder(-10, 10)) self.args.setdefault('variable_size', False) self.args['max_size'] = 10 self.args['_ec'] = ec for _ in range(1000): candidate = linear_set_generator(self.random, self.args) for i, v in six.iteritems(candidate): self.assertIsInstance(i, (int, numpy.int64, numpy.int32)) self.assertIsInstance(v, float) self.assertLessEqual(len(candidate), 10)