This commit is contained in:
Francesc Verdugo 2023-09-13 11:28:33 +02:00
parent 4e13c64114
commit 76789ca320

View File

@ -162,21 +162,10 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "a50706bc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"sort_neighbors (generic function with 1 method)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"function sort_neighbors(C)\n",
" n = size(C,1)\n",
@ -191,25 +180,10 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "2eeecdd6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4×4 Matrix{Int64}:\n",
" 0 2 3 2\n",
" 2 0 4 1\n",
" 3 4 0 3\n",
" 2 1 3 0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"C = [\n",
" 0 2 3 2\n",
@ -221,50 +195,20 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "6dd0288e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4-element Vector{Vector{Tuple{Int64, Int64}}}:\n",
" [(1, 0), (2, 2), (4, 2), (3, 3)]\n",
" [(2, 0), (4, 1), (1, 2), (3, 4)]\n",
" [(3, 0), (1, 3), (4, 3), (2, 4)]\n",
" [(4, 0), (2, 1), (1, 2), (3, 3)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"C_sorted = sort_neighbors(C)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "00608e1d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4-element Vector{Tuple{Int64, Int64}}:\n",
" (3, 0)\n",
" (1, 3)\n",
" (4, 3)\n",
" (2, 4)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"city = 3\n",
"C_sorted[city]"
@ -277,7 +221,7 @@
}
},
"cell_type": "markdown",
"id": "bc74f262",
"id": "7384a305",
"metadata": {},
"source": [
"<div>\n",
@ -295,21 +239,10 @@
},
{
"cell_type": "code",
"execution_count": 8,
"id": "0fdba520",
"execution_count": null,
"id": "2ddc2ec1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"visital_all_paths_recursive! (generic function with 1 method)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"function visital_all_paths(C_sorted,city)\n",
" num_cities = length(C_sorted)\n",
@ -341,23 +274,10 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "723a0f1a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"path = [1, 2, 4, 3]\n",
"path = [1, 2, 3, 4]\n",
"path = [1, 4, 2, 3]\n",
"path = [1, 4, 3, 2]\n",
"path = [1, 3, 4, 2]\n",
"path = [1, 3, 2, 4]\n"
]
}
],
"outputs": [],
"source": [
"city = 1\n",
"visital_all_paths(C_sorted,city)"
@ -388,21 +308,10 @@
},
{
"cell_type": "code",
"execution_count": 10,
"id": "7d154cb4",
"execution_count": null,
"id": "5989f0ac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tsp_serial_no_prune_recursive! (generic function with 1 method)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"function tsp_serial_no_prune(C_sorted,city)\n",
" num_cities = length(C_sorted)\n",
@ -439,21 +348,10 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"id": "d1be2bfc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"city = 1\n",
"min_distance = tsp_serial_no_prune(C_sorted,city)"
@ -469,21 +367,10 @@
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1bce8f5e",
"execution_count": null,
"id": "8241e0df",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tsp_serial_recursive! (generic function with 1 method)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"function tsp_serial(C_sorted,city)\n",
" num_cities = length(C_sorted)\n",
@ -523,21 +410,10 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"id": "998087f2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"city = 1\n",
"min_distance = tsp_serial(C_sorted,city)"
@ -553,29 +429,10 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": null,
"id": "e1eb74d8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 1.286362 seconds (1 allocation: 144 bytes)\n",
" 0.002820 seconds (1 allocation: 144 bytes)\n"
]
},
{
"data": {
"text/plain": [
"22"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"n = 11\n",
"using Random\n",
@ -597,7 +454,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": null,
"id": "354f5abb",
"metadata": {},
"outputs": [],
@ -607,24 +464,10 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": null,
"id": "7f8f6702",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3-element Vector{Int64}:\n",
" 2\n",
" 3\n",
" 4"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"if workers() == procs()\n",
" addprocs(3)\n",
@ -633,7 +476,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": null,
"id": "5c0603a1",
"metadata": {},
"outputs": [],
@ -712,29 +555,10 @@
},
{
"cell_type": "code",
"execution_count": 40,
"id": "67329b25",
"execution_count": null,
"id": "02801b66",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"path = [1, 2, 0, 0]\n",
"path = [1, 4, 0, 0]\n",
"path = [1, 3, 0, 0]\n",
" From worker 2:\tpath = [1, 2, 4, 3]\n",
" From worker 4:\tpath = [1, 3, 4, 2]\n",
" From worker 3:\tpath = [1, 4, 2, 3]\n",
" From worker 3:\tpath = [1, 4, 3, 2]\n",
" From worker 2:\tpath = [1, 2, 3, 4]\n",
" From worker 4:\tpath = [1, 3, 2, 4]\n",
" From worker 2:\tDone!\n",
" From worker 4:\tDone!\n",
" From worker 3:\tDone!\n"
]
}
],
"outputs": [],
"source": [
"C = [\n",
" 0 2 3 2\n",
@ -750,7 +574,7 @@
},
{
"cell_type": "markdown",
"id": "dba41fa8",
"id": "0d579e3d",
"metadata": {},
"source": [
"### How to track the global minimum distance?"
@ -758,24 +582,10 @@
},
{
"cell_type": "code",
"execution_count": 42,
"id": "0753e669",
"execution_count": null,
"id": "d0303392",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" From worker 3:\tmin_distance = 9223372036854775807\n",
" From worker 3:\tdistance = 9\n",
" From worker 4:\tmin_distance = 9\n",
" From worker 4:\tdistance = 6\n",
" From worker 2:\tmin_distance = 6\n",
" From worker 2:\tdistance = 6\n",
"min_distance = 6\n"
]
}
],
"outputs": [],
"source": [
"buffer = 1 # very important\n",
"min_distance_chnl = RemoteChannel(()->Channel{Int}(buffer))\n",
@ -798,7 +608,7 @@
},
{
"cell_type": "markdown",
"id": "d6051b8e",
"id": "1fabefcb",
"metadata": {},
"source": [
"### Final parallel implementation"
@ -806,7 +616,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": null,
"id": "96c649b7",
"metadata": {},
"outputs": [],
@ -903,36 +713,10 @@
},
{
"cell_type": "code",
"execution_count": 50,
"id": "e49242bb",
"execution_count": null,
"id": "67888155",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(path, distance) = ([1, 2, 0, 0], 2)\n",
"(path, distance) = ([1, 4, 0, 0], 2)\n",
"(path, distance) = ([1, 3, 0, 0], 3)\n",
" From worker 3:\t(path, distance, min_distance) = ([1, 3, 4, 2], 7, 7)\n",
" From worker 2:\t(path, distance, min_distance) = ([1, 2, 4, 3], 6, 6)\n",
" From worker 3:\tDone!\n",
" From worker 4:\t(path, distance, min_distance) = ([1, 4, 2, 3], 7, 6)\n",
" From worker 2:\tDone!\n",
" From worker 4:\tDone!\n"
]
},
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"city = 1\n",
"max_hops = 2\n",