Ejemplo n.º 1
0
 def test_custom(self):
     wvcpso_custom = ParticleSwarmAlgorithm(population_size=10,
                                            c1=2.0,
                                            c2=2.0,
                                            w=0.7,
                                            min_velocity=-4,
                                            max_velocity=4,
                                            seed=self.seed)
     wvcpso_customc = ParticleSwarmAlgorithm(population_size=10,
                                             c1=2.0,
                                             c2=2.0,
                                             w=0.7,
                                             min_velocity=-4,
                                             max_velocity=4,
                                             seed=self.seed)
     AlgorithmTestCase.test_algorithm_run(self, wvcpso_custom,
                                          wvcpso_customc, MyProblem())
Ejemplo n.º 2
0
 def test_griewank(self):
     wvcpso_griewank = ParticleSwarmAlgorithm(population_size=10,
                                              c1=2.0,
                                              c2=2.0,
                                              w=0.7,
                                              min_velocity=-4,
                                              max_velocity=4,
                                              seed=self.seed)
     wvcpso_griewankc = ParticleSwarmAlgorithm(population_size=10,
                                               c1=2.0,
                                               c2=2.0,
                                               w=0.7,
                                               min_velocity=-4,
                                               max_velocity=4,
                                               seed=self.seed)
     AlgorithmTestCase.test_algorithm_run(self, wvcpso_griewank,
                                          wvcpso_griewankc)
Ejemplo n.º 3
0
 def test_algorithm_info(self):
     al = ParticleSwarmAlgorithm.info()
     self.assertIsNotNone(al)
Ejemplo 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.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])
Ejemplo n.º 5
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)
Ejemplo 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.algorithms.basic import ParticleSwarmAlgorithm
from niapy.task import Task
from niapy.problems import Griewank

# we will run ParticleSwarmAlgorithm for 5 independent runs
algo = ParticleSwarmAlgorithm(population_size=100,
                              min_velocity=-4.0,
                              max_velocity=4.0)
for i in range(5):
    task = Task(problem=Griewank(dimension=10, lower=-600, upper=600),
                max_evals=10000)
    best = algo.run(task=task)
    print('%s -> %f' % (best[0], best[1]))
print(algo.get_parameters())

# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
Ejemplo 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.algorithms.basic import ParticleSwarmAlgorithm
from niapy.task import Task
from niapy.problems import Sphere
import numpy as np


def my_init(task, population_size, rng, **_kwargs):
    pop = 0.2 + rng.random((population_size, task.dimension)) * task.range
    fpop = np.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 = Task(problem=Sphere(dimension=10), max_evals=1000)
    algo = ParticleSwarmAlgorithm(population_size=10, c1=2.0, c2=2.0, w=0.7, min_velocity=-4, max_velocity=4, initialization_function=my_init)
    best = algo.run(task=task)
    print(best)