def test_BA_evals_fine(self):
     task = StoppingTask(D=10,
                         nFES=1000,
                         optType=OptimizationType.MINIMIZATION,
                         benchmark=Sphere())
     algo = BatAlgorithm(NP=25)
     algo.runTask(task)
     evals = task.evals()
     self.assertEqual(1000, evals)
 def test_BA_iters_to_fes(self):
     task = Task(D=10,
                 nGEN=1000,
                 optType=OptimizationType.MINIMIZATION,
                 benchmark=Sphere())
     algo = BatAlgorithm(task=task, NP=10)
     algo.run()
     evals = algo.task.evals()
     self.assertEqual(evals, 10010)
 def test_BA_iters_fine(self):
     task = StoppingTask(D=10,
                         nGEN=1000,
                         optType=OptimizationType.MINIMIZATION,
                         benchmark=Sphere())
     algo = BatAlgorithm(NP=25)
     algo.runTask(task)
     iters = task.iters()
     self.assertEqual(1000, iters)
 def test_BA_iters_to_fes(self):
     task = StoppingTask(D=10,
                         nGEN=1000,
                         optType=OptimizationType.MINIMIZATION,
                         benchmark=Sphere())
     algo = BatAlgorithm(NP=10)
     algo.runTask(task)
     evals = task.evals()
     self.assertEqual(10000, evals)
 def test_BA_evals_fine(self):
     task = Task(D=10,
                 nFES=1000,
                 optType=OptimizationType.MINIMIZATION,
                 benchmark=Sphere())
     algo = BatAlgorithm(task=task, NP=25)
     algo.run()
     evals = algo.task.evals()
     self.assertEqual(evals, 1000)
 def test_BA_iters_fine(self):
     task = Task(D=10,
                 nGEN=1000,
                 optType=OptimizationType.MINIMIZATION,
                 benchmark=Sphere())
     algo = BatAlgorithm(task=task, NP=25)
     algo.run()
     iters = algo.task.iters()
     self.assertEqual(iters, 1000)
Example #7
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
    ]
Example #8
0
class BATestCase(TestCase):
    def setUp(self):
        self.ba_custom = BatAlgorithm(10, 20, 10000, 0.5, 0.5, 0.0, 2.0,
                                      MyBenchmark())
        self.ba_griewank = BatAlgorithm(10, 40, 10000, 0.5, 0.5, 0.0, 2.0,
                                        'griewank')

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

    def test_griewank_works_fine(self):
        self.assertTrue(self.ba_griewank.run())
Example #9
0
    def setParameters(self, F=0.50, CR=0.90, CrossMutt=CrossBest1, **ukwargs):
        r"""Set core parameters of HybridBatAlgorithm algorithm.

		Arguments:
			F (Optional[float]): Scaling factor.
			CR (Optional[float]): Crossover.

		See Also:
			* :func:`NiaPy.algorithms.basic.BatAlgorithm.setParameters`
		"""
        BatAlgorithm.setParameters(self, **ukwargs)
        self.F, self.CR, self.CrossMutt = F, CR, CrossMutt
Example #10
0
    def setParameters(self, F=0.78, CR=0.35, CrossMutt=CrossBest1, **ukwargs):
        r"""**__init__(self, D, NP, nFES, A, r, Qmin, Qmax, benchmark)**.

		**Arguments:**

		F {decimal} -- scaling factor

		CR {decimal} -- crossover
		"""
        BatAlgorithm.setParameters(self, **ukwargs)
        self.F, self.CR, self.CrossMutt = F, CR, CrossMutt
        if ukwargs: logger.info('Unused arguments: %s' % (ukwargs))
Example #11
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
Example #12
0
 def setUp(self):
     self.ba_custom = BatAlgorithm(D=10,
                                   NP=20,
                                   nFES=1000,
                                   A=0.5,
                                   r=0.5,
                                   Qmin=0.0,
                                   Qmax=2.0,
                                   benchmark=MyBenchmark())
     self.ba_griewank = BatAlgorithm(NP=10,
                                     D=40,
                                     nFES=1000,
                                     A=0.5,
                                     r=0.5,
                                     Qmin=0.0,
                                     Qmax=2.0,
                                     benchmark='griewank')
Example #13
0
 def __init__(self, **kwargs):
     r"""Initialize BA feature selection algorithm.
     """
     self._params = dict(A=ParameterDefinition(MinMax(0.5, 1.0),
                                               param_type=float),
                         r=ParameterDefinition(MinMax(0.0, 0.5),
                                               param_type=float),
                         Qmin=ParameterDefinition(MinMax(0.0, 1.0),
                                                  param_type=float),
                         Qmax=ParameterDefinition(MinMax(1.0, 2.0),
                                                  param_type=float))
     self.__ba = BA(NP=10)
Example #14
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)
Example #15
0
    def typeParameters():
        r"""Get dictionary with functions for checking values of parameters.

		Returns:
			Dict[str, Callable]:
				* F (Callable[[Union[int, float]], bool]): TODO
				* CR (Callable[[float], bool]): TODO
		"""
        d = BatAlgorithm.typeParameters()
        d['F'] = lambda x: isinstance(x, (int, float)) and x > 0
        d['CR'] = lambda x: isinstance(x, float) and 0 <= x <= 1
        return d
Example #16
0
    def setParameters(self,
                      NP=40,
                      A=0.5,
                      r=0.5,
                      Qmin=0.0,
                      Qmax=2.0,
                      F=0.78,
                      CR=0.35,
                      CrossMutt=CrossBest1,
                      **ukwargs):
        r"""Set core parameters of HybridBatAlgorithm algorithm.

		Arguments:
			F (Optional[float]): Scaling factor.
			CR (Optional[float]): Crossover.

		See Also:
			* :func:`NiaPy.algorithms.basic.BatAlgorithm.setParameters`
		"""
        BatAlgorithm.setParameters(self, **ukwargs)
        self.A, self.r, self.Qmin, self.Qmax, self.F, self.CR, self.CrossMutt = A, r, Qmin, Qmax, F, CR, CrossMutt
        if ukwargs: logger.info('Unused arguments: %s' % (ukwargs))
Example #17
0
class BATestCase(TestCase):
    def setUp(self):
        self.ba_custom = BatAlgorithm(D=10,
                                      NP=20,
                                      nFES=1000,
                                      A=0.5,
                                      r=0.5,
                                      Qmin=0.0,
                                      Qmax=2.0,
                                      benchmark=MyBenchmark())
        self.ba_griewank = BatAlgorithm(NP=10,
                                        D=40,
                                        nFES=1000,
                                        A=0.5,
                                        r=0.5,
                                        Qmin=0.0,
                                        Qmax=2.0,
                                        benchmark='griewank')

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

    def test_griewank_works_fine(self):
        self.assertTrue(self.ba_griewank.run())
Example #18
0
 def test_parameter_type(self):
     d = BatAlgorithm.typeParameters()
     self.assertTrue(d['Qmax'](10))
     self.assertTrue(d['Qmin'](10))
     self.assertTrue(d['r'](10))
     self.assertFalse(d['r'](-10))
     self.assertFalse(d['r'](0))
     self.assertFalse(d['A'](0))
     self.assertFalse(d['A'](-19))
     self.assertTrue(d['NP'](10))
     self.assertFalse(d['NP'](-10))
     self.assertFalse(d['NP'](0))
     self.assertTrue(d['A'](10))
     self.assertFalse(d['Qmin'](None))
     self.assertFalse(d['Qmax'](None))
Example #19
0
    def initPopulation(self, task):
        r"""Initialize starting population.

		Args:
			task (Task): Optimization task.

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray[float], Dict[str, Any]]:
				1. Initialized population.
				2. Initialized populations fitness/function values.
				3. Additional arguments:
					* v (numpy.ndarray[float]): TODO
		"""
        Sol, Fitness, _ = BatAlgorithm.initPopulation(self, task)
        v = full([self.NP, task.D], 0.0)
        return Sol, Fitness, {'v': v}
Example #20
0
    def typeParameters():
        r"""Get dictionary with functions for checking values of parameters.

		Returns:
			Dict[str, Callable]:
				* F (Callable[[Union[int, float]], bool]): Scaling factor.
				* CR (Callable[[float], bool]): Crossover probability.

		See Also:
			* :func:`NiaPy.algorithms.basic.BatAlgorithm.typeParameters`
		"""
        d = BatAlgorithm.typeParameters()
        d.update({
            'F': lambda x: isinstance(x, (int, float)) and x > 0,
            'CR': lambda x: isinstance(x, float) and 0 <= x <= 1
        })
        return d
Example #21
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 BatAlgorithm
from NiaPy.util import StoppingTask, OptimizationType
from NiaPy.benchmarks import Sphere

# we will run Bat Algorithm for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10,
                        nGEN=1000,
                        optType=OptimizationType.MINIMIZATION,
                        benchmark=Sphere())
    algo = BatAlgorithm(NP=40, A=0.5, r=0.5, Qmin=0.0, Qmax=2.0)
    best = algo.run(task=task)
    print('%s -> %s' % (best[0], best[1]))
Example #22
0
        def evaluate(D, sol):
            val = 0.0
            for i in range(D):
                val = val + sol[i] * sol[i]
            return val

        return evaluate


# example using custom benchmark "MyBenchmark"
logger.info('Running with custom MyBenchmark...')
for i in range(10):
    Algorithm = BatAlgorithm(D=10,
                             NP=40,
                             nFES=10000,
                             A=0.5,
                             r=0.5,
                             Qmin=0.0,
                             Qmax=2.0,
                             benchmark=MyBenchmark())
    Best = Algorithm.run()
    logger.info(Best)

# example using predifined benchmark function
# available benchmarks are:
# - griewank
# - rastrigin
# - rosenbrock
# - sphere
logger.info('Running with default Griewank benchmark...')

griewank = Griewank()
Example #23
0
class BatAlgorithm(FeatureSelectionAlgorithm):
    r"""Implementation of feature selection using BA 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 = 'Bat Algorithm'

    def __init__(self, **kwargs):
        r"""Initialize BA feature selection algorithm.
        """
        self._params = dict(A=ParameterDefinition(MinMax(0.5, 1.0),
                                                  param_type=float),
                            r=ParameterDefinition(MinMax(0.0, 0.5),
                                                  param_type=float),
                            Qmin=ParameterDefinition(MinMax(0.0, 1.0),
                                                     param_type=float),
                            Qmax=ParameterDefinition(MinMax(1.0, 2.0),
                                                     param_type=float))
        self.__ba = BA(NP=10)

    def set_parameters(self, **kwargs):
        r"""Set the parameters/arguments of the algorithm.
        """
        kwargs['NP'] = self.__ba.NP
        self.__ba.setParameters(**kwargs)

    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:
            pandas.core.frame.DataFrame: 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.__ba.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.__ba.getParameters()))
Example #24
0
 def typeParameters():
     d = BatAlgorithm.typeParameters()
     d['F'] = lambda x: isinstance(x, (int, float)) and x > 0
     d['CR'] = lambda x: isinstance(x, float) and 0 <= x <= 1
     return d
Example #25
0
 def setUp(self):
     self.ba_custom = BatAlgorithm(10, 20, 10000, 0.5, 0.5, 0.0, 2.0,
                                   MyBenchmark())
     self.ba_griewank = BatAlgorithm(10, 40, 10000, 0.5, 0.5, 0.0, 2.0,
                                     'griewank')
Example #26
0
        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


# example using custom benchmark "MyBenchmark"
logger.info('Running with custom MyBenchmark...')
for i in range(10):
    Algorithm = BatAlgorithm(10, 40, 10000, 0.5, 0.5, 0.0, 2.0, MyBenchmark())
    Best = Algorithm.run()

    logger.info(Best)

# example using predifined benchmark function
# available benchmarks are:
# - griewank
# - rastrigin
# - rosenbrock
# - sphere
logger.info('Running with default Griewank benchmark...')

griewank = Griewank()

for i in range(10):
Example #27
0
File: hba.py Project: tuahk/NiaPy
 def setParameters(self, **kwargs):
     BatAlgorithm.setParameters(self, **kwargs)
     self.__setParams(**kwargs)
Example #28
0
File: hba.py Project: tuahk/NiaPy
 def __init__(self, **kwargs):
     BatAlgorithm.__init__(self,
                           name='HybridBatAlgorithm',
                           sName='HBA',
                           **kwargs)
Example #29
0
 def test_custom_works_fine(self):
     ba_custom = BatAlgorithm(D=self.D, NP=20, nFES=self.nFES, nGEN=self.nGEN, A=0.5, r=0.5, Qmin=0.0, Qmax=2.0, benchmark=MyBenchmark(), seed=self.seed)
     ba_customc = BatAlgorithm(D=self.D, NP=20, nFES=self.nFES, nGEN=self.nGEN, A=0.5, r=0.5, Qmin=0.0, Qmax=2.0, benchmark=MyBenchmark(), seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ba_custom, ba_customc)
Example #30
0
 def test_griewank_works_fine(self):
     ba_griewank = BatAlgorithm(NP=10, D=self.D, nFES=self.nFES, nGEN=self.nGEN, A=0.5, r=0.5, Qmin=0.0, Qmax=2.0, benchmark='griewank', seed=self.seed)
     ba_griewankc = BatAlgorithm(NP=10, D=self.D, nFES=self.nFES, nGEN=self.nGEN, A=0.5, r=0.5, Qmin=0.0, Qmax=2.0, benchmark='griewank', seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, ba_griewank, ba_griewankc)