Exemplo n.º 1
0
    def setUp(self):
        """Setup Tests"""
        N = 4
        M = 1
        eugene.Config.VAR = {
            'x' : np.linspace(0, float(M) * np.pi, 1),
            'y' :  np.linspace(0, 2.0 * float(M) * np.pi, 4)
        }
        eugene.Config.TRUTH = (eugene.Config.VAR['x'] ** 2.0) + (eugene.Config.VAR['y'] ** 2.0)

        self.ind1 = Individual(Tree(
            Node('n_add',
                Node('n_pow',
                    Node('x'),
                    Node(2.0)
                ),
                Node('n_pow',
                    Node('y'),
                    Node(2.0)
                )
            )
        ))
        self.ind2 = Individual(Tree(
            Node('n_add',
                Node('x'),
                Node('n_mul',
                    Node(10),
                    Node('n_abs',
                        Node(-4)
                    )
                )
            )
        ))
Exemplo n.º 2
0
class IndividualTests(unittest.TestCase):
    """
    Test the Individual Module.
    """

    def setUp(self):
        """Setup Tests"""
        N = 4
        M = 1
        eugene.Config.VAR = {
            'x' : np.linspace(0, float(M) * np.pi, 1),
            'y' :  np.linspace(0, 2.0 * float(M) * np.pi, 4)
        }
        eugene.Config.TRUTH = (eugene.Config.VAR['x'] ** 2.0) + (eugene.Config.VAR['y'] ** 2.0)

        self.ind1 = Individual(Tree(
            Node('n_add',
                Node('n_pow',
                    Node('x'),
                    Node(2.0)
                ),
                Node('n_pow',
                    Node('y'),
                    Node(2.0)
                )
            )
        ))
        self.ind2 = Individual(Tree(
            Node('n_add',
                Node('x'),
                Node('n_mul',
                    Node(10),
                    Node('n_abs',
                        Node(-4)
                    )
                )
            )
        ))

    def test_23_individual_size(self):
        """Check Individual size"""
        self.assertEqual(self.ind1.size, 7)

    def test_24_gene_expression(self):
        """Evaluate Gene Expression"""
        error, time, complexity = self.ind1.compute_gene_expression(rmse, eugene.Config.TRUTH)
        self.assertEqual(error, 0.0)
        self.assertEqual(complexity, 17.0)

        # def error_and_complexity(gene_expression, scale):
        #     """user fitness function, weighted combination of error and complexity"""
        #     print gene_expression
        #     weights = np.array([0.95, 0.025, 0.025])
        #     scaled_gene_expression = 1.0 / (gene_expression / scale)
        #
        #     return np.dot(scaled_gene_expression, weights)

    def test_25_mutate(self):
        """Test individual mutation"""
        ind1_orig = copy.deepcopy(self.ind1)
        self.assertEqual(repr(ind1_orig), repr(self.ind1))
        for _ in xrange(50):
            self.ind1.mutate()
        self.assertNotEqual(repr(ind1_orig), repr(self.ind1))

    def test_26_mutate(self):
        """Test individual mutation with pruning"""
        ind1_orig = copy.deepcopy(self.ind1)
        self.assertEqual(repr(ind1_orig), repr(self.ind1))
        for _ in xrange(20):
            self.ind1.mutate(pruning=True)
        self.assertNotEqual(repr(ind1_orig), repr(self.ind1))

    def test_27_crossover(self):
        """Test individual crossover"""
        self.assertNotEqual(repr(self.ind1), repr(self.ind2))
        child1, child2 = self.ind1.crossover(self.ind2)
        self.assertNotEqual(repr(child1), repr(self.ind1))
        self.assertNotEqual(repr(child2), repr(self.ind2))
        self.assertNotEqual(repr(child1), repr(child2))

    def test_28_individual_string(self):
        """Check individual display string"""
        correct_string = '[0:0] n_add (2) - {7|17}\n    |-[1:1] n_pow (1) - {3|5}\n    |     |-[2:2] x (0) - {1|1}\n    |     \\-[2:3] 2.0 (0) - {1|1}\n    \\-[1:4] n_pow (1) - {3|5}\n          |-[2:5] y (0) - {1|1}\n          \\-[2:6] 2.0 (0) - {1|1}'
        tree_string = self.ind1.display()

        self.assertEqual(tree_string, correct_string)