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())
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)
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)))
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 !")
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(), ))
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)
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)
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)
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)
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())
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 !")
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 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())
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())
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'))
# 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
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])
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])
def test_repr(self): _genome = G1DList.G1DList() self.assertIsInstance(repr(_genome), str)
def setUp(self): self.genome = G1DList.G1DList(2) self.genome.evaluator.set(lambda _: 0) self.ga = GSimpleGA.GSimpleGA(self.genome)
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])