Exemplo n.º 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())
Exemplo n.º 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())
Exemplo n.º 3
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.º 4
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.º 5
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.º 6
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()))
Exemplo n.º 7
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)