Beispiel #1
0
def ch_move_tail(_solution: List):  # usun najdalszy przejazd jaki wystepuje w rozwiazaniu
    Raportowanie.used_function('ch_move_tail')
    new_solution = deepcopy(_solution)
    max_p2p_for_truck = []  # maksymalny przejazd dla kazdej smieciarki
    max_p2p_for_truck_value = []  # i jego wartość
    for route in _solution:
        # print("route: ", route)
        if len(route) > 2:
            p2p_values = []
            for i in range( len(route) - 1):
                p2p_values.append(bin_locations[route[i]][route[i + 1]])
            # print(p2p_values)

            od = route[p2p_values.index(max(p2p_values))]
            do = route[p2p_values.index(max(p2p_values)) + 1]
            max_p2p_for_truck.append([od, do])
            max_p2p_for_truck_value.append(bin_locations[od][do])

        elif len(route) == 2:
            max_p2p_for_truck.append([route[0], route[1]])
            max_p2p_for_truck_value.append(bin_locations[route[0]][route[1]])

    index_of_max = max_p2p_for_truck_value.index(max(max_p2p_for_truck_value))  # zwroci index najdluzszego przejazdu
    [od, do] = max_p2p_for_truck[index_of_max]
    # print("od , do", od, do)

    # przenies koniec trasy do najblizszej smieciarki
    for i in range(len(new_solution)):
        if od in new_solution[i]:
            # print("solution[",i,"]",new_solution[i])
            bin_start_index = new_solution[i].index(do)

            tail = new_solution[i][bin_start_index:]
            # head = new_solution[i][bin_start_index:]
            # print("tail", tail)
            first_point = do
            end_point = tail[-1]
            # znajdz najblizszy punkt do first_point

            distances_from_first_point = bin_locations[do]
            distances_from_end_point = bin_locations[end_point]

            bins_from_first_point = range(len(bin_locations[do]))
            bins_from_end_point = range(len(bin_locations[end_point]))

            # slownik klucz - nr kosza wartosc odleglosc od niego
            bins_distances_from_first_point = dict(zip(bins_from_first_point, distances_from_first_point))
            bins_distances_from_end_point = dict(zip(bins_from_end_point,distances_from_end_point ))

            new_bins_distances_from_first_point = {}
            new_bins_distances_from_end_point = {}

            for bin_nr in bins_distances_from_first_point:
                if bin_nr not in new_solution[i]:
                    new_bins_distances_from_first_point[bin_nr] = bins_distances_from_first_point[bin_nr]

            for bin_nr in bins_distances_from_end_point:
                if bin_nr not in new_solution[i]:
                    new_bins_distances_from_end_point[bin_nr] = bins_distances_from_end_point[bin_nr]

            del new_bins_distances_from_first_point[0]
            del new_bins_distances_from_end_point[0]

            # znajdz min odleglosc
            min_from_end_point = min(new_bins_distances_from_first_point.items(), key=lambda x: x[1])
            min_from_first_point = min(new_bins_distances_from_end_point.items(), key=lambda x: x[1])

            if min_from_end_point[1] < min_from_first_point[1]:
                closest_bin = min_from_end_point[0]
            else:
                closest_bin = min_from_first_point[0]

            new_solution[i] = new_solution[i][:bin_start_index]

            bin_pos = [(index, row.index(closest_bin)) for index, row in enumerate(new_solution) if closest_bin in row][0] # bin_pos ->[ktora smieciarka, ktory   kosz z kolei]
            new_truck = bin_pos[0]
            new_solution[new_truck] += deepcopy(tail)
    # sprawdz zabronienia
    # if check_ban_t1(random_bin):
    if check_ban_t2(new_solution) and check_ban_t3(new_solution):
        return new_solution

    if aspiration(_solution, new_solution):  # jesli aspiracja zadziala
        return new_solution
    return new_solution
Beispiel #2
0
            function_name = 'ch_returns_poprawa'


        # print(x, " -> ", count_cost(x))

        cost_x0 = count_cost(x0)
        # print("nowy koszt: ",cost_x0)
        # ShowSolutions.show_routes(x0, bin_point_list)
        cost_x_opt = count_cost(x_opt)

        if cost_x0 < cost_x_opt:
            # print("xopt",x_opt, " --> ", cost_x_opt)
            # print("x0", x0, " --> ", cost_x0)

            x_opt = deepcopy(x0)
            Raportowanie.used_function(Raportowanie.klucz_slowny_lista_koszt_od_iteracji, [i, cost_x_opt])
            Raportowanie.used_function(function_name, i)
            solution_change = True
            iterations_without_change = 0
        else:
            iterations_without_change = iterations_without_change + 1

        '''skroc o 1 kadencje'''
        for type_T in TABU:
            for single_tabu in TABU[type_T]:
                if len(single_tabu) == 0 or single_tabu[-1] == 1:  # jesli kadencja = 0 to usuń zabronienie
                    TABU[type_T].remove(single_tabu)
                else:
                    single_tabu[-1] = single_tabu[-1] - 1

        '''Dodaj nowe elementy do listy TABU'''