Ejemplo n.º 1
0
 def do_algorithm(self, distance_matrix):
     vertex_list = np.array([np.random.randint(len(distance_matrix))])
     shortest_cycle = Cycle(vertex_list, Cycle.calculate_cycle_length(distance_matrix,
                                                                      vertex_list))
     while len(vertex_list) / len(distance_matrix) < GreedyCycleStrategy.VERTEX_RATIO:
         shortest_cycle = GreedyCycleStrategy.__find_shortest_cycle(distance_matrix, vertex_list)
         vertex_list = shortest_cycle.cycle
     return shortest_cycle
Ejemplo n.º 2
0
    def __find_nearest_neighbor(distance_matrix, vertex_list):
        remaining_vertex_list = np.delete(np.arange(len(distance_matrix)), vertex_list)
        last_vertex = vertex_list[-1]
        distance_list = np.copy(distance_matrix[last_vertex])
        remaining_distance_list = np.copy(distance_list[remaining_vertex_list])
        min_length = np.min(remaining_distance_list)
        nearest_neighbor = np.where(distance_list == min_length)[0][0]
        new_vertex_list = np.append(vertex_list, nearest_neighbor)

        return Cycle(new_vertex_list, Cycle.calculate_cycle_length(distance_matrix,
                                                                   new_vertex_list))
Ejemplo n.º 3
0
    def __find_shortest_cycle(distance_matrix, vertex_list):
        remaining_vertex_list = np.delete(np.arange(len(distance_matrix)), vertex_list)

        cycle_list = np.array([])
        for vertex in remaining_vertex_list:
            new_vertex_list = np.append(vertex_list, vertex)
            cycle_list = np.append(cycle_list,
                                   Cycle(new_vertex_list,
                                         Cycle.calculate_cycle_length(distance_matrix,
                                                                      new_vertex_list)))

        return min(cycle_list, key=lambda cycle: cycle.length)
Ejemplo n.º 4
0
Archivo: main.py Proyecto: paqu/AEM
def main():
    distance_matrix = np.array([[0, 1, 2, 3, 4], [1, 0, 2, 3, 4],
                                [2, 2, 0, 3, 4], [3, 3, 3, 0, 4],
                                [4, 4, 4, 4, 0]])
    vertex_list = np.array([0, 1, 2])
    length = Cycle.calculate_cycle_length(distance_matrix, vertex_list)
    strategy = RegretStrategy()
    result = strategy.do_algorithm(distance_matrix)
    print(length)
    print(result)
Ejemplo n.º 5
0
    def __find_max_regret_cycle(distance_matrix, vertex_list):
        regrets_list = np.array([])
        remaining_vertex_list = np.delete(np.arange(len(distance_matrix)),
                                          vertex_list)
        for vertex in remaining_vertex_list:
            regret = RegretStrategy.__calculate_regret(
                distance_matrix, vertex,
                np.delete(remaining_vertex_list,
                          np.where(remaining_vertex_list == vertex)))
            regrets_list = np.append(regrets_list, {
                'vertex': vertex,
                'regret': regret
            })

        max_regret = max(regrets_list,
                         key=lambda regret_dict: regret_dict['regret'])
        new_vertex_list = np.append(vertex_list, max_regret['vertex'])
        return Cycle(
            new_vertex_list,
            Cycle.calculate_cycle_length(distance_matrix, new_vertex_list))
Ejemplo n.º 6
0
    def do_algorithm(self, distance_matrix):
        vert_list = np.random.randint(0, 100, 10)
        vert_list = np.append(vert_list, vert_list[0])

        return Cycle(vert_list, random.randint(0, 1000))