Beispiel #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
    ]
Beispiel #2
0
 def test_custom_works_fine(self):
     mfo_custom = MothFlameOptimizer(D=self.D,
                                     NP=20,
                                     nFES=self.nFES,
                                     nGEN=self.nGEN,
                                     benchmark=MyBenchmark(),
                                     seed=self.seed)
     mfo_customc = MothFlameOptimizer(D=self.D,
                                      NP=20,
                                      nFES=self.nFES,
                                      nGEN=self.nGEN,
                                      benchmark=MyBenchmark(),
                                      seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, mfo_custom, mfo_customc)
Beispiel #3
0
 def test_griewank_works_fine(self):
     mfo_griewank = MothFlameOptimizer(NP=10,
                                       D=self.D,
                                       nFES=self.nFES,
                                       nGEN=self.nGEN,
                                       benchmark='griewank',
                                       seed=self.seed)
     mfo_griewankc = MothFlameOptimizer(NP=10,
                                        D=self.D,
                                        nFES=self.nFES,
                                        nGEN=self.nGEN,
                                        benchmark='griewank',
                                        seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, mfo_griewank, mfo_griewankc)
Beispiel #4
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

import random
from NiaPy.algorithms.basic import MothFlameOptimizer
from NiaPy.util import StoppingTask, OptimizationType
from NiaPy.benchmarks import Sphere

#we will run Moth Flame Optimizer for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10,
                        nGEN=1000,
                        optType=OptimizationType.MINIMIZATION,
                        benchmark=Sphere())
    algo = MothFlameOptimizer(NP=30)
    best = algo.run(task)
    print(best)
Beispiel #5
0
from NiaPy.algorithms.basic import MothFlameOptimizer

logging.basicConfig()
logger = logging.getLogger('examples')
logger.setLevel('INFO')


class MyBenchmark(object):
    def __init__(self):
        self.Lower = -11
        self.Upper = 11

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

        return evaluate


for i in range(10):
    Algorithm = MothFlameOptimizer(D=10,
                                   NP=20,
                                   nGEN=10000,
                                   seed=1234,
                                   benchmark=MyBenchmark())
    Best = Algorithm.run()
    logger.info(Best)