Exemplo n.º 1
0
class CSTestCase(TestCase):
    def setUp(self):
        self.pso_custom = ParticleSwarmAlgorithm(NP=40,
                                                 D=40,
                                                 nFES=1000,
                                                 C1=2.0,
                                                 C2=2.0,
                                                 w=0.7,
                                                 vMin=-4,
                                                 vMax=4,
                                                 benchmark=MyBenchmark())
        self.pso_griewank = ParticleSwarmAlgorithm(NP=40,
                                                   D=40,
                                                   nFES=1000,
                                                   C1=2.0,
                                                   C2=2.0,
                                                   w=0.7,
                                                   vMin=-4,
                                                   vMax=4,
                                                   benchmark='griewank')

    def test_custom_works_fine(self):
        self.assertTrue(self.pso_custom.run())

    def test_griewank_works_fine(self):
        self.assertTrue(self.pso_griewank.run())
Exemplo n.º 2
0
class CSTestCase(TestCase):
    def setUp(self):
        self.pso_custom = ParticleSwarmAlgorithm(40, 40, 1000, 2.0, 2.0, 0.7,
                                                 -4, 4, MyBenchmark())
        self.pso_griewank = ParticleSwarmAlgorithm(40, 40, 1000, 2.0, 2.0, 0.7,
                                                   -4, 4, 'griewank')

    def test_custom_works_fine(self):
        self.assertTrue(self.pso_custom.run())

    def test_griewank_works_fine(self):
        self.assertTrue(self.pso_griewank.run())
Exemplo n.º 3
0
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
    ]
Exemplo n.º 4
0
def plot_example():
    task = TaskConvPlot(D=50, nFES=50000, nGEN=10000, benchmark=MyBenchmark())
    algo = ParticleSwarmAlgorithm(NP=50,
                                  C1=2.0,
                                  C2=2.0,
                                  w=0.5,
                                  vMin=-5,
                                  vMax=5,
                                  task=task)
    best = algo.run()
    logger.info('%s %s' % (best[0], best[1]))
    input('Press [enter] to continue')
Exemplo n.º 5
0
def logging_example():
    task = TaskConvPrint(D=50, nFES=50000, nGEN=50000, benchmark=MyBenchmark())
    algo = ParticleSwarmAlgorithm(NP=50,
                                  C1=2.0,
                                  C2=2.0,
                                  w=0.5,
                                  vMin=-5,
                                  vMax=5,
                                  seed=None,
                                  task=task)
    best = algo.run()
    logger.info('%s %s' % (best[0], best[1]))
Exemplo n.º 6
0
def simple_example(runs=10):
    for i in range(10):
        algo = ParticleSwarmAlgorithm(NP=50,
                                      D=40,
                                      nFES=40000,
                                      C1=2.0,
                                      C2=2.0,
                                      w=0.5,
                                      vMin=-5,
                                      vMax=5,
                                      seed=i,
                                      benchmark=MyBenchmark())
        Best = algo.run()
        logger.info('%s %s' % (Best[0], Best[1]))
Exemplo n.º 7
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]]
Exemplo n.º 8
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

import random
from NiaPy.algorithms.basic import ParticleSwarmAlgorithm
from NiaPy.task import StoppingTask, OptimizationType
from NiaPy.benchmarks import Sphere

#we will run ParticleSwarmAlgorithm for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10, nFES=1000, optType=OptimizationType.MAXIMIZATION, benchmark=Sphere())
    algo = ParticleSwarmAlgorithm(NP=40, C1=2.0, C2=2.0, w=0.7, vMin=-4, vMax=4)
    best = algo.run(task=task)
    print best
Exemplo n.º 9
0
logging.basicConfig()
logger = logging.getLogger('examples')
logger.setLevel('INFO')

# For reproducive results
random.seed(1234)


class MyBenchmark(object):
    def __init__(self):
        self.Lower = -11
        self.Upper = 11

    def function(self):
        def evaluate(D, sol):
            val = 0.0
            for i in range(D):
                val = val + sol[i] * sol[i]
            return val

        return evaluate


for i in range(10):
    Algorithm = ParticleSwarmAlgorithm(50, 40, 40000, 2.0, 2.0, 0.5, -5, 5,
                                       MyBenchmark())
    Best = Algorithm.run()

    logger.info(Best)