Exemplo n.º 1
0
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
    ]
Exemplo n.º 2
0
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())
Exemplo n.º 3
0
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]))
Exemplo n.º 4
0
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())
Exemplo n.º 5
0
 def test_griewank_works_fine(self):
     ga_griewank = GeneticAlgorithm(NP=40,
                                    Ts=4,
                                    Mr=0.05,
                                    Cr=0.4,
                                    seed=self.seed)
     ga_griewankc = GeneticAlgorithm(NP=40,
                                     Ts=4,
                                     Mr=0.05,
                                     Cr=0.4,
                                     seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_griewank, ga_griewankc)
Exemplo n.º 6
0
 def test_custom_works_fine(self):
     ga_custom = GeneticAlgorithm(NP=40,
                                  Ts=4,
                                  Mr=0.05,
                                  Cr=0.4,
                                  seed=self.seed)
     ga_customc = GeneticAlgorithm(NP=40,
                                   Ts=4,
                                   Mr=0.05,
                                   Cr=0.4,
                                   seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_custom, ga_customc,
                                          MyBenchmark())
Exemplo n.º 7
0
 def test_mutation_urso(self):
     ga_crmt = GeneticAlgorithm(NP=40,
                                Ts=4,
                                Mr=0.05,
                                Cr=0.4,
                                Mutation=MutationUros,
                                seed=self.seed)
     ga_crmtc = GeneticAlgorithm(NP=40,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=0.4,
                                 Mutation=MutationUros,
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_crmt, ga_crmtc)
Exemplo n.º 8
0
 def test_multi_point_crossover_fine(self):
     ga_mpcr = GeneticAlgorithm(NP=40,
                                Ts=4,
                                Mr=0.05,
                                Cr=4,
                                Crossover=MultiPointCrossover,
                                seed=self.seed)
     ga_mpcrc = GeneticAlgorithm(NP=40,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=4,
                                 Crossover=MultiPointCrossover,
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_mpcr, ga_mpcrc)
Exemplo n.º 9
0
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]))
Exemplo n.º 10
0
 def test_reulete_selection(self):
     ga_crmt = GeneticAlgorithm(NP=40,
                                Ts=4,
                                Mr=0.05,
                                Cr=0.4,
                                Selection=RouletteSelection,
                                seed=self.seed)
     ga_crmtc = GeneticAlgorithm(NP=40,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=0.4,
                                 Selection=RouletteSelection,
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_crmt, ga_crmtc)
Exemplo n.º 11
0
 def test_creep_mutation_fine(self):
     ga_crmt = GeneticAlgorithm(NP=40,
                                Ts=4,
                                Mr=0.05,
                                Cr=0.4,
                                Mutation=CreepMutation,
                                seed=self.seed)
     ga_crmtc = GeneticAlgorithm(NP=40,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=0.4,
                                 Mutation=CreepMutation,
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_crmt, ga_crmtc)
Exemplo n.º 12
0
 def test_crossover_urso(self):
     ga_crmt = GeneticAlgorithm(NP=40,
                                Ts=4,
                                Mr=0.05,
                                Cr=0.4,
                                Crossover=CrossoverUros,
                                seed=self.seed)
     ga_crmtc = GeneticAlgorithm(NP=40,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=0.4,
                                 Crossover=CrossoverUros,
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_crmt, ga_crmtc)
Exemplo n.º 13
0
 def test_two_point_crossover_fine_c(self):
     ga_tpcr = GeneticAlgorithm(NP=40,
                                Ts=4,
                                Mr=0.05,
                                Cr=0.4,
                                Crossover=TwoPointCrossover,
                                seed=self.seed)
     ga_tpcrc = GeneticAlgorithm(NP=40,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=0.4,
                                 Crossover=TwoPointCrossover,
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_tpcr, ga_tpcrc,
                                          MyBenchmark())
Exemplo n.º 14
0
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]]
Exemplo n.º 15
0
 def test_custom_works_fine(self):
     ga_custom = GeneticAlgorithm(D=self.D,
                                  NP=40,
                                  nFES=self.nFES,
                                  nGEN=self.nGEN,
                                  Ts=4,
                                  Mr=0.05,
                                  Cr=0.4,
                                  benchmark=MyBenchmark(),
                                  seed=self.seed)
     ga_customc = GeneticAlgorithm(D=self.D,
                                   NP=40,
                                   nFES=self.nFES,
                                   nGEN=self.nGEN,
                                   Ts=4,
                                   Mr=0.05,
                                   Cr=0.4,
                                   benchmark=MyBenchmark(),
                                   seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_custom, ga_customc)
Exemplo n.º 16
0
 def test_griewank_works_fine(self):
     ga_griewank = GeneticAlgorithm(D=self.D,
                                    NP=40,
                                    nFES=self.nFES,
                                    nGEN=self.nGEN,
                                    Ts=4,
                                    Mr=0.05,
                                    Cr=0.4,
                                    benchmark='griewank',
                                    seed=self.seed)
     ga_griewankc = GeneticAlgorithm(D=self.D,
                                     NP=40,
                                     nFES=self.nFES,
                                     nGEN=self.nGEN,
                                     Ts=4,
                                     Mr=0.05,
                                     Cr=0.4,
                                     benchmark='griewank',
                                     seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_griewank, ga_griewankc)
Exemplo n.º 17
0
 def test_creep_mutation_fine(self):
     ga_crmt = GeneticAlgorithm(D=self.D,
                                NP=40,
                                nFES=self.nFES,
                                nGEN=self.nGEN,
                                Ts=4,
                                Mr=0.05,
                                Cr=0.4,
                                Mutation=CreepMutation,
                                benchmark='griewank',
                                seed=self.seed)
     ga_crmtc = GeneticAlgorithm(D=self.D,
                                 NP=40,
                                 nFES=self.nFES,
                                 nGEN=self.nGEN,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=0.4,
                                 Mutation=CreepMutation,
                                 benchmark='griewank',
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_crmt, ga_crmtc)
Exemplo n.º 18
0
 def test_multi_point_crossover_fine(self):
     ga_mpcr = GeneticAlgorithm(D=self.D,
                                NP=40,
                                nFES=self.nFES,
                                nGEN=self.nGEN,
                                Ts=4,
                                Mr=0.05,
                                Cr=4,
                                Crossover=MultiPointCrossover,
                                benchmark='griewank',
                                seed=self.seed)
     ga_mpcrc = GeneticAlgorithm(D=self.D,
                                 NP=40,
                                 nFES=self.nFES,
                                 nGEN=self.nGEN,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=4,
                                 Crossover=MultiPointCrossover,
                                 benchmark='griewank',
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_mpcr, ga_mpcrc)
Exemplo n.º 19
0
 def test_crossover_urso(self):
     ga_crmt = GeneticAlgorithm(D=self.D,
                                NP=40,
                                nFES=self.nFES,
                                nGEN=self.nGEN,
                                Ts=4,
                                Mr=0.05,
                                Cr=0.4,
                                Crossover=CrossoverUros,
                                benchmark='griewank',
                                seed=self.seed)
     ga_crmtc = GeneticAlgorithm(D=self.D,
                                 NP=40,
                                 nFES=self.nFES,
                                 nGEN=self.nGEN,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=0.4,
                                 Crossover=CrossoverUros,
                                 benchmark='griewank',
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_crmt, ga_crmtc)
Exemplo n.º 20
0
 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')
Exemplo n.º 21
0
 def test_reulete_selection(self):
     ga_crmt = GeneticAlgorithm(D=self.D,
                                NP=40,
                                nFES=self.nFES,
                                nGEN=self.nGEN,
                                Ts=4,
                                Mr=0.05,
                                Cr=0.4,
                                Selection=RouletteSelection,
                                benchmark='griewank',
                                seed=self.seed)
     ga_crmtc = GeneticAlgorithm(D=self.D,
                                 NP=40,
                                 nFES=self.nFES,
                                 nGEN=self.nGEN,
                                 Ts=4,
                                 Mr=0.05,
                                 Cr=0.4,
                                 Selection=RouletteSelection,
                                 benchmark='griewank',
                                 seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ga_crmt, ga_crmtc)
Exemplo n.º 22
0
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')
Exemplo n.º 23
0
# 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]))
Exemplo n.º 24
0
 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')
Exemplo n.º 25
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


def Fun(D, sol):
    val = 0.0
    for i in range(D):
        val = val + sol[i] * sol[i]
    return val


for i in range(10):
    Algorithm = GeneticAlgorithm(10, 40, 10000, 4, 0.05, 0.0, 2.0, Fun)
    Best = Algorithm.run()

    print(Best.toString())
Exemplo n.º 26
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)
Exemplo n.º 27
0
# 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 MutationUros, CrossoverUros
from NiaPy.task.task import StoppingTask, OptimizationType
from NiaPy.benchmarks import Sphere

# we will run Fireworks Algorithm for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10,
                        nFES=4000,
                        optType=OptimizationType.MINIMIZATION,
                        benchmark=Sphere())
    algo = GeneticAlgorithm(NP=100,
                            Crossover=CrossoverUros,
                            Mutation=MutationUros,
                            Cr=0.45,
                            Mr=0.9)
    best = algo.run(task=task)
    print('%s -> %s' % (best[0].x, best[1]))

# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3