# 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 FireflyAlgorithm from NiaPy.util import StoppingTask, OptimizationType from NiaPy.benchmarks import Sphere # we will run Firefly Algorithm for 5 independent runs for i in range(5): task = StoppingTask(D=10, nFES=1000, optType=OptimizationType.MINIMIZATION, benchmark=Sphere()) algo = FireflyAlgorithm(NP=20, alpha=0.5, betamin=0.2, gamma=1.0) best = algo.run(task=task) print('%s -> %s' % (best[0], best[1]))
sys.path.append('../') # End of fix from NiaPy.algorithms.basic import ParticleSwarmAlgorithm from NiaPy.util import StoppingTask, OptimizationType from NiaPy.benchmarks import Sphere from numpy import random as rand, apply_along_axis def customInit(task, NP, rnd=rand): pop = task.Lower + rnd.rand(NP, task.D) * 2 fpop = apply_along_axis(task.eval, 1, pop) return pop, fpop #we will run Particle Swarm Algorithm with custom Init function for 5 independent runs for i in range(5): task = StoppingTask(D=10, nFES=10000, optType=OptimizationType.MINIMIZATION, InitPopFunc=customInit, benchmark=Sphere()) algo = ParticleSwarmAlgorithm(NP=10, C1=2.0, C2=2.0, w=0.7, vMin=-4, vMax=4) best = algo.run(task=task) print(best)
# 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 AntColonyOptimization from NiaPy.util import StoppingTask, OptimizationType from NiaPy.benchmarks import Sphere from NiaPy.benchmarks import Ackley from NiaPy.benchmarks import Griewank # we will run Ant Colony Optimization for 5 independent runs for i in range(5): task = StoppingTask(D=10, nGEN=1000, optType=OptimizationType.MINIMIZATION, benchmark=Griewank()) algo = AntColonyOptimization(NP=40) best = algo.run(task=task) print('%s -> %s' % (best[0], 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 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('%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)
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)
def eval(self, A): r"""Check if is algorithm trying to evaluate solution out of bounds.""" self.assertTrue(self.isFeasible(A), 'Solution %s is not in feasible space!!!' % A) return StoppingTask.eval(self, A)