Skip to content

Commit 6ae0981

Browse files
authored
Merge pull request #8 from firefly-cpp/modifications
Add test
2 parents bd93aff + ce1a5ef commit 6ae0981

File tree

13 files changed

+88
-88
lines changed

13 files changed

+88
-88
lines changed

FireflyAlgorithm.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ dnf install python-fireflyalgorithm
2626

2727
```python
2828
import numpy as np
29-
from FireflyAlgorithm import FireflyAlgorithm
30-
29+
from fireflyalgorithm import FireflyAlgorithm
3130

3231
def sphere(x):
3332
return np.sum(x ** 2)
3433

34+
FA = FireflyAlgorithm()
35+
best = FA.run(function=sphere, dim=10, lb=-5, ub=5, max_evals=10000)
3536

36-
best = FireflyAlgorithm(function=sphere, dim=10, lb=-5, ub=5, max_evals=10000)
3737
print(best)
3838
```
3939

4040
## Reference Papers:
4141

42-
I. Fister Jr., X.-S. Yang, I. Fister, J. Brest. [Memetic firefly algorithm for combinatorial optimization](http://www.iztok-jr-fister.eu/static/publications/44.pdf) in Bioinspired Optimization Methods and their Applications (BIOMA 2012), B. Filipic and J.Silc, Eds.
43-
Jozef Stefan Institute, Ljubljana, Slovenia, 2012
42+
I. Fister Jr., X.-S. Yang, I. Fister, J. Brest. [Memetic firefly algorithm for combinatorial optimization](http://www.iztok-jr-fister.eu/static/publications/44.pdf) in Bioinspired Optimization Methods and their Applications (BIOMA 2012), B. Filipic and J.Silc, Eds.
43+
Jozef Stefan Institute, Ljubljana, Slovenia, 2012
4444

4545
I. Fister, I. Fister Jr., X.-S. Yang, J. Brest. [A comprehensive review of firefly algorithms](http://www.iztok-jr-fister.eu/static/publications/23.pdf). Swarm and Evolutionary Computation 13 (2013): 34-46.

README.rst

Lines changed: 0 additions & 42 deletions
This file was deleted.

examples/run.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
from fireflyalgorithm import FireflyAlgorithm
3+
4+
5+
def sphere(x):
6+
return np.sum(x ** 2)
7+
8+
9+
FA = FireflyAlgorithm()
10+
best = FA.run(function=sphere, dim=10, lb=-5, ub=5, max_evals=10000)
11+
12+
print(best)

fireflyalgorithm/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from fireflyalgorithm.fireflyalgorithm import FireflyAlgorithm
2+
3+
__all__ = ['FireflyAlgorithm']
4+
5+
__version__ = '0.2'

fireflyalgorithm/fireflyalgorithm.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import numpy as np
2+
from numpy.random import default_rng
3+
4+
5+
class FireflyAlgorithm:
6+
def __init__(self, pop_size=20, alpha=1.0, betamin=1.0, gamma=0.01, seed=None):
7+
self.pop_size = pop_size
8+
self.alpha = alpha
9+
self.betamin = betamin
10+
self.gamma = gamma
11+
self.rng = default_rng(seed)
12+
13+
def run(self, function, dim, lb, ub, max_evals):
14+
fireflies = self.rng.uniform(lb, ub, (self.pop_size, dim))
15+
intensity = np.apply_along_axis(function, 1, fireflies)
16+
best = np.min(intensity)
17+
18+
evaluations = self.pop_size
19+
new_alpha = self.alpha
20+
search_range = ub - lb
21+
22+
while evaluations <= max_evals:
23+
new_alpha *= 0.97
24+
for i in range(self.pop_size):
25+
for j in range(self.pop_size):
26+
if intensity[i] >= intensity[j]:
27+
r = np.sum(np.square(fireflies[i] - fireflies[j]), axis=-1)
28+
beta = self.betamin * np.exp(-self.gamma * r)
29+
steps = new_alpha * (self.rng.random(dim) - 0.5) * search_range
30+
fireflies[i] += beta * (fireflies[j] - fireflies[i]) + steps
31+
fireflies[i] = np.clip(fireflies[i], lb, ub)
32+
intensity[i] = function(fireflies[i])
33+
evaluations += 1
34+
best = min(intensity[i], best)
35+
return best

fireflyalgorithm/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

fireflyalgorithm/tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import logging
2+
3+
4+
def pytest_configure(config):
5+
r"""Disable verbose output when running tests."""
6+
logging.basicConfig(level=logging.DEBUG)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from unittest import TestCase
2+
3+
import numpy as np
4+
from fireflyalgorithm import FireflyAlgorithm
5+
import os
6+
7+
def sphere(x):
8+
return np.sum(x ** 2)
9+
10+
class TestFA(TestCase):
11+
def test_algorithm(self):
12+
FA = FireflyAlgorithm()
13+
best = FA.run(function=sphere, dim=10, lb=-5, ub=5, max_evals=10000)
14+
self.assertLess(best, 5)

pyproject.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ authors = ["firefly-cpp"]
66
license = "MIT"
77
readme = "README.md"
88

9-
packages = [
10-
{include = "FireflyAlgorithm.py"},
9+
include = [
10+
{ path="LICENSE", format="sdist" }
1111
]
1212

13+
1314
[tool.poetry.dependencies]
1415
python = "^3.7"
1516
numpy = [
1617
{ version = "^1.21.5", python = ">=3.7,<3.11" },
1718
{ version = "^1.22.0", python = "^3.11" }
1819
]
1920

21+
[tool.poetry.dev-dependencies]
22+
pytest = "^7.0.1"
23+
2024
[build-system]
2125
requires = ["poetry-core>=1.0.0"]
2226
build-backend = "poetry.core.masonry.api"

run.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/test_fireflyalgorithm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from fireflyalgorithm.tests.conftest import pytest_configure
2+
3+
__all__ = ["pytest_configure"]

0 commit comments

Comments
 (0)