{"id":170,"date":"2024-11-10T22:05:09","date_gmt":"2024-11-10T22:05:09","guid":{"rendered":"https:\/\/akosgombkoto.info\/?page_id=170"},"modified":"2025-01-15T09:21:59","modified_gmt":"2025-01-15T09:21:59","slug":"az-utazo-ugynok-problema","status":"publish","type":"page","link":"https:\/\/akosgombkoto.info\/en\/az-utazo-ugynok-problema\/","title":{"rendered":"The Traveling Salesman Problem"},"content":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; admin_label=&#8221;Header&#8221; _builder_version=&#8221;4.27.3&#8243; _module_preset=&#8221;default&#8221; custom_padding=&#8221;0px||0px||false|false&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_row column_structure=&#8221;1_2,1_2&#8243; _builder_version=&#8221;4.27.3&#8243; _module_preset=&#8221;default&#8221; custom_padding=&#8221;0px|||||&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_column type=&#8221;1_2&#8243; _builder_version=&#8221;4.16&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_image src=&#8221;https:\/\/akosgombkoto.info\/wp-content\/uploads\/2025\/01\/data-science-070-2.png&#8221; title_text=&#8221;data-science-070-2&#8243; _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; max_width_tablet=&#8221;500px&#8221; max_width_phone=&#8221;220px&#8221; max_width_last_edited=&#8221;off|tablet&#8221; max_height_tablet=&#8221;200px&#8221; max_height_phone=&#8221;100px&#8221; max_height_last_edited=&#8221;on|phone&#8221; custom_margin=&#8221;|||-8vw|false|false&#8221; custom_margin_tablet=&#8221;|||0vw|false|false&#8221; custom_margin_phone=&#8221;&#8221; custom_margin_last_edited=&#8221;on|tablet&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; sticky_enabled=&#8221;0&#8243;][\/et_pb_image][\/et_pb_column][et_pb_column type=&#8221;1_2&#8243; _builder_version=&#8221;4.27.3&#8243; _module_preset=&#8221;default&#8221; custom_padding=&#8221;||||false|false&#8221; custom_padding_tablet=&#8221;0px||||false|false&#8221; custom_padding_last_edited=&#8221;off|desktop&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_text _builder_version=&#8221;4.27.3&#8243; _module_preset=&#8221;d72c0383-6487-4f2c-ac5c-7d48a6376757&#8243; header_font=&#8221;Roboto Slab||||||||&#8221; header_text_color=&#8221;#000000&#8243; header_font_size=&#8221;52px&#8221; header_line_height=&#8221;1.2em&#8221; custom_margin=&#8221;||10px||false|false&#8221; header_font_size_tablet=&#8221;40px&#8221; header_font_size_phone=&#8221;20px&#8221; header_font_size_last_edited=&#8221;on|phone&#8221; locked=&#8221;off&#8221; global_colors_info=&#8221;{}&#8221;]<\/p>\n<h1>The Traveling Salesman Problem<\/h1>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; text_font=&#8221;Roboto Mono||||||||&#8221; text_text_color=&#8221;#fcb03a&#8221; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.8em&#8221; background_color=&#8221;#042f4f&#8221; custom_padding=&#8221;15px||15px|20px|true|false&#8221; global_module=&#8221;471&#8243; saved_tabs=&#8221;all&#8221; global_colors_info=&#8221;{}&#8221;]<\/p>\n<p><strong>Scroll down to the first section for industrial and e.g. HR application examples.<\/strong><\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/akosgombkoto.info\/wp-content\/uploads\/2024\/11\/04_Utazo_ugynok_problema_optimalizacio_brute_force_Kepernyokep-2024-11-09-224041.png&#8221; title_text=&#8221;04_Utazo_ugynok_problema_optimalizacio_brute_force_K\u00e9perny\u0151k\u00e9p 2024-11-09 224041&#8243; show_in_lightbox=&#8221;on&#8221; _builder_version=&#8221;4.27.3&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221;][\/et_pb_image][\/et_pb_column][\/et_pb_row][\/et_pb_section][et_pb_section fb_built=&#8221;1&#8243; admin_label=&#8221;Blog&#8221; _builder_version=&#8221;4.16&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_row _builder_version=&#8221;4.16&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;4.16&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_text _builder_version=&#8221;4.27.3&#8243; _module_preset=&#8221;default&#8221; text_font=&#8221;Roboto Mono||||||||&#8221; text_text_color=&#8221;#fcb03a&#8221; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.8em&#8221; custom_padding=&#8221;15px||15px|20px|true|false&#8221; global_colors_info=&#8221;{}&#8221; background_color=&#8221;#042f4f&#8221;]<\/p>\n<p><span>Industrial and e.g. HR Application Examples\n(Non-industrial examples, such as HR, are located below the industrial example block.):<\/span><\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; text_font=&#8221;Roboto Mono||||||||&#8221; text_text_color=&#8221;#FFFFFF&#8221; text_font_size=&#8221;16px&#8221; text_line_height=&#8221;1.8em&#8221; background_color=&#8221;#042f4f&#8221; custom_padding=&#8221;20px|20px|20px|20px|true|true&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; sticky_enabled=&#8221;0&#8243;]<\/p>\n<p style=\"text-align: justify;\"><strong style=\"font-size: 16px;\">Industrial Use Case:<\/strong><\/p>\n<p style=\"text-align: justify;\"><strong><\/strong>In manufacturing, the Traveling Salesman Problem (TSP) often arises in logistics optimization. The task is to find the shortest route that visits all necessary locations (sites, machines, warehouses, etc.) and returns to the starting point.<\/p>\n<p style=\"text-align: justify;\">Example in Manufacturing: Component Collection in Warehouses<br \/>Imagine a large production facility where various components need to be collected from different warehouses or separate buildings for the production process. The goal is for a forklift or collection robot to visit all the warehouses in the shortest path possible, minimizing travel time and costs.<\/p>\n<p style=\"text-align: justify;\">Application of the Traveling Salesman Problem in the Example<\/p>\n<p style=\"text-align: justify;\">Objective Definition:<\/p>\n<p style=\"text-align: justify;\">The task is to determine the shortest route for the collection vehicle or robot to visit all required warehouses, collect the components, and return to the starting point.<br \/>This saves time, fuel, and reduces the collection cycle time, speeding up production.<\/p>\n<p style=\"text-align: justify;\">Defining Points and Distances:<\/p>\n<p style=\"text-align: justify;\">Each warehouse is defined as a point, and the distances between these points are calculated (e.g., route length or travel time).<br \/>The algorithm uses the distances between all points and identifies the starting location.<\/p>\n<p style=\"text-align: justify;\">Algorithm Application:<\/p>\n<p style=\"text-align: justify;\" class=\"translation-block\">Various algorithms (e.g., brute force, dynamic programming, genetic algorithms, or heuristic approaches) can solve the TSP.\nThe algorithm computes the shortest route that touches all warehouses and returns to the start, optimizing the total distance.<\/p>\n<p style=\"text-align: justify;\">Implementation of Optimal Routes:<\/p>\n<p style=\"text-align: justify;\" class=\"translation-block\">Once the shortest route is determined, the vehicle or robot follows it, minimizing travel time.\nUsing the optimized route achieves significant time and cost savings, enhancing the efficiency of the production process.<\/p>\n<p style=\"text-align: justify;\">Scalability and Efficiency Improvements:<\/p>\n<p style=\"text-align: justify;\">If demands or routes change, re-running the TSP generates a new route. This is particularly useful for seasonal demand fluctuations requiring flexible route planning.<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; text_font=&#8221;Roboto Mono||||||||&#8221; text_text_color=&#8221;#FFFFFF&#8221; text_font_size=&#8221;16px&#8221; text_line_height=&#8221;1.8em&#8221; background_color=&#8221;#042f4f&#8221; custom_padding=&#8221;20px|20px|20px|20px|true|true&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; sticky_enabled=&#8221;0&#8243;]<\/p>\n<p style=\"text-align: justify;\">HR Example:<\/p>\n<p style=\"text-align: justify;\">1. Recruiters\u2019 Multi-Site Interview Tour:<\/p>\n<p style=\"text-align: justify;\">During recruitment, a recruiter might need to visit multiple locations (e.g., universities or event venues) in a day. The TSP helps determine the most efficient route, minimizing travel time between locations and allowing more time for interviews and networking.<\/p>\n<p style=\"text-align: justify;\">Organizing Employee Well-Being Programs Across Sites:<\/p>\n<p style=\"text-align: justify;\">When an HR team organizes well-being programs (e.g., health screenings or motivational events) at various locations, the TSP helps define the optimal route for the organizers. This ensures they can quickly reach all sites and hold events on schedule.<\/p>\n<p>[\/et_pb_text][et_pb_toggle title=&#8221;Az Utaz\u00f3 \u00dcgyn\u00f6k Probl\u00e9ma: (leny\u00edl\u00f3 tartalom &#8211; kattints a jobb sz\u00e9ls\u0151 + ikonra)&#8221; open_toggle_text_color=&#8221;#fcb03a&#8221; open_toggle_background_color=&#8221;#042f4f&#8221; closed_toggle_text_color=&#8221;#fcb03a&#8221; closed_toggle_background_color=&#8221;#042f4f&#8221; icon_color=&#8221;#fcb03a&#8221; open_icon_color=&#8221;#fcb03a&#8221; _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; title_font_size=&#8221;18px&#8221; closed_title_font_size=&#8221;18px&#8221; body_text_color=&#8221;#FFFFFF&#8221; body_font_size=&#8221;16px&#8221; body_line_height=&#8221;1.8em&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; sticky_enabled=&#8221;0&#8243;]<\/p>\n<p style=\"text-align: justify;\" class=\"translation-block\">The Traveling Salesman Problem: (expandable content - click the + icon on the right)\nThe Traveling Salesman Problem (visiting X cities in such a way that the total distance traveled is the shortest) was solved in this case using a \"Brute Force\" code. This is a typical optimization task.\nThis solution is suitable for small X values; however, as the number of cities or locations increases, the computation time grows exponentially.\nIn the \"Brute Force\" code (see the code snippet below), I specified 5 locations and also displayed the number of permutations, as shown in the Visual Studio code editor console in the image above (full-screen view recommended).<\/p>\n<p>[\/et_pb_toggle][et_pb_toggle title=&#8221;Az Utaz\u00f3 \u00dcgyn\u00f6k Probl\u00e9ma kifejt\u00e9se: (leny\u00edl\u00f3 tartalom &#8211; kattints a jobb sz\u00e9ls\u0151 + ikonra)&#8221; open_toggle_text_color=&#8221;#fcb03a&#8221; open_toggle_background_color=&#8221;#042f4f&#8221; closed_toggle_text_color=&#8221;#fcb03a&#8221; closed_toggle_background_color=&#8221;#042f4f&#8221; icon_color=&#8221;#fcb03a&#8221; open_icon_color=&#8221;#fcb03a&#8221; _builder_version=&#8221;4.27.3&#8243; _module_preset=&#8221;default&#8221; title_font_size=&#8221;18px&#8221; closed_title_font_size=&#8221;18px&#8221; body_text_color=&#8221;#FFFFFF&#8221; body_font_size=&#8221;16px&#8221; body_line_height=&#8221;1.8em&#8221; global_colors_info=&#8221;{}&#8221;]<\/p>\n<p style=\"text-align: justify;\">The Traveling Salesman Problem (TSP) is a classic combinatorial optimization problem aiming to find the most efficient route where a salesman (or agent) visits a set of cities, visiting each city exactly once, and finally returns to the starting city. The objective is to minimize the total distance traveled.<\/p>\n<p style=\"text-align: justify;\" class=\"translation-block\">Key Elements of the Problem:\nSet of Cities: A given number of cities that the salesman must visit.\nDistances: The distances between the cities are known (these could represent physical distance, time, cost, etc.).\nGoal: The salesman must:\nVisit each city exactly once.\nReturn to the starting city at the end.\nMinimize the total route length, time, or cost.\nWhy is TSP Challenging?\nTSP is an NP-hard (Non-Polynomial) problem, meaning no known algorithm can solve it quickly (in polynomial time) for all cases as the number of cities increases. The problem is characterized by the search for the optimal solution. The number of possible routes grows exponentially with the number of cities, making the problem computationally demanding very quickly.<\/p>\n<p style=\"text-align: justify;\">Example:<br \/>Suppose a salesman needs to visit 4 cities, and the following distances are given:<\/p>\n<p style=\"text-align: justify;\">Cities            A   B   C   D<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0A\u00a0 0\u00a0 \u00a012\u00a0 18\u00a0 25<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0B\u00a0 12\u00a0 0\u00a0 \u00a020\u00a0 30<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0C\u00a0 18\u00a0 20\u00a0 0\u00a0 \u00a015<br \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0D\u00a0 25\u00a0 30\u00a0 15\u00a0 0<\/p>\n<p style=\"text-align: justify;\">The question is: In what order should the cities be visited to minimize the total distance?\nIn this case, there are 3! (3 factorial, since the starting city is fixed) = 6 possible combinations to examine to find the optimal solution. As the number of cities increases, the complexity grows exponentially.<\/p>\n<p style=\"text-align: justify;\">Solutions:<\/p>\n<p style=\"text-align: justify;\">Brute-force Search: Tries all possible routes (but becomes slow with many cities).<\/p>\n<p style=\"text-align: justify;\">Heuristic Algorithms: When finding the exact solution is too costly, heuristic algorithms (like the nearest neighbor algorithm) attempt to find a good but not necessarily optimal solution.<\/p>\n<p style=\"text-align: justify;\">Dynamic Programming: Dynamic programming algorithms can solve the problem more efficiently for a smaller number of cities but still don\u2019t operate in polynomial time for larger cases.<\/p>\n<p style=\"text-align: justify;\">Genetic Algorithms and Other Evolutionary Approaches: These use principles of natural selection to solve problems.<\/p>\n<p style=\"text-align: justify;\">Summary:<\/p>\n<p style=\"text-align: justify;\">The Traveling Salesman Problem is a frequently encountered but computationally challenging problem focused on finding the optimal route for a set of cities. While no fast algorithm is known for the problem, many approaches are used to find efficient solutions, especially in cases where an exact solution is not feasible or necessary.<\/p>\n<p>[\/et_pb_toggle][et_pb_toggle title=&#8221;K\u00f3dmag: (leny\u00edl\u00f3 tartalom &#8211; kattints a jobb sz\u00e9ls\u0151 + ikonra)&#8221; open_toggle_text_color=&#8221;#fcb03a&#8221; open_toggle_background_color=&#8221;#042f4f&#8221; closed_toggle_text_color=&#8221;#fcb03a&#8221; closed_toggle_background_color=&#8221;#042f4f&#8221; icon_color=&#8221;#fcb03a&#8221; open_icon_color=&#8221;#fcb03a&#8221; _builder_version=&#8221;4.27.3&#8243; _module_preset=&#8221;default&#8221; title_font_size=&#8221;18px&#8221; closed_title_font_size=&#8221;18px&#8221; body_text_color=&#8221;#FFFFFF&#8221; body_font_size=&#8221;16px&#8221; body_line_height=&#8221;1.8em&#8221; global_colors_info=&#8221;{}&#8221;]<\/p>\n<pre class=\"aLF-aPX-K0-aPE\">import itertools\n\n# T\u00e1vols\u00e1gm\u00e1trix: t\u00e1vols\u00e1gok (b\u00e1rmilyen egys\u00e9gben) a v\u00e1rosok, t\u00e1rgyak, berendez\u00e9sek k\u00f6z\u00f6tt (5 v\u00e1rosra, t\u00e1rgyra, berendez\u00e9sre \u00e9rt\u00e9kekkel)\ndistance_matrix = [\n    [0, 12, 18, 22, 28],\n    [12, 0, 26, 24, 32],\n    [18, 26, 0, 34, 16],\n    [22, 24, 34, 0, 20],\n    [28, 32, 16, 20, 0]\n]\n\n# A v\u00e1rosok, t\u00e1rgyak, berendez\u00e9sek sz\u00e1ma\nnum_things = len(distance_matrix)\n\n# Az \u00f6sszes v\u00e1ros, t\u00e1rgy, berendez\u00e9s felsorol\u00e1sa (0, 1, 2, ..., num_things-1)\nthings = list(range(num_things))\n\n# Legr\u00f6videbb t\u00e1vols\u00e1g \u00e9s legjobb \u00fatvonal\nshortest_distance = float('inf')\nbest_route = None\nperm_num = 0\n\n# Minden lehets\u00e9ges permut\u00e1ci\u00f3 (\u00fatvonal) kisz\u00e1m\u00edt\u00e1sa\nfor perm in itertools.permutations(things[1:]):  # Az els\u0151 helysz\u00edn mindig ugyanaz, \u00edgy azt fix\u00e1ljuk\n    route = [0] + list(perm) + [0]  # K\u00f6r\u00fatvonal l\u00e9trehoz\u00e1sa\n    current_distance = 0\n    perm_num = perm_num+1\n\n    # Az \u00fatvonal \u00f6sszes t\u00e1vols\u00e1g\u00e1nak kisz\u00e1m\u00edt\u00e1sa\n    \n    for i in range(len(route) - 1):\n        current_distance += distance_matrix[route[i]][route[i + 1]]\n        \n    \n    print(\"Permut\u00e1ci\u00f3 sorsz\u00e1ma :\", perm_num, \"   \", \"T\u00e1vols\u00e1g:\", current_distance, \"t\u00e1vols\u00e1gegys\u00e9g\")  \n\n    # Ha tal\u00e1ltunk r\u00f6videbb \u00fatvonalat, friss\u00edtj\u00fck a legjobb \u00fatvonalat\n    if current_distance &lt; shortest_distance:\n        shortest_distance = current_distance\n        best_route = route\n\nprint()\nprint(f\"Legr\u00f6videbb \u00fatvonal: {best_route}\")\nprint(f\"Legr\u00f6videbb t\u00e1vols\u00e1g: {shortest_distance}\")<\/pre>\n<p><code><\/code><\/p>\n<p>[\/et_pb_toggle][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>","protected":false},"excerpt":{"rendered":"<p>Az utaz\u00f3 \u00fcgyn\u00f6k probl\u00e9maAz ipari \u00e9s p\u00e9ld\u00e1ul HR-es alkalmaz\u00e1si p\u00e9ld\u00e1\u00e9rt g\u00f6rgess lejjebb az els\u0151 bekezd\u00e9sre. Ipari \u00e9s p\u00e9ld\u00e1ul HR-es alkalmaz\u00e1si p\u00e9ld\u00e1k (nem ipari p\u00e9ld\u00e1k mint p\u00e9ld\u00e1ul a HR-es az ipari p\u00e9lda blokk alatt tal\u00e1lhat\u00f3):Ipari alkalmaz\u00e1si p\u00e9lda: Az utaz\u00f3 \u00fcgyn\u00f6k probl\u00e9m\u00e1ja (Traveling Salesman Problem, TSP) a gy\u00e1rt\u00f3iparban gyakran el\u0151fordul, f\u0151leg olyan ter\u00fcleteken, ahol a logisztikai hat\u00e9konys\u00e1g [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"class_list":["post-170","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/akosgombkoto.info\/en\/wp-json\/wp\/v2\/pages\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/akosgombkoto.info\/en\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/akosgombkoto.info\/en\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/akosgombkoto.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/akosgombkoto.info\/en\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":37,"href":"https:\/\/akosgombkoto.info\/en\/wp-json\/wp\/v2\/pages\/170\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/akosgombkoto.info\/en\/wp-json\/wp\/v2\/pages\/170\/revisions\/689"}],"wp:attachment":[{"href":"https:\/\/akosgombkoto.info\/en\/wp-json\/wp\/v2\/media?parent=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}