Exemplo n.º 1
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.task import Task
from niapy.problems import Sphere
from niapy.algorithms.basic import DynamicFireworksAlgorithm

# we will run Fireworks Algorithm for 5 independent runs
algo = DynamicFireworksAlgorithm(population_size=70,
                                 amplitude_init=0.1,
                                 amplitude_final=0.9)
for i in range(5):
    task = Task(problem=Sphere(dimension=10), max_iters=80)
    best = algo.run(task)
    print('%s -> %s' % (best[0], best[1]))
print(algo.get_parameters())

# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
Exemplo n.º 2
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.basic import EvolutionStrategyMp1
from niapy.task import Task
from niapy.problems import Sphere

# we will run Differential Evolution for 5 independent runs
for i in range(5):
    task = Task(problem=Sphere(dimension=10), max_evals=10000)
    algo = EvolutionStrategyMp1()
    best = algo.run(task)
    print('%s -> %f' % (best[0], best[1]))
Exemplo n.º 3
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.basic import ParticleSwarmAlgorithm
from niapy.task import Task, OptimizationType
from niapy.problems import Sphere

# we will run ParticleSwarmAlgorithm for 5 independent runs
for i in range(5):
    task = Task(problem=Sphere(dimension=10),
                max_evals=10000,
                optimization_type=OptimizationType.MAXIMIZATION)
    algo = ParticleSwarmAlgorithm(population_size=40,
                                  c1=2.0,
                                  c2=2.0,
                                  w=0.7,
                                  min_velocity=-4,
                                  max_velocity=4)
    best = algo.run(task=task)
    print(best[-1])
Exemplo n.º 4
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.task import Task
from niapy.algorithms.basic import DifferentialEvolution
from niapy.problems import Sphere

# 1 Number of function evaluations (nFES) as a stopping criteria
for i in range(10):
    task = Task(problem=Sphere(dimension=10), max_evals=10000)
    algo = DifferentialEvolution(population_size=40,
                                 crossover_probability=0.9,
                                 differential_weight=0.5)
    best = algo.run(task)
    print('%s -> %s' % (best[0], best[1]))

print('---------------------------------------')

# 2 Number of generations (iterations) as a stopping criteria
for i in range(10):
    task = Task(problem=Sphere(dimension=10), max_iters=1000)
    algo = DifferentialEvolution(population_size=40,
                                 crossover_probability=0.9,
                                 differential_weight=0.5)
    best = algo.run(task)
    print('%s -> %s' % (best[0], best[1]))
Exemplo n.º 5
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.basic import ComprehensiveLearningParticleSwarmOptimizer
from niapy.problems import Sphere
from niapy.task import Task

# we will run ParticleSwarmAlgorithm for 5 independent runs
algo = ComprehensiveLearningParticleSwarmOptimizer(population_size=50,
                                                   c1=.3,
                                                   c2=1.0,
                                                   m=5,
                                                   w=0.86,
                                                   min_velocity=-2,
                                                   max_velocity=2)
for i in range(5):
    task = Task(problem=Sphere(dimension=25), max_evals=20000)
    best = algo.run(task=task)
    print('%s -> %f' % (best[0], best[1]))
print(algo.get_parameters())

# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
Exemplo n.º 6
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.task import Task
from niapy.problems import Sphere
from niapy.algorithms.other import RandomSearch

task = Task(problem=Sphere(dimension=5), max_iters=5000)
algo = RandomSearch()
best = algo.run(task=task)
print(best)
Exemplo n.º 7
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.task import Task
from niapy.problems import Sphere
from niapy.algorithms.basic import DifferentialEvolution

# Storing improvements during the evolutionary cycle

task = Task(max_evals=10000, problem=Sphere(dimension=10))
algo = DifferentialEvolution(population_size=40, crossover_probability=0.9, differential_weight=0.5)
best = algo.run(task)
evals, x_f = task.convergence_data(x_axis='evals')
print(evals)  # print function evaluations
print(x_f)  # print values
Exemplo n.º 8
0
from niapy.algorithms.basic import (GreyWolfOptimizer, ParticleSwarmAlgorithm)
from niapy.problems import (Problem, Ackley, Griewank, Sphere, HappyCat)
"""Example demonstrating the use of niapy Runner."""


class MyProblem(Problem):
    def __init__(self, dimension, lower=-10, upper=10, *args, **kwargs):
        super().__init__(dimension, lower, upper, *args, **kwargs)

    def _evaluate(self, x):
        return np.sum(x**2)


runner = Runner(dimension=40,
                max_evals=100,
                runs=2,
                algorithms=[
                    GreyWolfOptimizer(), "FlowerPollinationAlgorithm",
                    ParticleSwarmAlgorithm(), "HybridBatAlgorithm",
                    "SimulatedAnnealing", "CuckooSearch"
                ],
                problems=[
                    Ackley(dimension=40),
                    Griewank(dimension=40),
                    Sphere(dimension=40),
                    HappyCat(dimension=40), "rastrigin",
                    MyProblem(dimension=40)
                ])

runner.run(export='dataframe', verbose=True)
Exemplo n.º 9
0
 def test_FA_iters(self):
     task = Task(max_iters=1000, problem=Sphere(10))
     algo = FireflyAlgorithm(population_size=10)
     algo.run_task(task)
     iters = task.iters
     self.assertEqual(1000, iters)
Exemplo n.º 10
0
 def test_FA_evals(self):
     task = Task(max_evals=1000, problem=Sphere(10))
     algo = FireflyAlgorithm(population_size=10)
     algo.run_task(task)
     evals = task.evals
     self.assertEqual(1000, evals)
Exemplo n.º 11
0
 def test_BA_iters_to_fes(self):
     task = Task(max_iters=1000, problem=Sphere(10))
     algo = BatAlgorithm(population_size=10)
     algo.run_task(task)
     evals = task.evals
     self.assertEqual(10010, evals)
Exemplo n.º 12
0
 def test_DE_iters(self):
     task = Task(max_iters=1000, problem=Sphere(10))
     algo = DifferentialEvolution(population_size=10, CR=0.9, F=0.5)
     algo.run_task(task)
     iters = task.iters
     self.assertEqual(1000, iters)
Exemplo n.º 13
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.task import Task
from niapy.problems import Sphere
from niapy.algorithms.basic import CatSwarmOptimization

task = Task(problem=Sphere(dimension=10), max_evals=1000, enable_logging=True)
algo = CatSwarmOptimization()
best = algo.run(task=task)
print('%s -> %s' % (best[0], best[1]))
# plot a convergence graph
task.plot_convergence(x_axis='evals')