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
def try_for_best(solution): # print("Starting solution:", solution) new_solutions = [] calls = get_calls_including_zeroes(solution) dummy_calls = calls[x.vehicles + 1] # print("Dummy calls:", dummy_calls) most_expensive_calls = get_most_expensive_calls(solution) most_expensive_call = 0 for key in most_expensive_calls.keys(): # print("Key:", key) if key in dummy_calls: most_expensive_call = key break if most_expensive_call == 0: return solution # print(most_expensive_call) dummy_most_expensive_removed = [i for i in dummy_calls if i != most_expensive_call] calls[x.vehicles + 1] = dummy_most_expensive_removed # print(dummy_most_expensive_removed) vehicle = random.randrange(1, len(x.vehicles_dict)) # print("Vehicle", vehicle) 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) new_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)) best_solution = solution # print("New solutions:", new_solutions) for sol in new_solutions: if f(sol) < f(best_solution) or random.uniform(0, 1) < 0.25: # print("Chosen solution:", sol) return sol return solution
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
def best_route(solution): calls = route_planner(solution) vehicle = random.randrange(1, x.vehicles + 1) if not calls[vehicle]: return solution route = calls[vehicle] global tested_solutions if route in tested_solutions: return solution tested_solutions.append(route) # if len(route) > 7: # return solution all_combs = list(set(itertools.permutations(route))) nl = [list(row) for row in all_combs] best_solution = solution for c in nl: rt = copy.deepcopy(get_calls_including_zeroes(solution)) rt[vehicle] = c rt[vehicle].append(0) # print(calls_to_solution(rt)) if f(calls_to_solution(rt)) < f(best_solution): best_solution = calls_to_solution(rt) return best_solution
def one_reinsert_most_expensive_from_dummy(solution): most_expensive_calls = most_expensive_dummy(solution) if not most_expensive_calls: return solution calls = get_calls_including_zeroes(solution) chosen_call = next(iter(most_expensive_calls)) vehicle = 0 for i in range(1, len(calls.keys())): if chosen_call in x.vehicles_dict.get(i).valid_calls: vehicle = i break if vehicle == 0: return solution calls[x.vehicles + 1] = [i for i in calls[x.vehicles + 1] if i != chosen_call] calls[vehicle].insert(0, chosen_call) calls[vehicle].insert(0, chosen_call) return calls_to_solution(calls)
def swingers(solution): # print(solution) calls = route_planner(solution) if not calls: return solution calls_w_0 = get_calls_including_zeroes(solution) # print("All calls:", calls) for vehicle in calls: # print("Vehicle:", vehicle) # print("Calls:", calls[vehicle]) if len(calls[vehicle]) <= 2: continue else: calls_w_0[vehicle] = neighbor_switch(calls, vehicle) # print(calls_w_0) calls_w_0[vehicle].append(0) # print("Calls after a swing:", calls_to_solution(calls_w_0)) return calls_to_solution(calls_w_0)
def one_reinsert_most_expensive_call(solution): calls = get_calls_including_zeroes(solution) if not calls[x.vehicles + 1]: 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) < 3: return solution most_expensive_call = cnt_list[random.randrange(0, 3)] if most_expensive_call in calls[x.vehicles + 1]: calls[x.vehicles + 1].remove(most_expensive_call) calls[x.vehicles + 1].remove(most_expensive_call) for i in range(1, x.vehicles + 2): 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 calls_to_solution(calls)