📋 المحتوى المنظم
📖 محتوى تعليمي مفصّل
نوع: محتوى تعليمي
على سبيل المثال، فكر في الطريق 1 ← 2 ← 1 الوارد في الحل المكون من طريقتين لنسخة مشكلة البائع المتجول الموضحة في الشكل السابق، حيث يتطلب قيد الاتصال أن تكون 1 + V2 ≥ V1، و V2 ≥ V1، وهذا مستحيل، فلذلك سيتم استبعاد الحل.
نوع: محتوى تعليمي
في المقابل، يتطلب الحل الصحيح 0 ← 3 ← 4 ← 1 ← 2 ← 0 أن تكون 1 + V4 ≥ V3، و V4 ≥ V3، وأن تكون 1 + V2 ≥ V1، و V2 ≥ V1، ويمكن تحقيق ذلك بضبط قيم V كما يأتي: 0 = V1 و 1 = V2 و 3 = V3 و 2 = V4، ولا تنطبق قيود الاتصال على الانتقالات التي تشمل موقع startstop (الانطلاق والتوقف).
نوع: محتوى تعليمي
تجمع الدالة التالية كل الأشياء معًا لإنشاء خوارزمية حل برمجة الأعداد الصحيحة المختلطة لمشكلة البائع المتجول.
نوع: محتوى تعليمي
from itertools import product
from mip import BINARY, INTEGER
from mip import Model
from mip import xsum, minimize
def MIP_solver(dist_matrix, location_ids, startstop):
solver = Model()#creates a solver
solver.verbose = 0 #setting this to 1 will print info on the progress of the solver
#creates every transition from every location to every other location
transitions = list(product(location_ids,location_ids))
N = len(location_ids) #number of locations
#create an empty square matrix full of 'None' values
x = numpy.full((N, N), None)
#adds binary decision variables indicating if transition (i->j) is included in the route
for i, j in transitions:
x[i, j]=solver.add_var(var_type = BINARY)
#objective function: minimizes the distance
solver.objective = minimize(xsum(dist_matrix[i,j]*x[i][j] for i,j in transitions))
#Arrive/Depart Constraints
for i in location_ids:
solver += xsum(x[i,j] for j in location_ids - {i}) == 1 #exactly 1 arrival
solver += xsum(x[j,i] for j in location_ids - {i}) == 1 #exactly 1 departure
#adds a binary decision variable for each location
y = [solver.add_var(var_type=INTEGER) for i in location_ids]
#adds connectivity constraints for transitions that do not include the startstop
for (i, j) in product(location_ids - {startstop}, location_ids - {startstop}):
if i != j: #ignores transitions from a location to itself
solver += y[j] - y[i] >=(N+1)*x[i,j] - N
solver.optimize() #solves the problem
#prints the solution
if solver.num_solutions: #if a solution was found
best_route = [startstop] #stores the best route
curr_loc = startstop #the currently visited location
while True:
for next_loc in location_ids:# for every possible next location
if x[curr_loc,next_loc].x == 1: #if x value for the curr_loc->next_loc transition is 1
best_route.append(next_loc) #appends the next location to the route
curr_loc=next_loc #visits the next location
break
if next_loc == startstop: #exits if route returns to the startstop
break
return best_route, solver.objective_value #returns the route and its total distance
نوع: METADATA
وزارة التعليم
Ministry of Education
2023 - 1447
نوع: METADATA
292
📄 النص الكامل للصفحة
على سبيل المثال، فكر في الطريق 1 ← 2 ← 1 الوارد في الحل المكون من طريقتين لنسخة مشكلة البائع المتجول الموضحة في الشكل السابق، حيث يتطلب قيد الاتصال أن تكون 1 + V2 ≥ V1، و V2 ≥ V1، وهذا مستحيل، فلذلك سيتم استبعاد الحل.في المقابل، يتطلب الحل الصحيح 0 ← 3 ← 4 ← 1 ← 2 ← 0 أن تكون 1 + V4 ≥ V3، و V4 ≥ V3، وأن تكون 1 + V2 ≥ V1، و V2 ≥ V1، ويمكن تحقيق ذلك بضبط قيم V كما يأتي: 0 = V1 و 1 = V2 و 3 = V3 و 2 = V4، ولا تنطبق قيود الاتصال على الانتقالات التي تشمل موقع startstop (الانطلاق والتوقف).تجمع الدالة التالية كل الأشياء معًا لإنشاء خوارزمية حل برمجة الأعداد الصحيحة المختلطة لمشكلة البائع المتجول.from itertools import product from mip import BINARY, INTEGER from mip import Model from mip import xsum, minimize def MIP_solver(dist_matrix, location_ids, startstop):
solver = Model()#creates a solver solver.verbose = 0 #setting this to 1 will print info on the progress of the solver
#creates every transition from every location to every other location transitions = list(product(location_ids,location_ids))
N = len(location_ids) #number of locations
#create an empty square matrix full of 'None' values x = numpy.full((N, N), None)
#adds binary decision variables indicating if transition (i->j) is included in the route for i, j in transitions:
x[i, j]=solver.add_var(var_type = BINARY)
#objective function: minimizes the distance solver.objective = minimize(xsum(dist_matrix[i,j]*x[i][j] for i,j in transitions))
#Arrive/Depart Constraints for i in location_ids:
solver += xsum(x[i,j] for j in location_ids - {i}) == 1 #exactly 1 arrival solver += xsum(x[j,i] for j in location_ids - {i}) == 1 #exactly 1 departure
#adds a binary decision variable for each location y = [solver.add_var(var_type=INTEGER) for i in location_ids]
#adds connectivity constraints for transitions that do not include the startstop for (i, j) in product(location_ids - {startstop}, location_ids - {startstop}):
if i != j: #ignores transitions from a location to itself solver += y[j] - y[i] >=(N+1)*x[i,j] - N solver.optimize() #solves the problem
#prints the solution if solver.num_solutions: #if a solution was found best_route = [startstop] #stores the best route curr_loc = startstop #the currently visited location while True:
for next_loc in location_ids:# for every possible next location if x[curr_loc,next_loc].x == 1: #if x value for the curr_loc->next_loc transition is 1
best_route.append(next_loc) #appends the next location to the route curr_loc=next_loc #visits the next location break if next_loc == startstop: #exits if route returns to the startstop break return best_route, solver.objective_value #returns the route and its total distance2023 - 1447
🎴 بطاقات تعليمية للمراجعة
عدد البطاقات: 5 بطاقة لهذه الصفحة
ما هو الشرط الأساسي لضمان عدم وجود دورات فرعية (subtours) في حل مشكلة البائع المتجول باستخدام البرمجة الخطية المختلطة (Mixed Integer Programming)؟
الإجابة: يتم فرض قيود الاتصال (connectivity constraints) التي تضمن أن أي مسار جزئي يبدأ من موقع معين وينتهي في موقع آخر يجب أن يحتوي على متغير قرار (decision variable) موجب، مما يمنع تكوين دورات فرعية منفصلة عن المسار الرئيسي.
الشرح: قيود الاتصال، مثل y[j] - y[i] >= (N+1)*x[i,j] - N، تضمن أن المتغير y (الذي يمثل ترتيب الزيارة) يزيد بشكل صحيح لكل انتقال نشط (x[i,j] = 1)، مما يمنع تشكيل دورات لا تشمل نقطة البداية/النهاية.
تلميح: فكر في القيود التي تربط بين متغيرات القرار الخاصة بالانتقالات بين المواقع.
في خوارزمية حل مشكلة البائع المتجول باستخدام البرمجة الخطية المختلطة، ما هي وظيفة المتغيرات الثنائية (binary decision variables) x[i, j]؟
الإجابة: تشير المتغيرات الثنائية x[i, j] إلى ما إذا كان الانتقال (أو الرحلة) من الموقع i إلى الموقع j مشمولاً في المسار النهائي أم لا. إذا كانت قيمتها 1، فهذا يعني أن الرحلة مضمنة؛ وإذا كانت 0، فهي غير مضمنة.
الشرح: المتغيرات الثنائية هي طريقة لتمثيل الاختيارات الثنائية (نعم/لا) في نماذج التحسين. في هذه الحالة، الاختيار هو تضمين الرحلة من i إلى j في المسار.
تلميح: تذكر أن هذه المتغيرات هي جزء من نموذج الحل، فماذا تمثل؟
ما هو الهدف من دالة `solver.objective = minimize(xsum(dist_matrix[i,j]*x[i][j] for i,j in transitions))` في خوارزمية حل مشكلة البائع المتجول؟
الإجابة: الهدف هو تقليل إجمالي المسافة المقطوعة في المسار. يتم ذلك عن طريق جمع حاصل ضرب مصفوفة المسافات (dist_matrix) في المتغيرات الثنائية التي تشير إلى الرحلات المشمولة (x[i,j]).
الشرح: وظيفة الهدف في نماذج التحسين هي ما نسعى إلى تحسينه. في مشكلة البائع المتجول، الهدف الأكثر شيوعاً هو تقليل التكلفة الإجمالية، والتي تمثل هنا المسافة.
تلميح: ابحث عن الكلمة الدالة التي تشير إلى الهدف من هذه العملية الرياضية.
في نموذج البرمجة الخطية المختلطة لمشكلة البائع المتجول، ما هي وظيفة القيود التالية: `xsum(x[i,j] for j in location_ids - {i}) == 1`؟
الإجابة: يضمن هذا القيد أن كل موقع (i) لديه بالضبط مغادرة واحدة (exactly one departure) إلى موقع آخر في المسار. بمعنى آخر، يتم الانتقال من كل موقع مرة واحدة فقط.
الشرح: يجب على البائع المتجول مغادرة كل مدينة يزورها مرة واحدة فقط للانتقال إلى مدينة أخرى. هذا القيد هو جزء أساسي من ضمان تكوين مسار صالح.
تلميح: فكر فيما يعنيه أن يكون لديك 'مغادرة واحدة' من كل موقع.
كيف تتعامل الخوارزمية مع موقع البداية/النهاية (startstop) عند تطبيق قيود الاتصال في حل مشكلة البائع المتجول؟
الإجابة: لا تنطبق قيود الاتصال (connectivity constraints) على الانتقالات التي تشمل موقع البداية/النهاية (startstop). هذا يسمح ببدء الرحلة والانتهاء منها دون التأثر بهذه القيود التي تستهدف منع الدورات الفرعية داخل المسار الرئيسي.
الشرح: عادةً ما يتم التعامل مع موقع البداية/النهاية كحالة خاصة في نماذج البائع المتجول. عدم تطبيق قيود الاتصال عليه يسهل بناء المسار الذي يبدأ وينتهي في نفس النقطة.
تلميح: ارجع إلى الجزء الذي يتم فيه تطبيق قيود الاتصال. هل تم استثناء أي مواقع؟