Skip to main content

Kozax: Flexible and Scalable Genetic Programming in JAX

Kozax introduces a general framework for evolving computer programs with genetic programming in JAX. With JAX, the computer programs can be vectorized and evaluated on parallel on CPU and GPU. Furthermore, just-in-time compilation provides massive speedups for evolving offspring. Check out the paper introducing Kozax.

Features

Kozax allows the user to:

  • define custom operators
  • define custom fitness functions
  • use trees flexibly, ranging from symbolic regression to reinforcement learning
  • evolve multiple trees simultaneously, even with different inputs
  • numerically optimise constants in the computer programs

How to use

You can install Kozax via pip with

pip install kozax

Below is a short demo showing how you can use kozax. First we generate data:

import jax
import jax.numpy as jnp
import jax.random as jr

key = jr.PRNGKey(0) #Initialize key
data_key, gp_key = jr.split(key) #Split key for data and genetic programming
x = jr.uniform(data_key, shape=(30,), minval=-5, maxval = 5) #Inputs
y = -0.1*x**3 + 0.3*x**2 + 1.5*x #Targets

Now we have to define a fitness function. This allows for much freedom, because you can use the computer program anyway you want to during evaluation.

from kozax.fitness_functions.base_fitness_function import BaseFitnessFunction

class FitnessFunction(BaseFitnessFunction):
  """
    The fitness function inherits the class BaseFitnessFunction and should implement the __call__ function, with the candidate, data and tree_evaluator as inputs. The tree_evaluator is used to compute the value of the candidate for each input. jax.vmap is used to vectorize the evaluation of the candidate over the inputs. The candidate's predictions are used to compute the fitness value with the mean squared error.
  """
  def __call__(self, candidate, data, tree_evaluator):
    X, Y = data
    predictions = jax.vmap(tree_evaluator, in_axes=[None, 0])(candidate, X)
    return jnp.mean(jnp.square(predictions-Y))

fitness_function = FitnessFunction()

Now we will use genetic programming to recover the equation from the data. This requires defining the hyperparameters, initializing the population and the general loop of evaluating and evolving the population.

from kozax.genetic_programming import GeneticProgramming

#Define hyperparameters
population_size = 500
num_generations = 100

#Initialize genetic programming strategy
strategy = GeneticProgramming(num_generations, population_size, fitness_function)

#Fit the strategy on the data. With verbose, we can print the intermediate solutions.
strategy.fit(gp_key, (x, y), verbose = True)

There are additional examples on how to use kozax on more complex problems.

Example Notebook Script
Symbolic regression of a dynamical system Notebook Script
Control policy optimization in Gymnax environment Notebook Script
Control policy optimization with dynamic memory Notebook Script
Optimization of a loss function to train a neural network Notebook Script

Citation

If you make use of this code in your research paper, please cite:

@article{de2025kozax,
  title={Kozax: Flexible and Scalable Genetic Programming in JAX},
  author={de Vries, Sigur and Keemink, Sander W and van Gerven, Marcel AJ},
  journal={arXiv preprint arXiv:2502.03047},
  year={2025}
}

Metadata

Release files for kozax 0.1.5

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

Source distribution (sdist)

Source distribution for kozax 0.1.5
File Size Uploaded Provenance Actions
kozax-0.1.5.tar.gz 37.1 kB Without provenance Details for kozax-0.1.5.tar.gz

Built distribution (wheel)

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

Total release size: 92.3 kB

Release files / kozax-0.1.5.tar.gz

Download URL kozax-0.1.5.tar.gz
Size 37.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e1a338672bc8609f80e7fdb786a3fbb83f54c028c7dde07c88b58fe3af15fe91
BLAKE2b-256 checksum
How to use checksums
3f7a3f2ab287540df6557ad9aa0f5db6cc3656e5c47b72c045cfad57e3843787
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.2

Release files / kozax-0.1.5-py3-none-any.whl

Download URL kozax-0.1.5-py3-none-any.whl
Size 55.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c83bbe16939ab275c10f675f42148c1f9e67e80849e66008d5ac27b2ae39885
BLAKE2b-256 checksum
How to use checksums
4e5a5e3ee46a45a1b1a4e700f4d49da8e1ac32789f9efacd5733a90e0ab7d57d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.2

Security for kozax 0.1.5

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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