예제 #1
0
class TestFinalStandardCovering(TestCase):
    def setUp(self):
        iteration = Iteration()
        self.crowding = Crowding()
        self.crowding.set_iteration(iteration)
        self.covering = FinalStandardCovering(self.crowding)
        self.covering.set_iteration(iteration)
        self.grammar = Grammar()
        self.first_right_symbol = Symbol('a', SymbolType.ST_TERMINAL)
        self.second_right_symbol = Symbol('b', SymbolType.ST_TERMINAL)
        self.left_symbol = Symbol('x', SymbolType.ST_TERMINAL)
        self.rule = Rule([
            self.left_symbol, self.first_right_symbol, self.second_right_symbol
        ])

    def test_add_new_rule(self):
        self.covering.crowding.add_rule = MagicMock()
        self.grammar.get_start_symbol = MagicMock(
            return_value=self.left_symbol)

        result = self.covering.add_new_rule(self.grammar,
                                            self.first_right_symbol,
                                            self.second_right_symbol)

        self.assertEqual(self.rule, result)
        self.covering.crowding.add_rule.assert_called_once_with(
            self.grammar, self.rule)
        self.grammar.get_start_symbol.assert_called_once_with()
예제 #2
0
class TestFinalCoveringPlus(TestCase):
    def setUp(self):
        iteration = Iteration()
        self.crowding = Crowding()
        self.crowding.set_iteration(iteration)
        self.covering = FinalCoveringPlus(self.crowding)
        self.covering.set_iteration(iteration)
        self.grammar = Grammar()
        self.first_right_symbol = Symbol('a', SymbolType.ST_TERMINAL)
        self.second_right_symbol = Symbol('b', SymbolType.ST_TERMINAL)
        self.left_symbol = Symbol('x', SymbolType.ST_TERMINAL)
        self.rule = sRule.from_symbols(0.5, self.left_symbol,
                                       self.first_right_symbol,
                                       self.second_right_symbol)

    def test_add_new_rule(self):
        self.crowding.add_rule = MagicMock()
        self.covering.produce_rule = MagicMock(return_value=self.rule)
        result = self.covering.add_new_rule(self.grammar, self.left_symbol,
                                            self.first_right_symbol)

        self.crowding.add_rule.assert_called_once()
        self.assertEqual(result, self.rule)

    def test_produce_rule(self):
        self.grammar.get_start_symbol = MagicMock(
            return_value=self.left_symbol)
        RandomUtils.get_random_probability = MagicMock(return_value=0.5)

        result = self.covering.produce_rule(self.grammar,
                                            self.first_right_symbol,
                                            self.second_right_symbol)
        expected = self.rule
        self.assertEqual(result, expected)
예제 #3
0
 def setUp(self):
     iteration = Iteration()
     self.crowding = Crowding()
     self.crowding.set_iteration(iteration)
     self.covering = FinalStandardCovering(self.crowding)
     self.covering.set_iteration(iteration)
     self.grammar = Grammar()
     self.first_right_symbol = Symbol('a', SymbolType.ST_TERMINAL)
     self.second_right_symbol = Symbol('b', SymbolType.ST_TERMINAL)
     self.left_symbol = Symbol('x', SymbolType.ST_TERMINAL)
     self.rule = Rule([
         self.left_symbol, self.first_right_symbol, self.second_right_symbol
     ])
예제 #4
0
 def setUp(self):
     iteration = Iteration()
     self.crowding = Crowding()
     self.crowding.set_iteration(iteration)
     self.covering = FinalCoveringPlus(self.crowding)
     self.covering.set_iteration(iteration)
     self.grammar = Grammar()
     self.first_right_symbol = Symbol('a', SymbolType.ST_TERMINAL)
     self.second_right_symbol = Symbol('b', SymbolType.ST_TERMINAL)
     self.left_symbol = Symbol('x', SymbolType.ST_TERMINAL)
     self.rule = sRule.from_symbols(0.5, self.left_symbol,
                                    self.first_right_symbol,
                                    self.second_right_symbol)
예제 #5
0
    def setUp(self):
        self.genetic_algorithm = GeneticAlgorithm(Crowding(), None)
        self.grammar = Grammar()

        self.symbol_v = Symbol('x', SymbolType.ST_TERMINAL)
        self.symbol_w = Symbol('w', SymbolType.ST_TERMINAL)
        self.symbol_x = Symbol('x', SymbolType.ST_TERMINAL)
        self.symbol_y = Symbol('y', SymbolType.ST_TERMINAL)
        self.symbol_z = Symbol('z', SymbolType.ST_TERMINAL)

        self.symbol_A = Symbol('A', SymbolType.ST_NON_TERMINAL)
        self.symbol_B = Symbol('B', SymbolType.ST_NON_TERMINAL)
        self.symbol_C = Symbol('C', SymbolType.ST_NON_TERMINAL)
        self.symbol_D = Symbol('D', SymbolType.ST_NON_TERMINAL)
        self.symbol_E = Symbol('E', SymbolType.ST_NON_TERMINAL)

        self.rule_1 = sRule.from_symbols(0.1, self.symbol_A, self.symbol_B,
                                         self.symbol_C)
        self.rule_2 = sRule.from_symbols(0.5, self.symbol_B, self.symbol_x,
                                         self.symbol_C)
        self.rule_3 = sRule.from_symbols(0.4, self.symbol_C, self.symbol_B,
                                         self.symbol_y)
        self.rule_4 = sRule.from_symbols(0.9, self.symbol_D, self.symbol_D,
                                         self.symbol_C)
        self.rule_5 = sRule.from_symbols(0.2, self.symbol_E, self.symbol_w)

        self.rule_1.fitness = 9
        self.rule_2.fitness = 12
        self.rule_3.fitness = 15
        self.rule_4.fitness = 6
        self.rule_5.fitness = 3

        self.rules = {
            self.rule_1, self.rule_2, self.rule_3, self.rule_4, self.rule_5
        }
        self.rules_list = [
            self.rule_1, self.rule_2, self.rule_3, self.rule_4, self.rule_5
        ]
        self.grammar.get_non_terminal_rules = MagicMock(
            return_value=self.rules)
        self.random_decision = RandomUtils.make_random_decision_with_probability
        self.random_non_terminal = RandomUtils.get_random_nonterminal_symbol_from