إنشاء خوارزمية حل القوة المفرطة لمشكلة البائع المتجول - كتاب الذكاء الإصطناعي - الصف 12 - الفصل 1 - المملكة العربية السعودية

الكتاب: كتاب الذكاء الإصطناعي - الصف 12 - الفصل 1 | المادة: الذكاء الإصطناعي | المرحلة: الصف 12 | الفصل الدراسي: 1

الدولة: المملكة العربية السعودية | المنهج: المنهج السعودي - وزارة التعليم

الدرس: إنشاء خوارزمية حل القوة المفرطة لمشكلة البائع المتجول

📚 معلومات الصفحة

الكتاب: كتاب الذكاء الإصطناعي - الصف 12 - الفصل 1 | المادة: الذكاء الإصطناعي | المرحلة: الصف 12 | الفصل الدراسي: 1

الدولة: المملكة العربية السعودية | المنهج: المنهج السعودي - وزارة التعليم

نوع المحتوى: example

مستوى الصعوبة: متوسط

📝 ملخص الصفحة

تقدم هذه الصفحة شرحًا عمليًا لإنشاء خوارزمية حل القوة المفرطة لمشكلة البائع المتجول (TSP) باستخدام لغة البرمجة بايثون. تبدأ بتعريف الدالة `brute_force_solver` التي تقبل مصفوفة المسافة ومعرفات المواقع وموقع الانطلاق والتوقف، وتستخدم أداة `permutations` من مكتبة `itertools` لتوليد جميع الطرق الممكنة (التباديل) باستثناء موقع الانطلاق والتوقف.

يتم حساب المسافة الإجمالية لكل طريق من خلال جمع المسافات بين المواقع المتتالية، بما في ذلك العودة إلى موقع الانطلاق والتوقف، ثم تحديث أفضل مسار وأقصر مسافة عند العثور على طريق أقصر. توضح الصفحة مثالًا على استخدام `permutations` لمجموعة صغيرة من المواقع، مما يظهر كيفية توليد جميع التباديل الممكنة.

تعتبر هذه الخوارزمية أساسية لفهم الطرق القائمة على التعداد الكامل في حل المشكلات التوافقية، وتسلط الضوء على أهمية التباديل في مشكلة البائع المتجول. يُظهر الكود المقدم كيفية تنفيذ هذه الخوارزمية خطوة بخطوة، مع توضيح المخرجات المتوقعة لمساعدة الطلاب على استيعاب المفاهيم.

📋 المحتوى المنظم

📖 محتوى تعليمي مفصّل

إنشاء خوارزمية حل القوة المفرطة لمشكلة البائع المتجول

نوع: محتوى تعليمي

إنشاء خوارزمية حل القوة المفرطة لمشكلة البائع المتجول

Creating a Brute-Force Solver for the Traveling Salesman Problem

نوع: محتوى تعليمي

Creating a Brute-Force Solver for the Traveling Salesman Problem

نوع: محتوى تعليمي

تستخدم الدالة التالية خوارزمية حل القوة المفرطة لتعداد جميع الطرق الممكنة (التباديل) وإظهار أقصر مسار. وتقبل هذه الدالة مصفوفة المسافة وموقع الانطلاق والتوقف الذي تظهره الدالة ()create_problem_instance. لاحظ أن الحل الممكن لنسخة مشكلة البائع المتجول (TSP) هي تبديل مدن، يبدأ من مدينة startstop (الانطلاق والتوقف) ثم ينتهي إليها.

Python Code for Brute-Force Solver

نوع: محتوى تعليمي

from itertools import permutations def brute_force_solver(dist_matrix, location_ids, startstop): # excludes the startstop location location_ids = location_ids - {startstop} # generate all possible routes (location permutations) all_routes = permutations(location_ids) best_distance = float('inf') # initializes to the highest possible number best_route = None # best route so far, initialized to None for route in all_routes: # for each route distance = 0 # total distance in this route curr_loc = startstop # current location for next_loc in route: distance += dist_matrix[curr_loc,next_loc] # adds the distance of this step curr_loc = next_loc # goes to the next location distance += dist_matrix[curr_loc,startstop] # goes to the startstop location if distance < best_distance: # if this route has lower distance than the best route best_distance = distance best_route = route # adds the startstop location at the beginning and end of the best route and returns return [startstop] + list(best_route) + [startstop], best_distance

نوع: محتوى تعليمي

تستخدم خوارزمية حل القوة المفرطة أداة ()permutations لإنشاء كل الطرق الممكنة. لاحظ أن موقع startstop (الانطلاق والتوقف) يُستبعد من التباديل؛ لأنه يجب أن يظهر دائمًا في بداية كل طريق ونهايته. فعلى سبيل المثال، إذا كانت لديك أربعة مواقع 0 و1 و2 و3، وكان الموقع 0 هو موقع startstop (الانطلاق والتوقف)، ستكون قائمة التباديل الممكنة كما يلي:

Python Code for Permutations Example

نوع: محتوى تعليمي

for route in permutations({1,2,3}): print(route)

Output of Permutations Example

نوع: محتوى تعليمي

(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)

نوع: METADATA

وزارة التعليم Ministry of Education 2023 - 1447

نوع: METADATA

288

🔍 عناصر مرئية

Python Code for Brute-Force Solver Function

A Python code block defining the `brute_force_solver` function. It imports `permutations` from `itertools`. The function takes `dist_matrix`, `location_ids`, and `startstop` as arguments. It excludes the `startstop` location from `location_ids` to generate permutations of the remaining locations. It initializes `best_distance` to infinity and `best_route` to None. It then iterates through all possible routes, calculates the total distance for each route, including the return to `startstop`, and updates `best_distance` and `best_route` if a shorter route is found. Finally, it returns the best route (starting and ending with `startstop`) and its distance.

Python Code for Permutations Example

A Python code block demonstrating the use of the `permutations` function. It iterates through all permutations of the set `{1, 2, 3}` and prints each permutation.

Output of Permutations Example

The text output showing all six possible permutations of the set `{1, 2, 3}`. Each permutation is enclosed in parentheses, with elements separated by commas.

📄 النص الكامل للصفحة

--- SECTION: إنشاء خوارزمية حل القوة المفرطة لمشكلة البائع المتجول --- إنشاء خوارزمية حل القوة المفرطة لمشكلة البائع المتجول--- SECTION: Creating a Brute-Force Solver for the Traveling Salesman Problem --- Creating a Brute-Force Solver for the Traveling Salesman Problemتستخدم الدالة التالية خوارزمية حل القوة المفرطة لتعداد جميع الطرق الممكنة (التباديل) وإظهار أقصر مسار. وتقبل هذه الدالة مصفوفة المسافة وموقع الانطلاق والتوقف الذي تظهره الدالة ()create_problem_instance. لاحظ أن الحل الممكن لنسخة مشكلة البائع المتجول (TSP) هي تبديل مدن، يبدأ من مدينة startstop (الانطلاق والتوقف) ثم ينتهي إليها.--- SECTION: Python Code for Brute-Force Solver --- from itertools import permutations def brute_force_solver(dist_matrix, location_ids, startstop): # excludes the startstop location location_ids = location_ids - {startstop} # generate all possible routes (location permutations) all_routes = permutations(location_ids) best_distance = float('inf') # initializes to the highest possible number best_route = None # best route so far, initialized to None for route in all_routes: # for each route distance = 0 # total distance in this route curr_loc = startstop # current location for next_loc in route: distance += dist_matrix[curr_loc,next_loc] # adds the distance of this step curr_loc = next_loc # goes to the next location distance += dist_matrix[curr_loc,startstop] # goes to the startstop location if distance < best_distance: # if this route has lower distance than the best route best_distance = distance best_route = route# adds the startstop location at the beginning and end of the best route and returns return [startstop] + list(best_route) + [startstop], best_distanceتستخدم خوارزمية حل القوة المفرطة أداة ()permutations لإنشاء كل الطرق الممكنة. لاحظ أن موقع startstop (الانطلاق والتوقف) يُستبعد من التباديل؛ لأنه يجب أن يظهر دائمًا في بداية كل طريق ونهايته. فعلى سبيل المثال، إذا كانت لديك أربعة مواقع 0 و1 و2 و3، وكان الموقع 0 هو موقع startstop (الانطلاق والتوقف)، ستكون قائمة التباديل الممكنة كما يلي:--- SECTION: Python Code for Permutations Example --- for route in permutations({1,2,3}): print(route)--- SECTION: Output of Permutations Example --- (1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)2023 - 1447--- VISUAL CONTEXT --- **FIGURE**: Python Code for Brute-Force Solver Function Description: A Python code block defining the `brute_force_solver` function. It imports `permutations` from `itertools`. The function takes `dist_matrix`, `location_ids`, and `startstop` as arguments. It excludes the `startstop` location from `location_ids` to generate permutations of the remaining locations. It initializes `best_distance` to infinity and `best_route` to None. It then iterates through all possible routes, calculates the total distance for each route, including the return to `startstop`, and updates `best_distance` and `best_route` if a shorter route is found. Finally, it returns the best route (starting and ending with `startstop`) and its distance. Context: This code provides a practical example of implementing a brute-force algorithm to solve the Traveling Salesman Problem by generating and evaluating all possible permutations of locations.**FIGURE**: Python Code for Permutations Example Description: A Python code block demonstrating the use of the `permutations` function. It iterates through all permutations of the set `{1, 2, 3}` and prints each permutation. Context: This example illustrates how the `permutations` function generates all possible orderings of a given set of elements, which is a core component of the brute-force approach to the Traveling Salesman Problem.**FIGURE**: Output of Permutations Example Description: The text output showing all six possible permutations of the set `{1, 2, 3}`. Each permutation is enclosed in parentheses, with elements separated by commas. Data: The output lists the following permutations: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1). Key Values: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1) Context: This output visually confirms the result of the `permutations` function, showing all possible orderings for a small set, which helps in understanding the combinatorial nature of the Traveling Salesman Problem.

🎴 بطاقات تعليمية للمراجعة

عدد البطاقات: 5 بطاقة لهذه الصفحة

ما هو الهدف الرئيسي لخوارزمية حل القوة المفرطة لمشكلة البائع المتجول؟

الإجابة: الهدف الرئيسي هو تعداد جميع الطرق الممكنة (التباديل) بين المدن لإيجاد أقصر مسار ممكن.

الشرح: خوارزمية القوة المفرطة تفحص كل الحلول الممكنة لتحديد الأفضل، وفي مشكلة البائع المتجول، هذا يعني فحص كل ترتيب ممكن للمدن.

تلميح: فكر في الاستراتيجية الأساسية التي تعتمد عليها هذه الخوارزمية لفحص جميع الاحتمالات.

لماذا يتم استبعاد موقع الانطلاق والتوقف (startstop) من قائمة المدن عند توليد التباديل في خوارزمية القوة المفرطة لحل مشكلة البائع المتجول؟

الإجابة: يتم استبعاد موقع الانطلاق والتوقف لأنه يجب أن يظهر دائمًا في بداية كل طريق ونهايته، ولا يتغير ترتيبه بالنسبة لنقطة البداية والنهاية.

الشرح: عندما نبحث عن أقصر مسار، نبدأ من مدينة معينة، نزور المدن الأخرى بترتيبات مختلفة، ثم نعود إلى نفس المدينة. موقع البداية والنهاية ثابت، لذا يتم النظر في تباديل المدن بينهما فقط.

تلميح: تذكر أن كل طريق يبدأ وينتهي في نفس المدينة. كيف يؤثر هذا على التباديل الداخلية؟

ما هي وظيفة الدالة `permutations` من مكتبة `itertools` في سياق حل مشكلة البائع المتجول باستخدام القوة المفرطة؟

الإجابة: تُستخدم الدالة `permutations` لتوليد جميع الترتيبات الممكنة (التباديل) للمدن التي لا تشمل موقع الانطلاق والتوقف.

الشرح: تسمح الدالة `permutations` بتجربة كل طريقة ممكنة لزيارة المدن المتبقية، وهو أمر ضروري لخوارزمية القوة المفرطة التي تحتاج إلى فحص كل الحلول الممكنة.

تلميح: ماذا تفعل هذه الدالة بالضبط مع مجموعة من العناصر؟

كيف يتم حساب المسافة الإجمالية لطريق معين في خوارزمية حل القوة المفرطة؟

الإجابة: يتم حساب المسافة الإجمالية بجمع المسافات بين كل مدينة متتالية في الطريق، بما في ذلك المسافة من آخر مدينة في التبديل إلى موقع الانطلاق والتوقف.

الشرح: تبدأ الخوارزمية من `startstop`، ثم تتجه إلى المدينة الأولى في التبديل، ثم إلى الثانية، وهكذا. بعد زيارة آخر مدينة في التبديل، يتم إضافة المسافة للعودة إلى `startstop`.

تلميح: كيف تتنقل الخوارزمية من مدينة إلى أخرى في مسار معين؟

ماذا يحدث إذا كانت المسافة الإجمالية لطريق معين أقل من أفضل مسافة تم العثور عليها حتى الآن؟

الإجابة: إذا كانت المسافة الإجمالية للطريق الحالي أقل من أفضل مسافة مسجلة، يتم تحديث أفضل مسافة وأفضل طريق لتسجيل هذا الطريق الجديد الأقصر.

الشرح: الهدف هو العثور على أقصر مسار ممكن. لذلك، كلما تم العثور على طريق أقصر، يتم حفظه كأفضل طريق حالي حتى يتم العثور على طريق أقصر منه.

تلميح: ما هو الهدف من مقارنة المسافات المختلفة؟