Skip to content

Commit 8dd231c

Browse files
authored
Merge pull request #15 from zStupan/master
Added a Command Line Interface
2 parents fe6137c + a0fa93d commit 8dd231c

File tree

7 files changed

+472
-111
lines changed

7 files changed

+472
-111
lines changed

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,51 @@ $ yay -Syyu python-fireflyalgorithm
4141
## Usage:
4242

4343
```python
44-
import numpy as np
4544
from fireflyalgorithm import FireflyAlgorithm
46-
47-
def sphere(x):
48-
return np.sum(x ** 2)
45+
from fireflyalgorithm.problems import sphere
4946

5047
FA = FireflyAlgorithm()
5148
best = FA.run(function=sphere, dim=10, lb=-5, ub=5, max_evals=10000)
5249

5350
print(best)
5451
```
5552

53+
### Command line interface
54+
55+
The package also comes with a simple command line interface which allows you to evaluate the algorithm on several
56+
popular test functions
57+
58+
```shell
59+
firefly-algorithm -h
60+
```
61+
62+
```text
63+
usage: firefly-algorithm [-h] --problem PROBLEM -d DIMENSION -l LOWER -u UPPER -nfes MAX_EVALS [-r RUNS] [--pop-size POP_SIZE] [--alpha ALPHA] [--beta-min BETA_MIN] [--gamma GAMMA] [--seed SEED]
64+
65+
Evaluate the Firefly Algorithm on one or more test functions
66+
67+
options:
68+
-h, --help show this help message and exit
69+
--problem PROBLEM Test problem to evaluate
70+
-d DIMENSION, --dimension DIMENSION
71+
Dimension of the problem
72+
-l LOWER, --lower LOWER
73+
Lower bounds of the problem
74+
-u UPPER, --upper UPPER
75+
Upper bounds of the problem
76+
-nfes MAX_EVALS, --max-evals MAX_EVALS
77+
Max number of fitness function evaluations
78+
-r RUNS, --runs RUNS Number of runs of the algorithm
79+
--pop-size POP_SIZE Population size
80+
--alpha ALPHA Randomness strength
81+
--beta-min BETA_MIN Attractiveness constant
82+
--gamma GAMMA Absorption coefficient
83+
--seed SEED Seed for the random number generator
84+
```
85+
86+
**Note:** The CLI script can also run as a python module (python -m niaarm ...)
87+
88+
5689
## Reference Papers:
5790

5891
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.

fireflyalgorithm/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from fireflyalgorithm.fireflyalgorithm import FireflyAlgorithm
22

3-
__all__ = ['FireflyAlgorithm']
3+
__all__ = ["FireflyAlgorithm"]
44

5-
__version__ = '0.3.4'
5+
__version__ = "0.3.4"

fireflyalgorithm/__main__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
from fireflyalgorithm import cli
3+
4+
5+
if __name__ == "__main__":
6+
sys.exit(cli.main())

fireflyalgorithm/cli.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import argparse
2+
import numpy as np
3+
from fireflyalgorithm.problems import PROBLEMS, get_problem
4+
from fireflyalgorithm.fireflyalgorithm import FireflyAlgorithm
5+
6+
7+
def get_parser():
8+
parser = argparse.ArgumentParser(
9+
prog="firefly-algorithm",
10+
description="Evaluate the Firefly Algorithm on one or more test functions",
11+
)
12+
13+
problem_functions = list(PROBLEMS.keys())
14+
parser.add_argument(
15+
"--problem",
16+
type=str,
17+
required=True,
18+
choices=problem_functions,
19+
metavar="PROBLEM",
20+
help="Test problem to evaluate",
21+
)
22+
parser.add_argument(
23+
"-d", "--dimension", type=int, required=True, help="Dimension of the problem"
24+
)
25+
parser.add_argument(
26+
"-l", "--lower", type=float, required=True, help="Lower bounds of the problem"
27+
)
28+
parser.add_argument(
29+
"-u", "--upper", type=float, required=True, help="Upper bounds of the problem"
30+
)
31+
parser.add_argument(
32+
"-nfes",
33+
"--max-evals",
34+
type=int,
35+
required=True,
36+
help="Max number of fitness function evaluations",
37+
)
38+
parser.add_argument(
39+
"-r", "--runs", type=int, default=1, help="Number of runs of the algorithm"
40+
)
41+
parser.add_argument("--pop-size", type=int, default=20, help="Population size")
42+
parser.add_argument("--alpha", type=float, default=1.0, help="Randomness strength")
43+
parser.add_argument(
44+
"--beta-min", type=float, default=1.0, help="Attractiveness constant"
45+
)
46+
parser.add_argument(
47+
"--gamma", type=float, default=0.01, help="Absorption coefficient"
48+
)
49+
parser.add_argument("--seed", type=int, help="Seed for the random number generator")
50+
return parser
51+
52+
53+
def main():
54+
parser = get_parser()
55+
args = parser.parse_args()
56+
57+
algorithm = FireflyAlgorithm(
58+
args.pop_size, args.alpha, args.beta_min, args.gamma, args.seed
59+
)
60+
problem = get_problem(args.problem)
61+
dim = args.dimension
62+
lb = args.lower
63+
ub = args.upper
64+
max_evals = args.max_evals
65+
runs = args.runs
66+
67+
fitness = np.empty(runs)
68+
for i in range(runs):
69+
fitness[i] = algorithm.run(problem, dim, lb, ub, max_evals)
70+
71+
if runs == 1:
72+
print(f"Best fitness: {fitness[0]}")
73+
else:
74+
print(f"Best: {fitness.min()}")
75+
print(f"Worst: {fitness.max()}")
76+
print(f"Mean: {fitness.mean()}")
77+
print(f"Std: {fitness.std()}")
78+
79+
return 0

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy