From aafbdb3283da2c84bfaa40bfb3845db5fff15b4b Mon Sep 17 00:00:00 2001 From: zStupan Date: Sun, 5 Nov 2023 20:14:09 +0100 Subject: [PATCH 1/4] added test problem implementations --- fireflyalgorithm/problems.py | 268 +++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 fireflyalgorithm/problems.py diff --git a/fireflyalgorithm/problems.py b/fireflyalgorithm/problems.py new file mode 100644 index 0000000..e0747e6 --- /dev/null +++ b/fireflyalgorithm/problems.py @@ -0,0 +1,268 @@ +import numpy as np + + +def ackley(x): + a = 20 + b = 0.2 + c = 2 * np.pi + dim = len(x) + + val1 = np.sum(np.square(x)) + val2 = np.sum(np.cos(c * x)) + + temp1 = -b * np.sqrt(val1 / dim) + temp2 = val2 / dim + + return -a * np.exp(temp1) - np.exp(temp2) + a + np.exp(1) + + +def alpine1(x): + return np.sum(np.abs(np.sin(x) + 0.1 * x)) + + +def alpine2(x): + return np.prod(np.sqrt(x) * np.sin(x)) + + +def cigar(x): + return x[0] ** 2 + 1000000 * np.sum(x[1:] ** 2) + + +def cosine_mixture(x): + return -0.1 * np.sum(np.cos(5 * np.pi * x)) - np.sum(x**2) + + +def csendes(x): + mask = x != 0 + return np.sum(np.power(x[mask], 6) * (2 + np.sin(1 / x[mask]))) + + +def dixon_price(x): + dim = len(x) + indices = np.arange(2, dim) + val = np.sum(indices * (2 * x[2:] ** 2 - x[1 : dim - 1]) ** 2) + return (x[0] - 1) ** 2 + val + + +def griewank(x): + dim = len(x) + i = np.arange(1, dim + 1) + val1 = np.sum(x**2 / 4000) + val2 = np.prod(np.cos(x / np.sqrt(i))) + return val1 - val2 + 1 + + +def katsuura(x): + dim = len(x) + k = np.atleast_2d(np.arange(1, 33)).T + i = np.arange(0, dim * 1) + inner = np.round(2**k * x) * (2 ** (-k)) + return np.prod(np.sum(inner, axis=0) * (i + 1) + 1) + + +def levy(x): + w = 1 + (x - 1) / 4 + wi = w[:-1] + term1 = np.sin(np.pi * w[0]) ** 2 + term2 = np.sum((wi - 1) ** 2 * (1 + 10 * np.sin(np.pi * wi + 1))) + term3 = (w[-1] - 1) ** 2 * (1 + np.sin(2 * np.pi * w[-1]) ** 2) + return term1 + term2 + term3 + + +def michalewicz(x): + dim = len(x) + m = 10 + i = np.arange(1, dim + 1) + return -np.sum(np.sin(x) * np.sin(i * x**2 / np.pi) ** (2 * m)) + + +def perm1(x): + dim = len(x) + beta = 0.5 + k = np.atleast_2d(np.arange(dim) + 1).T + j = np.atleast_2d(np.arange(dim) + 1) + s = (j**k + beta) * ((x / j) ** k - 1) + return np.sum(np.sum(s, axis=1) ** 2) + + +def perm2(x): + dim = len(x) + beta = 10 + k = np.atleast_2d(np.arange(dim) + 1).T + j = np.atleast_2d(np.arange(dim) + 1) + s = (j + beta) * (x**k - (1 / j) ** k) + return np.sum(np.sum(s, axis=1) ** 2) + + +def pinter(x): + dim = len(x) + x = np.asarray(x) + sub = np.roll(x, 1) + add = np.roll(x, -1) + indices = np.arange(1, dim + 1) + + a = sub * np.sin(x) + np.sin(add) + b = (sub * sub) - 2 * x + 3 * add - np.cos(x) + 1 + + val1 = np.sum(indices * x * x) + val2 = np.sum(20 * indices * np.power(np.sin(a), 2)) + val3 = np.sum(indices * np.log10(1 + indices * np.power(b, 2))) + + return val1 + val2 + val3 + + +def powell(x): + x1 = x[0::4] + x2 = x[1::4] + x3 = x[2::4] + x4 = x[3::4] + + term1 = (x1 + 10 * x2) ** 2 + term2 = 5 * (x3 - x4) ** 2 + term3 = (x2 - 2 * x3) ** 4 + term4 = 10 * (x1 - x4) ** 4 + return np.sum(term1 + term2 + term3 + term4) + + +def quing(x): + dim = len(x) + return np.sum(np.power(x**2 - np.arange(1, dim + 1), 2)) + + +def quintic(x): + return np.sum(np.abs(x**5 - 3 * x**4 + 4 * x**3 + 2 * x**2 - 10 * x - 4)) + + +def rastrigin(x): + dim = len(x) + return 10 * dim + np.sum(x**2 - 10 * np.cos(2 * np.pi * x)) + + +def rosenbrock(x): + return np.sum(100.0 * (x[1:] - x[:-1] ** 2) ** 2 + (1 - x[:-1]) ** 2, axis=0) + + +def salomon(x): + val = np.sqrt(np.sum(x**2)) + return 1 - np.cos(2 * np.pi * val) + 0.1 * val + + +def schaffer2(x): + return ( + 0.5 + + (np.sin(x[0] ** 2 - x[1] ** 2) ** 2 - 0.5) + / (1 + 0.001 * (x[0] ** 2 + x[1] ** 2)) ** 2 + ) + + +def schaffer4(x): + return ( + 0.5 + + (np.cos(np.sin(x[0] ** 2 - x[1] ** 2)) ** 2 - 0.5) + / (1 + 0.001 * (x[0] ** 2 + x[1] ** 2)) ** 2 + ) + + +def schwefel(x): + dim = len(x) + return 418.9829 * dim - np.sum(x * np.sin(np.sqrt(np.abs(x)))) + + +def schwefel21(x): + return np.amax(np.abs(x)) + + +def schwefel22(x): + return np.sum(np.abs(x)) + np.prod(np.abs(x)) + + +def sphere(x): + return np.sum(x**2) + + +def step(x): + return np.sum(np.floor(np.abs(x))) + + +def step2(x): + return np.sum(np.floor(x + 0.5) ** 2) + + +def styblinski_tang(x): + return 0.5 * np.sum(x**4 - 16 * x**2 + 5 * x) + + +def trid(x): + sum1 = np.sum((x - 1) ** 2) + sum2 = np.sum(x[1:] * x[:-1]) + return sum1 - sum2 + + +def weierstrass(x): + dim = len(x) + kmax = 20 + a = 0.5 + b = 3 + + k = np.atleast_2d(np.arange(kmax + 1)).T + t1 = a**k * np.cos(2 * np.pi * b**k * (x + 0.5)) + t2 = dim * np.sum(a**k.T * np.cos(np.pi * b**k.T)) + + return np.sum(np.sum(t1, axis=0)) - t2 + + +def whitley(x): + xi = x + xj = np.atleast_2d(x).T + + temp = 100 * ((xi**2) - xj) + (1 - xj) ** 2 + inner = (temp**2 / 4000) - np.cos(temp) + 1 + return np.sum(np.sum(inner, axis=0)) + + +def zakharov(x): + dim = len(x) + sum1 = np.sum(x**2) + sum2 = 0.5 * np.sum(np.arange(1, dim + 1) * x) + return sum1 + sum2**2 + sum2**4 + + +PROBLEMS = { + "ackley": ackley, + "alpine1": alpine1, + "alpine2": alpine2, + "cigar": cigar, + "cosine_mixture": cosine_mixture, + "csendes": csendes, + "dixon_price": dixon_price, + "griewank": griewank, + "katsuura": katsuura, + "levy": levy, + "michalewicz": michalewicz, + "perm1": perm1, + "perm2": perm2, + "pinter": pinter, + "powell": powell, + "quing": quing, + "quintic": quintic, + "rastrigin": rastrigin, + "rosenbrock": rosenbrock, + "salomon": salomon, + "schaffer2": schaffer2, + "schaffer4": schaffer4, + "schwefel": schwefel, + "schwefel21": schwefel21, + "schwefel22": schwefel22, + "sphere": sphere, + "step": step, + "step2": step2, + "styblinski_tang": styblinski_tang, + "trid": trid, + "weierstrass": weierstrass, + "whitley": whitley, + "zakharov": zakharov, +} + + +def get_problem(name): + return PROBLEMS[name] From ba4dfe4617626a29d84f519d992ea4b0ccdf01a6 Mon Sep 17 00:00:00 2001 From: zStupan Date: Sun, 5 Nov 2023 20:15:19 +0100 Subject: [PATCH 2/4] updated dependencies --- poetry.lock | 171 +++++++++++++++++++++---------------------------- pyproject.toml | 11 ++-- 2 files changed, 77 insertions(+), 105 deletions(-) diff --git a/poetry.lock b/poetry.lock index 79fe85b..0bf4182 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,34 +1,26 @@ -[[package]] -name = "attrs" -version = "22.2.0" -description = "Classes Without Boilerplate" -category = "dev" -optional = false -python-versions = ">=3.6" - -[package.extras] -cov = ["attrs", "coverage-enable-subprocess", "coverage[toml] (>=5.3)"] -dev = ["attrs"] -docs = ["furo", "sphinx", "myst-parser", "zope.interface", "sphinx-notfound-page", "sphinxcontrib-towncrier", "towncrier"] -tests = ["attrs", "zope.interface"] -tests-no-zope = ["hypothesis", "pympler", "pytest (>=4.3.0)", "pytest-xdist", "cloudpickle", "mypy (>=0.971,<0.990)", "pytest-mypy-plugins"] -tests_no_zope = ["hypothesis", "pympler", "pytest (>=4.3.0)", "pytest-xdist", "cloudpickle", "mypy (>=0.971,<0.990)", "pytest-mypy-plugins"] +# This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand. [[package]] name = "colorama" version = "0.4.6" description = "Cross-platform colored terminal text." -category = "dev" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] [[package]] name = "exceptiongroup" -version = "1.1.0" +version = "1.1.3" description = "Backport of PEP 654 (exception groups)" -category = "dev" optional = false python-versions = ">=3.7" +files = [ + {file = "exceptiongroup-1.1.3-py3-none-any.whl", hash = "sha256:343280667a4585d195ca1cf9cef84a4e178c4b6cf2274caef9859782b567d5e3"}, + {file = "exceptiongroup-1.1.3.tar.gz", hash = "sha256:097acd85d473d75af5bb98e41b61ff7fe35efe6675e4f9370ec6ec5126d160e9"}, +] [package.extras] test = ["pytest (>=6)"] @@ -37,33 +29,75 @@ test = ["pytest (>=6)"] name = "iniconfig" version = "2.0.0" description = "brain-dead simple config-ini parsing" -category = "dev" optional = false python-versions = ">=3.7" +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] [[package]] name = "numpy" -version = "1.24.1" +version = "1.26.1" description = "Fundamental package for array computing in Python" -category = "main" optional = false -python-versions = ">=3.8" +python-versions = "<3.13,>=3.9" +files = [ + {file = "numpy-1.26.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:82e871307a6331b5f09efda3c22e03c095d957f04bf6bc1804f30048d0e5e7af"}, + {file = "numpy-1.26.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:cdd9ec98f0063d93baeb01aad472a1a0840dee302842a2746a7a8e92968f9575"}, + {file = "numpy-1.26.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d78f269e0c4fd365fc2992c00353e4530d274ba68f15e968d8bc3c69ce5f5244"}, + {file = "numpy-1.26.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8ab9163ca8aeb7fd32fe93866490654d2f7dda4e61bc6297bf72ce07fdc02f67"}, + {file = "numpy-1.26.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:78ca54b2f9daffa5f323f34cdf21e1d9779a54073f0018a3094ab907938331a2"}, + {file = "numpy-1.26.1-cp310-cp310-win32.whl", hash = "sha256:d1cfc92db6af1fd37a7bb58e55c8383b4aa1ba23d012bdbba26b4bcca45ac297"}, + {file = "numpy-1.26.1-cp310-cp310-win_amd64.whl", hash = "sha256:d2984cb6caaf05294b8466966627e80bf6c7afd273279077679cb010acb0e5ab"}, + {file = "numpy-1.26.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:cd7837b2b734ca72959a1caf3309457a318c934abef7a43a14bb984e574bbb9a"}, + {file = "numpy-1.26.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:1c59c046c31a43310ad0199d6299e59f57a289e22f0f36951ced1c9eac3665b9"}, + {file = "numpy-1.26.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d58e8c51a7cf43090d124d5073bc29ab2755822181fcad978b12e144e5e5a4b3"}, + {file = "numpy-1.26.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6081aed64714a18c72b168a9276095ef9155dd7888b9e74b5987808f0dd0a974"}, + {file = "numpy-1.26.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:97e5d6a9f0702c2863aaabf19f0d1b6c2628fbe476438ce0b5ce06e83085064c"}, + {file = "numpy-1.26.1-cp311-cp311-win32.whl", hash = "sha256:b9d45d1dbb9de84894cc50efece5b09939752a2d75aab3a8b0cef6f3a35ecd6b"}, + {file = "numpy-1.26.1-cp311-cp311-win_amd64.whl", hash = "sha256:3649d566e2fc067597125428db15d60eb42a4e0897fc48d28cb75dc2e0454e53"}, + {file = "numpy-1.26.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:1d1bd82d539607951cac963388534da3b7ea0e18b149a53cf883d8f699178c0f"}, + {file = "numpy-1.26.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:afd5ced4e5a96dac6725daeb5242a35494243f2239244fad10a90ce58b071d24"}, + {file = "numpy-1.26.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a03fb25610ef560a6201ff06df4f8105292ba56e7cdd196ea350d123fc32e24e"}, + {file = "numpy-1.26.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dcfaf015b79d1f9f9c9fd0731a907407dc3e45769262d657d754c3a028586124"}, + {file = "numpy-1.26.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:e509cbc488c735b43b5ffea175235cec24bbc57b227ef1acc691725beb230d1c"}, + {file = "numpy-1.26.1-cp312-cp312-win32.whl", hash = "sha256:af22f3d8e228d84d1c0c44c1fbdeb80f97a15a0abe4f080960393a00db733b66"}, + {file = "numpy-1.26.1-cp312-cp312-win_amd64.whl", hash = "sha256:9f42284ebf91bdf32fafac29d29d4c07e5e9d1af862ea73686581773ef9e73a7"}, + {file = "numpy-1.26.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:bb894accfd16b867d8643fc2ba6c8617c78ba2828051e9a69511644ce86ce83e"}, + {file = "numpy-1.26.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:e44ccb93f30c75dfc0c3aa3ce38f33486a75ec9abadabd4e59f114994a9c4617"}, + {file = "numpy-1.26.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9696aa2e35cc41e398a6d42d147cf326f8f9d81befcb399bc1ed7ffea339b64e"}, + {file = "numpy-1.26.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a5b411040beead47a228bde3b2241100454a6abde9df139ed087bd73fc0a4908"}, + {file = "numpy-1.26.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:1e11668d6f756ca5ef534b5be8653d16c5352cbb210a5c2a79ff288e937010d5"}, + {file = "numpy-1.26.1-cp39-cp39-win32.whl", hash = "sha256:d1d2c6b7dd618c41e202c59c1413ef9b2c8e8a15f5039e344af64195459e3104"}, + {file = "numpy-1.26.1-cp39-cp39-win_amd64.whl", hash = "sha256:59227c981d43425ca5e5c01094d59eb14e8772ce6975d4b2fc1e106a833d5ae2"}, + {file = "numpy-1.26.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl", hash = "sha256:06934e1a22c54636a059215d6da99e23286424f316fddd979f5071093b648668"}, + {file = "numpy-1.26.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:76ff661a867d9272cd2a99eed002470f46dbe0943a5ffd140f49be84f68ffc42"}, + {file = "numpy-1.26.1-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:6965888d65d2848e8768824ca8288db0a81263c1efccec881cb35a0d805fcd2f"}, + {file = "numpy-1.26.1.tar.gz", hash = "sha256:c8c6c72d4a9f831f328efb1312642a1cafafaa88981d9ab76368d50d07d93cbe"}, +] [[package]] name = "packaging" -version = "23.0" +version = "23.2" description = "Core utilities for Python packages" -category = "dev" optional = false python-versions = ">=3.7" +files = [ + {file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"}, + {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, +] [[package]] name = "pluggy" -version = "1.0.0" +version = "1.3.0" description = "plugin and hook calling mechanisms for python" -category = "dev" optional = false -python-versions = ">=3.6" +python-versions = ">=3.8" +files = [ + {file = "pluggy-1.3.0-py3-none-any.whl", hash = "sha256:d89c696a773f8bd377d18e5ecda92b7a3793cbe66c87060a6fb58c7b6e1061f7"}, + {file = "pluggy-1.3.0.tar.gz", hash = "sha256:cf61ae8f126ac6f7c451172cf30e3e43d3ca77615509771b3a984a0730651e12"}, +] [package.extras] dev = ["pre-commit", "tox"] @@ -71,14 +105,16 @@ testing = ["pytest", "pytest-benchmark"] [[package]] name = "pytest" -version = "7.2.1" +version = "7.4.3" description = "pytest: simple powerful testing with Python" -category = "dev" optional = false python-versions = ">=3.7" +files = [ + {file = "pytest-7.4.3-py3-none-any.whl", hash = "sha256:0d009c083ea859a71b76adf7c1d502e4bc170b80a8ef002da5806527b9591fac"}, + {file = "pytest-7.4.3.tar.gz", hash = "sha256:d989d136982de4e3b29dabcc838ad581c64e8ed52c11fbe86ddebd9da0818cd5"}, +] [package.dependencies] -attrs = ">=19.2.0" colorama = {version = "*", markers = "sys_platform == \"win32\""} exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""} iniconfig = "*" @@ -87,81 +123,20 @@ pluggy = ">=0.12,<2.0" tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} [package.extras] -testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] +testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] [[package]] name = "tomli" version = "2.0.1" description = "A lil' TOML parser" -category = "dev" optional = false python-versions = ">=3.7" - -[metadata] -lock-version = "1.1" -python-versions = "^3.9" -content-hash = "ee07901be93db48ea18c563d48b12653e83a575fe902db609850e0f31719612c" - -[metadata.files] -attrs = [ - {file = "attrs-22.2.0-py3-none-any.whl", hash = "sha256:29e95c7f6778868dbd49170f98f8818f78f3dc5e0e37c0b1f474e3561b240836"}, - {file = "attrs-22.2.0.tar.gz", hash = "sha256:c9227bfc2f01993c03f68db37d1d15c9690188323c067c641f1a35ca58185f99"}, -] -colorama = [ - {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, - {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, -] -exceptiongroup = [ - {file = "exceptiongroup-1.1.0-py3-none-any.whl", hash = "sha256:327cbda3da756e2de031a3107b81ab7b3770a602c4d16ca618298c526f4bec1e"}, - {file = "exceptiongroup-1.1.0.tar.gz", hash = "sha256:bcb67d800a4497e1b404c2dd44fca47d3b7a5e5433dbab67f96c1a685cdfdf23"}, -] -iniconfig = [ - {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, - {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, -] -numpy = [ - {file = "numpy-1.24.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:179a7ef0889ab769cc03573b6217f54c8bd8e16cef80aad369e1e8185f994cd7"}, - {file = "numpy-1.24.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:b09804ff570b907da323b3d762e74432fb07955701b17b08ff1b5ebaa8cfe6a9"}, - {file = "numpy-1.24.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f1b739841821968798947d3afcefd386fa56da0caf97722a5de53e07c4ccedc7"}, - {file = "numpy-1.24.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0e3463e6ac25313462e04aea3fb8a0a30fb906d5d300f58b3bc2c23da6a15398"}, - {file = "numpy-1.24.1-cp310-cp310-win32.whl", hash = "sha256:b31da69ed0c18be8b77bfce48d234e55d040793cebb25398e2a7d84199fbc7e2"}, - {file = "numpy-1.24.1-cp310-cp310-win_amd64.whl", hash = "sha256:b07b40f5fb4fa034120a5796288f24c1fe0e0580bbfff99897ba6267af42def2"}, - {file = "numpy-1.24.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:7094891dcf79ccc6bc2a1f30428fa5edb1e6fb955411ffff3401fb4ea93780a8"}, - {file = "numpy-1.24.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:28e418681372520c992805bb723e29d69d6b7aa411065f48216d8329d02ba032"}, - {file = "numpy-1.24.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e274f0f6c7efd0d577744f52032fdd24344f11c5ae668fe8d01aac0422611df1"}, - {file = "numpy-1.24.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0044f7d944ee882400890f9ae955220d29b33d809a038923d88e4e01d652acd9"}, - {file = "numpy-1.24.1-cp311-cp311-win32.whl", hash = "sha256:442feb5e5bada8408e8fcd43f3360b78683ff12a4444670a7d9e9824c1817d36"}, - {file = "numpy-1.24.1-cp311-cp311-win_amd64.whl", hash = "sha256:de92efa737875329b052982e37bd4371d52cabf469f83e7b8be9bb7752d67e51"}, - {file = "numpy-1.24.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:b162ac10ca38850510caf8ea33f89edcb7b0bb0dfa5592d59909419986b72407"}, - {file = "numpy-1.24.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:26089487086f2648944f17adaa1a97ca6aee57f513ba5f1c0b7ebdabbe2b9954"}, - {file = "numpy-1.24.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:caf65a396c0d1f9809596be2e444e3bd4190d86d5c1ce21f5fc4be60a3bc5b36"}, - {file = "numpy-1.24.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b0677a52f5d896e84414761531947c7a330d1adc07c3a4372262f25d84af7bf7"}, - {file = "numpy-1.24.1-cp38-cp38-win32.whl", hash = "sha256:dae46bed2cb79a58d6496ff6d8da1e3b95ba09afeca2e277628171ca99b99db1"}, - {file = "numpy-1.24.1-cp38-cp38-win_amd64.whl", hash = "sha256:6ec0c021cd9fe732e5bab6401adea5a409214ca5592cd92a114f7067febcba0c"}, - {file = "numpy-1.24.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:28bc9750ae1f75264ee0f10561709b1462d450a4808cd97c013046073ae64ab6"}, - {file = "numpy-1.24.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:84e789a085aabef2f36c0515f45e459f02f570c4b4c4c108ac1179c34d475ed7"}, - {file = "numpy-1.24.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8e669fbdcdd1e945691079c2cae335f3e3a56554e06bbd45d7609a6cf568c700"}, - {file = "numpy-1.24.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ef85cf1f693c88c1fd229ccd1055570cb41cdf4875873b7728b6301f12cd05bf"}, - {file = "numpy-1.24.1-cp39-cp39-win32.whl", hash = "sha256:87a118968fba001b248aac90e502c0b13606721b1343cdaddbc6e552e8dfb56f"}, - {file = "numpy-1.24.1-cp39-cp39-win_amd64.whl", hash = "sha256:ddc7ab52b322eb1e40521eb422c4e0a20716c271a306860979d450decbb51b8e"}, - {file = "numpy-1.24.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl", hash = "sha256:ed5fb71d79e771ec930566fae9c02626b939e37271ec285e9efaf1b5d4370e7d"}, - {file = "numpy-1.24.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ad2925567f43643f51255220424c23d204024ed428afc5aad0f86f3ffc080086"}, - {file = "numpy-1.24.1-pp38-pypy38_pp73-win_amd64.whl", hash = "sha256:cfa1161c6ac8f92dea03d625c2d0c05e084668f4a06568b77a25a89111621566"}, - {file = "numpy-1.24.1.tar.gz", hash = "sha256:2386da9a471cc00a1f47845e27d916d5ec5346ae9696e01a8a34760858fe9dd2"}, -] -packaging = [ - {file = "packaging-23.0-py3-none-any.whl", hash = "sha256:714ac14496c3e68c99c29b00845f7a2b85f3bb6f1078fd9f72fd20f0570002b2"}, - {file = "packaging-23.0.tar.gz", hash = "sha256:b6ad297f8907de0fa2fe1ccbd26fdaf387f5f47c7275fedf8cce89f99446cf97"}, -] -pluggy = [ - {file = "pluggy-1.0.0-py2.py3-none-any.whl", hash = "sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3"}, - {file = "pluggy-1.0.0.tar.gz", hash = "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159"}, -] -pytest = [ - {file = "pytest-7.2.1-py3-none-any.whl", hash = "sha256:c7c6ca206e93355074ae32f7403e8ea12163b1163c976fee7d4d84027c162be5"}, - {file = "pytest-7.2.1.tar.gz", hash = "sha256:d45e0952f3727241918b8fd0f376f5ff6b301cc0777c6f9a556935c92d8a7d42"}, -] -tomli = [ +files = [ {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, ] + +[metadata] +lock-version = "2.0" +python-versions = "^3.9,<3.13" +content-hash = "aa27aa586ebf5e13d1936db455cb5da27cc641ae1ffc6434877928a3baa9bb82" diff --git a/pyproject.toml b/pyproject.toml index 6ebe666..29de9fc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,14 +13,11 @@ include = [ ] [tool.poetry.dependencies] -python = "^3.9" -numpy = [ - { version = "^1.21.5", python = ">=3.7,<3.11" }, - { version = "^1.22.0", python = "^3.11" } -] +python = "^3.9,<3.13" +numpy = "^1.26.1" -[tool.poetry.dev-dependencies] -pytest = "^7.0.1" +[tool.poetry.group.test.dependencies] +pytest = "^7.4.3" [build-system] requires = ["poetry-core"] From bd190dec27c1e43542e9e7ea7adf8183957e20e8 Mon Sep 17 00:00:00 2001 From: zStupan Date: Sun, 5 Nov 2023 20:16:06 +0100 Subject: [PATCH 3/4] Added command line interface --- fireflyalgorithm/__init__.py | 4 +- fireflyalgorithm/__main__.py | 6 +++ fireflyalgorithm/cli.py | 79 ++++++++++++++++++++++++++++++++++++ pyproject.toml | 3 ++ 4 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 fireflyalgorithm/__main__.py create mode 100644 fireflyalgorithm/cli.py diff --git a/fireflyalgorithm/__init__.py b/fireflyalgorithm/__init__.py index 48f3ef7..cb5e64a 100644 --- a/fireflyalgorithm/__init__.py +++ b/fireflyalgorithm/__init__.py @@ -1,5 +1,5 @@ from fireflyalgorithm.fireflyalgorithm import FireflyAlgorithm -__all__ = ['FireflyAlgorithm'] +__all__ = ["FireflyAlgorithm"] -__version__ = '0.3.4' +__version__ = "0.3.4" diff --git a/fireflyalgorithm/__main__.py b/fireflyalgorithm/__main__.py new file mode 100644 index 0000000..75c1ac9 --- /dev/null +++ b/fireflyalgorithm/__main__.py @@ -0,0 +1,6 @@ +import sys +from fireflyalgorithm import cli + + +if __name__ == "__main__": + sys.exit(cli.main()) diff --git a/fireflyalgorithm/cli.py b/fireflyalgorithm/cli.py new file mode 100644 index 0000000..b08d4fd --- /dev/null +++ b/fireflyalgorithm/cli.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 29de9fc..5051837 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,9 @@ numpy = "^1.26.1" [tool.poetry.group.test.dependencies] pytest = "^7.4.3" +[tool.poetry.scripts] +firefly-algorithm = 'fireflyalgorithm.cli:main' + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" From a0fa93d6968e3be1472dce1c87c7ffa24d4e3f1b Mon Sep 17 00:00:00 2001 From: zStupan Date: Sun, 5 Nov 2023 20:25:35 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c325acc..8516155 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,8 @@ $ 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) @@ -53,6 +50,42 @@ 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. 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