Skip to main content

🐝 abcopt

Canonical Artificial Bee Colony (ABC) optimization in pure Python, with a scikit-learn compatible hyperparameter search.

PyPI tests Python License scikit-learn Dependencies

abcopt implements the Artificial Bee Colony algorithm proposed by Derviş Karaboğa (2005) at Erciyes University, step for step as in the reference implementation published by the ABC research group. It is useful for two things:

  1. Black-box optimization: minimize any objective function within box constraints, with no gradients required.
  2. Hyperparameter tuning: a drop-in alternative to GridSearchCV / RandomizedSearchCV for any scikit-learn compatible estimator (scikit-learn, LightGBM, XGBoost, …).

✨ Features

  • Canonical algorithm, verified: employed, onlooker and scout bee phases exactly as in the Erciyes University reference code. A test runs both implementations on the same random-number stream and checks that they evaluate identical points and return identical results.
  • scikit-learn API: ABCSearchCV exposes fit, predict, score, best_params_, best_score_ and best_estimator_.
  • Mixed search spaces: float (optionally log-scaled), int and categorical hyperparameters.
  • Reproducible: pass random_state and 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: 48 tests, including the reference-equivalence test, run on every push via GitHub Actions.
  • Lightweight: depends only on numpy and scikit-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) = number of employed bees = number of onlooker bees. The colony size (NumberOfPopulation in the reference code) is 2 × pop_size.
max_evals Total objective-function evaluation budget
limit A source is abandoned once its failed-trial counter reaches limit (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 like GridSearchCV (use neg_* scorers for errors).
  • error_score: score given to candidates that fail to fit (a RuntimeWarning is issued); "raise" stops the search instead.
  • n_jobs: parallel jobs inside each cross-validation.
  • After fit: best_params_, best_score_, best_estimator_ (when refit=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 Visit the sources in turn and improve source i with probability p_i = 0.9 · fit_i / max(fit) + 0.1, until pop_size onlookers have been placed, so promising regions are exploited more.
Scout bee Once per cycle, the source with the most failed trials is replaced by a random one if its counter has reached limit, 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%. (This case study was run with abcopt 0.1, before the reference-alignment changes of 0.2.1.)

ABC vs. standard FedAvg

📓 Full notebook and analysis · Related repo: fedLearningwithABC


📊 Benchmark

abcopt was compared with Random Search and Optuna TPE under identical evaluation budgets, using abcopt 0.2.1 (full notebook).

Black-box functions (D = 10, 10 seeds), best value found (lower is better):

Function ABC @ 1k TPE @ 1k ABC @ 10k TPE @ 2k Random @ 10k
Sphere 1.5 0.27 4e-11 0.22 12.3
Rosenbrock 1.3e3 88.5 0.51 114 1.1e4
Rastrigin 29.9 59.7 0.09 47.3 71.2
Ackley 12.8 5.58 0.0003 5.8 16.7

Long-budget convergence

Takeaways

  • With a short budget, TPE is usually ahead; ABC beats Random Search everywhere and wins on the highly multimodal Rastrigin.
  • With a larger budget, ABC converges to near the global optimum on all four functions while TPE stalls.
  • ABC's own overhead is tiny: about 0.02 s vs 24 s for TPE over 2,000 evaluations.
  • For hyperparameter tuning with only 50 candidates, the three methods are within noise on test accuracy, and TPE finds the best CV score. For very small budgets of expensive evaluations, TPE is the better choice.

🗺️ Roadmap

  • Categorical and log-scaled hyperparameters
  • random_state for reproducibility
  • Unit tests and continuous integration
  • Parallel cross-validation (n_jobs)
  • Benchmark against Random Search and Optuna TPE
  • 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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for abcopt 0.2.1
File Size Uploaded
abcopt-0.2.1.tar.gz 19.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for abcopt 0.2.1
File Interpreter ABI Platform
abcopt-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.7 kB

Release files / abcopt-0.2.1.tar.gz

Download URL abcopt-0.2.1.tar.gz
Size 19.5 kB
Tags Source
SHA-256 checksum
How to use checksums
58e75087682f51bd3504b33e812269e8e3256782adfb21cbd36ac51ddb49fc5c
BLAKE2b-256 checksum
How to use checksums
7d9b53bf111ecdda781d13801680f9b25e97e5ea4f079b15f311991655a4a2ba
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 13, 2026.

Transparency log

Release files / abcopt-0.2.1-py3-none-any.whl

Download URL abcopt-0.2.1-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
33cfbd8408b14c93f4cff98e4f3f22b3188803dd9033edaf50a08439b83fcd3d
BLAKE2b-256 checksum
How to use checksums
3b4678761b34ed82d66bb81545d51db09647310f653d79171bece9f0146c8238
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 13, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.2

2 release files

This release

0.2.1 This release

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page