def test_createAlleles_default(self): _alleles = GAllele.GAlleles(allele_list=None) self.assertTrue(hasattr(_alleles, 'allele_list'), True) self.assertTrue(hasattr(_alleles, 'homogeneous'), True) self.assertEqual(_alleles.allele_list, []) _alleles = GAllele.GAlleles(allele_list=[1, 2, 3]) self.assertEqual(_alleles.allele_list, [1, 2, 3]) _alleles = GAllele.GAlleles(homogeneous=True) self.assertEqual(_alleles.homogeneous, True)
def test_Alleles_slicing(self): # includes slice operation, getitem and setitem _alleles = GAllele.GAlleles(allele_list=[1, 2, 3]) self.assertEqual(_alleles[1], 2) with self.assertRaises(Exception): _ = _alleles[4] _alleles[1] = 5 self.assertEqual(_alleles[1], 5) self.assertEqual(_alleles[0:2], [1, 5])
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_Alleles_repr(self): _alleles = GAllele.GAlleles(allele_list=[1, 2, 3]) self.assertIsInstance(repr(_alleles), str) _alleles = GAllele.GAlleles(allele_list=[1, 2, 3], homogeneous=True) self.assertIsInstance(repr(_alleles), str)
def test_Alleles_len(self): _alleles = GAllele.GAlleles(allele_list=[1, 2, 3]) self.assertEqual(len(_alleles), 3) _alleles = GAllele.GAlleles(allele_list=[1, 2, 3], homogeneous=True) self.assertEqual(len(_alleles), 1)
def test_Alleles_iter(self): _alleles = GAllele.GAlleles(allele_list=[1, 2, 3]) self.assertIsInstance(iter(_alleles), Iterable) _alleles = GAllele.GAlleles(allele_list=[1, 2, 3], homogeneous=True) self.assertIsInstance(iter(_alleles), Iterable)
def test_Alleles_slicing_homogeneous(self): _alleles = GAllele.GAlleles(allele_list=[1, 2, 3], homogeneous=True) self.assertEqual(_alleles[2], 1) _alleles[1] = 5 self.assertEqual(_alleles[0], 5)
def test_Alleles_add(self): _alleles1 = GAllele.GAlleles(allele_list=[1, 2, 3]) _alleles1.add(4) self.assertEqual(_alleles1.allele_list, [1, 2, 3, 4])