Exemplo n.º 1
0
 def test_griewank_works_fine(self):
     gwo_griewank = GreyWolfOptimizer(NP=10,
                                      D=self.D,
                                      nFES=self.nFES,
                                      nGEN=self.nGEN,
                                      benchmark='griewank',
                                      seed=self.seed)
     gwo_griewankc = GreyWolfOptimizer(NP=10,
                                       D=self.D,
                                       nFES=self.nFES,
                                       nGEN=self.nGEN,
                                       benchmark='griewank',
                                       seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, gwo_griewank, gwo_griewankc)
Exemplo n.º 2
0
 def test_custom_works_fine(self):
     gwo_custom = GreyWolfOptimizer(D=self.D,
                                    NP=20,
                                    nFES=self.nFES,
                                    nGEN=self.nGEN,
                                    benchmark=MyBenchmark(),
                                    seed=self.seed)
     gwo_customc = GreyWolfOptimizer(D=self.D,
                                     NP=20,
                                     nFES=self.nFES,
                                     nGEN=self.nGEN,
                                     benchmark=MyBenchmark(),
                                     seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, gwo_custom, gwo_customc)
Exemplo n.º 3
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
Exemplo n.º 4
0
 def test_get_algorithm_fine(self):
     algorithm = MyCustomAlgorithm()
     gwo = GreyWolfOptimizer()
     self.assertEqual(algorithm,
                      self.algorithm_utility.get_algorithm(algorithm))
     self.assertEqual(gwo, self.algorithm_utility.get_algorithm(gwo))
     self.assertTrue(
         isinstance(
             self.algorithm_utility.get_algorithm("GreyWolfOptimizer"),
             GreyWolfOptimizer))
Exemplo n.º 5
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)
Exemplo n.º 6
0
 def test_griewank_works_fine(self):
     gwo_griewank = GreyWolfOptimizer(NP=10, seed=self.seed)
     gwo_griewankc = GreyWolfOptimizer(NP=10, seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, gwo_griewank, gwo_griewankc)
Exemplo n.º 7
0
logging.basicConfig()
logger = logging.getLogger('examples')
logger.setLevel('INFO')

# For reproducive results
random.seed(1234)


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 = GreyWolfOptimizer(D=10,
                                  NP=20,
                                  nFES=10000,
                                  benchmark=MyBenchmark())
    Best = Algorithm.run()
    logger.info(Best)
Exemplo n.º 8
0
 def setUp(self):
     self.gwo_custom = GreyWolfOptimizer(D=10, NP=20, nFES=1000, benchmark=MyBenchmark())
     self.gwo_sphere = GreyWolfOptimizer(NP=10, D=20, nFES=1000, benchmark='sphere')
Exemplo n.º 9
0
    Ackley,
    Griewank,
    Sphere,
    HappyCat
)


"""Example demonstrating the use of NiaPy Runner."""


runner = Runner(
    D=40,
    nFES=100,
    nRuns=2,
    useAlgorithms=[
        GreyWolfOptimizer(),
        "FlowerPollinationAlgorithm",
        ParticleSwarmAlgorithm(),
        "HybridBatAlgorithm",
        "SimulatedAnnealing",
        "CuckooSearch"],
    useBenchmarks=[
        Ackley(),
        Griewank(),
        Sphere(),
        HappyCat(),
        "rastrigin"]
)

print(runner.run(verbose=True))
Exemplo n.º 10
0
            val = 0.0
            for i in range(D):
                val += sol[i]**2
            return val

        return evaluate


# custom initialization population function
def MyInit(task, NP, rnd=rand, **kwargs):
    pop = 0.2 + rnd.rand(NP, task.D) * task.bRange
    fpop = apply_along_axis(task.eval, 1, pop)
    return pop, fpop


# we will run 10 repetitions of Grey Wolf Optimizer against our custom MyBenchmark benchmark function
for i in range(10):
    task = StoppingTask(D=20,
                        nGEN=100,
                        optType=OptimizationType.MINIMIZATION,
                        benchmark=MyBenchmark())

    # parameter is population size
    algo = GreyWolfOptimizer(NP=20, InitPopFunc=MyInit)

    # running algorithm returns best found minimum
    best = algo.run(task)

    # printing best minimum
    print(best[-1])
Exemplo n.º 11
0
import logging
from NiaPy.algorithms.basic import GreyWolfOptimizer

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

    logger.info(Best)
Exemplo n.º 12
0
 def setUp(self):
     self.gwo_custom = GreyWolfOptimizer(10, 20, 1000, MyBenchmark())
     self.gwo_sphere = GreyWolfOptimizer(10, 20, 1000, 'sphere')
Exemplo n.º 13
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

from NiaPy.algorithms.basic import GreyWolfOptimizer
from NiaPy.task import StoppingTask

# we will run 10 repetitions of Grey Wolf Optimizer against Pinter benchmark function
for i in range(10):
    task = StoppingTask(D=10, nFES=1000, benchmark='pinter')
    algorithm = GreyWolfOptimizer(NP=20)
    best = algorithm.run(task)
    print(best)
Exemplo n.º 14
0
 def test_custom_works_fine(self):
     gwo_custom = GreyWolfOptimizer(NP=20, seed=self.seed)
     gwo_customc = GreyWolfOptimizer(NP=20, seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, gwo_custom, gwo_customc,
                                          MyBenchmark())
Exemplo n.º 15
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

from NiaPy.algorithms.basic import GreyWolfOptimizer
from NiaPy.benchmarks import Pinter

# initialize Pinter benchamrk with custom bound
pinterCustom = Pinter(-5, 5)

# we will run 10 repetitions of Grey Wolf Optimizer against Pinter benchmark function
for i in range(10):
    # first parameter takes dimension of problem
    # second parameter is population size
    # third parameter takes the number of function evaluations
    # fourth parameter is benchmark function
    algorithm = GreyWolfOptimizer(10, 20, 10000, pinterCustom)

    # running algorithm returns best found minimum
    best = algorithm.run()

    # printing best minimum
    print(best)
Exemplo n.º 16
0
from NiaPy.algorithms.basic import GreyWolfOptimizer


# we will run 10 repetitions of Grey Wolf Optimizer against Pinter benchmark function
for i in range(10):
    # first paradeFordPrefect
    # meter takes dimension of problem
    # second parameter is population size
    # third parameter takes the number of function evaluations
    # fourth parameter is benchmark function
    algorithm = GreyWolfOptimizer(10, 20, 10000, 'pinter')

    # running algorithm returns best found minimum
    best = algorithm.run()

    # printing best minimum
    print(best)
Exemplo n.º 17
0
# End of fix

import logging
from NiaPy.algorithms.basic import GreyWolfOptimizer

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 = GreyWolfOptimizer(10, 20, 10000, 'sphere')
    Best = Algorithm.run()

    logger.info(Best)
Exemplo n.º 18
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 GreyWolfOptimizer
from NiaPy.util import StoppingTask, OptimizationType
from NiaPy.benchmarks import Sphere

# we will run Grey Wolf Optimizer for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10,
                        nFES=10000,
                        optType=OptimizationType.MINIMIZATION,
                        benchmark=Sphere())
    algo = GreyWolfOptimizer(NP=40)
    best = algo.run(task=task)
    print(best)
Exemplo n.º 19
0
    def __init__(self):
        Benchmark.__init__(self, -10, 10)

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

        return evaluate


runner = Runner(D=40,
                nFES=100,
                nRuns=2,
                useAlgorithms=[
                    GreyWolfOptimizer(), "FlowerPollinationAlgorithm",
                    ParticleSwarmAlgorithm(), "HybridBatAlgorithm",
                    "SimulatedAnnealing", "CuckooSearch"
                ],
                useBenchmarks=[
                    Ackley(),
                    Griewank(),
                    Sphere(),
                    HappyCat(), "rastrigin",
                    MyBenchmark()
                ])

runner.run(export='json', verbose=True)