diff --git a/README.md b/README.md index 44c04208..7bd26c2e 100644 --- a/README.md +++ b/README.md @@ -110,33 +110,33 @@ $ python Let's go through a basic and advanced example. ## Basic Example -Let’s say, we want to try out Gray Wolf Optimizer algorithm against the Pintér problem function. Firstly, we have to create new file, with name, for example *basic_example.py*. Then we have to import chosen algorithm from NiaPy, so we can use it. Afterwards we initialize GreyWolfOptimizer class instance and run the algorithm. Given bellow is the complete source code of basic example. +Let’s say, we want to try out PSO against the Pintér problem function. Firstly, we have to create new file, with name, for example *basic_example.py*. Then we have to import chosen algorithm from NiaPy, so we can use it. Afterwards we initialize ParticleSwarmAlgorithm class instance and run the algorithm. Given bellow is the complete source code of basic example. ```python -from niapy.algorithms.basic import GreyWolfOptimizer +from niapy.algorithms.basic import ParticleSwarmAlgorithm from niapy.task import Task -# we will run 10 repetitions of Grey Wolf Optimizer against the Pinter problem +# we will run 10 repetitions of Weighed, velocity clamped PSO on the Pinter problem for i in range(10): - task = Task(problem='pinter', dimension=10, max_evals=1000) - algorithm = GreyWolfOptimizer(population_size=20) - best = algorithm.run(task) - print(best[-1]) + task = Task(problem='pinter', dimension=10, max_evals=10000) + algorithm = ParticleSwarmAlgorithm(population_size=100, w=0.9, c1=0.5, c2=0.3, min_velocity=-1, max_velocity=1) + best_x, best_fit = algorithm.run(task) + print(best_fit) ``` Given example can be run with *python basic_example.py* command and should give you similar output as following: ```sh -0.27046073106003377 -50.89301186976975 -1.089147452727528 -1.18418058254198 -102.46876441081712 -0.11237241605812048 -1.8869331711450696 -0.04861881403346098 -2.5748611081742325 -135.6754069530421 +0.008773534890863646 +0.036616190934621755 +186.75116812592546 +0.024186452828927896 +263.5697469837348 +45.420706924365916 +0.6946753611091367 +7.756100204780568 +5.839673314425907 +0.06732518679742806 ``` ## Advanced Example @@ -154,7 +154,7 @@ Now we should have something similar as is shown in code snippet bellow. import numpy as np from niapy.task import Task from niapy.problems import Problem -from niapy.algorithms.basic import GreyWolfOptimizer +from niapy.algorithms.basic import ParticleSwarmAlgorithm # our custom problem class @@ -172,28 +172,27 @@ Now, all we have to do is to initialize our algorithm as in previous examples an my_problem = MyProblem(dimension=20) for i in range(10): task = Task(problem=my_problem, max_iters=100) - algo = GreyWolfOptimizer(population_size=20) + algo = ParticleSwarmAlgorithm(population_size=100, w=0.9, c1=0.5, c2=0.3, min_velocity=-1, max_velocity=1) # running algorithm returns best found minimum - best = algo.run(task) - + best_x, best_fit = algo.run(task) # printing best minimum - print(best[-1]) + print(best_fit) ``` Now we can run our advanced example with following command: *python advanced_example.py*. The results should be similar to those bellow. ```sh -7.606465129178389e-09 -5.288697102580944e-08 -6.875762169124336e-09 -1.386574251424837e-08 -2.174923591233085e-08 -2.578545710051624e-09 -1.1400628541972142e-08 -2.99387377733644e-08 -7.029492316948289e-09 -7.426212520156997e-09 +0.002455614050761476 +0.000557652972392164 +0.0029791325679865413 +0.0009443595274525336 +0.001012658824492069 +0.0006837236892816072 +0.0026789725774685495 +0.005017746993004601 +0.0011654473402322196 +0.0019074442166293853 ``` For more usage examples please look at [examples](/examples) folder. diff --git a/README.rst b/README.rst index 680faec5..175ffbd0 100644 --- a/README.rst +++ b/README.rst @@ -72,7 +72,7 @@ To install NiaPy on Arch Linux, please use an `AUR helper `__ folder. diff --git a/docs/source/getting_started.rst b/docs/source/getting_started.rst index d2e8ef50..b7a9d248 100644 --- a/docs/source/getting_started.rst +++ b/docs/source/getting_started.rst @@ -20,20 +20,21 @@ Basic example ------------- In this example, let's say, we want to try out Gray Wolf Optimizer algorithm against the Pintér problem. Firstly, we have to create new file, with name, for example *basic_example.py*. Then we have to import chosen -algorithm from NiaPy, so we can use it. Afterwards we initialize GreyWolfOptimizer class instance and run the algorithm. +algorithm from NiaPy, so we can use it. Afterwards we initialize ParticleSwarmAlgorithm class instance and run the algorithm. Given bellow is complete source code of basic example. .. code:: python - from niapy.algorithms.basic import GreyWolfOptimizer + from niapy.algorithms.basic import ParticleSwarmAlgorithm from niapy.task import Task - # we will run 10 repetitions of Grey Wolf Optimizer against the Pinter problem + # we will run 10 repetitions of Weighed, velocity clamped PSO on the Pinter problem for i in range(10): - task = Task(problem='pinter', dimension=10, max_evals=1000) - algorithm = GreyWolfOptimizer(population_size=20) - best = algorithm.run(task) - print(best[-1]) + task = Task(problem='pinter', dimension=10, max_evals=10000) + algorithm = ParticleSwarmAlgorithm(population_size=100, w=0.9, c1=0.5, c2=0.3, min_velocity=-1, max_velocity=1) + best_x, best_fit = algorithm.run(task) + print(best_fit) + Given example can be run with ``python basic_example.py`` command and should give you similar output as @@ -41,60 +42,61 @@ following: .. code:: bash - 0.27046073106003377 - 50.89301186976975 - 1.089147452727528 - 1.18418058254198 - 102.46876441081712 - 0.11237241605812048 - 1.8869331711450696 - 0.04861881403346098 - 2.5748611081742325 - 135.6754069530421 + 0.008773534890863646 + 0.036616190934621755 + 186.75116812592546 + 0.024186452828927896 + 263.5697469837348 + 45.420706924365916 + 0.6946753611091367 + 7.756100204780568 + 5.839673314425907 + 0.06732518679742806 + Customize problem bounds ~~~~~~~~~~~~~~~~~~~~~~~~~~ By default, the Pintér problem has the bound set to -10 and 10. We can override those predefined -values very easily. We will modify our basic example to run Grey Wolf Optimizer against Pintér problem +values very easily. We will modify our basic example to run PSO against Pintér problem function with custom problem bounds set to -5 and 5. Given bellow is the complete source code of customized basic example. .. code:: python - from niapy.algorithms.basic import GreyWolfOptimizer + from niapy.algorithms.basic import ParticleSwarmAlgorithm from niapy.task import Task from niapy.problems import Pinter # initialize Pinter problem with custom bound pinter = Pinter(dimension=20, lower=-5, upper=5) - # we will run 10 repetitions of Grey Wolf Optimizer against Pinter problem function + # we will run 10 repetitions of PSO against Pinter problem function for i in range(10): task = Task(problem=pinter, max_iters=100) - algo = GreyWolfOptimizer(population_size=20) + algorithm = ParticleSwarmAlgorithm(population_size=100, w=0.9, c1=0.5, c2=0.3, min_velocity=-1, max_velocity=1) # running algorithm returns best found coordinates and fitness - best = algo.run(task) + best_x, best_fit = algorithm.run(task) # printing best minimum - print(best[-1]) + print(best_fit) Given example can be run with ``python basic_example.py`` command and should give you similar output as following: .. code:: bash - 3.6505427897004535e-05 - 3.8199245597156976e-05 - 0.0001411622032519498 - 3.756895566558108e-06 - 4.424570228729335e-05 - 6.114113555664476e-06 - 1.3978581995165064e-05 - 5.5851861300797835e-06 - 7.909208902574658e-06 - 2.4419767659672064e-05 + 352.42267398695526 + 15.962765124936741 + 356.51781541486224 + 195.64616754731315 + 99.92445777071993 + 142.36934412674793 + 1.9566799783197366 + 350.4330002633882 + 183.93200436114898 + 208.5557966507149 Advanced example ---------------- @@ -111,7 +113,7 @@ Now we should have something similar as is shown in code snippet bellow. from niapy.task import Task from niapy.problems import Problem - from niapy.algorithms.basic import GreyWolfOptimizer + from niapy.algorithms.basic import ParticleSwarmAlgorithm import numpy as np # our custom Problem class @@ -131,29 +133,29 @@ instance of our *MyProblem* class. my_problem = MyProblem(dimension=20) for i in range(10): task = Task(problem=my_problem, max_iters=100) - algo = GreyWolfOptimizer(population_size=20) + algorithm = ParticleSwarmAlgorithm(population_size=100, w=0.9, c1=0.5, c2=0.3, min_velocity=-1, max_velocity=1) # running algorithm returns best found minimum - best = algo.run(task) + best_x, best_fit = algorithm.run(task) # printing best minimum - print(best[-1]) + print(best_fit) Now we can run our advanced example with following command python advanced_example.py. The results should be similar to those bellow. .. code:: bash - 7.606465129178389e-09 - 5.288697102580944e-08 - 6.875762169124336e-09 - 1.386574251424837e-08 - 2.174923591233085e-08 - 2.578545710051624e-09 - 1.1400628541972142e-08 - 2.99387377733644e-08 - 7.029492316948289e-09 - 7.426212520156997e-09 + 0.0009232355257327939 + 0.0012993317932349976 + 0.0026231249714186128 + 0.001404157010165644 + 0.0012822904697534436 + 0.002202199078241452 + 0.00216496834770605 + 0.0010092926171364153 + 0.0007432303831633373 + 0.0006545778971016809 Advanced example with custom population initialization ------------------------------------------------------ @@ -168,9 +170,9 @@ a random number generator and optional parameters. Such population initializatio # custom population initialization function 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 + pop = 0.2 + rng.random((population_size, task.dimension)) * task.range + fitness = np.apply_along_axis(task.eval, 1, pop) + return pop, fitness The complete example would look something like this. @@ -180,7 +182,7 @@ The complete example would look something like this. import numpy as np from niapy.task import Task from niapy.problems import Problem - from niapy.algorithms.basic import GreyWolfOptimizer + from niapy.algorithms.basic import ParticleSwarmAlgorithm # our custom Problem class class MyProblem(Problem): @@ -192,36 +194,36 @@ The complete example would look something like this. # custom population initialization function def my_init(task, population_size, rng, **kwargs): - pop = 0.2 + rng.random(population_size, task.dimension) * task.range + 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 10 repetitions of Grey Wolf Optimizer against our custom MyProblem problem function + # we will run 10 repetitions of PSO against our custom MyProblem problem function my_problem = MyProblem(dimension=20) for i in range(10): task = Task(problem=my_problem, max_iters=100) - algo = GreyWolfOptimizer(population_size=20, initialization_function=my_init) + algorithm = ParticleSwarmAlgorithm(population_size=100, w=0.9, c1=0.5, c2=0.3, min_velocity=-1, max_velocity=1, initialization_function=my_init) # running algorithm returns best found minimum - best = algo.run(task) + best_x, best_fit = algorithm.run(task) # printing best minimum - print(best[-1]) + print(best_fit) And results when running the above example should be similar to those bellow. .. code:: bash - 4.708930032276375e-08 - 3.074627144384774e-08 - 3.4164735698703244e-08 - 4.9961114415227386e-08 - 7.804954011212186e-09 - 8.54822031684741e-08 - 1.8625917477836128e-08 - 1.0765481838194546e-08 - 4.535387196032371e-08 - 1.3303233444716197e-07 + 0.0370956467450487 + 0.0036632556827966758 + 0.0017599467532291731 + 0.0006688678943170477 + 0.0010923591711792472 + 0.001714310421328247 + 0.002196032177635475 + 0.0011230918470056704 + 0.0007371056198024898 + 0.013706530361724643 Runner example -------------- diff --git a/niapy/algorithms/basic/pso.py b/niapy/algorithms/basic/pso.py index 3af289ad..ae8bce47 100644 --- a/niapy/algorithms/basic/pso.py +++ b/niapy/algorithms/basic/pso.py @@ -316,6 +316,19 @@ def set_parameters(self, **kwargs): kwargs.pop('w', None), kwargs.pop('min_velocity', None), kwargs.pop('max_velocity', None) super().set_parameters(w=1, min_velocity=-np.inf, max_velocity=np.inf, **kwargs) + def get_parameters(self): + r"""Get parameters of the algorithm. + + Returns: + Dict[str, Any]: Algorithm parameters. + + """ + params = super().get_parameters() + params.pop('w', None) + params.pop('min_velocity', None) + params.pop('max_velocity', None) + return params + class OppositionVelocityClampingParticleSwarmOptimization(ParticleSwarmAlgorithm): r"""Implementation of Opposition-Based Particle Swarm Optimization with Velocity Clamping.