예제 #1
0
    def test_make_random_decision_with_probability(self):
        random.random = MagicMock(return_value=0.5)
        result = RandomUtils.make_random_decision_with_probability(0.7)
        self.assertTrue(result)

        random.random = MagicMock(return_value=0.9)
        result = RandomUtils.make_random_decision_with_probability(0.7)
        self.assertFalse(result)
예제 #2
0
 def crossover(self, parents: Tuple[Rule, Rule]) -> Tuple[Rule, Rule, 'CrossoverReport']:
     parents_copy = pickle.loads(pickle.dumps(parents, -1))
     report = CrossoverReport()
     if RandomUtils.make_random_decision_with_probability(self.crossing_probability):
         report = self.cross_rules(parents_copy)
         self.adjust_rules_after_crossover(parents_copy)
     return parents_copy[0], parents_copy[1], report
예제 #3
0
    def test_get_random_nonterminal_symbol_from(self):
        grammar = Grammar()
        symbol_1 = Symbol('a', SymbolType.ST_TERMINAL)
        symbol_2 = Symbol('b', SymbolType.ST_TERMINAL)

        grammar.nonTerminalSymbols = [symbol_1, symbol_2]
        result = RandomUtils.get_random_nonterminal_symbol_from(grammar)
        self.assertIsNotNone(result)
        self.assertEqual(2, len(grammar.nonTerminalSymbols))

        grammar.nonTerminalSymbols = []
        result = RandomUtils.get_random_nonterminal_symbol_from(grammar)
        self.assertIsNone(result)

        grammar.nonTerminalSymbols = [symbol_1, symbol_2]
        random.choice = MagicMock(return_value=symbol_1)
        result = RandomUtils.get_random_nonterminal_symbol_from(grammar)
        self.assertEqual(result, symbol_1)
예제 #4
0
 def cross_rules(self, pair: Tuple[Rule, Rule]) -> 'CrossoverReport':
     report = CrossoverReport()
     if RandomUtils.make_random_decision_with_probability(0.5):
         pair[0].right1, pair[1].right1 = pair[1].right1, pair[0].right1
         report.first_right_crossed = True
     else:
         pair[0].right2, pair[1].right2 = pair[1].right2, pair[0].right2
         report.second_right_crossed = True
     return report
예제 #5
0
def __apply_aggressive_and_final_covering(cyk_values: CykValues, i: int,
                                          j: int):
    """
    Performs aggressive or final covering on the given cell of the cyk table
    :param i:
    :param j:
    :return:
    """
    new_rule = None
    valid_combinations_of_indexes = []
    for m in range(i):
        tmp_symbols_1 = __get_cell_symbols(cyk_values, m, j)
        tmp_symbols_2 = __get_cell_symbols(cyk_values, i - m - 1, j + m + 1)
        if len(tmp_symbols_1) > 0 and len(tmp_symbols_2) > 0:
            valid_combinations_of_indexes.append(m)
    if len(valid_combinations_of_indexes) > 0:
        random = randint(0, len(valid_combinations_of_indexes) - 1)
        symbols_1 = __get_cell_symbols(cyk_values,
                                       valid_combinations_of_indexes[random],
                                       j)
        symbols_2 = __get_cell_symbols(
            cyk_values, i - valid_combinations_of_indexes[random] - 1,
            j + valid_combinations_of_indexes[random] + 1)
        index_1 = randint(0, len(symbols_1) - 1)
        index_2 = randint(0, len(symbols_2) - 1)
        if i is not len(cyk_values.sequence) - 1:
            if RandomUtils.make_random_decision_with_probability(
                    float(
                        cyk_values.settings.get_value(
                            'covering', 'aggressive_covering_probability'))):
                covering = cyk_values.aggressive_covering
                new_rule = covering.add_new_rule(cyk_values.grammar,
                                                 symbols_1[index_1],
                                                 symbols_2[index_2])
        elif cyk_values.settings.get_value(
                'covering', 'is_full_covering_allowed') == "True":
            covering = cyk_values.final_covering
            new_rule = covering.add_new_rule(cyk_values.grammar,
                                             symbols_1[index_1],
                                             symbols_2[index_2])
        if new_rule is not None:
            new_rule.tmp_used = True
            new_cell_rule = sCellRule(
                new_rule, Coordinates(valid_combinations_of_indexes[random],
                                      j),
                Coordinates(i - valid_combinations_of_indexes[random] - 1,
                            j + valid_combinations_of_indexes[random] + 1))
            cyk_values.rules_table[i][j].append(new_cell_rule)
            __calculate_cell(
                cyk_values, cyk_values.probability_array[
                    valid_combinations_of_indexes[random]][j][
                        new_rule.right1.index], cyk_values.probability_array[
                            i - valid_combinations_of_indexes[random] -
                            1][j + valid_combinations_of_indexes[random] +
                               1][new_rule.right2.index],
                cyk_values.probability_array[i][j][new_rule.left.index],
                new_rule)
예제 #6
0
파일: SGCSCyk.py 프로젝트: ounold/pyGCS
 def __apply_aggressive_and_final_covering(self, i: int, j: int):
     """
     Performs aggressive or final covering on the given cell of the cyk table
     :param i:
     :param j:
     :return:
     """
     new_rule = None
     valid_combinations_of_indexes = []
     for m in self.iteration_generator(i):
         tmp_symbols_1 = self.__get_cell_symbols(m, j)
         tmp_symbols_2 = self.__get_cell_symbols(i - m - 1, j + m + 1)
         if len(tmp_symbols_1) > 0 and len(tmp_symbols_2) > 0:
             valid_combinations_of_indexes.append(m)
     if len(valid_combinations_of_indexes) > 0:
         random = randint(0, len(valid_combinations_of_indexes) - 1)
         symbols_1 = self.__get_cell_symbols(
             valid_combinations_of_indexes[random], j)
         symbols_2 = self.__get_cell_symbols(
             i - valid_combinations_of_indexes[random] - 1,
             j + valid_combinations_of_indexes[random] + 1)
         index_1 = randint(0, len(symbols_1) - 1)
         index_2 = randint(0, len(symbols_2) - 1)
         # print("Need rule: {}". format(symbols_1[index_1], symbols_2[index_2]))
         if i is not len(self.sequence) - 1:
             if RandomUtils.make_random_decision_with_probability(
                     float(
                         self.__settings.get_value(
                             'covering',
                             'aggressive_covering_probability'))):
                 covering = self.aggressive_covering
                 new_rule = covering.add_new_rule(self.grammar,
                                                  symbols_1[index_1],
                                                  symbols_2[index_2])
         elif self.__settings.get_value(
                 'covering', 'is_full_covering_allowed') == "True":
             covering = self.final_covering
             new_rule = covering.add_new_rule(self.grammar,
                                              symbols_1[index_1],
                                              symbols_2[index_2])
         if new_rule is not None:
             self.__generated_rules_count += 1
             new_rule.tmp_used = True
             new_cell_rule = sCellRule(
                 new_rule,
                 Coordinates(valid_combinations_of_indexes[random], j),
                 Coordinates(i - valid_combinations_of_indexes[random] - 1,
                             j + valid_combinations_of_indexes[random] + 1))
             self.rules_table[i][j].append(new_cell_rule)
             self.__Stochastic.calculate_cell(
                 self.mode, self.default_value, self.probability_array,
                 Coordinates(valid_combinations_of_indexes[random], j),
                 Coordinates(i - valid_combinations_of_indexes[random] - 1,
                             j + valid_combinations_of_indexes[random] + 1),
                 Coordinates(i, j), new_rule)
예제 #7
0
 def run(self, grammar: Grammar) -> None:
     self.save_grammar_state(grammar)
     ga_prob = self.get_ga_probability(grammar)
     if RandomUtils.make_random_decision_with_probability(ga_prob):
         # print("GA launched")
         new_rules_number = self.calculate_number_of_rules_to_add(grammar)
         for i in range(int(new_rules_number / 2)):
             self.add_new_pair(grammar)
     else:
         # print("GA not launched")
         pass
예제 #8
0
 def test_get_random_probability(self):
     result = RandomUtils.get_random_probability()
     self.assertGreaterEqual(result, 0)
     self.assertLessEqual(result, 1)
예제 #9
0
 def mutate_symbol(self, symbol: Symbol, grammar: Grammar) -> Symbol:
     if RandomUtils.make_random_decision_with_probability(self.mutation_probability):
         return RandomUtils.get_random_nonterminal_symbol_from(grammar) or symbol
     else:
         return symbol
예제 #10
0
 def invert(self, rule: Rule) -> bool:
     if RandomUtils.make_random_decision_with_probability(self.inversion_probability):
         self.invert_rule(rule)
         self.adjust_rules_after_inverse(rule)
         return True
     return False