Catalog PyPI
PACKAGE EXPLORERView in registry

abcopt

PyPI

Canonical Artificial Bee Colony (ABC) optimization with a scikit-learn compatible hyperparameter search

artificialbeecolonyhyperparameterintelligencemetaheuristicsoptimizationscikit-learnswarm
Versions collected1
Direct dependencies4From release 0.2.2
Last observed20 Oct, 17:15 UTC

README

Version 0.2.2 documentation

🐝 abcopt

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

PyPI Python tests License: MIT

abcopt implements the Artificial Bee Colony algorithm of Derviş Karaboğa (2005) step for step as in the reference implementation published by the ABC research group at Erciyes University, and wraps it in ABCSearchCV, a drop-in alternative to GridSearchCV and RandomizedSearchCV.

ABC on the 2-D Rastrigin function

A real CanonicalABCSolver run on the 2-D Rastrigin function (many local minima, one global minimum ★).
Employed bees move one dimension at a time, onlookers revisit the best sources, and a scout restarts an exhausted source at a random spot.
Numbers are failed-trial counters. Generated by docs/make_animation.py.

Highlights

  • Verified canonical ABC. A test runs abcopt and the Erciyes University reference code on the same random-number stream and checks that both evaluate identical points and return identical results.
  • scikit-learn native. ABCSearchCV provides fit, predict, predict_proba, score, best_params_, best_estimator_ and a cv_results_ table in the same format as GridSearchCV.
  • Mixed search spaces. Continuous (optionally log-scaled), integer and categorical hyperparameters.
  • No wasted runs. Configurations that ABC proposes twice are served from a cache, which roughly halves tuning time on discrete spaces without changing the search.
  • Reproducible and predictable. Fixed evaluation budget (max_evals) and random_state for identical runs.
  • Lightweight. Depends only on numpy and scikit-learn.

Contents: Installation · Quick start · How ABC works · Benchmark · API reference · Notes and limitations · Examples · Citation

Installation

pip install abcopt

Requires Python 3.9+, NumPy and scikit-learn.

Quick start

Tune a scikit-learn model

from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import HistGradientBoostingClassifier
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},
    "max_iter":         {"type": "int",   "range": (50, 300)},
    "max_leaf_nodes":   {"type": "int",   "range": (8, 64)},
    "l2_regularization": {"type": "float", "range": (1e-6, 1.0), "log": True},
}

search = ABCSearchCV(
    HistGradientBoostingClassifier(random_state=0),
    param_space,
    cv=5,
    scoring="accuracy",
    max_evals=60,       # candidates evaluated by ABC
    random_state=0,
)
search.fit(X, y)

print(search.best_score_, search.best_params_)
search.predict(X[:5])   # best_estimator_, refit on all data

Any scikit-learn compatible estimator works, including XGBoost and LightGBM.

Minimize a function

import numpy as np
from abcopt import CanonicalABCSolver

def rastrigin(x):
    return 10 * len(x) + np.sum(x**2 - 10 * np.cos(2 * np.pi * x))

solver = CanonicalABCSolver(rastrigin, n_params=5, lb=[-5.12] * 5, ub=[5.12] * 5,
                            max_evals=20_000, random_state=0)
best_x, best_f, history = solver.solve()
import pandas as pd
import matplotlib.pyplot as plt

results = pd.DataFrame(search.cv_results_)          # one row per unique configuration
results.sort_values("rank_test_score").head()

evals, best = zip(*search.history_)                  # best CV score after each ABC cycle
plt.plot(evals, best); plt.xlabel("candidates evaluated"); plt.ylabel("best CV 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. Three kinds of bees share the work:

One cycle of the canonical ABC

PhaseWhat happensWhy
Employed beesEach source changes one random dimension relative to a random other source, v_ij = x_ij + φ·(x_ij − x_kj), φ ∈ [−1, 1], and keeps the better of old and new.Local search around every source
Onlooker beesSources are visited in turn; source i gets an extra try with probability p_i = 0.9 · fit_i / max(fit) + 0.1, until pop_size tries have been made.More effort on promising regions
Scout beeOnce per cycle, the source with the most failed tries is replaced by a random one if its counter has reached limit.Escapes local minima

Fitness is derived from cost as 1 / (1 + f) for f ≥ 0 and 1 + |f| otherwise. The best solution found so far is always kept.

Benchmark

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

Black-box functions (D = 10, mean over 10 seeds, lower is better):

FunctionABC @ 1kTPE @ 1kABC @ 10kTPE @ 2kRandom @ 10k
Sphere1.50.274e-110.2212.3
Rosenbrock1.3e388.50.511141.1e4
Rastrigin29.959.70.0947.371.2
Ackley12.85.580.00035.816.7

Convergence with a longer budget

  • 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 close to the global optimum on all four functions, while TPE stalls.
  • ABC's own overhead is negligible: about 0.02 s versus 24 s for TPE over 2,000 evaluations.
  • For hyperparameter tuning with about 50 candidates, the three methods end within noise of each other on test accuracy.

When to use which: TPE for very small budgets of expensive evaluations (for example deep-learning tuning); ABC when evaluations are cheap enough to afford thousands, when the landscape is rugged, or when you want a dependency-light scikit-learn drop-in.

API reference

CanonicalABCSolver

CanonicalABCSolver(objective_func, n_params, lb, ub,
                   pop_size=20, max_evals=1000, limit=None, random_state=None)
ParameterDescription
objective_funcCallable taking a 1-D np.ndarray and returning a scalar cost to minimize. NaN is treated as the worst cost.
n_paramsNumber of dimensions
lb, ubLower and upper bound of each dimension
pop_sizeNumber of food sources (SN), equal to the number of employed and of onlooker bees. The colony size in the reference code is 2 × pop_size.
max_evalsBudget of objective evaluations
limitA source is abandoned once its failed-trial counter reaches limit. Default: pop_size × n_params.
random_stateSeed or np.random.Generator

solve() returns (best_params, best_cost, history), where history lists (evaluations_used, best_cost_so_far) after initialization and after every cycle.

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, cache=True, verbose=False)

Search space (param_space), one entry per hyperparameter:

KindSpecification
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"]}

Parameters

ParameterDescription
cv, scoring, n_jobs, refitAs in GridSearchCV. Higher scores are better; use neg_* scorers for errors.
pop_size, max_evals, limit, random_statePassed to CanonicalABCSolver. max_evals counts every candidate ABC proposes.
error_scoreScore for candidates that fail to fit (a RuntimeWarning is issued); "raise" stops the search.
cacheReuse the score of a configuration ABC has already evaluated instead of cross-validating it again. Saves time; the search is unchanged.

fit(X, y=None, groups=None, **fit_params): groups is passed to group-aware splitters and fit_params (for example sample_weight) to the estimator.

Attributes after fit

AttributeContent
best_params_, best_score_, best_index_Best configuration, its mean CV score and its row in cv_results_
best_estimator_, refit_time_Best configuration refit on all data (refit=True)
cv_results_One row per unique configuration, in GridSearchCV format
results_Every candidate in proposal order with params, score and cached
history_(evaluations_used, best_score_so_far) after each cycle
n_evals_, n_unique_evals_, n_splits_Candidates evaluated, distinct configurations (cross-validation runs), CV folds

Methods: predict, predict_proba, predict_log_proba, decision_function, transform, score and classes_, whenever the underlying estimator supports them.

Notes and limitations

  • Budget semantics. As in the reference implementation, the budget is a number of objective evaluations, not of cycles. With ABCSearchCV, cached repeats still count towards max_evals.
  • Integer and categorical parameters are searched as continuous values and rounded. Categorical choices become equal-width intervals of one dimension, so neighbouring choices are treated as "close"; this works well for a handful of choices.
  • Sequential by design. Bees are evaluated one after another, exactly as in the reference algorithm; parallelism comes from n_jobs inside each cross-validation.
  • Minimization. CanonicalABCSolver minimizes; to maximize, return the negative value.

Examples

ExampleContent
BenchmarkABC vs Random Search vs Optuna TPE on test functions and two tuning tasks
Federated learning on MedMNISTABC tunes FedAvg's learning rate and momentum on non-IID PathMNIST (5 clients, Dirichlet α = 0.5); test accuracy rises from 78.05% to 85.28%. Run with abcopt 0.1.
AnimationRecords every phase of a solver run and renders the GIF above

Development

git clone https://github.com/yusufkorkmazyigit/abc-optimizer-lib.git
cd abc-optimizer-lib
pip install -e ".[test]"
pytest

The test suite includes the reference-equivalence test (tests/test_reference_equivalence.py) and runs on Python 3.9, 3.11 and 3.12 for every push. Release notes are in CHANGELOG.md. Issues and pull requests are welcome.

Citation

If abcopt helps your work, please cite the original algorithm and this software:

@techreport{karaboga2005abc,
  author      = {Karaboğa, Derviş},
  title       = {An Idea Based on Honey Bee Swarm for Numerical Optimization},
  institution = {Erciyes University, Engineering Faculty, Computer Engineering Department},
  number      = {TR06},
  year        = {2005}
}

@software{korkmazyigit_abcopt,
  author  = {Korkmazyiğit, Yusuf},
  title   = {abcopt: Canonical Artificial Bee Colony optimization for Python},
  url     = {https://github.com/yusufkorkmazyigit/abc-optimizer-lib},
  version = {0.2.2},
  year    = {2026}
}

License

MIT. See LICENSE. The reference port used in the tests is MIT licensed by its authors.

Developed by Yusuf Korkmazyiğit, Computer Engineering, Erciyes University.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
0.2.213 Oct 20264

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
yusufkorkmazyigit/abc-optimizer-lib

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 17:15 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:5d8cac896217d2a220f4612ab8aa5009cb4bcd5797d27691fde5de8aa6e3c924