def RunAlgorithm(self, NUM_RUNS,D,Np,nFES,BenchFunction): rawData = np.zeros(NUM_RUNS) for i in range(NUM_RUNS): task = StoppingTask(D=D, nFES=nFES, optType=OptimizationType.MINIMIZATION, benchmark=BenchFunction) algo = FireflyAlgorithm(NP=Np, alpha=0.5, betamin=0.2, gamma=1.0) best = algo.run(task=task) rawData[i] = best[1] print(rawData[i]) return rawData
def test_FA_iters_fine(self): task = StoppingTask(D=10, nGEN=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = FireflyAlgorithm(NP=25) algo.runTask(task) iters = task.iters() self.assertEqual(1000, iters)
def test_FA_evals_fine(self): task = StoppingTask(D=10, nFES=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = FireflyAlgorithm(NP=25) algo.runTask(task) evals = task.evals() self.assertEqual(1000, evals)
def test_BA_iters_to_fes(self): task = StoppingTask(D=10, nGEN=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = BatAlgorithm(NP=10) algo.runTask(task) evals = task.evals() self.assertEqual(10010, evals)
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 setUp(self): self.D = 20 rng = default_rng() self.x, self.task = default_rng().uniform( -100, 100, self.D), StoppingTask(D=self.D, nFES=230, nGEN=np.inf, benchmark=MyBenchmark()) self.s1, self.s2, self.s3 = Individual(x=self.x, e=False), Individual( task=self.task, rng=rng), Individual(task=self.task)
def executeGA(typeBenchmark): task = StoppingTask( D=dimensoes, nFES=numeroAvaliacoes, optType=tipoOtimizacao, logger=True, benchmark=typeBenchmark, ) algo = GeneticAlgorithm( NP=tamanhoPopulacao, Crossover=crossover, Mutation=mutation, Cr=taxaCruzamento, Mr=taxaMutacao, ) best = algo.run(task=task) return [task, best[1], best[0]]
def executePSO(typeBenchmark): task = StoppingTask( D=dimensoes, nFES=numeroAvaliacoes, optType=tipoOtimizacao, benchmark=typeBenchmark, ) algo = ParticleSwarmAlgorithm( NP=tamanhoPopulacao, C1=componenteCognitivo, C2=componenteSocial, w=pesoInercial, vMin=velocidadeMinima, vMax=velocidadeMaxima, ) best = algo.run(task=task) return [task, best[1], best[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 from NiaPy.algorithms.modified import MultiStrategyDifferentialEvolutionMTSv1 from NiaPy.algorithms.basic.de import CrossCurr2Best1, CrossBest1 from NiaPy.task.task 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 = MultiStrategyDifferentialEvolutionMTSv1( NP=70, F=0.5, CR=0.4, strategies=(CrossBest1, CrossCurr2Best1)) best = algo.run(task=task) print('%s -> %s' % (best[0].x, best[1])) # vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
def setUp(self): self.D = 20 self.x, self.task = rnd.uniform(-100, 100, self.D), StoppingTask( D=self.D, nFES=230, nGEN=inf, benchmark=MyBenchmark()) self.s1, self.s2, self.s3 = Individual(x=self.x, e=False), Individual( task=self.task, rand=rnd), Individual(task=self.task)
# encoding=utf8 # This is temporary fix to import module from parent folder # It will be removed when package is published on PyPI from NiaPy.benchmarks import Sphere from NiaPy.task.task import StoppingTask, OptimizationType from NiaPy.algorithms.basic import BeesAlgorithm import sys sys.path.append('../') # End of fix # we will run Bees Algorithm for 5 independent runs for i in range(5): task = StoppingTask(D=20, nGEN=2, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = BeesAlgorithm(NP=50, m=20, e=10, nep=20, nsp=15, ngh=7) best = algo.run(task=task) print('%s -> %s' % (best[0], best[1]))
# 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 ParticleSwarmAlgorithm from NiaPy.task.task import StoppingTask, OptimizationType from NiaPy.benchmarks import Benchmark class MyBenchmark(Benchmark): def __init__(self): Benchmark.__init__(self, -10, 10) def function(self): def evaluate(D, sol): val = 0.0 for i in range(D): val += sol[i] ** 2 return val return evaluate # we will run Particle Swarm Algorithm with on custom benchmark for i in range(1): task = StoppingTask(D=10, nGEN=1000, optType=OptimizationType.MINIMIZATION, benchmark=MyBenchmark()) algo = ParticleSwarmAlgorithm(NP=40, C1=2.0, C2=2.0, w=0.7, vMin=-4, vMax=4) best = algo.run(task=task) print('%s -> %s ' % (best[0], best[1]))
# 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 BareBonesFireworksAlgorithm from NiaPy.task.task import StoppingTask from NiaPy.benchmarks import Sphere # we will run Fireworks Algorithm for 5 independent runs for i in range(5): task = StoppingTask(D=10, nFES=1000, benchmark=Sphere()) algo = BareBonesFireworksAlgorithm() best = algo.run(task) print('%s -> %s' % (best[0], best[1])) # vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
# 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 import logging from NiaPy.task.task 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('%s -> %s' % (best[0].x, best[1])) 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)
NUM_RUNS = 25 startWholeAlgoTime = time.time() with pd.ExcelWriter('results.xlsx') as writer: for Np in ArrayOfNP: for nFES in ArrayOfnFES: for D in ArrayOfD: tempDataFrame = [] for BenchFunction in ArrayOfBenchmarks: resultArray = [] rawData = np.zeros(NUM_RUNS) start = time.time() for i in range(NUM_RUNS): task = StoppingTask( D=D, nFES=nFES, optType=OptimizationType.MINIMIZATION, benchmark=BenchFunction) algo = FireflyAlgorithm(NP=Np, alpha=0.5, betamin=0.2, gamma=1.0) best = algo.run(task=task) rawData[i] = best[1] end = time.time() elapsedTime = end - start processedData = BasicStatistics(rawData) #statistics resultArray.append(processedData.min_value()) resultArray.append(processedData.max_value()) resultArray.append(processedData.mean())