Skip to main content

Genetic programming library in JAX.

Project description

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}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kozax-0.0.8.tar.gz (30.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kozax-0.0.8-py3-none-any.whl (47.1 kB view details)

Uploaded Python 3

File details

Details for the file kozax-0.0.8.tar.gz.

File metadata

  • Download URL: kozax-0.0.8.tar.gz
  • Upload date:
  • Size: 30.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for kozax-0.0.8.tar.gz
Algorithm Hash digest
SHA256 fc6ba66fa6a2bd8d178d8c5c3d521c7716d318683235ae9298a263fa1a33e7ca
MD5 4b99c7c5802b0f9b60df74408f17b878
BLAKE2b-256 defa218e7ea1245f35e0d451014b00dd2505893bdd1fe143b5b95a94e5eb61e1

See more details on using hashes here.

File details

Details for the file kozax-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: kozax-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 47.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for kozax-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 2530d1b03d162e92de6032a7f7fd32953d901b8e9ebf4325f42a2fb1b0118a5a
MD5 bd42262209a0ce1db8bc7a54931872b9
BLAKE2b-256 e8a786ca2fc4c20e625b83d5da24cd1fe81f45ce7073477d5beef49cd819177d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page