def test_cross_curr2best1(self): de_curr2best1 = DifferentialEvolution(CrossMutt=CrossCurr2Best1, seed=self.seed) de_curr2best1c = DifferentialEvolution(CrossMutt=CrossCurr2Best1, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_curr2best1, de_curr2best1c)
def test_griewank_works_fine(self): de_griewank = DifferentialEvolution(NP=10, CR=0.5, F=0.9, seed=self.seed) de_griewankc = DifferentialEvolution(NP=10, CR=0.5, F=0.9, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_griewank, de_griewankc)
def test_CrossBest2(self): de_best2 = DifferentialEvolution(nFES=self.nFES, nGEN=self.nGEN, D=self.D, CrossMutt=CrossBest2, seed=self.seed) de_best2c = DifferentialEvolution(nFES=self.nFES, nGEN=self.nGEN, D=self.D, CrossMutt=CrossBest2, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_best2, de_best2c)
def test_CrossRand1(self): de_rand1 = DifferentialEvolution(nFES=self.nFES, nGEN=self.nGEN, D=self.D, CrossMutt=CrossRand1, seed=self.seed) de_rand1c = DifferentialEvolution(nFES=self.nFES, nGEN=self.nGEN, D=self.D, CrossMutt=CrossRand1, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_rand1, de_rand1c)
def test_CrossCurr2Best1(self): de_curr2best1 = DifferentialEvolution(nFES=self.nFES, nGEN=self.nGEN, D=self.D, CrossMutt=CrossCurr2Best1, seed=self.seed) de_curr2best1c = DifferentialEvolution(nFES=self.nFES, nGEN=self.nGEN, D=self.D, CrossMutt=CrossCurr2Best1, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_curr2best1, de_curr2best1c)
def test_DE_iters_fine(self): task = StoppingTask(D=10, nGEN=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = DifferentialEvolution(NP=40, CR=0.9, F=0.5) algo.runTask(task) iters = task.iters() self.assertEqual(1000, iters)
def test_DE_evals_fine(self): task = Task(D=10, nFES=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = DifferentialEvolution(task=task, NP=40, CR=0.9, F=0.5) algo.run() evals = algo.task.evals() self.assertEqual(evals, 1000)
def test_Custom_works_fine(self): de_custom = DifferentialEvolution(D=self.D, NP=40, nFES=self.nFES, nGEN=self.nGEN, F=0.5, CR=0.9, benchmark=MyBenchmark(), seed=self.seed) de_customc = DifferentialEvolution(D=self.D, NP=40, nFES=self.nFES, nGEN=self.nGEN, F=0.5, CR=0.9, benchmark=MyBenchmark(), seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_custom, de_customc)
def test_griewank_works_fine(self): de_griewank = DifferentialEvolution(NP=10, D=self.D, nFES=self.nFES, nGEN=self.nGEN, CR=0.5, F=0.9, benchmark='griewank', seed=self.seed) de_griewankc = DifferentialEvolution(NP=10, D=self.D, nFES=self.nFES, nGEN=self.nGEN, CR=0.5, F=0.9, benchmark='griewank', seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_griewank, de_griewankc)
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 ]
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 from NiaPy.algorithms.basic import DifferentialEvolution from NiaPy.util import StoppingTask, OptimizationType from NiaPy.benchmarks import Sphere # we will run Differential Evolution for 5 independent runs for i in range(5): task = StoppingTask(D=10, nFES=5000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = DifferentialEvolution(NP=50, F=0.5, CR=0.9) best = algo.run(task=task) print('%s -> %s' % (best[0].x, best[1]))
sys.path.append('../') # End of fix import random import logging from NiaPy.util import StoppingTask, OptimizationType from NiaPy.algorithms.basic import DifferentialEvolution from NiaPy.benchmarks import Griewank, Sphere #1 Number of function evaluations (nFES) as a stopping criteria for i in range(10): task = StoppingTask(D=10, nFES=10000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = DifferentialEvolution(NP=40, CR=0.9, F=0.5) best = algo.run(task) print(best) print('---------------------------------------') #2 Number of generations (iterations) as a stopping criteria for i in range(10): task = StoppingTask(D=10, nGEN=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = DifferentialEvolution(NP=40, CR=0.9, F=0.5) best = algo.run(task) print(best)
sys.path.append('../') # End of fix import random import logging import numpy as np from NiaPy import Runner from NiaPy.util import Task, TaskConvPrint, TaskConvPlot, OptimizationType, getDictArgs from NiaPy.algorithms.modified import SelfAdaptiveDifferentialEvolution from NiaPy.algorithms.basic import DifferentialEvolution, MonkeyKingEvolutionV3 from NiaPy.benchmarks import Griewank, Sphere from NiaPy.algorithms.statistics import BasicStatistics NUM_RUNS = 10 # define number of runs stats = np.zeros(NUM_RUNS) for i in range(NUM_RUNS): task = Task( D=10, nFES=10000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) print ("Working on run: " + str(i+1)) algo = DifferentialEvolution(task=task, NP=40, CR=0.9, F=0.5) best = algo.run() stats[i] = best[1] # save best stat = BasicStatistics(stats) print stat.generate_standard_report() # show basic stats
def test_custom_works_fine(self): de_custom = DifferentialEvolution(F=0.5, CR=0.9, seed=self.seed) de_customc = DifferentialEvolution(F=0.5, CR=0.9, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_custom, de_customc, MyBenchmark())
def test_CrossRand2(self): de_rand2 = DifferentialEvolution(CrossMutt=CrossRand2, seed=self.seed) de_rand2c = DifferentialEvolution(CrossMutt=CrossRand2, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_rand2, de_rand2c)
def test_cross_rand1(self): de_rand1 = DifferentialEvolution(CrossMutt=CrossRand1, seed=self.seed) de_rand1c = DifferentialEvolution(CrossMutt=CrossRand1, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_rand1, de_rand1c)
def test_cross_best2(self): de_best2 = DifferentialEvolution(CrossMutt=CrossBest2, seed=self.seed) de_best2c = DifferentialEvolution(CrossMutt=CrossBest2, seed=self.seed) AlgorithmTestCase.algorithm_run_test(self, de_best2, de_best2c)