From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Tue, 10 Jan 2023 13:32:47 +0000 (-0500) Subject: random swap explanation X-Git-Url: http://git.skullheadx.com/nixos/README?a=commitdiff_plain;h=49eb6e9118038b2664b2d4d25c1ac960820c9eaf;p=The-Traveling-Salesman-Problem.git random swap explanation --- 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[:]