def test_custom_works_fine(self): abc_custom = ArtificialBeeColonyAlgorithm(NP=10, Limit=2, seed=self.seed) abc_customc = ArtificialBeeColonyAlgorithm(NP=10, Limit=2, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, abc_custom, abc_customc, MyBenchmark())
def setUp(self): self.abc_custom = ArtificialBeeColonyAlgorithm(NP=10, D=40, nFES=4000, benchmark=MyBenchmark()) self.abc_griewank = ArtificialBeeColonyAlgorithm(NP=10, D=40, nFES=4000, benchmark='griewank')
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 ]
class ABCTestCase(TestCase): def setUp(self): self.abc_custom = ArtificialBeeColonyAlgorithm(10, 40, 10000, MyBenchmark()) self.abc_griewank = ArtificialBeeColonyAlgorithm( 10, 40, 10000, 'griewank') def test_custom_works_fine(self): self.assertTrue(self.abc_custom.run()) def test_griewank_works_fine(self): self.assertTrue(self.abc_griewank.run())
def test_type_parameters(self): d = ArtificialBeeColonyAlgorithm.typeParameters() self.assertEqual(len(d), 2) self.assertTrue(d['NP'](10)) self.assertFalse(d['NP'](0)) self.assertFalse(d['NP'](-10)) self.assertTrue(d['Limit'](10)) self.assertFalse(d['Limit'](0)) self.assertFalse(d['Limit'](-10))
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)
# 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.basic import ArtificialBeeColonyAlgorithm from NiaPy.task.task import StoppingTask, OptimizationType from NiaPy.benchmarks import Sphere # we will run Artificial Bee Colony Algorithm for 5 independent runs for i in range(5): task = StoppingTask(D=10, nFES=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = ArtificialBeeColonyAlgorithm(NP=40, Limit=2) best = algo.run(task=task) print('%s -> %s' % (best[0].x, best[1]))
# 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.basic import ArtificialBeeColonyAlgorithm def Fun(D, sol): val = 0.0 for i in range(D): val = val + sol[i] * sol[i] return val for i in range(10): Algorithm = ArtificialBeeColonyAlgorithm(10, 40, 10000, -5.0, 5.0, Fun) Best = Algorithm.run() print(Best)
from NiaPy.algorithms.basic import ArtificialBeeColonyAlgorithm logging.basicConfig() logger = logging.getLogger('examples') logger.setLevel('INFO') # For reproducive results random.seed(1234) class MyBenchmark(object): def __init__(self): self.Lower = -5 self.Upper = 5 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 = ArtificialBeeColonyAlgorithm(10, 40, 10000, MyBenchmark()) Best = Algorithm.run() logger.info(Best)
def test_griewank_works_fine(self): abc_griewank = ArtificialBeeColonyAlgorithm(NP=10, D=self.D, nFES=self.nFES, nGEN=self.nGEN, benchmark='griewank', seed=self.seed) abc_griewankc = ArtificialBeeColonyAlgorithm(NP=10, D=self.D, nFES=self.nFES, nGEN=self.nGEN, benchmark='griewank', seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, abc_griewank, abc_griewankc)
def test_custom_works_fine(self): abc_custom = ArtificialBeeColonyAlgorithm(NP=10, D=self.D, nFES=self.nFES, nGEN=self.nGEN, Limit=2, benchmark=MyBenchmark(), seed=self.seed) abc_customc = ArtificialBeeColonyAlgorithm(NP=10, D=self.D, nFES=self.nFES, nGEN=self.nGEN, Limit=2, benchmark=MyBenchmark(), seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, abc_custom, abc_customc)
logger.setLevel('INFO') # For reproducive results random.seed(1234) class MyBenchmark(object): def __init__(self): self.Lower = -5 self.Upper = 5 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 = ArtificialBeeColonyAlgorithm(NP=10, D=40, nFES=10000, benchmark=MyBenchmark()) Best = Algorithm.run() logger.info(Best) # vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
def test_griewank_works_fine(self): abc_griewank = ArtificialBeeColonyAlgorithm(NP=10, seed=self.seed) abc_griewankc = ArtificialBeeColonyAlgorithm(NP=10, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, abc_griewank, abc_griewankc)
def setUp(self): self.abc_custom = ArtificialBeeColonyAlgorithm(10, 40, 10000, MyBenchmark()) self.abc_griewank = ArtificialBeeColonyAlgorithm( 10, 40, 10000, 'griewank')