Exemplo n.º 1
0
    def test_double(self):
        from dcgpy import expression_double as expression
        from dcgpy import kernel_set_double as kernel_set

        # Construction
        ex = expression(inputs=1,
                        outputs=1,
                        rows=1,
                        cols=6,
                        levels_back=6,
                        arity=2,
                        kernels=kernel_set(["sum", "mul", "div", "diff"])(),
                        n_eph=0,
                        seed=33)

        ex = expression(inputs=1,
                        outputs=1,
                        rows=1,
                        cols=6,
                        levels_back=6,
                        arity=2,
                        kernels=kernel_set(["sum", "mul", "div", "diff"])(),
                        n_eph=2,
                        seed=33)
        # Ephemeral value attributes tests
        self.assertEqual(ex.eph_val, [1, 2])
        self.assertEqual(ex.eph_symb, ["c1", "c2"])
        ex.eph_val = [-0.2, 0.3]
        self.assertEqual(ex.eph_val, [-0.2, 0.3])
        ex.eph_symb = ["d1", "d2"]
        self.assertEqual(ex.eph_symb, ["d1", "d2"])
Exemplo n.º 2
0
    def test_gdual_vdouble(self):
        from dcgpy import expression_gdual_vdouble as expression
        from dcgpy import kernel_set_gdual_vdouble as kernel_set
        from pyaudi import gdual_vdouble as gdual

        expression(inputs=1,
                   outputs=1,
                   rows=1,
                   cols=6,
                   levels_back=6,
                   arity=2,
                   kernels=kernel_set(["sum", "mul", "div", "diff"])(),
                   n_eph=0,
                   seed=20)
Exemplo n.º 3
0
    def test_gdual_vdouble(self):
        from dcgpy import expression_gdual_vdouble as expression
        from dcgpy import kernel_set_gdual_vdouble as kernel_set
        from pyaudi import gdual_vdouble as gdual

        ex = expression(1,1,1,6,6,2,kernel_set(["sum","mul", "div", "diff"])(), 32)
        self.assertEqual(ex([gdual([1, 2, -1, 2], "x", 2)]), [gdual([1, 1, 1, 1])])
Exemplo n.º 4
0
    def test_gdual_vdouble(self):
        from dcgpy import expression_gdual_vdouble as expression
        from dcgpy import kernel_set_gdual_vdouble as kernel_set
        from pyaudi import gdual_vdouble as gdual

        ex = expression(1, 1, 1, 6, 6, 2,
                        kernel_set(["sum", "mul", "div", "diff"])(), 32)
        self.assertEqual(ex([gdual([1, 2, -1, 2], "x", 2)]),
                         [gdual([1, 1, 1, 1])])
Exemplo n.º 5
0
    def test_double(self):
        from dcgpy import expression_double as expression
        from dcgpy import kernel_set_double as kernel_set

        ex = expression(1,1,1,6,6,2,kernel_set(["sum","mul", "div", "diff"])(), 32)
        self.assertEqual(ex([1.]), [1])
        self.assertEqual(ex([2.]), [1])
        self.assertEqual(ex([-1.]), [1])
        self.assertEqual(ex([-2.]), [1])
Exemplo n.º 6
0
    def test_double(self):
        from dcgpy import expression_double as expression
        from dcgpy import kernel_set_double as kernel_set

        ex = expression(1, 1, 1, 6, 6, 2,
                        kernel_set(["sum", "mul", "div", "diff"])(), 32)
        self.assertEqual(ex([1.]), [1])
        self.assertEqual(ex([2.]), [1])
        self.assertEqual(ex([-1.]), [1])
        self.assertEqual(ex([-2.]), [1])
Exemplo n.º 7
0
    def test_loss_double(self):
        from dcgpy import expression_double as expression
        from dcgpy import kernel_set_double as kernel_set
        import numpy as np

        ex = expression(1, 1, 1, 6, 6, 2,
                        kernel_set(["sum", "mul", "div", "diff"])(), 32)
        x = 1.
        loss_list = ex.loss([[x]], [ex([x])], "MSE")
        loss_array = ex.loss(np.array([[x]]), np.array([ex([x])]), "MSE")
        self.assertEqual(loss_list, loss_array)
Exemplo n.º 8
0
    def test_gdual_double(self):
        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

        ex = expression(1, 1, 1, 6, 6, 2,
                        kernel_set(["sum", "mul", "div", "diff"])(), 0, 20)
        self.assertEqual(ex([gdual(1, "x", 2)]), [gdual(0.)])
        self.assertEqual(ex([gdual(2, "x", 2)]), [gdual(0.)])
        self.assertEqual(ex([gdual(-1, "x", 2)]), [gdual(0.)])
        self.assertEqual(ex([gdual(-2, "x", 2)]), [gdual(0.)])
Exemplo n.º 9
0
    def test_double(self):
        from dcgpy import expression_double as expression
        from dcgpy import kernel_set_double as kernel_set

        # Construction
        ex = expression(1, 1, 1, 6, 6, 2,
                        kernel_set(["sum", "mul", "div", "diff"])(), 0, 33)
        self.assertEqual(ex([1.]), [0.5])
        self.assertEqual(ex([2.]), [1.])
        self.assertEqual(ex([-1.]), [-0.5])
        self.assertEqual(ex([-2.]), [-1.])
        ex = expression(1, 1, 1, 6, 6, 2,
                        kernel_set(["sum", "mul", "div", "diff"])(), 2, 33)
        # Ephemeral value attributes tests
        self.assertEqual(ex.eph_val, [1, 2])
        self.assertEqual(ex.eph_symb, ["c1", "c2"])
        ex.eph_val = [-0.2, 0.3]
        self.assertEqual(ex.eph_val, [-0.2, 0.3])
        ex.eph_symb = ["d1", "d2"]
        self.assertEqual(ex.eph_symb, ["d1", "d2"])
Exemplo n.º 10
0
    def test_loss_gdual_vdouble(self):
        from dcgpy import expression_gdual_vdouble as expression
        from dcgpy import kernel_set_gdual_vdouble as kernel_set
        from pyaudi import gdual_vdouble as gdual
        import numpy as np

        ex = expression(1, 1, 1, 6, 6, 2,
                        kernel_set(["sum", "mul", "div", "diff"])(), 0, 33)
        x = gdual([1., 2.], "x", 3)
        loss_list = ex.loss([[x]], [ex([x])], "MSE")
        loss_array = ex.loss(np.array([[x]]), np.array([ex([x])]), "MSE")
        self.assertEqual(loss_list, loss_array)
Exemplo n.º 11
0
    def test_loss_double(self):
        from dcgpy import expression_double as expression
        from dcgpy import kernel_set_double as kernel_set
        import numpy as np

        ex = expression(inputs=1,
                        outputs=1,
                        rows=1,
                        cols=6,
                        levels_back=6,
                        arity=2,
                        kernels=kernel_set(["sum", "mul", "div", "diff"])(),
                        n_eph=0,
                        seed=33)
        x = 1.
        loss_list = ex.loss([[x]], [ex([x])], "MSE")
        loss_array = ex.loss(np.array([[x]]), np.array([ex([x])]), "MSE")
        self.assertEqual(loss_list, loss_array)
Exemplo n.º 12
0
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(inputs = 1, 
                outputs = 1, 
                rows = 1, 
                cols = 6, 
                levels_back = 6, 
                arity = 2, 
                kernels = ks(), 
                n_eph = 0, 
                seed = 4232123212)


# 2 - Define the symbol set to be used in visualizing the expression
# (in our case, 1 input variable named "x") and visualize the expressionin_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
Exemplo n.º 13
0
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])