コード例 #1
0
def x_o(calls, index):
    vehicle = calls[index]
    if len(vehicle) > 3:
        call = random.choice(vehicle[:-1])
        vehicle.remove(call)
        vehicle.insert(random.randrange(0, len(vehicle[:-1])), call)
    return list_to_solution(calls)
コード例 #2
0
def three_exchange(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    rand_ub = x.vehicles
    rand = random.randrange(0, rand_ub)
    three_exchange_list = calls[rand]
    if len(three_exchange_list) < 6:
        return solution
    else:
        rand1 = random.choice(three_exchange_list)
        rand2 = random.choice(three_exchange_list)
        rand3 = random.choice(three_exchange_list)

        if rand1 == rand2 or rand2 == rand3 or rand3 == rand1 or rand1 == 0 or rand2 == 0 or rand3 == 0:
            return solution
        rand1_indexes = get_index_positions(three_exchange_list, rand1)
        rand2_indexes = get_index_positions(three_exchange_list, rand2)
        rand3_indexes = get_index_positions(three_exchange_list, rand3)

        three_exchange_list[rand1_indexes[0]], three_exchange_list[rand2_indexes[0]], three_exchange_list[
            rand3_indexes[0]] = three_exchange_list[rand2_indexes[0]], three_exchange_list[rand3_indexes[0]], \
                                three_exchange_list[rand1_indexes[0]]

        three_exchange_list[rand1_indexes[1]], three_exchange_list[rand2_indexes[1]], three_exchange_list[
            rand3_indexes[1]] = three_exchange_list[rand2_indexes[1]], three_exchange_list[rand3_indexes[1]], \
                                three_exchange_list[rand1_indexes[1]]

        calls[rand] = three_exchange_list
    return list_to_solution(calls)
コード例 #3
0
def move_vehicle_to_dummy(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    dummy = x.vehicles
    vehicle = random.randrange(0, dummy - 1)
    del calls[vehicle][-1]
    calls[dummy].extend(calls[vehicle])
    calls[vehicle] = [0]
    return list_to_solution(calls)
コード例 #4
0
ファイル: shuffle.py プロジェクト: chrhein/metaheuristics
def shuffle(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    for vehicle in calls[:-1]:
        if len(vehicle) == 1:
            continue
        del vehicle[-1]
        random.shuffle(vehicle)
        vehicle.append(0)
    return list_to_solution(calls)
コード例 #5
0
def change_route(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    vehicle = random.randrange(0, x.vehicles - 1)
    c = calls[vehicle]
    del c[-1]
    len_c = len(c)
    if not c:
        return solution
    c.insert(random.randrange(0, len_c), c.pop(random.randrange(0, len_c)))
    c.append(0)
    return list_to_solution(calls)
コード例 #6
0
def take_from_dummy_place_first_suitable(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    dummy_calls = calls[x.vehicles]
    if not dummy_calls:
        return solution
    call = random.choice(dummy_calls)
    dummy_removed = [i for i in dummy_calls if i != call]
    calls[x.vehicles] = dummy_removed
    vehicle = random.randrange(0, x.vehicles - 1)
    calls[vehicle].insert(random.randrange(0, len(calls[vehicle])), call)
    calls[vehicle].insert(random.randrange(0, len(calls[vehicle])), call)
    return list_to_solution(calls)
コード例 #7
0
def one_reinsert_from_dummy(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    dummy = calls[len(calls)-1]
    if not dummy:
        return solution
    chosen_call = random.choice(dummy)
    calls[len(calls)-1] = [i for i in dummy if i != chosen_call]
    vehicle = calls[random.randrange(0, len(calls)-1)]
    if len(vehicle) < 1:
        vehicle.insert(0, chosen_call)
        vehicle.insert(0, chosen_call)
    else:
        vehicle.insert(random.randrange(0, len(vehicle)), chosen_call)
        vehicle.insert(random.randrange(0, len(vehicle)), chosen_call)
    return list_to_solution(calls)
コード例 #8
0
def fill_vehicle(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    dummy_calls = calls[x.vehicles]
    if not dummy_calls:
        return solution
    call = random.choice(dummy_calls)
    for vehicle in x.vehicles_dict:
        if not calls[vehicle] and call in x.vehicles_dict.get(
                vehicle).valid_calls:
            calls[vehicle].insert(0, call)
            calls[vehicle].insert(0, call)
            dummy_removed = [i for i in dummy_calls if i != call]
            calls[x.vehicles] = dummy_removed
            break
    return list_to_solution(calls)
コード例 #9
0
def one_reinsert(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    rand_ub = x.vehicles
    rand = random.randrange(0, rand_ub)
    one_reinsert_list = calls[rand]
    if len(one_reinsert_list) <= 1:
        return solution
    else:
        rand1 = random.choice(one_reinsert_list)
        if rand1 == 0:
            return solution
        one_reinsert_list = [i for i in one_reinsert_list if i != rand1]
        calls[rand] = one_reinsert_list
    rand = random.randrange(1, x.vehicles)
    calls[rand].insert(0, rand1)
    calls[rand].insert(0, rand1)
    return list_to_solution(calls)
コード例 #10
0
def most_expensive_one_reinsert_from_dummy(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    if not calls[x.vehicles]:
        return solution
    cost_no_transport = get_most_expensive_calls(solution)
    cnt_list = list(cost_no_transport.keys())
    dummy = calls[len(calls)-1]
    chosen_call = cnt_list[0]
    if chosen_call not in dummy:
        return solution
    calls[len(calls)-1] = [i for i in dummy if i != chosen_call]
    vehicle = calls[random.randrange(0, len(calls)-1)]
    if len(vehicle) < 1:
        vehicle.insert(0, chosen_call)
        vehicle.insert(0, chosen_call)
    else:
        vehicle.insert(random.randrange(0, len(vehicle)), chosen_call)
        vehicle.insert(random.randrange(0, len(vehicle)), chosen_call)
    return list_to_solution(calls)
コード例 #11
0
def move_to_dummy(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    vehicle = random.randrange(0, x.vehicles)
    tampered_calls = calls[vehicle]
    if not tampered_calls:
        return solution
    call = random.choice(tampered_calls)
    # print("Chosen call:", call)
    if call == 0 or not call:
        return solution
    tampered_calls.remove(call)
    tampered_calls.remove(call)
    calls[vehicle] = tampered_calls
    vehicle = x.vehicles
    tampered_calls = calls[vehicle]
    tampered_calls.insert(0, call)
    tampered_calls.insert(0, call)
    calls[vehicle] = tampered_calls
    return list_to_solution(calls)
コード例 #12
0
def one_reinsert_most_expensive(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    v = calls[random.randrange(0, x.vehicles)]
    if not v:
        return solution
    cost_no_transport = get_most_expensive_calls(solution)
    cnt_list = list(cost_no_transport.keys())
    if not cnt_list or len(cnt_list) <= 2:
        return solution
    z = int(len(cnt_list) / 2)
    most_expensive_call = cnt_list[random.randrange(0, int(z))]
    if most_expensive_call in v:
        v = [i for i in v if i != most_expensive_call]
        for i in range(1, x.vehicles + 1):
            vehicle = x.vehicles_dict[i]
            if most_expensive_call in vehicle.valid_calls:
                calls[i].insert(random.randrange(0, len(calls[i])), most_expensive_call)
                calls[i].insert(random.randrange(0, len(calls[i])), most_expensive_call)
                break
    return list_to_solution(calls)
コード例 #13
0
def move_to_next_valid_vehicle(solution):
    calls = get_routes_as_list_w_zeroes(solution)
    vehicle = random.randrange(0, x.vehicles)
    tampered_calls = calls[vehicle]
    if not tampered_calls:
        return solution
    call = random.choice(tampered_calls)
    if call == 0 or not call:
        return solution
    tampered_calls = [i for i in tampered_calls if i != call]
    calls[vehicle] = tampered_calls
    vehicle = vehicle % x.vehicles
    del calls[vehicle][-1]
    tampered_calls = calls[vehicle]
    if len(tampered_calls) < 1:
        tampered_calls.insert(0, call)
        tampered_calls.insert(0, call)
    else:
        tampered_calls.insert(random.randrange(0, len(tampered_calls)), call)
        tampered_calls.insert(random.randrange(0, len(tampered_calls)), call)
    calls[vehicle].append(0)
    calls[vehicle] = tampered_calls
    return list_to_solution(calls)