Beispiel #1
0
 def test_custom_works_fine(self):
     hba_custom = HybridBatAlgorithm(D=self.D,
                                     NP=40,
                                     nFES=self.nFES,
                                     nGEN=self.nGEN,
                                     A=0.5,
                                     r=0.5,
                                     F=0.5,
                                     CR=0.9,
                                     Qmin=0.0,
                                     Qmax=2.0,
                                     benchmark=MyBenchmark(),
                                     seed=self.seed)
     hba_customc = HybridBatAlgorithm(D=self.D,
                                      NP=40,
                                      nFES=self.nFES,
                                      nGEN=self.nGEN,
                                      A=0.5,
                                      r=0.5,
                                      F=0.5,
                                      CR=0.9,
                                      Qmin=0.0,
                                      Qmax=2.0,
                                      benchmark=MyBenchmark(),
                                      seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, hba_custom, hba_customc)
Beispiel #2
0
class HBATestCase(TestCase):
    def setUp(self):
        self.hba_custom = HybridBatAlgorithm(D=10,
                                             NP=40,
                                             nFES=1000,
                                             A=0.5,
                                             r=0.5,
                                             F=0.5,
                                             CR=0.9,
                                             Qmin=0.0,
                                             Qmax=2.0,
                                             benchmark=MyBenchmark())
        self.hba_griewank = HybridBatAlgorithm(D=10,
                                               NP=40,
                                               nFES=1000,
                                               A=0.5,
                                               r=0.5,
                                               F=0.5,
                                               CR=0.9,
                                               Qmin=0.0,
                                               Qmax=2.0,
                                               benchmark='griewank')

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

    def test_griewank_works_fine(self):
        self.assertTrue(self.hba_griewank.run())
Beispiel #3
0
 def test_griewank_works_fine(self):
     hba_griewank = HybridBatAlgorithm(D=self.D,
                                       NP=40,
                                       nFES=self.nFES,
                                       nGEN=self.nGEN,
                                       A=0.5,
                                       r=0.5,
                                       F=0.5,
                                       CR=0.9,
                                       Qmin=0.0,
                                       Qmax=2.0,
                                       benchmark='griewank',
                                       seed=self.seed)
     hba_griewankc = HybridBatAlgorithm(D=self.D,
                                        NP=40,
                                        nFES=self.nFES,
                                        nGEN=self.nGEN,
                                        A=0.5,
                                        r=0.5,
                                        F=0.5,
                                        CR=0.9,
                                        Qmin=0.0,
                                        Qmax=2.0,
                                        benchmark='griewank',
                                        seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, hba_griewank, hba_griewankc)
Beispiel #4
0
class HBATestCase(TestCase):
    def setUp(self):

        self.hba_custom = HybridBatAlgorithm(10, 40, 1000, 0.5, 0.5, 0.5, 0.9,
                                             0.0, 2.0, MyBenchmark())
        self.hba_griewank = HybridBatAlgorithm(10, 40, 1000, 0.5, 0.5, 0.5,
                                               0.9, 0.0, 2.0, 'griewank')

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

    def test_griewank_works_fine(self):
        self.assertTrue(self.hba_griewank.run())
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_griewank_works_fine(self):
     hba_griewank = HybridBatAlgorithm(NP=40,
                                       A=0.5,
                                       r=0.5,
                                       F=0.5,
                                       CR=0.9,
                                       Qmin=0.0,
                                       Qmax=2.0,
                                       seed=self.seed)
     hba_griewankc = HybridBatAlgorithm(NP=40,
                                        A=0.5,
                                        r=0.5,
                                        F=0.5,
                                        CR=0.9,
                                        Qmin=0.0,
                                        Qmax=2.0,
                                        seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, hba_griewank, hba_griewankc)
Beispiel #7
0
 def test_custom_works_fine(self):
     hba_custom = HybridBatAlgorithm(NP=40,
                                     A=0.5,
                                     r=0.5,
                                     F=0.5,
                                     CR=0.9,
                                     Qmin=0.0,
                                     Qmax=2.0,
                                     seed=self.seed)
     hba_customc = HybridBatAlgorithm(NP=40,
                                      A=0.5,
                                      r=0.5,
                                      F=0.5,
                                      CR=0.9,
                                      Qmin=0.0,
                                      Qmax=2.0,
                                      seed=self.seed)
     AlgorithmTestCase.algorithm_run_test(self, hba_custom, hba_customc,
                                          MyBenchmark())
Beispiel #8
0
 def setUp(self):
     self.hba_custom = HybridBatAlgorithm(D=10,
                                          NP=40,
                                          nFES=1000,
                                          A=0.5,
                                          r=0.5,
                                          F=0.5,
                                          CR=0.9,
                                          Qmin=0.0,
                                          Qmax=2.0,
                                          benchmark=MyBenchmark())
     self.hba_griewank = HybridBatAlgorithm(D=10,
                                            NP=40,
                                            nFES=1000,
                                            A=0.5,
                                            r=0.5,
                                            F=0.5,
                                            CR=0.9,
                                            Qmin=0.0,
                                            Qmax=2.0,
                                            benchmark='griewank')
Beispiel #9
0
            estimator = KNeighborsClassifier(n_neighbors=solution[1],
                                             weights=solution[2],
                                             algorithm=solution[3],
                                             leaf_size=solution[4])
            kfold = StratifiedKFold(n_splits=10,
                                    shuffle=True,
                                    random_state=self.seed)
            self.ten_fold_scores = cross_validate(estimator,
                                                  self.X_validate,
                                                  self.y_validate,
                                                  cv=kfold,
                                                  scoring=['accuracy'])


scores = []
algorithm = HybridBatAlgorithm(4, 40, 100, 0.9, 0.1, 0.001, 0.9, 0.0, 2.0,
                               KNNBreastCancerBenchmark())
best = algorithm.run()

print('Optimal KNN parameters are:')
best_solution = []

for score in scores:
    if score[0] == best:
        best_solution = score

print(best_solution)

model = KNNBreastCancerClassifier()
model.run_10_fold(solution=best_solution)
model.run_10_fold()
Beispiel #10
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

import random
from NiaPy.algorithms.modified import HybridBatAlgorithm
from NiaPy.task.task import StoppingTask, OptimizationType
from NiaPy.benchmarks import Sphere

#we will run Hybrid Bat Algorithm for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10, nFES=4000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere())
    algo = HybridBatAlgorithm(NP=40, A=0.5, r=0.5, F=0.5, CR=0.9, Qmin=0.0, Qmax=2.0)
    best = algo.run(task=task)
    print(best)
Beispiel #11
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 = HybridBatAlgorithm(10, 40, 10000, 0.9, 0.1, 0.001, 0.9, 0.0,
                                   2.0, MyBenchmark())
    Best = Algorithm.run()

    logger.info(Best)
Beispiel #12
0
    def setUp(self):

        self.hba_custom = HybridBatAlgorithm(10, 40, 1000, 0.5, 0.5, 0.5, 0.9,
                                             0.0, 2.0, MyBenchmark())
Beispiel #13
0
 def test_type_parameters(self):
     d = HybridBatAlgorithm.typeParameters()
     self.assertIsNotNone(d.pop('NP', None))
     self.assertIsNotNone(d.pop('F', None))
     self.assertIsNotNone(d.pop('CR', None))
Beispiel #14
0
 def test_algorithm_info(self):
     self.assertIsNotNone(HybridBatAlgorithm.algorithmInfo())