Пример #1
0
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())
Пример #2
0
 def test_AlleleList_add(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     _allelelist.add(4)
     self.assertEqual(_allelelist.options, [1, 2, 3, 4])
Пример #3
0
 def test_AlleleList_slicing(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     self.assertEqual(_allelelist[0:2], [1, 2])
     self.assertEqual(_allelelist[1], 2)
     _allelelist[1] = 4
     self.assertEqual(_allelelist[1], 4)
Пример #4
0
 def test_AlleleList_clear(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     _allelelist.clear()
     self.assertEqual(_allelelist.options, [])
Пример #5
0
 def test_AlleleList_getRandomAllele(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     random_allele = _allelelist.getRandomAllele()
     self.assertIn(random_allele, _allelelist.options)
Пример #6
0
 def test_createAlleleList_default(self):
     _allelelist = GAllele.GAlleleList()
     self.assertEqual(_allelelist.options, [])
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     self.assertEqual(_allelelist.options, [1, 2, 3])
Пример #7
0
 def test_AlleleList_repr(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     self.assertIsInstance(repr(_allelelist), str)
Пример #8
0
 def test_AlleleList_remove(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     _allelelist.remove(2)
     self.assertEqual(_allelelist.options, [1, 3])
Пример #9
0
 def test_AlleleList_len(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     self.assertEqual(len(_allelelist), 3)
Пример #10
0
 def test_AlleleList_iter(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     self.assertIsInstance(iter(_allelelist), Iterable)