Quick start examples

C++

After following the Installation guide you will be able to compile and run your first C++ dCGP program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <dcgp/expression.hpp>
#include <dcgp/kernel_set.hpp>

using namespace dcgp;

int main() {
    // 1- Instantiate a random expression using the 4 basic arithmetic operations
    kernel_set<gdual_d> ks({"sum", "diff", "div", "mul"});
    expression<gdual_d> ex(1, 1, 1, 6, 6, 2, ks(), 123212321);

    // 2 - Define the symbol set (in our case, 1 input variable named "x") and visualize the expression
    std::vector<std::string> in_sym({"x"});
    stream(std::cout, "Expression: ", ex(in_sym)[0], "\n");

    // 3 - Define a gdual number of value 1.2 and truncation order 2
    gdual_d x(1.2, "x", 2);

    // 4 - Compute the output of the expression and its second derivative in x = 1.2 and visualize
    stream(std::cout, "Expression in x=1.2: ", ex({x})[0], "\n");
    stream(std::cout, "Second derivative: ", ex({x})[0].get_derivative({2}), "\n");

    // 5 - Mutate the expression with 2 random mutations of active genes and visualize
    ex.mutate_active(2);
    stream(std::cout, "Mutated expression: ", ex(in_sym)[0], "\n");
}

Place it into a getting_started.cpp text file and compile it with:

g++ -std=c++11 getting_started.cpp -lmpfr -lgmp -pthread

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from dcgpy import expression_gdual_double as expression
from dcgpy import kernel_set_gdual_double as kernel_set
from pyaudi import gdual_double as gdual

# 1- Instantiate a random expression using the 4 basic arithmetic operations
ks = kernel_set(["sum", "diff", "div", "mul"])
ex = expression(1, 1, 1, 6, 6, 2, ks(), 4232123212)

# 2 - Define the symbol set (in our case, 1 input variable named "x") and print the expression
in_sym = ["x"]
print("Expression:", ex(in_sym)[0])

# 3 - Print the simplified expression
print("Simplified expression:", ex.simplify(in_sym))

# 4 - Visualize the dCGP graph
ex.visualize(in_sym)

# 5 - Define a gdual number of value 1.2 and truncation order 2
x = gdual(1.2, "x", 2)

# 6 - Compute the output of the expression and its second derivative in x = 1.2 and print
print("Expression in x=1.2:", ex([x])[0])
print("Second derivative:", ex([x])[0].get_derivative([2]))

# 5 - Mutate the expression with 2 random mutations of active genes and print
ex.mutate_active(2)
print("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 do enjoy dcgpy the most.

Notebooks

Follow the links below to visualize Jupyter notebooks on the use of dCGP: