def test_FA_evals_fine(self):
     task = StoppingTask(D=10,
                         nFES=1000,
                         optType=OptimizationType.MINIMIZATION,
                         benchmark=Sphere())
     algo = FireflyAlgorithm(NP=25)
     algo.runTask(task)
     evals = task.evals()
     self.assertEqual(1000, evals)
 def test_FA_iters_fine(self):
     task = StoppingTask(D=10,
                         nGEN=1000,
                         optType=OptimizationType.MINIMIZATION,
                         benchmark=Sphere())
     algo = FireflyAlgorithm(NP=25)
     algo.runTask(task)
     iters = task.iters()
     self.assertEqual(1000, iters)
Пример #3
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
    ]
Пример #4
0
def run_defult():
	for i in range(10):
		Algorithm = FireflyAlgorithm(D=10, NP=20, nFES=50000, alpha=0.5, betamin=0.2, gamma=1.0, benchmark=MyBenchmark())
		Best = Algorithm.run()
		plt.plot(global_vector)
		global_vector = []
		logger.info(Best)
	plt.xlabel('Number of evaluations')
	plt.ylabel('Fitness function value')
	plt.title('Convergence plot')
	plt.show()
Пример #5
0
class FATestCase(TestCase):
    def setUp(self):
        self.fa = FireflyAlgorithm(10, 20, 1000, 0.5, 0.2, 1.0, MyBenchmark())

        self.fa_griewank = FireflyAlgorithm(10, 20, 1000, 0.5, 0.2, 1.0,
                                            'griewank')

    def test_works_fine(self):
        self.assertTrue(self.fa.run())

    def test_griewank_works_fine(self):
        self.assertTrue(self.fa_griewank.run())
Пример #6
0
    def __init__(self, algorithm_name, objective, maxfeval, population=30):
        super().__init__(algorithm_name, objective, maxfeval)
        self.algorithm_name = algorithm_name
        self.algo = None  #this is the suggest function from hyperopt
        if algorithm_name not in __all__:
            raise Exception('NiaPy does not have algorithm :' +
                            str(algorithm_name))

        elif self.algorithm_name == 'NiaPyABC':
            self.algo = ArtificialBeeColonyAlgorithm(NP=population, Limit=100)

        elif self.algorithm_name == 'NiaPyBat':
            self.algo = BatAlgorithm(NP=population)

        elif self.algorithm_name == 'NiaPyCuckooSearch':
            self.algo = CuckooSearch(N=population, pa=0.2, alpha=0.5)

        elif self.algorithm_name == 'NiaPyDifferentialEvolution':
            self.algo = DifferentialEvolution(NP=population, F=1, CR=0.8)

        elif self.algorithm_name == 'NiaPyFireflyAlgorithm':
            self.algo = FireflyAlgorithm(NP=population,
                                         alpha=0.5,
                                         betamin=0.2,
                                         gamma=1.0)

        elif self.algorithm_name == 'NiaPyGeneticAlgorithm':
            self.algo = FireflyAlgorithm(NP=population,
                                         Crossover=UniformCrossover,
                                         Mutation=UniformMutation,
                                         Cr=0.45,
                                         Mr=0.9)

        elif self.algorithm_name == 'NiaPyGWO':
            self.algo = GreyWolfOptimizer(NP=population)
        # config
        elif self.algorithm_name == 'NiaPyNelderMead':
            self.algo = NelderMeadMethod()
        # config
        elif self.algorithm_name == 'NiaPyPSO':
            self.algo = ParticleSwarmAlgorithm(NP=population,
                                               C1=2,
                                               C2=2,
                                               w=0.9,
                                               vMin=-1.5,
                                               vMax=1.5)

        # config

        # config
        # config
        elif self.algorithm_name == 'NiaPySimulatedAnnealing':
            self.algo = SimulatedAnnealing(coolingMethod=coolLinear)
Пример #7
0
def simple_example(runs=10,
                   D=10,
                   nFES=50000,
                   seed=None,
                   optType=OptimizationType.MINIMIZATION,
                   optFunc=MinMB,
                   **no):
    for i in range(10):
        algo = FireflyAlgorithm(D=D,
                                NP=20,
                                nFES=nFES,
                                alpha=0.5,
                                betamin=0.2,
                                gamma=1.0,
                                seed=seed,
                                optType=optType,
                                benchmark=optFunc())
        Best = algo.run()
        logger.info('%s %s' % (Best[0], Best[1]))
Пример #8
0
def logging_example(D=10,
                    nFES=50000,
                    seed=None,
                    optType=OptimizationType.MINIMIZATION,
                    optFunc=MinMB,
                    **no):
    task = TaskConvPrint(D=D,
                         nFES=nFES,
                         nGEN=50000,
                         optType=optType,
                         benchmark=optFunc())
    algo = FireflyAlgorithm(NP=20,
                            alpha=0.5,
                            betamin=0.2,
                            gamma=1.0,
                            seed=seed,
                            task=task)
    best = algo.run()
    logger.info('%s %s' % (best[0], best[1]))
Пример #9
0
 def test_type_parameters(self):
     d = FireflyAlgorithm.typeParameters()
     self.assertTrue(d['alpha'](10))
     self.assertFalse(d['alpha'](-10))
     self.assertTrue(d['betamin'](10))
     self.assertFalse(d['betamin'](-10))
     self.assertTrue(d['gamma'](10))
     self.assertFalse(d['gamma'](-10))
     self.assertTrue(d['NP'](1))
     self.assertFalse(d['NP'](0))
     self.assertFalse(d['NP'](-1))
Пример #10
0
 def test_griewank_works_fine(self):
     fa_griewank = FireflyAlgorithm(D=self.D,
                                    NP=20,
                                    nFES=self.nFES,
                                    nGEN=self.nGEN,
                                    alpha=0.5,
                                    betamin=0.2,
                                    gamma=1.0,
                                    benchmark='griewank',
                                    seed=self.seed)
     fa_griewankc = FireflyAlgorithm(D=self.D,
                                     NP=20,
                                     nFES=self.nFES,
                                     nGEN=self.nGEN,
                                     alpha=0.5,
                                     betamin=0.2,
                                     gamma=1.0,
                                     benchmark='griewank',
                                     seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, fa_griewank, fa_griewankc)
Пример #11
0
 def test_works_fine(self):
     fa = FireflyAlgorithm(D=self.D,
                           NP=20,
                           nFES=self.nFES,
                           nGEN=self.nGEN,
                           alpha=0.5,
                           betamin=0.2,
                           gamma=1.0,
                           benchmark=MyBenchmark(),
                           seed=self.seed)
     fac = FireflyAlgorithm(D=self.D,
                            NP=20,
                            nFES=self.nFES,
                            nGEN=self.nGEN,
                            alpha=0.5,
                            betamin=0.2,
                            gamma=1.0,
                            benchmark=MyBenchmark(),
                            seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, fa, fac)
Пример #12
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 FireflyAlgorithm

algo = FireflyAlgorithm()
print(algo.algorithmInfo())
Пример #13
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 FireflyAlgorithm
from NiaPy.util import StoppingTask, OptimizationType
from NiaPy.benchmarks import Sphere

# we will run Firefly Algorithm for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10,
                        nFES=1000,
                        optType=OptimizationType.MINIMIZATION,
                        benchmark=Sphere())
    algo = FireflyAlgorithm(NP=20, alpha=0.5, betamin=0.2, gamma=1.0)
    best = algo.run(task=task)
    print('%s -> %s' % (best[0], best[1]))
Пример #14
0
    def __get_algorithm(algorithm, population_size, random_state):
        if isinstance(algorithm, str):
            if algorithm not in SUPPORTED_ALGORITHMS:
                raise ValueError(
                    f'"{algorithm}" is not in supported algorithms: {", ".join(SUPPORTED_ALGORITHMS)}'
                )

            algorithm_obj = None

            if algorithm == 'fa':
                algorithm_obj = FireflyAlgorithm(seed=random_state)
                algorithm_obj.setParameters(alpha=1, betamin=1, gamma=2)
            elif algorithm == 'ba':
                algorithm_obj = BatAlgorithm(seed=random_state)
                algorithm_obj.setParameters(A=0.9, r=0.1, Qmin=0.0, Qmax=2.0)
            elif algorithm == 'hba':
                algorithm_obj = HybridBatAlgorithm(seed=random_state)
                algorithm_obj.setParameters(A=0.9, r=0.1, Qmin=0.0, Qmax=2.0)
            elif algorithm == 'hsaba':
                algorithm_obj = HybridSelfAdaptiveBatAlgorithm(
                    seed=random_state)
                algorithm_obj.setParameters(A=0.9, r=0.1, Qmin=0.0, Qmax=2.0)
            elif algorithm == 'gwo':
                algorithm_obj = GreyWolfOptimizer(seed=random_state)

            algorithm_obj.setParameters(NP=population_size)

            return algorithm_obj

        return algorithm
Пример #15
0
    def setUp(self):
        self.fa = FireflyAlgorithm(10, 20, 1000, 0.5, 0.2, 1.0, MyBenchmark())

        self.fa_griewank = FireflyAlgorithm(10, 20, 1000, 0.5, 0.2, 1.0,
                                            'griewank')
Пример #16
0
import logging
from NiaPy.algorithms.basic import FireflyAlgorithm

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 = FireflyAlgorithm(10, 20, 10000, 0.5, 0.2, 1.0, MyBenchmark())
    Best = Algorithm.run()

    logger.info(Best)
Пример #17
0
 def test_works_fine(self):
     fa = FireflyAlgorithm(NP=20, alpha=0.5, betamin=0.2, gamma=1.0, seed=self.seed)
     fac = FireflyAlgorithm(NP=20, alpha=0.5, betamin=0.2, gamma=1.0, seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, fa, fac, MyBenchmark())
Пример #18
0
 def test_griewank_works_fine(self):
     fa_griewank = FireflyAlgorithm(NP=20, alpha=0.5, betamin=0.2, gamma=1.0, seed=self.seed)
     fa_griewankc = FireflyAlgorithm(NP=20, alpha=0.5, betamin=0.2, gamma=1.0, seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, fa_griewank, fa_griewankc)