🐝 abcopt
Canonical Artificial Bee Colony (ABC) optimization in pure Python, with a scikit-learn compatible hyperparameter search.
abcopt is a faithful implementation of the Artificial Bee Colony algorithm proposed by Derviş Karaboğa (2005) at Erciyes University. It is useful for two things:
- Black-box optimization: minimize any objective function within box constraints, with no gradients required.
- Hyperparameter tuning: a drop-in alternative to
GridSearchCV/RandomizedSearchCVfor any scikit-learn compatible estimator (scikit-learn, LightGBM, XGBoost, …).
✨ Features
- Canonical algorithm: employed, onlooker (roulette-wheel selection) and scout bee phases, following the original paper; one dimension is perturbed per move.
- scikit-learn API:
ABCSearchCVexposesfit,predict,score,best_params_,best_score_andbest_estimator_. - Mixed search spaces:
float(optionally log-scaled),intandcategoricalhyperparameters. - Reproducible: pass
random_stateand every run is identical. - Evaluation budget: you set
max_evals, so the run cost is predictable. - Robust: failing candidates raise a warning instead of being hidden (or stop the search with
error_score="raise"), and invalid inputs fail fast with clear messages. - Convergence history: every run returns its best-so-far curve for plotting and comparison.
- Tested: 30 unit tests run on every push via GitHub Actions.
- Lightweight: depends only on
numpyandscikit-learn.
📦 Installation
pip install abcopt
from abcopt import ABCSearchCV, CanonicalABCSolver
For development:
git clone https://github.com/yusufkorkmazyigit/abc-optimizer-lib.git
cd abc-optimizer-lib
pip install -e ".[test]"
🚀 Quick Start
1. Hyperparameter tuning with ABCSearchCV
import lightgbm as lgb
from sklearn.datasets import load_breast_cancer
from abcopt import ABCSearchCV
X, y = load_breast_cancer(return_X_y=True)
param_space = {
"learning_rate": {"type": "float", "range": (0.01, 0.3), "log": True},
"n_estimators": {"type": "int", "range": (50, 500)},
"num_leaves": {"type": "int", "range": (20, 100)},
"boosting_type": {"type": "categorical", "choices": ["gbdt", "dart"]},
}
search = ABCSearchCV(
estimator=lgb.LGBMClassifier(verbosity=-1),
param_space=param_space,
cv=3,
scoring="accuracy",
pop_size=20, # number of food sources
max_evals=100, # total objective evaluations (each one = one CV run)
random_state=42, # reproducible search
)
search.fit(X, y)
print("Best CV score:", search.best_score_)
print("Best params: ", search.best_params_)
y_pred = search.predict(X) # uses best_estimator_, refit on the full data
2. Function minimization with CanonicalABCSolver
import numpy as np
from abcopt import CanonicalABCSolver
def sphere(x):
return np.sum(x ** 2) # global minimum: 0 at x = 0
solver = CanonicalABCSolver(
objective_func=sphere,
n_params=3,
lb=[-10, -10, -10],
ub=[10, 10, 10],
max_evals=2000,
random_state=0,
)
best_x, best_cost, history = solver.solve()
print(f"x* = {best_x}, f(x*) = {best_cost:.5f}")
3. Plotting convergence
import matplotlib.pyplot as plt
evals, costs = zip(*history) # solver: best cost so far
# evals, scores = zip(*search.history_) # ABCSearchCV: best CV score so far
plt.plot(evals, costs)
plt.xlabel("Function evaluations")
plt.ylabel("Best cost")
plt.title("ABC convergence")
plt.show()
📖 API Reference
CanonicalABCSolver
CanonicalABCSolver(objective_func, n_params, lb, ub,
pop_size=20, max_evals=1000, limit=None, random_state=None)
| Parameter | Description |
|---|---|
objective_func |
Callable taking a 1-D np.ndarray and returning a scalar cost to minimize |
n_params |
Number of dimensions |
lb, ub |
Lower / upper bounds per dimension |
pop_size |
Number of food sources (SN) |
max_evals |
Total objective-function evaluation budget |
limit |
Trials without improvement before a source is abandoned (default: pop_size × n_params) |
random_state |
Seed (or np.random.Generator) for reproducible runs |
NaN objective values are treated as the worst possible cost.
solve() returns (best_params, best_cost, history), where history is a list of (eval_count, best_cost) tuples.
ABCSearchCV
ABCSearchCV(estimator, param_space, cv=3, scoring="accuracy",
pop_size=20, max_evals=200, limit=None, random_state=None,
n_jobs=None, error_score=np.nan, refit=True, verbose=False)
param_space: one entry per hyperparameter:
| Spec | Example |
|---|---|
| Continuous | {"type": "float", "range": (0.01, 0.3)} |
| Continuous, log scale | {"type": "float", "range": (1e-4, 1e-1), "log": True} |
| Integer (both ends inclusive) | {"type": "int", "range": (2, 12)} |
| Categorical | {"type": "categorical", "choices": ["gini", "entropy"]} |
scoring: any scikit-learn scorer; higher is better, exactly likeGridSearchCV(useneg_*scorers for errors).error_score: score given to candidates that fail to fit (aRuntimeWarningis issued);"raise"stops the search instead.n_jobs: parallel jobs inside each cross-validation.- After
fit:best_params_,best_score_,best_estimator_(whenrefit=True),history_,results_(every candidate with its score),n_evals_. - Methods:
predict,predict_proba,score.
🧠 How ABC Works
ABC models the foraging behaviour of a honey bee colony. Each food source is a candidate solution, and its nectar amount is the solution's fitness.
| Phase | Role |
|---|---|
| Employed bees | Each one improves its own food source by perturbing one dimension towards or away from a random neighbour: v_ij = x_ij + φ·(x_ij − x_kj), φ ∈ [−1, 1]. Greedy selection keeps the better solution. |
| Onlooker bees | Pick sources with probability proportional to fitness (roulette wheel), so promising regions are exploited more. |
| Scout bees | A source that has not improved for limit trials is abandoned and replaced by a random one, which keeps exploration alive. |
Fitness is derived from cost as 1 / (1 + f) for f ≥ 0 and 1 + |f| otherwise.
🔬 Case Study: Federated Learning on MedMNIST
The library was used to tune a federated learning setup on PathMNIST (colon pathology images, 9 classes). The data is split across 5 simulated hospitals with a Dirichlet distribution (α = 0.5), a non-IID setting where FedAvg with default hyperparameters struggles.
CanonicalABCSolver searched the clients' learning rate (0.001–0.1) and SGD momentum (0.0–0.9), scoring each candidate with a short 3-round FL simulation as a proxy task. The best configuration was then trained for 15 rounds:
| Method | LR | Momentum | Test accuracy (round 15) |
|---|---|---|---|
| Standard FedAvg | 0.010 | 0.50 | 78.05% |
| ABC-tuned FedAvg | 0.069 | 0.57 | 85.28% (+7.2 pts) |
For reference, centralized training with the default hyperparameters reached 80.29%.
📓 Full notebook and analysis · Related repo: fedLearningwithABC
🗺️ Roadmap
- Categorical and log-scaled hyperparameters
-
random_statefor reproducibility - Unit tests and continuous integration
- Parallel cross-validation (
n_jobs) - Benchmarks against
GridSearchCV,RandomizedSearchCVand Optuna - Parallel evaluation of candidates within a colony
- PyPI release
Contributions and issues are welcome.
🧪 Development
pip install -e ".[test]"
pytest
See CHANGELOG.md for release notes.
📚 Reference
Karaboğa, D. (2005). An Idea Based on Honey Bee Swarm for Numerical Optimization. Technical Report TR06, Erciyes University, Engineering Faculty, Computer Engineering Department.
If you use this library in your research, please cite the original paper above and link to this repository.
📝 License
Released under the MIT License.
Developed by Yusuf Korkmazyiğit, Computer Engineering, Erciyes University.
Metadata
Release files for abcopt 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| abcopt-0.2.0.tar.gz | 16.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| abcopt-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.3 kB
Release files / abcopt-0.2.0.tar.gz
| Download URL | abcopt-0.2.0.tar.gz |
|---|---|
| Size | 16.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
997329d32a6af21cdc8ef6d6d3223729f446c78e4fd2f45168466a88ff1a435e
|
|
BLAKE2b-256 checksum How to use checksums |
e98cc75326d5278bca9e662a283b847f0c83695b0a3784b5a3e228e9c7aa277d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / abcopt-0.2.0-py3-none-any.whl
| Download URL | abcopt-0.2.0-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
64e93aac819b84cae5210ff12eae94838fd8937de79123eadb37bba4c5e81140
|
|
BLAKE2b-256 checksum How to use checksums |
7754a56a6e458b8a0c84be610ad050647cfddd6b475b895fda8f731916465850
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency log