Quick start examples

C++

After following the Installation guide you will be able to compile and run your first C++ dCGP program, put the following text into a getting_started.cpp file:

 1#include <iostream>
 2#include <dcgp/expression.hpp>
 3#include <dcgp/kernel_set.hpp>
 4#include <audi/gdual.hpp>
 5
 6using namespace dcgp;
 7
 8int main() {
 9    // 1- Instantiate a random expression using the 4 basic arithmetic operations
10    kernel_set<audi::gdual_d> basic_set({"sum", "diff", "div", "mul"});
11    // Number of independent variables (input dimensionality of the expression)
12    unsigned n_input = 1u;
13    // Number of outputs (output dimensionality of the expression)
14    unsigned n_output = 1u;
15    // Number of rows of the cartesian representation of the expression
16    unsigned rows = 1u;
17    // Number of columns of the cartesian representation of the expression
18    unsigned cols = 6u;
19    // Number of levels-back (controlling the minimum number of allowed operations in the formula,
20    // if uncertain set it to cols + 1)
21    unsigned lb = 7u;
22    // Arity of the various kernels (the fundamental blocks that build up the expression)
23    unsigned arity = 2u;
24    // Maximum number of costants in the formula (a.k.a. ephemeral constants)
25    unsigned n_eph = 0u;
26    // Random seed used to generate the initial formula and all of its subsequent mutations
27    unsigned seed = 4232123212u;
28
29    expression<audi::gdual_d> ex(n_input, n_output, rows, cols, lb, arity, basic_set(), n_eph, seed);
30
31    // 2 - Define the symbol set to be used in visualizing the expression
32    // (in our case, 1 input variable named "x") and visualize the expression
33    std::vector<std::string> in_sym({"x"});
34    audi::print("Expression: ", ex(in_sym)[0], "\n");
35
36    // 3 - Define a gdual number of value 1.2 and truncation order 2
37    audi::gdual_d x(1.2, "x", 2);
38
39    // 4 - Compute the output of the expression and its second derivative in x = 1.2 and visualize
40    audi::print("\nExpression in x=1.2: \n", ex({x})[0], "\n");
41    audi::print("\nSecond derivative: ", ex({x})[0].get_derivative({2}), "\n");
42
43    // 5 - Mutate the expression with 2 random mutations of active genes and visualize
44    ex.mutate_active(2);
45    audi::print("\nMutated expression: ", ex(in_sym)[0], "\n");
46}

To compile it, create also, in the same folder, a CmakeLists.txt file with the following content:

project(getting_started)

cmake_minimum_required(VERSION 3.8)

find_package(dcgp REQUIRED)

add_executable(getting_started getting_started.cpp)
target_link_libraries(getting_started Dcgp::dcgp)

set_property(TARGET getting_started PROPERTY CXX_STANDARD 17)
set_property(TARGET getting_started PROPERTY CXX_STANDARD_REQUIRED YES)
set_property(TARGET getting_started PROPERTY CXX_EXTENSIONS NO)

then:

$ mkdir build
$ cd build
$ cmake ../
$ make
$ ./getting_started

Python

If you have successfully compiled and installed dcgpy following the Installation guide you will be able to test its use by running the following script:

 1from dcgpy import expression_gdual_double as expression
 2from dcgpy import kernel_set_gdual_double as kernel_set
 3from pyaudi import gdual_double as gdual
 4
 5# 1- Instantiate a random expression using the 4 basic arithmetic operations
 6ks = kernel_set(["sum", "diff", "div", "mul"])
 7ex = expression(inputs = 1, 
 8                outputs = 1, 
 9                rows = 1, 
10                cols = 6, 
11                levels_back = 6, 
12                arity = 2, 
13                kernels = ks(), 
14                n_eph = 0, 
15                seed = 4232123212)
16
17
18# 2 - Define the symbol set to be used in visualizing the expression
19# (in our case, 1 input variable named "x") and visualize the expression
20in_sym = ["x"]
21print("Expression:", ex(in_sym)[0])
22
23# 3 - Print the simplified expression
24print("Simplified expression:", ex.simplify(in_sym))
25
26# 4 - Visualize the dCGP graph
27ex.visualize(in_sym)
28
29# 5 - Define a gdual number of value 1.2 and truncation order 2
30x = gdual(1.2, "x", 2)
31
32# 6 - Compute the output of the expression and its second derivative in x = 1.2 and print
33print("Expression in x=1.2:", ex([x])[0])
34print("Second derivative:", ex([x])[0].get_derivative([2]))
35
36# 5 - Mutate the expression with 2 random mutations of active genes and print
37ex.mutate_active(2)
38print("Mutated expression:", ex(in_sym)[0])

Place it into a getting_started.py text file and run it with:

$ python getting_started.py

We recommend the use of Jupyter or Ipython to enjoy dcgpy the most.