Exemple #1
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())
Exemple #2
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())
Exemple #3
0
                                             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()

print('best model mean test accuracy: ' +
Exemple #4
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)
Exemple #5
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)