def test_createAlleleRange(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertEqual(_allelerange.beginEnd, [(10, 20)]) self.assertEqual(_allelerange.minimum, 10) self.assertEqual(_allelerange.maximum, 20) _allelerange = GAllele.GAlleleRange(1.0, 2.0, real=True) self.assertEqual(_allelerange.real, True)
def test_AlleleRange_slicing(self): _allelerange = GAllele.GAlleleRange(10, 20) _allelerange.add(30, 40) self.assertEqual(_allelerange[0], (10, 20)) _allelerange[1] = (50, 60) self.assertEqual(_allelerange[1], (50, 60)) with self.assertRaises(ValueError): _allelerange[1] = (60, 50)
def test_AlleleRange_add(self): _allelerange = GAllele.GAlleleRange(10, 20) _allelerange.add(30, 40) self.assertEqual(_allelerange.beginEnd, [(10, 20), (30, 40)]) self.assertEqual(_allelerange.minimum, 10) self.assertEqual(_allelerange.maximum, 40) with self.assertRaises(ValueError): _allelerange.add(40, 30)
def test_AlleleRange_getRandomAllele(self): _allelerange = GAllele.GAlleleRange(10, 20) random_allele = _allelerange.getRandomAllele() self.assertTrue( random_allele, any([x[0] <= random_allele <= x[1] for x in _allelerange.beginEnd])) _allelerange.add(30, 40) random_allele = _allelerange.getRandomAllele() self.assertTrue( random_allele, any([x[0] <= random_allele <= x[1] for x in _allelerange.beginEnd])) _allelerange = GAllele.GAlleleRange(1.0, 2.0, real=True) random_allele = _allelerange.getRandomAllele() self.assertTrue( random_allele, any([x[0] <= random_allele <= x[1] for x in _allelerange.beginEnd]))
def run_main(): # Genome instance setOfAlleles = GAllele.GAlleles() # From 0 to 10 we can have only some # defined ranges of integers for i in range(11): a = GAllele.GAlleleRange(0, i) setOfAlleles.add(a) # From 11 to 19 we can have a set # of elements for i in range(11, 20): # You can even add objects instead of strings or # primitive values a = GAllele.GAlleleList(['a', 'b', 'xxx', 666, 0]) setOfAlleles.add(a) genome = G1DList.G1DList(20) genome.setParams(allele=setOfAlleles) # The evaluator function (objective function) genome.evaluator.set(eval_func) # This mutator and initializator will take care of # initializing valid individuals based on the allele set # that we have defined before genome.mutator.set(G1DListMutatorAllele) genome.initializator.set(G1DListInitializatorAllele) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) ga.setGenerations(40) # Do the evolution, with stats dump # frequency of 10 generations ga.evolve(freq_stats=5) # Best individual print(ga.bestIndividual())
def test_AlleleRange_repr(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertIsInstance(repr(_allelerange), str)
def test_AlleleRange_len(self): _allelerange = GAllele.GAlleleRange(10, 20) _allelerange.add(30, 40) self.assertEqual(len(_allelerange), 2)
def test_AlleleRange_real(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertEqual(_allelerange.getReal(), False) _allelerange.setReal(flag=True) self.assertEqual(_allelerange.getReal(), True)
def test_AlleleRange_clear(self): _allelerange = GAllele.GAlleleRange(10, 20) _allelerange.clear() self.assertEqual(_allelerange.beginEnd, [])
def test_AlleleRange_getMinimum(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertEqual(_allelerange.getMaximum(), 20)
def test_AlleleRange_iter(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertIsInstance(iter(_allelerange), Iterable)