Ejemplo n.º 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())
Ejemplo n.º 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])
Ejemplo n.º 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)
Ejemplo n.º 4
0
 def test_AlleleList_clear(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     _allelelist.clear()
     self.assertEqual(_allelelist.options, [])
Ejemplo n.º 5
0
 def test_AlleleList_getRandomAllele(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     random_allele = _allelelist.getRandomAllele()
     self.assertIn(random_allele, _allelelist.options)
Ejemplo n.º 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])
Ejemplo n.º 7
0
 def test_AlleleList_repr(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     self.assertIsInstance(repr(_allelelist), str)
Ejemplo n.º 8
0
 def test_AlleleList_remove(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     _allelelist.remove(2)
     self.assertEqual(_allelelist.options, [1, 3])
Ejemplo n.º 9
0
 def test_AlleleList_len(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     self.assertEqual(len(_allelelist), 3)
Ejemplo n.º 10
0
 def test_AlleleList_iter(self):
     _allelelist = GAllele.GAlleleList(options=[1, 2, 3])
     self.assertIsInstance(iter(_allelelist), Iterable)