Metadata-Version: 2.4
Name: metaheuristicpy
Version: 3.2.0
Summary: A Python-based package of Metaheuristic Optimization Algorithms for Solving Continous and Discrete Optimization Problem
Author: I Made Satria Bimantara
Author-email: satriabimantara.imd@gmail.com
Keywords: Optimization,Metaheuristic,Swarm Intelligence,Evolutionary Algorithm,Soft Computing
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: BSD License
Classifier: Framework :: Jupyter
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Operating System :: OS Independent
Classifier: Natural Language :: Indonesian
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pandas==1.5.3
Requires-Dist: numpy==1.24.2
Requires-Dist: openpyxl==3.1.5
Requires-Dist: matplotlib==3.6.3
Requires-Dist: seaborn==0.11.1
Requires-Dist: scipy==1.10.0
Requires-Dist: scikit-learn==1.2.1
Provides-Extra: cec
Requires-Dist: opfunu==1.0.0; extra == "cec"
Provides-Extra: test
Requires-Dist: pytest>=8; extra == "test"
Provides-Extra: fs
Requires-Dist: xgboost>=2.0; extra == "fs"
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: keywords
Dynamic: license-file
Dynamic: provides-extra
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# metaheuristicpy

A Python-based package of Metaheuristic Optimization Algorithms for Solving Continous and Discrete Optimization Problem

Version 3.2.0 adds a **unified continuous core** with 47 algorithms and the **Unified Binarization Framework (UBF)**
that turns every one of them into a wrapper feature-selection method. The legacy classes of 3.1.0 keep their API.

<hr>

## Package layout

| Module | Content |
|---|---|
| `metaheuristicpy.core` | Vectorised optimizer base (`Optimizer`: population, FE budget, global best, convergence history) and 47 algorithms (`ALGORITHMS`, `get_optimizer`) |
| `metaheuristicpy.continous.benchmark_math` | Legacy classes (AOA, EGO, EEGWO, GA, GCRA, GWO, IGWO, KMA, mGWO, PSOClassic, SMA, WaOA, WOA, MO) + every other core algorithm with the same `FUNCTIONS`/`solve()` API; classic formulas and CEC 2017/2020/2022 (`'cec2022_f1'`, via `opfunu`) |
| `metaheuristicpy.discrete.feature_selection` | Legacy binary classes + `UnifiedBinaryFS` and one `B<NAME>` class per core algorithm (e.g. `BPLO`, `BHHO`, `BGWO`) |

### Algorithms of the unified core

| Group | Acronyms (source of the implementation) |
|---|---|
| Official MATLAB/Python code (`_references/metaheuristic_codes`) | HHO, HGS, RUN, INFO, RIME, AO (Artemisinin), PO (Parrot), PLO, MGO, FATA, ECO, EGO |
| Established algorithms, paper equations | PSO, GA, GWO, MGWO, EEGWO, IGWO, WOA, SSA, SMA, AOA (Archimedes), GTO, GCRA, MO, KMA, WaOA |
| Hybrids / improved variants | SAR, EPO, PUMA, HGWODE, GWOWOA, PSOGWO, IAKMA, mEGO |
| Recent paper-only algorithms (2022-2025) | HO, HOA, FLA, PFA, APO, CFOA, CFO, BEO, MBKA, AHO, DIO, DRA |

Every class states its reference; lines that differ from the reference code are marked `deviation:` and
operators reconstructed from illegible equations are marked `simplified:` in the docstring.

## Quick start

```python
from metaheuristicpy.core import get_optimizer
res = get_optimizer("PLO", n_agents=30, max_iter=300, seed=0).optimize(lambda x: (x ** 2).sum(), dim=10, lb=-100, ub=100)
res.best_fitness, res.history          # best value and best-so-far value per iteration

from metaheuristicpy.continous.benchmark_math import HHO
HHO(N_AGENTS=30, MAX_ITERATIONS=300, FUNCTIONS={"name": "cec2022_f6", "dimension": 10, "objective": "min"}).solve()

from metaheuristicpy.discrete.feature_selection import UnifiedBinaryFS, BPLO
fs = UnifiedBinaryFS("PLO", n_agents=20, max_iter=50, seed=0).fit(X_train, y_train)   # CV fitness on train only
X_train_sel, X_test_sel = fs.transform(X_train), fs.transform(X_test)
fs.summary()                           # selected features, fitness, real classifier fits, memo hits, time
BPLO(N_AGENTS=20, MAX_ITERATIONS=50).fit(X_tr, y_tr, X_val, y_val).solve()   # legacy-style API
```

## Unified Binarization Framework (UBF)

The continuous optimizer is untouched; the framework owns the mapping to feature subsets:

1. **Transfer function** — S1-S4, V1-V4 or the default time-varying sigmoid `T(x,τ)=1/(1+e^{-x/τ})`, τ decaying
   from 4 to 0.25 with the consumed share of the FE budget (explorative → exploitative).
2. **Binarization rule** — stochastic standard rule (S-shaped) or elite-referenced complement rule (V-shaped).
3. **Fitness memoisation** — identical masks are scored once.
4. **Elite bit-flip local search** — 10% of the FE budget on the final best mask.

Fitness (minimised): `F(S) = 0.99·(1 − F1_macro(S)) + 0.01·|S|/D`, F1 from stratified 3-fold CV of KNN (k=5) on
the training split.

## Test report

```bash
python -m pytest tests -q                         # unit tests
python benchmarks/benchmark_cec2022.py            # CEC 2022, D=10
```

Result on Windows 11, Python 3.11.0, numpy 1.24.4, scikit-learn 1.2.1, opfunu 1.0.0: **107 passed in 18.69s**.

| Test file | What is checked |
|---|---|
| `tests/test_core_continuous.py` | every one of the 47 algorithms improves on a random point, stays inside the bounds, respects the FE budget, keeps a monotone best-so-far curve, is reproducible with a seed; maximisation, batch objectives, invalid arguments, legacy API and CEC 2022 wrapper |
| `tests/test_ubf_feature_selection.py` | UBF with TV-sigmoid, S2 and V2 transfer functions selects a non-empty subset that beats chance, memo counters, validation-set mode, one `B<NAME>` class per algorithm, transfer-function ranges, argument validation |
| `tests/test_legacy_discrete.py` | regression of the `_initialize_agents` lower-bound fix; legacy binary classes still return a 0/1 subset |

### Continuous benchmark: CEC 2022 (F1-F12, D = 10)

Setting: D = 10, 30 agents, 300 iterations (9,000 FEs), 10 independent runs (seeds 0-9), error = f(x_best) - f*.
Ranks are computed per function on the mean error (1 = best) and averaged over the 12 functions.
Raw runs: `benchmarks/results/cec2022_d10_raw.csv`; mean errors of all algorithms: `cec2022_d10_mean_error.csv`.

| Rank | Algorithm | Mean rank (F1-F12) | Wins | Mean time / run (s) |
|---:|---|---:|---:|---:|
| 1 | SAR | 5.00 | 6 | 8.4 |
| 2 | APO | 5.25 | 2 | 6.2 |
| 3 | INFO | 10.75 | 0 | 8.8 |
| 4 | MGO | 11.08 | 0 | 5.1 |
| 5 | PLO | 11.17 | 1 | 5.0 |
| 6 | CFOA | 11.58 | 0 | 7.0 |
| 7 | HGWODE | 11.75 | 0 | 6.3 |
| 8 | PUMA | 12.50 | 0 | 7.5 |
| 9 | WaOA | 13.42 | 0 | 3.9 |
| 10 | RUN | 13.42 | 0 | 12.6 |
| 11 | GA | 14.75 | 0 | 4.8 |
| 12 | RIME | 14.75 | 0 | 4.3 |
| 13 | SMA | 14.92 | 1 | 3.7 |
| 14 | DRA | 15.00 | 0 | 9.0 |
| 15 | PFA | 15.50 | 0 | 6.4 |
| 16 | PSO | 15.75 | 0 | 3.7 |
| 17 | MGWO | 16.83 | 0 | 3.4 |
| 18 | FLA | 18.75 | 1 | 7.5 |
| 19 | BEO | 19.00 | 0 | 5.8 |
| 20 | GTO | 19.58 | 0 | 3.8 |
| 21 | HGS | 20.17 | 0 | 5.0 |
| 22 | SSA | 20.58 | 1 | 3.3 |
| 23 | DIO | 23.00 | 0 | 6.9 |
| 24 | PSOGWO | 23.67 | 0 | 5.3 |
| 25 | GWO | 23.83 | 0 | 3.4 |
| 26 | HO | 23.92 | 0 | 7.4 |
| 27 | AOA | 24.92 | 0 | 3.3 |
| 28 | AHO | 25.83 | 0 | 8.3 |
| 29 | ECO | 26.00 | 0 | 7.9 |
| 30 | PO | 26.83 | 0 | 4.6 |
| 31 | FATA | 27.00 | 0 | 5.7 |
| 32 | IAKMA | 27.33 | 0 | 7.5 |
| 33 | CFO | 29.00 | 0 | 6.0 |
| 34 | MBKA | 29.58 | 0 | 7.7 |
| 35 | AO | 30.33 | 0 | 6.3 |
| 36 | GWOWOA | 30.75 | 0 | 5.4 |
| 37 | KMA | 31.92 | 0 | 4.9 |
| 38 | IGWO | 33.83 | 0 | 3.3 |
| 39 | HOA | 38.17 | 0 | 7.3 |
| 40 | EGO | 39.08 | 0 | 4.3 |
| 41 | WOA | 40.00 | 0 | 3.4 |
| 42 | mEGO | 40.00 | 0 | 7.7 |
| 43 | HHO | 41.08 | 0 | 4.4 |
| 44 | GCRA | 42.58 | 0 | 4.8 |
| 45 | MO | 44.83 | 0 | 6.8 |
| 46 | EEGWO | 46.33 | 0 | 3.6 |
| 47 | EPO | 46.67 | 0 | 5.6 |

Mean error of the ten best-ranked algorithms:

| Algorithm | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 |
|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|
| SAR | 8.21e-03 | 3.23e+00 | 4.26e-11 | 4.66e-01 | 1.55e-05 | 1.16e+04 | 3.18e+01 | 2.53e+01 | 5.90e-01 | 1.99e+02 | 1.57e-02 | 1.66e+02 |
| APO | 2.14e+01 | 7.50e+00 | 1.72e-08 | 2.36e-01 | 6.35e-04 | 6.37e+03 | 2.26e+01 | 1.84e+01 | 8.10e+01 | 2.00e+02 | 1.05e+00 | 1.67e+02 |
| INFO | 3.30e-13 | 5.34e+00 | 1.25e-13 | 3.72e-01 | 4.35e-01 | 1.16e+04 | 2.15e+01 | 2.21e+01 | 3.61e+02 | 2.55e+02 | 8.78e+01 | 1.67e+02 |
| MGO | 2.03e+02 | 6.78e+00 | 1.30e-04 | 3.23e-01 | 8.53e-02 | 2.61e+04 | 3.51e+01 | 2.70e+01 | 5.68e+01 | 2.02e+02 | 3.93e+00 | 1.67e+02 |
| PLO | 3.98e+02 | 7.03e+00 | 1.32e-04 | 1.69e-01 | 1.61e-01 | 1.33e+04 | 3.54e+01 | 5.03e+01 | 1.73e+02 | 2.07e+02 | 7.48e+00 | 1.67e+02 |
| CFOA | 3.05e+00 | 3.49e+00 | 2.50e-05 | 3.38e-01 | 7.82e-02 | 3.28e+04 | 3.57e+01 | 6.40e+01 | 3.89e+01 | 2.15e+02 | 3.26e-01 | 1.68e+02 |
| HGWODE | 6.67e-04 | 2.12e+01 | 5.37e-10 | 3.72e-01 | 8.97e-03 | 2.92e+04 | 3.09e+01 | 2.58e+01 | 2.92e+02 | 2.12e+02 | 2.63e+02 | 1.66e+02 |
| PUMA | 2.61e+01 | 9.85e+00 | 9.88e-07 | 7.65e-01 | 1.31e-02 | 2.13e+04 | 4.90e+01 | 2.87e+01 | 3.20e+02 | 2.01e+02 | 2.21e+00 | 1.66e+02 |
| WaOA | 1.11e+00 | 1.06e+01 | 2.41e-06 | 3.13e-01 | 2.90e-01 | 2.01e+04 | 3.79e+01 | 2.61e+01 | 2.24e+02 | 2.99e+02 | 9.28e-01 | 1.80e+02 |
| RUN | 3.62e-04 | 8.40e+00 | 6.72e-09 | 2.25e-01 | 5.71e-01 | 1.63e+04 | 4.99e+01 | 5.59e+01 | 2.95e+02 | 2.61e+02 | 8.77e+01 | 1.68e+02 |

Algorithms whose operators pull towards the origin (e.g. EEGWO, HHO, WOA, MO) reach 0 on the unshifted classic
functions but rank low on the shifted and rotated CEC suite; this is expected and is why CEC is reported.

### Discrete benchmark: feature selection on UCI datasets

Protocol: 8 UCI datasets (wine, wdbc, ionosphere, sonar, vehicle, dermatology, spectf, musk1), stratified 70/30 split,
min-max scaling fitted on train. The wrapper search uses only the training split (3-fold CV F1-macro of KNN, k=5,
fitness 0.99·(1−F1) + 0.01·|S|/D, 20 agents × 50 iterations = 1,000 FEs incl. 10% elite local search, TV-sigmoid transfer,
10 runs). The selected subset is scored on the untouched test split with KNN, DT, SVM, MLP, RF and XGBoost.
Baselines: all features, SelectKBest with ANOVA-F and with chi², k tuned by the same CV on train.

Table values are means over 8 datasets × 6 classifiers × 10 runs; *mean rank* is the Friedman rank of mean F1 per
(dataset, classifier) block. Friedman test over all methods: χ² = 107.6, p = 2.8e-06.

| Rank | Method | F1-macro | Accuracy | ROC-AUC | Selected features | Search time (s) | Mean rank |
|---:|---|---:|---:|---:|---:|---:|---:|
| 1 | AllFeatures | 0.8534 | 0.8839 | 0.9245 | 50.0 | 0.0 | 18.70 |
| 2 | BBEO | 0.8514 | 0.8803 | 0.9160 | 20.9 | 17.9 | 21.08 |
| 3 | BSAR | 0.8515 | 0.8814 | 0.9165 | 20.7 | 809.4 | 21.21 |
| 4 | BSMA | 0.8519 | 0.8807 | 0.9182 | 21.0 | 25.4 | 21.27 |
| 5 | BKMA | 0.8508 | 0.8791 | 0.9166 | 21.1 | 20.6 | 22.05 |
| 6 | BHOA | 0.8503 | 0.8799 | 0.9175 | 24.6 | 16.9 | 22.20 |
| 7 | BMBKA | 0.8504 | 0.8792 | 0.9192 | 20.7 | 17.5 | 22.21 |
| 8 | BAOA | 0.8509 | 0.8805 | 0.9185 | 21.0 | 22.2 | 22.67 |
| 9 | BPSOGWO | 0.8502 | 0.8791 | 0.9186 | 21.1 | 16.3 | 22.77 |
| 10 | BHO | 0.8490 | 0.8787 | 0.9132 | 13.2 | 18.7 | 22.82 |
| 11 | BMGWO | 0.8499 | 0.8792 | 0.9168 | 21.4 | 19.6 | 22.89 |
| 12 | BPSO | 0.8501 | 0.8797 | 0.9154 | 21.8 | 40.3 | 22.91 |
| 13 | BAO | 0.8494 | 0.8790 | 0.9152 | 22.2 | 20.1 | 23.14 |
| 14 | BWaOA | 0.8483 | 0.8783 | 0.9131 | 23.0 | 20.0 | 23.26 |
| 15 | BSSA | 0.8495 | 0.8796 | 0.9148 | 21.5 | 18.9 | 23.41 |
| 16 | BGA | 0.8492 | 0.8785 | 0.9142 | 21.5 | 29.4 | 23.56 |
| 17 | BPLO | 0.8497 | 0.8798 | 0.9171 | 21.6 | 19.6 | 23.59 |
| 18 | Chi2 | 0.8425 | 0.8740 | 0.9093 | 20.5 | 0.3 | 24.34 |
| 19 | BGWO | 0.8487 | 0.8793 | 0.9148 | 22.4 | 26.8 | 24.47 |
| 20 | BGCRA | 0.8490 | 0.8778 | 0.9164 | 22.1 | 18.8 | 24.50 |
| 21 | BDIO | 0.8476 | 0.8786 | 0.9148 | 22.0 | 17.3 | 24.78 |
| 22 | BIAKMA | 0.8470 | 0.8771 | 0.9160 | 20.0 | 18.7 | 24.84 |
| 23 | BHHO | 0.8490 | 0.8786 | 0.9135 | 13.4 | 16.7 | 24.98 |
| 24 | BMO | 0.8483 | 0.8777 | 0.9151 | 21.3 | 20.7 | 25.01 |
| 25 | BGWOWOA | 0.8489 | 0.8787 | 0.9154 | 21.3 | 18.4 | 25.08 |
| 26 | BEEGWO | 0.8492 | 0.8780 | 0.9166 | 21.1 | 21.9 | 25.24 |
| 27 | BECO | 0.8484 | 0.8784 | 0.9150 | 19.1 | 14.9 | 25.36 |
| 28 | BmEGO | 0.8485 | 0.8784 | 0.9142 | 17.6 | 18.2 | 25.60 |
| 29 | BGTO | 0.8472 | 0.8780 | 0.9132 | 20.8 | 21.9 | 25.96 |
| 30 | BINFO | 0.8470 | 0.8758 | 0.9122 | 20.9 | 16.5 | 26.17 |
| 31 | BAPO | 0.8484 | 0.8773 | 0.9172 | 21.4 | 17.7 | 26.20 |
| 32 | BEGO | 0.8472 | 0.8780 | 0.9110 | 16.8 | 23.4 | 26.51 |
| 33 | BCFO | 0.8482 | 0.8778 | 0.9150 | 21.4 | 17.5 | 26.59 |
| 34 | BEPO | 0.8473 | 0.8759 | 0.9097 | 8.7 | 8.8 | 26.61 |
| 35 | BAHO | 0.8476 | 0.8783 | 0.9157 | 21.0 | 17.1 | 26.74 |
| 36 | BHGWODE | 0.8468 | 0.8766 | 0.9126 | 21.0 | 18.4 | 26.92 |
| 37 | BPFA | 0.8477 | 0.8788 | 0.9146 | 21.1 | 17.3 | 27.25 |
| 38 | BIGWO | 0.8477 | 0.8769 | 0.9160 | 20.7 | 20.1 | 27.29 |
| 39 | BRUN | 0.8474 | 0.8779 | 0.9145 | 15.8 | 18.1 | 27.35 |
| 40 | BDRA | 0.8473 | 0.8768 | 0.9149 | 20.7 | 17.5 | 27.49 |
| 41 | ANOVA | 0.8438 | 0.8754 | 0.9117 | 18.5 | 0.3 | 27.75 |
| 42 | BCFOA | 0.8479 | 0.8781 | 0.9137 | 21.9 | 16.9 | 28.27 |
| 43 | BPO | 0.8471 | 0.8776 | 0.9142 | 20.9 | 18.4 | 28.59 |
| 44 | BMGO | 0.8458 | 0.8760 | 0.9132 | 21.1 | 18.7 | 29.39 |
| 45 | BWOA | 0.8458 | 0.8764 | 0.9109 | 13.2 | 16.1 | 29.75 |
| 46 | BRIME | 0.8459 | 0.8771 | 0.9148 | 21.8 | 15.7 | 29.92 |
| 47 | BHGS | 0.8458 | 0.8767 | 0.9131 | 21.6 | 17.9 | 30.36 |
| 48 | BFLA | 0.8468 | 0.8762 | 0.9142 | 20.8 | 16.3 | 31.48 |
| 49 | BPUMA | 0.8448 | 0.8752 | 0.9136 | 21.4 | 17.8 | 31.48 |
| 50 | BFATA | 0.8443 | 0.8743 | 0.9123 | 11.2 | 16.7 | 32.98 |

Reading: the binary metaheuristics keep the F1 of the full feature set within ~0.2 points while using ~58% fewer
features, and are ahead of the ANOVA-F and chi² filters. The best-ranked wrappers are close to each other; a
Wilcoxon signed-rank test of the best-ranked one against every other method (Holm-corrected) is in
`benchmarks/results/fs_uci_wilcoxon_best.csv`. Full study (one notebook per algorithm, convergence plots, ROC
curves, confusion matrices): research repository `#17-binarymetaheuristic_for_fs`.

## Changelog

- **3.2.0** — unified core (47 algorithms), UBF feature selection, CEC 2017/2020/2022 support, pytest suite.
  Fix: `BaseMetaheuristics._initialize_agents` read the lower bound from `UB`, so every initial position of the
  legacy discrete classes was 1.0.
- 3.1.0 and earlier — legacy continuous and discrete classes.

<hr>

# Cite

If you think this package is useful, please cite this paper in your project: <br>
[1] Bimantara, I.M.S., Sanjaya ER, N., Purwitasari, D. (2023). Character Entity Recognition Using Hybrid Binary-Particle Swarm Optimization and Conditional Random Field on Balinese Folklore Text. In: Delir Haghighi, P., et al. Information Integration and Web Intelligence. iiWAS 2023. Lecture Notes in Computer Science, vol 14416. Springer, Cham. https://doi.org/10.1007/978-3-031-48316-5_15 <br>
[2] Bimantara, I. M. S., & Widiartha, I. M. (2023). Optimization of K-Means Clustering Using Particle Swarm Optimization Algorithm for Grouping Traveler Reviews Data on Tripadvisor Sites. Jurnal Ilmiah Kursor, 12(1), 1-10. <br>
[3] Supriana, I. W., Raharja, M. A., Bimantara, I. M. S., & Bramantya, D. (2021). Implementasi dua model crossover pada algoritma genetika untuk optimasi penggunaan ruang perkuliahan. Jurnal RESISTOR (Rekayasa Sistem Komputer), 4(2), 167-177. <br>
[4] BIMANTARA, I Made Satria et al. Implementasi Generalized Learning Vector Quantization (GLVQ) dan Particle Swarm Optimization (PSO) Untuk Klasifikasi Kanker Payudara. JELIKU (Jurnal Elektronik Ilmu Komputer Udayana), [S.l.], v. 10, n. 4, p. 307-318, may 2022. ISSN 2654-5101. Available at: <https://ojs.unud.ac.id/index.php/jlk/article/view/85746>. Date accessed: 31 may 2025. doi: https://doi.org/10.24843/JLK.2022.v10.i04.p01. <br>

# Contributors and Developer

- I Made Satria Bimantara
- email: satriabimantara.idm@gmail.com
