Beispiel #1
0
 def mutate(self):
     equal_list = []
     for i in range(len(self.ids)):
         id = self.ids[i]
         mutated_id = id.mutate()
         if mutated_id not in self.ids:
             adjacency_matrix = AdjacencyMatrix.parse(mutated_id)
             if adjacency_matrix.is_connected():
                 score = DegreeAndDiameterCalculator.calculate(
                     adjacency_matrix)
                 if self.is_score_better_than_self(score):
                     new_tree = GeneticTree([mutated_id],
                                            degree=score[0],
                                            diameter=score[1],
                                            score1=score[2] + score[3])
                     self.child = new_tree
                     self.iterations_without_change = 0
                     return new_tree
                 elif self.is_score_equal_than_self(score):
                     equal_list.append(mutated_id)
                     print("  Id " + str(id) + " (" + str(i) + "/" +
                           str(len(self.ids)) +
                           ") mutated to equal score id " + str(mutated_id))
     if len(equal_list) > 0:
         self.ids += equal_list
         self.iterations_without_change = 0
     else:
         self.iterations_without_change += 1
     return self
def recursively_create_ids(i: int, symbols_to_check: list, symbols: list):
    global num, results, test
    for symbol in symbols_to_check:
        if Symbol.check(symbols[-1] if len(symbols) > 0 else None, symbol):
            symbols.append(symbol)
            if i + 1 == wanted_length:
                id = Id(symbols)
                if id.check() is None:
                    # print(str(id))
                    adj_matrix = AdjacencyMatrix.parse(id)
                    if adj_matrix.is_connected():
                        score = DegreeAndDiameterCalculator.calculate(
                            adj_matrix)
                        score = score[0] + score[1]
                    else:
                        score = ((number_of_nodes - 1) * 2) + (
                            number_of_nodes - adj_matrix.
                            get_number_of_elements_from_biggest_component())
                    for substring_len in range(1, len(id) + 1):
                        for j in range(len(id) - substring_len + 1):
                            chars = str(Id(symbols[j:j + substring_len]))
                            if chars in results:
                                results[chars][0] += 1
                                results[chars][1] += score
                            else:
                                results[chars] = [1, score]
                            # print(" " + str(Id(chars)))
                num += 1
            else:
                recursively_create_ids(
                    i + 1,
                    Symbol.symbols() if i + 2 < wanted_length else
                    Symbol.ending_symbols(), symbols)
            symbols.pop()
 def random_connected_id(print_tries: bool = False):
     from classes.AdjacencyMatrix import AdjacencyMatrix
     adjacency_matrix = AdjacencyMatrix.create_unconnected_matrix()
     while not adjacency_matrix.is_connected():
         new_id = Id.random()
         adjacency_matrix = AdjacencyMatrix.parse(new_id)
         if print_tries:
             print("Trying initial id: " + str(new_id))
     return new_id
Beispiel #4
0
 def __init__(self,
              ids: list,
              child=None,
              degree: int = -1,
              diameter: int = -1,
              score1: int = -1):
     self.ids = ids
     self.child = child
     if degree == -1:
         score = DegreeAndDiameterCalculator.calculate(
             AdjacencyMatrix.parse(ids[0]))
         degree = score[0]
         diameter = score[1]
         score1 = score[2] + score[3]
     self.degree = degree
     self.diameter = diameter
     self.score1 = score1
     self.iterations_without_change = 0