class GATestCase(TestCase): def setUp(self): self.ga_custom = GeneticAlgorithm(D=10, NP=40, nFES=1000, Ts=4, Mr=0.05, Cr=0.4, benchmark=MyBenchmark()) self.ga_griewank = GeneticAlgorithm(D=10, NP=40, nFES=1000, Ts=4, Mr=0.05, Cr=0.4, benchmark='griewank') self.ga_tpcr = GeneticAlgorithm(D=10, NP=40, nFES=1000, Ts=4, Mr=0.05, Cr=0.4, Crossover=TwoPointCrossover, benchmark='griewank') self.ga_mpcr = GeneticAlgorithm(D=10, NP=40, nFES=1000, Ts=4, Mr=0.05, Cr=4, Crossover=MultiPointCrossover, benchmark='griewank') self.ga_crmt = GeneticAlgorithm(D=10, NP=40, nFES=1000, Ts=4, Mr=0.05, Cr=0.4, Mutation=CreepMutation, benchmark='griewank') def test_custom_works_fine(self): self.assertTrue(self.ga_custom.run()) def test_griewank_works_fine(self): self.assertTrue(self.ga_griewank.run()) def test_two_point_crossover_fine(self): self.assertTrue(self.ga_tpcr.run()) def test_multi_point_crossover_fine(self): self.assertTrue(self.ga_mpcr.run()) def test_creep_mutation_fine(self): self.assertTrue(self.ga_crmt.run())
class GATestCase(TestCase): def setUp(self): self.ga_custom = GeneticAlgorithm(10, 40, 1000, 4, 0.05, 0.4, MyBenchmark()) self.ga_griewank = GeneticAlgorithm(10, 40, 1000, 4, 0.05, 0.4, 'griewank') def test_custom_works_fine(self): self.assertTrue(self.ga_custom.run()) def test_griewank_works_fine(self): self.assertTrue(self.ga_griewank.run())
def optimize(bench, algo): average_mfo = 0 average_de = 0 average_abc = 0 average_pso = 0 average_ba = 0 average_fa = 0 average_ga = 0 for i in np.arange(epoch): mfo = MothFlameOptimizer(D=dim, NP=pop, nGEN=maxIter, benchmark=bench) de = DifferentialEvolution(D=dim, NP=pop, nGEN=maxIter, benchmark=bench) abc = ArtificialBeeColonyAlgorithm(D=dim, NP=pop, nFES=maxIter, benchmark=bench) pso = ParticleSwarmAlgorithm(D=dim, NP=pop, nGEN=maxIter, benchmark=bench) ba = BatAlgorithm(D=dim, NP=pop, nFES=maxIter, benchmark=bench) fa = FireflyAlgorithm(D=dim, NP=pop, nFES=maxIter, benchmark=bench) ga = GeneticAlgorithm(D=dim, NP=pop, nFES=maxIter, benchmark=bench) gen, best_de = de.run() gen, best_mfo = mfo.run() gen, best_abc = abc.run() gen, best_pso = pso.run() gen, best_ba = ba.run() gen, best_fa = fa.run() gen, best_ga = ga.run() average_mfo += best_de / epoch average_de += best_mfo / epoch average_abc += best_abc / epoch average_pso += best_pso / epoch average_ba += best_ba / epoch average_fa += best_fa / epoch average_ga += best_ga / epoch print(algo, ': DE Average of Bests over', epoch, 'run: ', average_de) print(algo, ': MFO Average of Bests over', epoch, 'run: ', average_mfo) print(algo, ': ABC Average of Bests over', epoch, 'run: ', average_abc) print(algo, ': PSO Average of Bests over', epoch, 'run: ', average_pso) print(algo, ': BA Average of Bests over', epoch, 'run: ', average_ba) print(algo, ': FA Average of Bests over', epoch, 'run: ', average_fa) print(algo, ': GA Average of Bests over', epoch, 'run: ', average_ga) return [ average_de, average_mfo, average_abc, average_pso, average_ba, average_fa, average_ga ]
def simple_example(runs=10): for i in range(10): algo = GeneticAlgorithm(D=10, NP=40, nFES=100000, Ts=5, Mr=0.5, Cr=0.4, benchmark=MyBenchmark()) Best = algo.run() logger.info('%s %s' % (Best[0], Best[1]))
def logging_example(): task = TaskConvPrint(D=10, nFES=50000, nGEN=50000, benchmark=MyBenchmark()) algo = GeneticAlgorithm(NP=40, Ts=4, Mr=0.2, Cr=0.5, Selection=RouletteSelection, Mutation=MutationUros, Crossover=CrossoverUros, seed=None, task=task) # algo = GeneticAlgorithm(NP=50, Ts=10, Mr=0.5, Cr=0.5, task=task) best = algo.run() logger.info('%s %s' % (best[0], best[1]))
def executeGA(typeBenchmark): task = StoppingTask( D=dimensoes, nFES=numeroAvaliacoes, optType=tipoOtimizacao, logger=True, benchmark=typeBenchmark, ) algo = GeneticAlgorithm( NP=tamanhoPopulacao, Crossover=crossover, Mutation=mutation, Cr=taxaCruzamento, Mr=taxaMutacao, ) best = algo.run(task=task) return [task, best[1], best[0]]
# This is temporary fix to import module from parent folder # It will be removed when package is published on PyPI import sys sys.path.append('../') # End of fix import random from NiaPy.algorithms.basic import GeneticAlgorithm for i in range(10): Algorithm = GeneticAlgorithm(10, 40, 10000, 4, 0.05, 0.4, 'sphere') Best = Algorithm.run() print(Best)
# encoding=utf8 # This is temporary fix to import module from parent folder # It will be removed when package is published on PyPI import sys sys.path.append('../') # End of fix from NiaPy.algorithms.basic import GeneticAlgorithm from NiaPy.algorithms.basic.ga import UniformCrossover, UniformMutation from NiaPy.task import StoppingTask from NiaPy.benchmarks import Sphere # we will run Genetic Algorithm for 5 independent runs for i in range(5): task = StoppingTask(D=10, nFES=10000, benchmark=Sphere()) algo = GeneticAlgorithm(NP=100, Crossover=UniformCrossover, Mutation=UniformMutation, Cr=0.45, Mr=0.9) best = algo.run(task=task) print('%s -> %s' % (best[0], best[1]))
def plot_example(): task = TaskConvPlot(D=50, nFES=50000, nGEN=10000, benchmark=MyBenchmark()) algo = GeneticAlgorithm(NP=40, Ts=5, Mr=0.5, Cr=0.4, task=task) best = algo.run() logger.info('%s %s' % (best[0], best[1])) input('Press [enter] to continue')