Skip to content

Added a Command Line Interface #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,51 @@ $ yay -Syyu python-fireflyalgorithm
## Usage:

```python
import numpy as np
from fireflyalgorithm import FireflyAlgorithm

def sphere(x):
return np.sum(x ** 2)
from fireflyalgorithm.problems import sphere

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

print(best)
```

### Command line interface

The package also comes with a simple command line interface which allows you to evaluate the algorithm on several
popular test functions

```shell
firefly-algorithm -h
```

```text
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]

Evaluate the Firefly Algorithm on one or more test functions

options:
-h, --help show this help message and exit
--problem PROBLEM Test problem to evaluate
-d DIMENSION, --dimension DIMENSION
Dimension of the problem
-l LOWER, --lower LOWER
Lower bounds of the problem
-u UPPER, --upper UPPER
Upper bounds of the problem
-nfes MAX_EVALS, --max-evals MAX_EVALS
Max number of fitness function evaluations
-r RUNS, --runs RUNS Number of runs of the algorithm
--pop-size POP_SIZE Population size
--alpha ALPHA Randomness strength
--beta-min BETA_MIN Attractiveness constant
--gamma GAMMA Absorption coefficient
--seed SEED Seed for the random number generator
```

**Note:** The CLI script can also run as a python module (python -m niaarm ...)


## Reference Papers:

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.
Expand Down
4 changes: 2 additions & 2 deletions fireflyalgorithm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fireflyalgorithm.fireflyalgorithm import FireflyAlgorithm

__all__ = ['FireflyAlgorithm']
__all__ = ["FireflyAlgorithm"]

__version__ = '0.3.4'
__version__ = "0.3.4"
6 changes: 6 additions & 0 deletions fireflyalgorithm/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
from fireflyalgorithm import cli


if __name__ == "__main__":
sys.exit(cli.main())
79 changes: 79 additions & 0 deletions fireflyalgorithm/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import argparse
import numpy as np
from fireflyalgorithm.problems import PROBLEMS, get_problem
from fireflyalgorithm.fireflyalgorithm import FireflyAlgorithm


def get_parser():
parser = argparse.ArgumentParser(
prog="firefly-algorithm",
description="Evaluate the Firefly Algorithm on one or more test functions",
)

problem_functions = list(PROBLEMS.keys())
parser.add_argument(
"--problem",
type=str,
required=True,
choices=problem_functions,
metavar="PROBLEM",
help="Test problem to evaluate",
)
parser.add_argument(
"-d", "--dimension", type=int, required=True, help="Dimension of the problem"
)
parser.add_argument(
"-l", "--lower", type=float, required=True, help="Lower bounds of the problem"
)
parser.add_argument(
"-u", "--upper", type=float, required=True, help="Upper bounds of the problem"
)
parser.add_argument(
"-nfes",
"--max-evals",
type=int,
required=True,
help="Max number of fitness function evaluations",
)
parser.add_argument(
"-r", "--runs", type=int, default=1, help="Number of runs of the algorithm"
)
parser.add_argument("--pop-size", type=int, default=20, help="Population size")
parser.add_argument("--alpha", type=float, default=1.0, help="Randomness strength")
parser.add_argument(
"--beta-min", type=float, default=1.0, help="Attractiveness constant"
)
parser.add_argument(
"--gamma", type=float, default=0.01, help="Absorption coefficient"
)
parser.add_argument("--seed", type=int, help="Seed for the random number generator")
return parser


def main():
parser = get_parser()
args = parser.parse_args()

algorithm = FireflyAlgorithm(
args.pop_size, args.alpha, args.beta_min, args.gamma, args.seed
)
problem = get_problem(args.problem)
dim = args.dimension
lb = args.lower
ub = args.upper
max_evals = args.max_evals
runs = args.runs

fitness = np.empty(runs)
for i in range(runs):
fitness[i] = algorithm.run(problem, dim, lb, ub, max_evals)

if runs == 1:
print(f"Best fitness: {fitness[0]}")
else:
print(f"Best: {fitness.min()}")
print(f"Worst: {fitness.max()}")
print(f"Mean: {fitness.mean()}")
print(f"Std: {fitness.std()}")

return 0
Loading