def setUp(self): self.D = 40 self.hs_custom = HarmonySearch(D=self.D, nFES=1000, benchmark=MyBenchmark()) self.hs_griewank = HarmonySearch(D=self.D, nFES=1000, benchmark=Griewank())
def plot_example(D=10, nFES=50000): task = TaskConvPlot(D=D, nFES=nFES, nGEN=50000, benchmark=MyBenchmark()) algo = HarmonySearch(HMS=50, r_accept=0.7, r_pa=0.2, b_range=1.1, task=task) best = algo.run() logger.info('%s %s' % (best[0], best[1])) input('Press [enter] to continue')
def logging_example(D=10, nFES=50000): task = TaskConvPrint(D=D, nFES=nFES, nGEN=50000, benchmark=MyBenchmark()) algo = HarmonySearch(HMS=50, r_accept=0.7, r_pa=0.2, b_range=1.1, seed=None, task=task) best = algo.run() logger.info('%s %s' % (best[0], best[1]))
def simple_example(runs=10, D=10, nFES=50000): for i in range(runs): algo = HarmonySearch(D=D, nFES=nFES, HMS=50, r_accept=0.7, r_pa=0.2, b_range=1.1, benchmark=MyBenchmark()) best = algo.run() logger.info('%s %s' % (best[0], best[1]))
def test_custom_works_fine(self): hs_costom = HarmonySearch(D=self.D, nFES=self.nFES, nGEN=self.nGEN, seed=self.seed, benchmark=MyBenchmark()) hs_costomc = HarmonySearch(D=self.D, nFES=self.nFES, nGEN=self.nGEN, seed=self.seed, benchmark=MyBenchmark()) AlgorithmTestCase.algorithm_run_test(self, hs_costom, hs_costomc)
def test_griewank_works_fine(self): hs_griewank = HarmonySearch(D=self.D, nFES=self.nFES, nGEN=self.nGEN, seed=self.seed, benchmark=Griewank()) hs_griewankc = HarmonySearch(D=self.D, nFES=self.nFES, nGEN=self.nGEN, seed=self.seed, benchmark=Griewank()) AlgorithmTestCase.algorithm_run_test(self, hs_griewank, hs_griewankc)
class HSTestCase(TestCase): def setUp(self): self.D = 40 self.hs_custom = HarmonySearch(D=self.D, nFES=1000, benchmark=MyBenchmark()) self.hs_griewank = HarmonySearch(D=self.D, nFES=1000, benchmark=Griewank()) def test_custom_works_fine(self): fun = MyBenchmark().function() x = self.hs_custom.run() self.assertTrue(x) self.assertAlmostEqual(fun(self.D, x[0]), x[1], delta=1e2) def test_griewank_works_fine(self): fun = Griewank().function() x = self.hs_griewank.run() self.assertTrue(x) self.assertAlmostEqual(fun(self.D, x[0]), x[1], delta=1e2)
def test_type_parameters(self): d = HarmonySearch.typeParameters() self.assertIsNotNone(d.get('HMS', None)) self.assertTrue(d['HMS'](10)) self.assertFalse(d['HMS'](-10)) self.assertFalse(d['HMS'](-10.3)) self.assertIsNotNone(d.get('r_accept', None)) self.assertTrue(d['r_accept'](.3)) self.assertTrue(d['r_accept'](0.99)) self.assertFalse(d['r_accept'](-0.99)) self.assertFalse(d['r_accept'](9)) self.assertIsNotNone(d.get('r_pa', None)) self.assertTrue(d['r_pa'](.3)) self.assertTrue(d['r_pa'](0.99)) self.assertFalse(d['r_pa'](-0.99)) self.assertFalse(d['r_pa'](9)) self.assertIsNotNone(d.get('b_range', None)) self.assertTrue(d['b_range'](10)) self.assertFalse(d['b_range'](-10)) self.assertFalse(d['b_range'](-10.3))
def test_griewank_works_fine(self): hs_griewank = HarmonySearch(seed=self.seed) hs_griewankc = HarmonySearch(seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, hs_griewank, hs_griewankc)
def test_custom_works_fine(self): hs_costom = HarmonySearch(seed=self.seed) hs_costomc = HarmonySearch(seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, hs_costom, hs_costomc, MyBenchmark())
# 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 HarmonySearch from NiaPy.util import StoppingTask, OptimizationType from NiaPy.benchmarks import Sphere #we will run Harmony Search for 5 independent runs for i in range(5): task = StoppingTask(D=10, nFES=10000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = HarmonySearch() best = algo.run(task=task) print(best)