Skip to main content

Genetic programming library in JAX.

Project description

Kozax: 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.

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

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)
key, data_key, init_key = jr.split(key, 3)
x = jr.uniform(data_key, shape=(30,), minval=-5, maxval = 5)
y = -0.1*x**3 + 0.3*x**2 + 1.5*x

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. The fitness function should have a __call__ method that receives a candidate, the data and a function that is necessary to evaluate the tree.

class FitnessFunction:
    def __call__(self, candidate, data, tree_evaluator):
        _X, _Y = data
        pred = jax.vmap(tree_evaluator, in_axes=[None, 0])(candidate, _X)
        return jnp.mean(jnp.square(pred-_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

strategy = GeneticProgramming(num_generations, population_size, fitness_function)

population = strategy.initialize_population(init_key)

for g in range(num_generations):
    key, eval_key, sample_key = jr.split(key, 3)
    fitness, population = strategy.evaluate_population(population, (x[:,None], y[:,None]), eval_key)

    if g < (num_generations-1):
        population = strategy.evolve(population, fitness, sample_key)

best_fitnesses, best_solutions = strategy.get_statistics()
print(f"The best solution is {strategy.to_string(best_solutions[-1])} with a fitness of {best_fitnesses[-1]}")

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

Citation

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

@article{de2024discovering,
  title={Discovering Dynamic Symbolic Policies with Genetic Programming},
  author={de Vries, Sigur and Keemink, Sander and van Gerven, Marcel},
  journal={arXiv preprint arXiv:2406.02765},
  year={2024}
}

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.1.tar.gz (29.0 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.1-py3-none-any.whl (48.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kozax-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e8a4dd2b694b51906166309ae2b756ebb9cc96de968495e9417324612a61ca6f
MD5 5797e8303e121fd579f79764c30c75d6
BLAKE2b-256 587ca1f3fdf9203e01fe0d3a275777d344e667403e0b268e54cefd31ca55663b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for kozax-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 88c8550e3393fbad2b6fefb98148ab6a31e2a4acdad3bea6947f074f9eedfeb8
MD5 c8ea1d456a2ddd69b34658c02ebce0fd
BLAKE2b-256 18e013d4f55b5ffe5ae5e5077194de206aaa5784ebf3d69daa0933c6c57bdbac

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