Beispiel #1
0
def simulated_annealing_new(init_solution):
    best_solution = init_solution
    incumbent = init_solution
    t0 = 38
    t = t0
    a = 0.998
    p1 = 0.25
    p2 = 0.5
    clear_br()
    for i in range(1, 10000):
        progress_bar(i)
        rand = random.uniform(0, 1)
        if rand < p1:
            new_solution = one_reinsert_most_expensive_from_dummy(incumbent)
        elif rand < p1 + p2:
            new_solution = obo.fill_vehicles(incumbent)
        else:
            new_solution = best_route(incumbent)
        delta_e = f(new_solution) - f(incumbent)
        rand_ii = random.uniform(0, 1)
        p = math.e * (-delta_e / t)
        if check_solution(new_solution) and delta_e < 0:
            incumbent = new_solution
            if f(incumbent) < f(best_solution):
                best_solution = incumbent
        elif check_solution(new_solution) and rand_ii < p:
            incumbent = new_solution
        t = a * t
    return best_solution
def simulated_annealing(init_solution):
    best_solution = init_solution
    incumbent = init_solution
    t0 = 100
    t = t0
    a = 0.998
    p1, p2 = 0.33, 0.33
    for i in range(1, 10000):
        rand = random.uniform(0, 1)
        if rand < p1:
            new_solution = two_exchange(incumbent)
        elif rand < p1 + p2:
            new_solution = three_exchange(incumbent)
        else:
            new_solution = one_reinsert(incumbent)
        delta_e = f(new_solution)-f(incumbent)
        rand_ii = random.uniform(0, 1)
        p = math.e * (-delta_e/t)
        if check_solution(new_solution) and delta_e < 0:
            incumbent = new_solution
            if f(incumbent) < f(best_solution):
                best_solution = incumbent
        elif check_solution(new_solution) and rand_ii < p:
            incumbent = new_solution
        t = a * t
    return best_solution
Beispiel #3
0
def brute_force_random_generator():
    while True:
        sol = random_solution()
        print(sol)
        # print("\r %s" % sol, end='')
        if check_solution(sol) and no_zeroes(sol):
            print("Valid:", check_solution(sol))
            break
Beispiel #4
0
def swap(solution):
    calls = get_calls_including_zeroes(solution)
    vehicle = random.randrange(1, x.vehicles)
    route = calls[vehicle]
    it = 0
    while not route or len(route) <= 3:
        vehicle = random.randrange(1, x.vehicles)
        route = calls[vehicle]
        if it == 25:
            return solution
        else:
            it += 1
    new_route = route
    new_calls = calls
    new_calls[vehicle] = new_route
    it = 0
    while f(calls_to_solution(new_calls)) > f(solution) and not check_solution(
            calls_to_solution(new_calls)):
        rand1 = random.randrange(0, len(new_route))
        rand2 = random.randrange(0, len(new_route))
        if rand1 == 0 or rand2 == 0 or (rand1 == rand2):
            continue
        new_route[rand1], new_route[rand2] = new_route[rand2], new_route[rand1]
        new_calls[vehicle] = new_route
        if it == 100:
            return solution
        else:
            it += 1
    c = calls_to_solution(new_calls)
    return c
Beispiel #5
0
def best_objective(solution):
    routes = list(itertools.permutations(solution))
    best_solution = solution
    for route in routes:
        if f(route) < f(best_solution) and check_solution(route):
            best_solution = route
    return best_solution
def random_search(init_solution):
    best_solution = init_solution
    for _ in range(1, 10000):
        current = random_solution()
        if check_solution(current) and f(current) < f(best_solution):
            best_solution = current

    return best_solution
Beispiel #7
0
def fast_reinsert(solution):
    rand = random.randrange(1, x.calls)
    new_s = [i for i in solution if i != rand]
    new_s.insert(random.randrange(0, len(solution)), rand)
    new_s.insert(random.randrange(0, len(solution)), rand)
    if check_solution(new_s):
        return new_s
    return solution
Beispiel #8
0
def local_search(init_solution):
    best_solution = init_solution
    p1, p2 = 0.33, 0.33
    for i in range(1, 10000):
        rand = random.uniform(0, 1)
        if rand < p1:
            current = two_exchange(best_solution)
        elif rand < p1 + p2:
            current = three_exchange(best_solution)
        else:
            current = one_reinsert(best_solution)
        if check_solution(current) and f(current) < f(best_solution):
            best_solution = current
    return best_solution
Beispiel #9
0
def tabu_shuffle(solution):
    calls = get_calls_including_zeroes(solution)
    rand_vehicle = random.randrange(1, x.vehicles)

    if len(calls[rand_vehicle]) < 2:
        return solution

    current = solution
    new_sol = swingers(solution)
    # print("New sol:", new_sol)
    if f(new_sol) < f(current):
        current = new_sol
    elif check_solution(new_sol) and random.uniform(0, 1) < 0.33:
        current = new_sol
    return current
Beispiel #10
0
def try_for_best(solution):
    if solution in tested_solutions:
        return solution
    tested_solutions.append(solution)
    new_solutions = []
    calls = get_calls_including_zeroes(solution)
    dummy_calls = calls[x.vehicles + 1]
    most_expensive_calls = most_expensive_dummy(solution)
    mec = list(most_expensive_calls.keys())

    most_expensive_call = mec.pop(0)
    calls_wo_dummy = copy.deepcopy(calls)
    calls_wo_dummy.pop(x.vehicles + 1)
    sample = random.sample(calls_wo_dummy.keys(), 2)
    dummy_most_expensive_removed = [i for i in dummy_calls if i != most_expensive_call]
    calls[x.vehicles + 1] = dummy_most_expensive_removed
    for vehicle in sample:
        new_calls = copy.deepcopy(calls)
        new_calls[vehicle].insert(0, most_expensive_call)
        if len(calls[vehicle]) > 1:
            for index in range(len(calls[vehicle])):
                # print("Calls in loop:", calls)
                newnew_calls = copy.deepcopy(new_calls)
                newnew_calls[vehicle].insert(index + 1, most_expensive_call)
                if calls_to_solution(newnew_calls) in tested_solutions:
                    continue
                else:
                    new_solutions.append(calls_to_solution(newnew_calls))
                    tested_solutions.append(calls_to_solution(newnew_calls))
                # print("New calls in loop:", newnew_calls)

        else:
            # print("Calls:", calls)
            new_calls[vehicle].insert(0, most_expensive_call)
            # print("New calls:", new_calls)
            new_solutions.append(calls_to_solution(new_calls))
            tested_solutions.append(calls_to_solution(new_calls))

    best_solution = solution
    for sol in new_solutions:
        if f(sol) < f(best_solution) and check_solution(sol):
            return sol
    return solution
Beispiel #11
0
def adaptive_large_neighborhood_search(init_solution, runtime):
    s = init_solution
    s_cost = f(s)
    best = init_solution
    best_cost = f(best)
    global found_solutions
    found_solutions.clear()

    operators = ops()
    break_its = its_without_updates_break()
    curr_weights = []
    usage = []
    total_usage = []

    for i in range(len(operators)):
        curr_weights.append(1.0)
        usage.append(0)
        total_usage.append(0)

    prev_weights = curr_weights.copy()
    end = time.time() + runtime
    its_since_upd, iteration, diversification_its = 0, 0, 0
    par = parameters()

    t0 = par[0]
    t = t0
    a = par[1]
    weights_refresh_rate = par[2]
    diversification_rate = par[3]
    tot_its = 0

    while time.time() < end:

        if its_since_upd == break_its:
            s = init_solution
            s_cost = f(s)
            tot_its += 1
            t0 = par[0]
            t = t0
            its_since_upd = 0
            curr_weights.clear()
            usage.clear()
            found_solutions.clear()
            for i in range(len(operators)):
                curr_weights.append(1.0)
                usage.append(0)
            prev_weights = curr_weights.copy()

        if its_since_upd > diversification_rate:
            current = obo.move_to_dummy(s)
            diversification_its += 1
        else:
            current = s

        if iteration % weights_refresh_rate == 0 and iteration > 0:
            prev_weights = curr_weights
            curr_weights = regulate_weights(prev_weights, curr_weights, usage)
            for i in range(len(operators)):
                usage[i] = 0

        chosen_op = random.choices(operators, prev_weights, k=1).pop(0)
        if chosen_op == "take_from_dummy_place_first_suitable":
            oc = obo.take_from_dummy_place_first_suitable
        elif chosen_op == "swap":
            oc = swap
        elif chosen_op == "triple_swap":
            oc = triple_swap
        elif chosen_op == "one_reinsert_from_dummy":
            oc = one_reinsert_from_dummy
        elif chosen_op == "one_reinsert_most_expensive_call":
            oc = one_reinsert_most_expensive_call
        elif chosen_op == "most_expensive_one_reinsert_from_dummy":
            oc = most_expensive_one_reinsert_from_dummy
        elif chosen_op == "move_vehicle_to_dummy":
            oc = obo.move_vehicle_to_dummy
        elif chosen_op == "move_to_next_valid_vehicle":
            oc = obo.move_to_next_valid_vehicle
        elif chosen_op == "change_route":
            oc = obo.change_route
        elif chosen_op == "shuffle":
            oc = shuffle
        elif chosen_op == "x_one_reinserts_inside_vehicle":
            oc = x_one_reinserts_inside_vehicle
        elif chosen_op == "x_one_reinserts_inside_vehicle_dsc":
            oc = x_one_reinserts_inside_vehicle_dsc

        op_index = operators.index(chosen_op)
        op = operator(oc, current, curr_weights, op_index, usage,
                      total_usage[op_index], s_cost, best_cost)
        current, curr_weights, usage, total_usage[op_index], f_curr = op[
            0], op[1], op[2], op[3], op[4]

        delta_e = f_curr - s_cost
        rand_ii = random.uniform(0, 1)
        p = math.e * (-delta_e / t)

        if check_solution(current) and delta_e < 0:
            s = current
            s_cost = f(s)
            its_since_upd = 0
            if s_cost < best_cost:
                best = s.copy()
                best_cost = f(best)
        elif check_solution(current) and rand_ii < p:
            s = current
            its_since_upd = 0
        else:
            its_since_upd += 1

        t = a * t
        iteration += 1

    usage_dict = {}

    for i in range(len(operators)):
        usage_dict[operators[i]] = total_usage[i]

    u_d = {
        k: v
        for k, v in sorted(
            usage_dict.items(), key=lambda item: item[1], reverse=True)
    }

    for key, value in u_d.items():
        print("%d: %s" % (value, key))
    print("\n%d: diversification" % diversification_its)
    print("\nTotal iterations:", (iteration + diversification_its), "\n")

    return best
Beispiel #12
0
def weighted_one_insert(solution):
    new_sol = one_reinsert(solution)
    if check_solution(new_sol) and f(new_sol) < f(solution):
        return new_sol
    return solution