Beispiel #1
0
class GWOTestCase(TestCase):
    def setUp(self):
        self.gwo_custom = GreyWolfOptimizer(10, 20, 1000, MyBenchmark())
        self.gwo_sphere = GreyWolfOptimizer(10, 20, 1000, 'sphere')

    def test_custom_works_fine(self):
        self.assertTrue(self.gwo_custom.run())

    def test_sphere_works_fine(self):
        self.assertTrue(self.gwo_sphere.run())
Beispiel #2
0
class GWOTestCase(TestCase):

    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')

    def test_custom_works_fine(self):
        self.assertTrue(self.gwo_custom.run())

    def test_sphere_works_fine(self):
        self.assertTrue(self.gwo_sphere.run())
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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
Beispiel #6
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))
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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())
Beispiel #11
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])
Beispiel #12
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')
Beispiel #13
0
class GreyWolfOptimizer(FeatureSelectionAlgorithm):
    r"""Implementation of feature selection using GWO algorithm.

    Date:
        2020
    
    Author:
        Luka Pečnik

    Reference:
        The implementation is adapted according to the following article:
        D. Fister, I. Fister, T. Jagrič, I. Fister Jr., J. Brest. A novel self-adaptive differential evolution for feature selection using threshold mechanism . In: Proceedings of the 2018 IEEE Symposium on Computational Intelligence (SSCI 2018), pp. 17-24, 2018.
    
    Reference URL: 
        http://iztok-jr-fister.eu/static/publications/236.pdf   

    License:
        MIT

    See Also:
        * :class:`niaaml.preprocessing.feature_selection.feature_selection_algorithm.FeatureSelectionAlgorithm`
    """
    Name = 'Grey Wolf Optimizer'

    def __init__(self, **kwargs):
        r"""Initialize GWO feature selection algorithm.
        """
        super(GreyWolfOptimizer, self).__init__()
        self.__gwo = GWO(NP=10)

    def __final_output(self, sol):
        r"""Calculate final array of features.

        Arguments:
            sol (numpy.ndarray[float]): Individual of population/ possible solution.

        Returns:
            numpy.ndarray[bool]: Mask of selected features.
        """
        selected = numpy.ones(sol.shape[0] - 1, dtype=bool)
        threshold = sol[sol.shape[0] - 1]
        for i in range(sol.shape[0] - 1):
            if sol[i] < threshold:
                selected[i] = False
        return selected

    def select_features(self, x, y, **kwargs):
        r"""Perform the feature selection process.

        Arguments:
            x (pandas.core.frame.DataFrame): Array of original features.
            y (pandas.core.series.Series) Expected classifier results.

        Returns:
            numpy.ndarray[bool]: Mask of selected features.
        """
        num_features = x.shape[1]
        benchmark = _FeatureSelectionThresholdBenchmark(x, y)
        task = StoppingTask(D=num_features + 1, nFES=1000, benchmark=benchmark)
        best = self.__gwo.run(task)
        return self.__final_output(benchmark.get_best_solution())

    def to_string(self):
        r"""User friendly representation of the object.

        Returns:
            str: User friendly representation of the object.
        """
        return FeatureSelectionAlgorithm.to_string(self).format(
            name=self.Name,
            args=self._parameters_to_string(self.__gwo.getParameters()))
Beispiel #14
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)
Beispiel #15
0
 def __init__(self, **kwargs):
     r"""Initialize GWO feature selection algorithm.
     """
     super(GreyWolfOptimizer, self).__init__()
     self.__gwo = GWO(NP=10)
Beispiel #16
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))
Beispiel #17
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)
Beispiel #18
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)
Beispiel #19
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)
Beispiel #20
0
 def setUp(self):
     self.gwo_custom = GreyWolfOptimizer(10, 20, 1000, MyBenchmark())
     self.gwo_sphere = GreyWolfOptimizer(10, 20, 1000, 'sphere')
Beispiel #21
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)
Beispiel #22
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)
Beispiel #23
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)