From 49eb6e9118038b2664b2d4d25c1ac960820c9eaf Mon Sep 17 00:00:00 2001 From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Tue, 10 Jan 2023 08:32:47 -0500 Subject: [PATCH] random swap explanation --- README.md | 7 +++++++ tour_improvements/random_swapping.py | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0c71d09..82dc369 100644 --- a/README.md +++ b/README.md @@ -114,3 +114,10 @@ This method of improving a candidate solution created by a heuristic is called l **Random Swapping** +One easy tour improvement to implement is random swapping. We simply randomly select two nodes which we swap in the tour, if the resulting route has less cost, we use this new route. + +-------------------------------------------------------------------------------------------------------------- + +**2-Opt Improvement** + +O \ No newline at end of file diff --git a/tour_improvements/random_swapping.py b/tour_improvements/random_swapping.py index 18a2313..3fc88bc 100644 --- a/tour_improvements/random_swapping.py +++ b/tour_improvements/random_swapping.py @@ -1,7 +1,7 @@ from graph import calculate_route from random import sample - +# For swapping once def random_swap(route: list) -> list: r = route[:] @@ -13,7 +13,7 @@ def random_swap(route: list) -> list: return r - +# check and swap if better n times def random_swapping(route: list, n: int) -> list: d = calculate_route(route) r = route[:] -- 2.54.0