Skip to main content

BERT Mutation for EC-KitY Genetic Programming

eckity-bert-gp provides the BERT mutation operator for tree-based genetic programming in EC-KitY.

The operator is described in “BERT Mutation: Deep Transformer Model for Masked Uniform Mutation in Genetic Programming”, Mathematics 2025, 13(5), 779 (paper). It masks selected GP-tree nodes and uses a compact BERT masked-language model to sample replacements that preserve the required node arity.

Installation

pip install eckity-bert-gp

Public API

from eckity_bert_gp import BertGPEckity, BertMutation

BertMutation owns and trains the BERT policy. BertGPEckity adapts that policy to EC-KitY's genetic-programming operator interface.

Usage

The BERT model needs the function names, terminal names, fitness callback, and mappings back to the EC-KitY functions:

import numpy as np
from eckity.base.untyped_functions import f_add, f_div, f_mul, f_sub
from eckity_bert_gp import BertGPEckity, BertMutation

function_set = [f_add, f_sub, f_mul, f_div]
terminal_set = ["x", "y", "z"]
function_mappings = {function.__name__: function for function in function_set}

bert_model = BertMutation(
    operators_list=np.array(list(function_mappings)),
    constant_names=terminal_set,
    get_fitness_func=evaluator.evaluate_individual,
    context_size=256,
    word_embedding_dim=20,
    n_layers=1,
    n_attention_heads=1,
    function_mappings=function_mappings,
)

bert_mutation = BertGPEckity(
    bert_model=bert_model,
    probability=1.0,
    node_probability=0.1,
)

Add bert_mutation to the EC-KitY subpopulation's operators_sequence.

  • get_fitness_func accepts an EC-KitY GP tree and returns its fitness.
  • Fitness direction is read automatically from the EC-KitY tree individual.
  • function_mappings maps each function name used by BERT back to the callable stored in GP trees.
  • terminals_mappings optionally maps each name in constant_names to the value stored in GP trees. Mapped values must be unique and hashable; the default preserves the supplied names.
  • allow_constant_terminals=True enables the reserved const token and requires every mutated tree to define an erc_range. Set it to False for trees without ephemeral random constants.
  • batch_size must be at least 2 because policy rewards are normalized within each training batch.
  • probability controls whether the EC-KitY mutation operator runs.
  • node_probability controls the probability of masking each tree node.
  • context_size must be large enough for the longest tree representation expected during evolution.

The model is initialized locally from BertConfig; installing or constructing the operator does not download pretrained model weights.

Compatibility

  • Python 3.10 or newer
  • EC-KitY 0.4.x
  • NumPy 2.0.2 or newer
  • SciPy 1.13.0 or newer
  • PyTorch 2.7.1 or newer
  • Transformers 4.50.0 or newer

These bounds are compatible with eckity-dnc and eckity-bert-ga; none of the three packages directly depends on another operator package.

Repository experiment

The repository includes the paper's experiment runner and datasets. They are development resources and are not included in the wheel.

Install the development dependencies and run the symbolic-regression example:

python -m pip install -e ".[dev]"
python runner.py

Additional benchmark data is stored under data/, and Artificial Ant maps are stored under ant_opt/.

Development

With uv:

uv sync --extra dev --resolution lowest-direct
uv run pytest
uv build

Release preparation and manual PyPI upload commands are documented in RELEASING.md.

License

This project is licensed under the BSD 3-Clause License. See LICENSE.

Metadata

Release files for eckity-bert-gp 0.1.3

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

Source distribution (sdist)

Source distribution for eckity-bert-gp 0.1.3
File Size Uploaded
eckity_bert_gp-0.1.3.tar.gz 15.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for eckity-bert-gp 0.1.3
File Interpreter ABI Platform
eckity_bert_gp-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 26.1 kB

Release files / eckity_bert_gp-0.1.3.tar.gz

Download URL eckity_bert_gp-0.1.3.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a589238b1da5da124e2a3fc05696d12032c7637f2fbc0b2b7570e96fd7f2a530
BLAKE2b-256 checksum
How to use checksums
6f59b5f7551bad8b35a9e108845ca0b2bdb31f06fd81edd8069a5876175563b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.20

Release files / eckity_bert_gp-0.1.3-py3-none-any.whl

Download URL eckity_bert_gp-0.1.3-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d2f8018f6b7bd1fcbe5b137658590b8577d4f216d23e11b08b42b97ee8e6c3d
BLAKE2b-256 checksum
How to use checksums
9d42e40a93c08295db6538c171f35c1dfbc36da3c5c10a0f6ac6e1e56085805e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.20

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.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