コード例 #1
0
def run_main():
    # Genome instance
    genome = G1DList.G1DList(1)
    genome.setParams(rangemin=-60.0, rangemax=60.0)

    # Change the initializator to Real values
    genome.initializator.set(G1DListInitializatorReal)

    # Change the mutator to Gaussian Mutator
    genome.mutator.set(G1DListMutatorRealGaussian)

    # Removes the default crossover
    genome.crossover.clear()

    # The evaluator function (objective function)
    genome.evaluator.set(eval_func)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setMinimax(Consts.minimaxType["minimize"])

    pop = ga.getPopulation()
    pop.scaleMethod.set(Scaling.SigmaTruncScaling)

    ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(100)

    # Do the evolution
    ga.evolve(10)

    # Best individual
    print(ga.bestIndividual())
コード例 #2
0
def run_main():
    # Genome instance, 1D List of 50 elements
    genome = G1DList.G1DList(50)

    # Sets the range max and min of the 1D List
    genome.setParams(rangemin=0, rangemax=10)

    # The evaluator function (evaluation function)
    genome.evaluator.set(eval_func)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)

    # Set the Roulette Wheel selector method, the number of generations and
    # the termination criteria
    ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(500)
    ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
    ga.setMinimax(Consts.minimaxType["minimize"])

    # Do the evolution, with stats dump
    # frequency of 20 generations
    best_individual = ga.evolve(freq_stats=20)

    # Best individual
    print(best_individual)
コード例 #3
0
def run_main():
    genome = G1DList.G1DList(len(sentence))
    genome.setParams(rangemin=min(numeric_sentence),
                     rangemax=max(numeric_sentence),
                     bestrawscore=0.00,
                     gauss_mu=1,
                     gauss_sigma=4)

    genome.initializator.set(G1DListInitializatorInteger)
    genome.mutator.set(G1DListMutatorIntegerGaussian)
    genome.evaluator.set(lambda genome: sum(
        [abs(a - b) for a, b in zip(genome, numeric_sentence)]))

    ga = GSimpleGA.GSimpleGA(genome)
    # ga.stepCallback.set(evolve_callback)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.terminationCriteria.set(GSimpleGA.RawScoreCriteria)
    ga.setPopulationSize(60)
    ga.setMutationRate(0.02)
    ga.setCrossoverRate(0.9)
    ga.setGenerations(5000)
    ga.evolve(freq_stats=100)

    best = ga.bestIndividual()
    print("Best individual score: %.2f" % (best.score, ))
    print(''.join(map(chr, best)))
コード例 #4
0
def main_run():
    global cm, coords, WIDTH, HEIGHT

    coords = [(random.randint(0, WIDTH), random.randint(0, HEIGHT))
              for i in range(CITIES)]
    cm = cartesian_matrix(coords)
    genome = G1DList.G1DList(len(coords))

    genome.evaluator.set(lambda chromosome: tour_length(cm, chromosome))
    genome.crossover.set(G1DListCrossoverEdge)
    genome.mutator.set(G1DListMutatorSwap)
    genome.initializator.set(G1DListTSPInitializatorRandom)

    # 3662.69
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setGenerations(GENERATION_COUNT)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setCrossoverRate(1.0)
    ga.setMutationRate(0.02)
    ga.setPopulationSize(80)

    # This is to make a video
    if PIL_SUPPORT:
        ga.stepCallback.set(evolve_callback)
    # 21666.49

    ga.evolve(freq_stats=500)
    best = ga.bestIndividual()

    if PIL_SUPPORT:
        write_tour_to_img(coords, best, f"{RESULTS_DIRECTORY}/tsp_result.png")
    else:
        print("No PIL detected, cannot plot the graph !")
コード例 #5
0
def run_main():
    genome = G1DList.G1DList(BOARD_SIZE)
    genome.setParams(bestrawscore=BOARD_SIZE, rounddecimal=2)
    genome.initializator.set(queens_init)
    genome.mutator.set(G1DListMutatorSwap)
    genome.crossover.set(G1DListCrossoverCutCrossfill)
    genome.evaluator.set(queens_eval)

    ga = GSimpleGA.GSimpleGA(genome)
    ga.terminationCriteria.set(GSimpleGA.RawScoreCriteria)
    ga.setMinimax(Consts.minimaxType["maximize"])

    ga.setPopulationSize(100)
    ga.setGenerations(250)
    ga.setMutationRate(0.02)
    ga.setCrossoverRate(1.0)

    sqlite_adapter = DBAdapters.DBSQLite(identify="queens")
    ga.setDBAdapter(sqlite_adapter)

    #vpython_adapter = DBAdapters.DBVPythonGraph(identify="queens", frequency=1)
    #ga.setDBAdapter(vpython_adapter)

    ga.evolve(freq_stats=10)

    best = ga.bestIndividual()
    print(best)
    print("Best individual score: %.2f\n" % (best.getRawScore(), ))
コード例 #6
0
def run_main():
    # Genome instance
    genome = G1DList.G1DList(5)
    genome.setParams(rangemin=-8,
                     rangemax=8,
                     bestrawscore=0.00,
                     rounddecimal=2)
    genome.initializator.set(G1DListInitializatorReal)
    genome.mutator.set(G1DListMutatorRealGaussian)

    # The evaluator function (objective function)
    genome.evaluator.set(ackley)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setGenerations(1000)
    ga.setMutationRate(0.04)
    ga.terminationCriteria.set(GSimpleGA.RawScoreCriteria)

    # Create DB Adapter and set as adapter
    # sqlite_adapter = DBAdapters.DBSQLite(identify="ackley")
    # ga.setDBAdapter(sqlite_adapter)

    # Do the evolution, with stats dump
    # frequency of 10 generations
    ga.evolve(freq_stats=50)

    # Best individual
    best = ga.bestIndividual()
    print("\nBest individual score: %.2f" % (best.getRawScore(), ))
    print(best)
コード例 #7
0
def run_main():
    # Genome instance
    genome = G1DList.G1DList(15)
    genome.setParams(rangemin=-1, rangemax=1.1)
    genome.initializator.set(G1DListInitializatorReal)
    genome.mutator.set(G1DListMutatorRealRange)

    # The evaluator function (objective function)
    genome.evaluator.set(rosenbrock)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(4000)
    ga.setCrossoverRate(0.9)
    ga.setPopulationSize(100)
    ga.setMutationRate(0.03)

    ga.evolve(freq_stats=500)

    # Best individual
    best = ga.bestIndividual()
    print("\nBest individual score: %.2f" % (best.score, ))
    print(best)
コード例 #8
0
def run_main():
    # Genome instance
    genome = G1DList.G1DList(20)
    genome.setParams(rangemin=-5.2,
                     rangemax=5.30,
                     bestrawscore=0.00,
                     rounddecimal=2)
    genome.initializator.set(G1DListInitializatorReal)
    genome.mutator.set(G1DListMutatorRealGaussian)

    genome.evaluator.set(rastrigin)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.terminationCriteria.set(GSimpleGA.RawScoreCriteria)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setGenerations(3000)
    ga.setCrossoverRate(0.8)
    ga.setPopulationSize(100)
    ga.setMutationRate(0.06)

    ga.evolve(freq_stats=50)

    best = ga.bestIndividual()
    print(best)
コード例 #9
0
def run_main():
    genome = G1DList.G1DList(140)
    genome.setParams(rangemin=-5.12, rangemax=5.13)
    genome.initializator.set(G1DListInitializatorReal)
    genome.mutator.set(G1DListMutatorRealGaussian)
    genome.evaluator.set(sphere)

    ga = GSimpleGA.GSimpleGA(genome, seed=666)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setGenerations(1500)
    ga.setMutationRate(0.01)
    ga.evolve(freq_stats=500)
コード例 #10
0
def run_main():
    # Genome instance
    genome = G1DList.G1DList(200)
    genome.setParams(rangemin=0, rangemax=10)

    # The evaluator function (objective function)
    genome.evaluator.set(eval_func)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(800)
    ga.stepCallback.set(evolve_callback)

    # Do the evolution
    ga.evolve()

    # Best individual
    print(ga.bestIndividual())
コード例 #11
0
def main_run():
    global cm, coords, WIDTH, HEIGHT, CITIES
    path = os.path.join(os.path.dirname(__file__), 'data/fri26.tsp')
    problem = tsplib95.load(path)
    print(list(problem.get_nodes()))

    CITIES = len(list(problem.get_nodes()))
    for i in range(0, CITIES):
        for j in range(0, CITIES):
            edge = i, j
            weight = problem.get_weight(*edge)
            cm[i, j] = weight

    genome = G1DList.G1DList(CITIES)

    genome.setParams(dist=cm)
    genome.evaluator.set(lambda chromosome: tour_length(cm, chromosome))
    genome.crossover.set(G1DListCrossoverPMX)
    genome.mutator.set(G1DListMutatorDisplacement)
    genome.initializator.set(G1DListTSPInitializatorRandom)

    # 3662.69
    ga = GSimpleGA.GSimpleGA(genome)
    ga.setGenerations(GENERATION_COUNT)
    ga.setMinimax(Consts.minimaxType["minimize"])
    ga.setCrossoverRate(1.0)
    ga.setMutationRate(0.02)
    ga.setPopulationSize(80)
    ga.selector.set(SelectionRank.SelectorLinearRanking)

    # This is to make a video
    if PIL_SUPPORT:
        ga.stepCallback.set(evolve_callback)
    # 21666.49

    ga.evolve(freq_stats=1)
    best = ga.bestIndividual()

    if PIL_SUPPORT:
        write_tour_to_img(coords, best, f"{RESULTS_DIRECTORY}/tsp_result.png")
    else:
        print("No PIL detected, cannot plot the graph !")
コード例 #12
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())
コード例 #13
0
def run_main():
    # Genome instance
    genome = G1DList.G1DList(40)

    # The gauss_mu and gauss_sigma is used to the Gaussian Mutator, but
    # if you don't specify, the mutator will use the defaults
    genome.setParams(rangemin=0, rangemax=10, gauss_mu=4, gauss_sigma=6)
    genome.mutator.set(G1DListMutatorIntegerGaussian)

    # The evaluator function (objective function)
    genome.evaluator.set(eval_func)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)
    # ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(800)

    # Do the evolution, with stats dump
    # frequency of 10 generations
    ga.evolve(freq_stats=150)

    # Best individual
    print(ga.bestIndividual())
コード例 #14
0
def run_main():
    # Genome instance, 1D List of 50 elements
    genome = G1DList.G1DList(50)

    # Sets the range max and min of the 1D List
    genome.setParams(rangemin=0, rangemax=10)

    # The evaluator function (evaluation function)
    genome.evaluator.set(eval_func)

    # Genetic Algorithm Instance
    ga = GSimpleGA.GSimpleGA(genome)

    # Set the Roulette Wheel selector method, the number of generations and
    # the termination criteria
    ga.selector.set(Selectors.GRouletteWheel)
    ga.setGenerations(500)
    ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)

    # Sets the DB Adapter, the resetDB flag will make the Adapter recreate
    # the database and erase all data every run, you should use this flag
    # just in the first time, after the pyevolve.db was created, you can
    # omit it.
    sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True)
    ga.setDBAdapter(sqlite_adapter)


    # Do the evolution, with stats dump
    # frequency of 20 generations
    best_individual = ga.evolve(freq_stats=20)

    # Best individual
    print(best_individual)

    # Another way to find best individual
    print(ga.bestIndividual())
コード例 #15
0
ファイル: test_genomes.py プロジェクト: ati-ozgur/Pyevolve
 def test_create1DList_default(self):
     _genome = G1DList.G1DList()
     self.assertTrue(hasattr(_genome, 'genomeSize'))
     self.assertTrue(hasattr(_genome, 'initializator'))
     self.assertTrue(hasattr(_genome, 'mutator'))
     self.assertTrue(hasattr(_genome, 'crossover'))
コード例 #16
0

# This function is the evaluation function, we want
# to give high score to more zero'ed chromosomes
def eval_func(chromosome):
    score = 0.0

    # iterate over the chromosome
    for value in chromosome:
        if value == 0:
            score += 0.5
    return score


# Genome instance
genome = G1DList.G1DList(100)
genome.setParams(rangemin=0, rangemax=10)

# The evaluator function (objective function)
genome.evaluator.set(eval_func)

# Genetic Algorithm Instance
ga = GSimpleGA.GSimpleGA(genome, 666)
ga.setGenerations(80)
ga.setMutationRate(0.2)

# Create DB Adapter and set as adapter
#sqlite_adapter = DBAdapters.DBSQLite(identify="ex6", resetDB=True)  # noqa
#ga.setDBAdapter(sqlite_adapter)  # noqa

# Using CSV Adapter
コード例 #17
0
ファイル: test_genomes.py プロジェクト: ati-ozgur/Pyevolve
 def test_1DList_add(self):
     _genome1 = G1DList.G1DList(size=3)
     _genome1[:] = [1, 2, 3]
     other = 2
     result = _genome1 + other
     self.assertEqual(result.genomeList, [3, 4, 5])
コード例 #18
0
ファイル: test_genomes.py プロジェクト: ati-ozgur/Pyevolve
 def test_1DList_sub(self):
     _genome1 = G1DList.G1DList(size=3)
     _genome1[:] = [1, 2, 3]
     other = 2
     result = _genome1 - other
     self.assertEqual(result.genomeList, [-1, 0, 1])
コード例 #19
0
ファイル: test_genomes.py プロジェクト: ati-ozgur/Pyevolve
 def test_repr(self):
     _genome = G1DList.G1DList()
     self.assertIsInstance(repr(_genome), str)
コード例 #20
0
 def setUp(self):
     self.genome = G1DList.G1DList(2)
     self.genome.evaluator.set(lambda _: 0)
     self.ga = GSimpleGA.GSimpleGA(self.genome)
コード例 #21
0
ファイル: test_genomes.py プロジェクト: ati-ozgur/Pyevolve
 def test_1DList_mul(self):
     _genome1 = G1DList.G1DList(size=3)
     _genome1[:] = [1, 2, 3]
     other = 2
     result = _genome1 * other
     self.assertEqual(result.genomeList, [2, 4, 6])