|
| 1 | +""" |
| 2 | + move_one_random_task!(path_value::BitMatrix, graph::AbstractGraph) |
| 3 | +
|
| 4 | +Select one random (uniform) task and move it to another random (uniform) feasible vehicle |
| 5 | +""" |
| 6 | +function move_one_random_task!(path_value::BitMatrix, graph::AbstractGraph) |
| 7 | + nb_tasks = size(path_value, 2) |
| 8 | + selected_task = rand(DiscreteUniform(1, nb_tasks)) |
| 9 | + selected_vehicle = find_first_one(@view path_value[:, selected_task]) |
| 10 | + |
| 11 | + can_be_inserted = Int[] |
| 12 | + # do not empty if already empty |
| 13 | + empty_encountered = false #sum(@view path_value[selected_vehicle, :]) == 1 ? true : false |
| 14 | + for i in 1:nb_tasks |
| 15 | + if i == selected_vehicle |
| 16 | + continue |
| 17 | + end |
| 18 | + # else |
| 19 | + is_empty = false |
| 20 | + if selected_task > 1 |
| 21 | + before = @view path_value[i, 1:(selected_task - 1)] |
| 22 | + if any(before) |
| 23 | + precedent_task = selected_task - find_first_one(reverse(before)) |
| 24 | + if !has_edge(graph, precedent_task + 1, selected_task + 1) |
| 25 | + continue |
| 26 | + end |
| 27 | + elseif empty_encountered |
| 28 | + continue |
| 29 | + else # if !empty_encountered |
| 30 | + is_empty = true |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + if selected_task < nb_tasks |
| 35 | + after = @view path_value[i, (selected_task + 1):end] |
| 36 | + if any(after) |
| 37 | + next_task = |
| 38 | + selected_task + |
| 39 | + find_first_one(@view path_value[i, (selected_task + 1):end]) |
| 40 | + if !has_edge(graph, selected_task + 1, next_task + 1) |
| 41 | + continue |
| 42 | + end |
| 43 | + elseif empty_encountered |
| 44 | + continue |
| 45 | + elseif !empty_encountered && is_empty |
| 46 | + empty_encountered = true |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + push!(can_be_inserted, i) |
| 51 | + end |
| 52 | + if length(can_be_inserted) == 0 |
| 53 | + @warn "No space to be inserted" selected_task path_value |
| 54 | + return nothing |
| 55 | + end |
| 56 | + new_vehicle = rand(can_be_inserted) |
| 57 | + path_value[selected_vehicle, selected_task] = false |
| 58 | + path_value[new_vehicle, selected_task] = true |
| 59 | + return nothing |
| 60 | +end |
| 61 | + |
| 62 | +""" |
| 63 | + local_search(solution::Solution, instance::AbstractInstance; nb_it::Integer=100) |
| 64 | +
|
| 65 | +Very simple local search heuristic, using the neighborhood defined by `move_one_random_task` |
| 66 | +""" |
| 67 | +function local_search(solution::Solution, instance::AbstractInstance; nb_it::Integer=100) |
| 68 | + best_solution = copy(solution.path_value) |
| 69 | + best_value = evaluate_solution(solution, instance) |
| 70 | + history_x = [0] |
| 71 | + history_y = [best_value] |
| 72 | + |
| 73 | + candidate_solution = copy(solution.path_value) |
| 74 | + for it in 1:nb_it |
| 75 | + move_one_random_task!(candidate_solution, instance.graph) |
| 76 | + |
| 77 | + value = evaluate_solution(candidate_solution, instance) |
| 78 | + if value <= best_value # keep changes |
| 79 | + best_solution = copy(candidate_solution) |
| 80 | + best_value = value |
| 81 | + push!(history_x, it) |
| 82 | + push!(history_y, best_value) |
| 83 | + else # revert changes |
| 84 | + candidate_solution = copy(best_solution) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + return Solution(best_solution, instance), best_value, history_x, history_y |
| 89 | +end |
| 90 | + |
| 91 | +""" |
| 92 | + heuristic_solution(instance::AbstractInstance; nb_it=100) |
| 93 | +
|
| 94 | +Very simple heuristic, using [`local_search`](@ref) |
| 95 | + initialised with the solution of the deterministic Linear program |
| 96 | +""" |
| 97 | +function heuristic_solution(instance::AbstractInstance; nb_it=100) |
| 98 | + _, initial_solution = solve_deterministic_VSP(instance) |
| 99 | + sol, _, _, _ = local_search(initial_solution, instance; nb_it=nb_it) |
| 100 | + return sol |
| 101 | +end |
0 commit comments