コード例 #1
0
ファイル: test_simplification.py プロジェクト: lacava/pyshgp
 def setUp(self):
     self.atom_gens = [
         lambda: np.random.random(),
     ]
     self.R = Spawner(self.atom_gens)
     self.gn = self.R.random_plush_genome(1)
     self.individual = Individual(self.gn)
コード例 #2
0
class TestSpawnMethods(unittest.TestCase):
    def setUp(self):
        self.atom_gens = [
            Instruction("noop", lambda s: s, [], 0),
            lambda: np.random.random()
        ]
        self.R = Spawner(self.atom_gens)

    def test_generate_close_count(self):
        c = self.R.generate_close_count()
        self.assertIsInstance(c, int)
        self.assertTrue(c <= 3)
        self.assertTrue(c >= 0)

    def test_atom_to_plush_gene(self):
        a = self.atom_gens[0]
        g = self.R.atom_to_plush_gene(a)
        self.assertIsInstance(g, pl.Gene)

    def test_random_plush_gene(self):
        self.assertIsInstance(self.R.random_plush_gene(), pl.Gene)

    def test_random_plush_genome_with_size(self):
        gn = self.R.random_plush_genome_with_size(5)
        self.assertEqual(len(gn), 5)
        self.assertIsInstance(gn[0], pl.Gene)

    def test_random_plush_genome(self):
        gn = self.R.random_plush_genome(5)
        self.assertTrue(len(gn) <= 5)
        self.assertTrue(len(gn) > 0)
        self.assertIsInstance(gn[0], pl.Gene)
コード例 #3
0
 def setUp(self):
     self.atom_gens = [lambda: np.random.random()]
     self.R = Spawner(self.atom_gens)
     self.pop = p.Population()
     self.pop.append(p.Individual(self.R.random_plush_genome(5)))
     self.pop.append(p.Individual(self.R.random_plush_genome(5)))
     self.pop.append(p.Individual(self.R.random_plush_genome(5)))
コード例 #4
0
ファイル: test_simplification.py プロジェクト: lacava/pyshgp
class TestSimplificationFunctions(unittest.TestCase):
    def setUp(self):
        self.atom_gens = [
            lambda: np.random.random(),
        ]
        self.R = Spawner(self.atom_gens)
        self.gn = self.R.random_plush_genome(1)
        self.individual = Individual(self.gn)

    def test_silent_n_random_genes(self):
        silent_n_random_genes(self.gn, 1)
        self.assertTrue(self.gn[0].is_silent)

    def test_noop_n_random_genes(self):
        noop_n_random_genes(self.gn, 1)
        self.assertTrue(self.gn[0].atom.name == '_exec_noop')

    def test_simplify_once(self):
        self.gn = simplify_once(self.gn)
        a = self.gn[0].is_silent
        b = (hasattr(self.gn[0].atom, 'name')
             and self.gn[0].atom.name == '_exec_noop')
        self.assertTrue(a or b)

    def test_simplify_by_dataset(self):
        simplify_by_dataset(self.individual, np.array([[0, 0], [0, 0]]),
                            np.array([0, 0]), 'regression')

    def test_simplify_by_function(self):
        simplify_by_function(self.individual, lambda s: [0, 0, 0])
コード例 #5
0
class TestEvaluateFunctions(unittest.TestCase):
    def setUp(self):
        self.atom_gens = [
            lambda: np.random.random(),
        ]
        self.R = Spawner(self.atom_gens)
        self.gn = self.R.random_plush_genome(1)
        self.individual = Individual(self.gn)

    def test_evaluate_with_function(self):
        def ef(program):
            return [1, 2, 3]

        i = evaluate_with_function(self.individual, ef)
        self.assertTrue(hasattr(i, 'error_vector'))

    def test_evaluate_for_regression(self):
        X = np.array([[1, 2], [3, 4]])
        y = np.array([3, 7])
        i = evaluate_for_regression(self.individual, X, y)
        self.assertTrue(hasattr(i, 'total_error'))

    def evaluate_for_classification(self):
        X = np.array([[1, 2], [3, 4]])
        y = np.array([3, 7])
        i = evaluate_for_classification(self.individual, X, y)
        self.assertTrue(hasattr(i, 'total_error'))
コード例 #6
0
class TestAlternationMethods(unittest.TestCase):

    def setUp(self):
        self.R = Spawner(atom_gens)
        self.i1 = p.Individual(self.R.random_plush_genome(5))
        self.i2 = p.Individual(self.R.random_plush_genome(5))

    def test_produce(self):
        al = v.Alternation(rate=0.5)
        child = al.produce((self.i1, self.i2))
        self.assertFalse(hasattr(child, 'total_error'))
コード例 #7
0
class TestPerturbIntegerMutationMethods(unittest.TestCase):

    def setUp(self):
        self.R = Spawner(atom_gens)
        self.i1 = p.Individual(self.R.random_plush_genome(15))

    def test_produce(self):
        pim = v.PerturbIntegerMutation(rate=0.9)
        child = pim.produce([self.i1], self.R)
        self.assertEqual(len(child.genome), len(self.i1.genome))
        self.assertFalse(hasattr(child, 'total_error'))
コード例 #8
0
class TestVariationOperatorPipelineMethods(unittest.TestCase):

    def setUp(self):
        self.R = Spawner(atom_gens)
        self.i1 = p.Individual(self.R.random_plush_genome(5))
        self.i2 = p.Individual(self.R.random_plush_genome(5))

    def test_produce(self):
        al = v.VariationOperatorPipeline(
            (v.Alternation(rate=0.5), v.TweakStringMutation(rate=0.7))
        )
        child = al.produce((self.i1, self.i2), self.R)
        self.assertFalse(hasattr(child, 'total_error'))
コード例 #9
0
class TestIndividualMethods(unittest.TestCase):
    def setUp(self):
        self.atom_gens = [
            lambda: np.random.random(),
        ]
        self.R = Spawner(self.atom_gens)
        self.i = p.Individual(self.R.random_plush_genome_with_size(4))

    def test_run_program(self):
        inputs = [1, 2, 3]
        result = self.i.run_program(inputs, ['_integer'])
        self.assertEqual(len(result), 1)
        self.assertIsNone(result[0])
コード例 #10
0
 def setUp(self):
     self.atom_gens = [
         Instruction("noop", lambda s: s, [], 0),
         lambda: np.random.random()
     ]
     self.R = Spawner(self.atom_gens)
コード例 #11
0
 def setUp(self):
     self.R = Spawner(atom_gens)
     self.i1 = p.Individual(self.R.random_plush_genome(5))
     self.i2 = p.Individual(self.R.random_plush_genome(5))
コード例 #12
0
 def setUp(self):
     self.atom_gens = [
         lambda: np.random.random(),
     ]
     self.R = Spawner(self.atom_gens)
     self.i = p.Individual(self.R.random_plush_genome_with_size(4))
コード例 #13
0
class TestPopulationMethods(unittest.TestCase):
    def setUp(self):
        self.atom_gens = [lambda: np.random.random()]
        self.R = Spawner(self.atom_gens)
        self.pop = p.Population()
        self.pop.append(p.Individual(self.R.random_plush_genome(5)))
        self.pop.append(p.Individual(self.R.random_plush_genome(5)))
        self.pop.append(p.Individual(self.R.random_plush_genome(5)))

    def test_pop_evaluate_by_function(self):
        def ef(program):
            return [1, 2, 3]

        self.pop.evaluate_by_function(ef)
        self.assertEqual(self.pop[0].error_vector, [1, 2, 3])
        self.assertEqual(self.pop[2].error_vector, [1, 2, 3])

    def test_pop_evaluate_by_dataset(self):
        X = np.array([[1, 2], [3, 4]])
        y = np.array([3, 7])
        self.pop.evaluate_by_dataset(X, y, 'regression')
        self.assertTrue(hasattr(self.pop[0], 'error_vector'))
        self.assertTrue(hasattr(self.pop[1], 'error_vector'))
        self.assertTrue(hasattr(self.pop[2], 'error_vector'))
        self.assertIsNotNone(self.pop[0].error_vector)
        self.assertIsNotNone(self.pop[1].error_vector)
        self.assertIsNotNone(self.pop[2].error_vector)

    def test_lexicase(self):
        def ef(program):
            return [1, 2, 3]

        self.pop.evaluate_by_function(ef)
        parent = self.pop.select(method='lexicase')
        self.assertEqual(parent.error_vector, [1, 2, 3])

    def test_eplexicase(self):
        def ef(program):
            return [1, 2, 3]

        self.pop.evaluate_by_function(ef)
        parent = self.pop.select(method='epsilon_lexicase')
        self.assertEqual(parent.error_vector, [1, 2, 3])

    def test_tourn(self):
        def ef(program):
            return [1, 2, 3]

        self.pop.evaluate_by_function(ef)
        parent = self.pop.select(method='tournament')
        self.assertEqual(parent.error_vector, [1, 2, 3])

    def test_lowest_error(self):
        def ef(program):
            return [np.random.random(), np.random.random()]

        self.pop.evaluate_by_function(ef)
        err = self.pop.lowest_error()
        self.assertTrue(err <= self.pop[0].total_error)
        self.assertTrue(err <= self.pop[1].total_error)
        self.assertTrue(err <= self.pop[2].total_error)

    def test_average_error(self):
        def ef(program):
            return [np.random.random(), np.random.random()]

        self.pop.evaluate_by_function(ef)
        err = self.pop.average_error()
        self.assertTrue(err < 2)
        self.assertTrue(err > 0)

    def test_unique(self):
        def ef(program):
            return [np.random.random(), np.random.random()]

        self.pop.evaluate_by_function(ef)
        unique = self.pop.unique()
        self.assertEqual(unique, 3)

    def test_best_program(self):
        def ef(program):
            return [np.random.random(), np.random.random()]

        self.pop.evaluate_by_function(ef)
        best = self.pop.best_program()
        self.assertIsInstance(best, list)

    def best_program_error_vector(self):
        def ef(program):
            return [np.random.random(), np.random.random()]

        self.pop.evaluate_by_function(ef)
        best_err = self.pop.best_program_error_vector()
        self.assertIsInstance(best_err, list)
        self.assertEqual(len(best_err), 2)
コード例 #14
0
 def setUp(self):
     self.balenced = ["_open", 1, "_close", "_open", 2, "_close"]
     self.unbal = ["_open", "_open", 1, "_close", "_close", 3, "_close"]
     R = Spawner([lambda: 0])
     self.gn = R.random_plush_genome_with_size(5)