Cartesian Genetic Programming for Julia
Switch branches/tags
Nothing to show
Clone or download
Latest commit d82cc4b Oct 25, 2018


Cartesian Genetic Programming for julia.

This implementation of CGP includes various extensions, including RCGP, MTCGP, genetic crossover, and more.

Development is still ongoing, so be warned that major changes might happen. Stable resources in other languages can be found on the CGP site.


Note currently only known to work with Julia v0.6

Mac specific install of Julia v0.6

  • download it
wget -P ~/Downloads
sudo hdiutil attach ~/Downloads/julia-0.6.4-mac64.dmg
  • double click the newly mounted drive and drag Julia-0.6 to the Applications directory.

  • add julia to your path so you can use the commandline (change .zshrc to .bashrc if using bash)

echo export "PATH=/Applications/\$PATH" >> ~/.zshrc


From the julia REPL call:

Pkg.add.(["Logging", "PaddedViews", "Distributions", "YAML", "ArgParse", "TestImages", "Colors", "QuartzImageIO"])


CGP.jl comes with exhaustive tests which can offer examples of detailed usage of the different genetic operators and CGP extensions. To run all tests, use

julia run_tests.jl

Note you may currently need to run the tests a few times to get through them successfully.


Four different examples are found in the experiments folder:


XOR evolves a function to compute the xor function over nsamples number of randomly generated bits strings of nbits length. This can be run simply with

julia experiments/xor.jl

but various options can be set using command line arguments:

julia experiments/xor.jl --nbits 5 --nsamples 30

Data tasks

Classification and regression can be performed with the data.jl script. Two example datasets have been provided in the data directory: Wisconsin Breast Cancer and Abalone. The first two lines specify the number of inputs and outputs, and the second two lines specify the number of training samples and test samples. Data are read in the order provided in the files, and must be normalized between -1.0 and 1.0 before use in data.jl. To change any of the data preparation steps, modify the read_data function in data.jl.

Classification is the default problem and uses accuracy as the evolutionary fitness. To run classification on the Breast Cancer set, use:

julia experiments/data.jl --data data/cancer.dt --log cancer.log

Regression can be specified as an option on the command line:

julia experiments/data.jl --data data/abalone.dt --log abalone.log --fitness regression

OpenAI gym environments

gym.jl uses PyCall.jl to evolve CGP programs for the OpenAI gym environments. pybullet environments are also included as an alternative to the mujoco environments used by OpenAI. Please see the PyCall.jl documentation for setting up a python environment with the necessary packages (gym and pybullet).

To evolve a program for the MountainCarContinuous-v0 environment, run:

julia experiments/gym.jl --total_evals 200 --seed 1


atari.jl uses ArcadeLearningEnvironment.jl to evolve programs which play Atari games. This is a more complex example, as it uses Mixed-Type CGP by default (program inputs are RGB Arrays, and outputs are scalar actions.) ROMs are available from Atari-py. Once ROMs have been configured, a CGP agent can be evolved using:

julia experiments/atari.jl --id boxing

This requires a long runtime. Parallelizing evaluations is a currently planned improvement: see this issue.


Configuration is handled by src/config.jl. Configurable options are read in through YAML files (an example being cfg/test.yaml) and then are accessed in the CGP.Config module. YAML files are loaded into configuration by running


Redefinition of a configuration value will overwrite the previous value, so be careful about ordering of configuration files. The example files also show usage of the CGP.Config.add_arg_settings! function, which allows for all configuration values to be passed via the command line.

Function set configuration

All node functions are contained in the CGP.Config.functions array and are populated in the configuration files. The syntax for function definitions allows for mixed type CGP by defining a function for both scalar and vector node inputs. The function ordering is:

  • (x, y)
  • (x, y)
  • (x, y)
  • (x, y)

As shorthand, a single function can be specified for all four input types. If two function definitions are provided, the first will apply to the two first two input types where x is a scalar, and the second definition will apply to the second two input types. cfg/atari.yaml demonstrates many different function definitions.


A comprehensive review of genetic operators for CGP is underway and is intended to be the reference for this library, but until then a link to this repository would be appreciated in any works which use it.