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

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

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

الدرس: برمجة الأعداد الصحيحة المختلطة لمشكلة البائع المتجول

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

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

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

نوع المحتوى: تمارين وأسئلة

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

📝 ملخص الصفحة

تقدم هذه الصفحة تمرينًا برمجيًا لإنشاء خوارزمية لحل مشكلة البائع المتجول باستخدام برمجة الأعداد الصحيحة المختلطة (MIP). يتضمن النص مقطعًا برمجيًا غير مكتمل بلغة Python يتطلب من المستخدم إكمال الأجزاء الناقصة لتنفيذ الخوارزمية بشكل صحيح.

يبدأ التمرين بإنشاء متغيرات القرار الثنائية التي تمثل الانتقالات بين المواقع، بهدف تقليل المسافة الإجمالية للرحلة. يتم تعريف قيود الوصول والمغادرة لضمان زيارة كل موقع مرة واحدة فقط، مع إضافة قيود الاتصال لمنع تكوين دورات جزئية في المسار.

يستخدم النص مكتبات مثل `numpy` ووظائف مثل `product` لإنشاء جميع الانتقالات الممكنة. يتطلب التمرين فهمًا لمفاهيم البرمجة الرياضية والتحسين، مما يجعله مناسبًا لمتعلمي البرمجة المتقدمة أو طلاب علوم الحاسوب.

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

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

6

نوع: QUESTION

أَنشِئ خوارزمية حل برمجة الأعداد الصحيحة المختلطة لمشكلة البائع المتجول، من خلال إكمال المقطع البرمجي التالي، بحيث تنتقي متغيرات القرار وقيود الاتصال انتقاء صحيحًا:

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

def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

نوع: METADATA

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

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

--- SECTION: 6 --- أَنشِئ خوارزمية حل برمجة الأعداد الصحيحة المختلطة لمشكلة البائع المتجول، من خلال إكمال المقطع البرمجي التالي، بحيث تنتقي متغيرات القرار وقيود الاتصال انتقاء صحيحًا:def MIP_solver(dist_matrix, location_ids, startstop):solver = ______________() # 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(____________________(location_ids, location_ids))N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________)# objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions))# Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1# Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem2023 - 1445

✅ حلول أسئلة الكتاب الرسمية

عدد الأسئلة: 10

سؤال (1): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: Model

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** عند البدء في بناء نموذج برمجة خطية أو برمجية عددية صحيحة (MIP) باستخدام مكتبات مثل `mip` في بايثون، نحتاج أولاً إلى إنشاء كائن يمثل النموذج الرياضي الذي سيحتوي على المتغيرات والقيود.
  2. **الخطوة 2 (التطبيق):** بالنظر إلى التعليق البرمجي `# creates a solver` في السطر الأول، نجد أننا بحاجة لاستدعاء الفئة الأساسية المسؤولة عن بناء هذا النموذج.
  3. **الخطوة 3 (النتيجة):** بناءً على التقاليد البرمجية في هذه المكتبات، فإن الكلمة المناسبة لإنشاء النموذج هي: **Model**

سؤال (2): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: product

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** في مسائل المسارات (مثل مسألة البائع المتجول)، نحتاج إلى تمويل جميع الاحتمالات الممكنة للانتقال من موقع إلى آخر، وهو ما يسمى بالضرب الديكارتي للمجموعات.
  2. **الخطوة 2 (التطبيق):** الدالة التي تقوم بإنشاء جميع الأزواج الممكنة (i, j) من قائمة المواقع `location_ids` هي دالة موجودة في مكتبة `itertools` وتستخدم بكثرة في هذا السياق.
  3. **الخطوة 3 (النتيجة):** لذلك، الدالة المطلوبة لإنشاء قائمة الانتقالات هي: **product**

سؤال (3): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: add_var

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** بعد إنشاء كائن النموذج (solver)، يجب علينا تعريف المتغيرات التي سيتخذ المحلل (solver) قراراً بشأن قيمتها.
  2. **الخطوة 2 (التطبيق):** في الكود، نقوم بالمرور على جميع الانتقالات `transitions` ونريد إضافة متغير قرار لكل انتقال. الوظيفة البرمجية التابعة للكائن `solver` والمسؤولة عن إضافة متغير جديد هي `add_var`.
  3. **الخطوة 3 (النتيجة):** إذن، الأمر البرمجي الصحيح هو: **add_var**

سؤال (4): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: BINARY

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** متغيرات القرار في مسائل المسارات عادة ما تكون ثنائية، أي أنها تأخذ القيمة (1) إذا تم اختيار المسار و (0) إذا لم يتم اختياره.
  2. **الخطوة 2 (التطبيق):** عند تعريف المتغير باستخدام `add_var` في مكتبة `mip` أو ما يشابهها، نحدد نوع المتغير `var_type`. بما أننا نتحدث عن اختيار مسار من عدمه، فنحن بحاجة لنوع البيانات الثنائي.
  3. **الخطوة 3 (النتيجة):** لذلك، القيمة المناسبة لنوع المتغير هي: **BINARY**

سؤال (5): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: minimize

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** دالة الهدف (Objective function) هي المعادلة التي نسعى لتحسينها، وفي مسائل المسافات والتكاليف، يكون الهدف دائماً هو تقليل القيمة الإجمالية.
  2. **الخطوة 2 (التطبيق):** التعليق البرمجي يقول `# objective function: minimizes the distance`. في لغات النمذجة الرياضية، نستخدم دالة صريحة لتحديد أن الهدف هو التقليل.
  3. **الخطوة 3 (النتيجة):** بناءً على ذلك، الكلمة المطلوبة هي: **minimize**

سؤال (6): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: x[j][i]

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** قيود الوصول والمغادرة (Arrive/Depart Constraints) تضمن أن كل موقع يتم الدخول إليه مرة واحدة والخروج منه مرة واحدة. القيد الأول هنا يتعلق بالتدفق الداخل إلى الموقع `i`.
  2. **الخطوة 2 (التطبيق):** لتمثيل التدفق الداخل إلى الموقع `i` من جميع المواقع الأخرى `j` في المصفوفة `x` التي تمثل الانتقالات، يجب أن يكون الدليل الثاني هو `i` (الوجهة).
  3. **الخطوة 3 (النتيجة):** إذن، التعبير الصحيح لجمع الانتقالات القادمة إلى الموقع هو: **x[j][i]**

سؤال (7): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: x[i][j]

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** هذا القيد يكمل قيد التدفق، حيث يركز على التدفق الخارج من الموقع `i` إلى أي موقع آخر `j`.
  2. **الخطوة 2 (التطبيق):** في مصفوفة القرار `x` التي تمثل الانتقالات، لتمثيل الخروج من `i` إلى `j` نضع `i` كدليل أول (المصدر) و `j` كدليل ثانٍ (الوجهة).
  3. **الخطوة 3 (النتيجة):** لذلك، التعبير الصحيح لجمع الانتقالات الخارجة من الموقع هو: **x[i][j]**

سؤال (8): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: add_var

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** لمنع تكون مسارات فرعية (Sub-tours)، نحتاج إلى متغيرات إضافية (غالباً ما تسمى متغيرات MTZ) لكل موقع.
  2. **الخطوة 2 (التطبيق):** تماماً كما فعلنا مع متغيرات الانتقال `x` في الخطوات السابقة، نحتاج هنا لإضافة متغيرات جديدة `y` إلى النموذج `solver`.
  3. **الخطوة 3 (النتيجة):** الدالة المستخدمة لإضافة هذه المتغيرات هي نفسها: **add_var**

سؤال (9): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: INTEGER

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** متغيرات `y` في قيود منع المسارات الفرعية تمثل ترتيب زيارة المواقع، وبالتالي يجب أن تكون أعداداً صحيحة لتمثيل الرتبة أو التسلسل.
  2. **الخطوة 2 (التطبيق):** بما أن هذه المتغيرات ليست ثنائية (0 أو 1 فقط) بل يمكن أن تأخذ قيم تسلسلية، فإننا نحدد نوعها كأعداد صحيحة.
  3. **الخطوة 3 (النتيجة):** إذن، نوع المتغير المطلوب هنا هو: **INTEGER**

سؤال (10): def MIP_solver(dist_matrix, location_ids, startstop): solver = ______________() # 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(____________________(location_ids, location_ids)) N = len(location_ids) # number of locations # creates 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.____________________(var_type=______________) # objective function: minimizes the distance solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)) # Arrive/Depart Constraints for i in location_ids: solver += xsum(____________________ for j in location_ids - {i}) == 1 solver += xsum(____________________ for j in location_ids - {i}) == 1 # Adds a binary decision variable for each location y = [solver.____________________(var_type=______________) 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.____________________() # solves the problem

الإجابة: optimize

خطوات الحل:

  1. **الخطوة 1 (المفهوم):** بعد تعريف النموذج، المتغيرات، دالة الهدف، والقيود، تأتي المرحلة الأخيرة وهي إصدار الأمر للمحلل للبدء في البحث عن الحل الأمثل.
  2. **الخطوة 2 (التطبيق):** في مكتبات البرمجة الرياضية، تسمى هذه العملية عادةً "التحسين" أو "البحث عن الحل الأفضل" بناءً على القيود المعطاة.
  3. **الخطوة 3 (النتيجة):** الأمر البرمجي النهائي لتشغيل المحلل هو: **optimize**

📝 أسئلة اختبارية

عدد الأسئلة: 9

سؤال 6: x[i, j] = solver.____________________(var_type=______________)

  • أ) addVariable('Binary')
  • ب) createVar('Integer')
  • ج) addConstraint('Binary')
  • د) newVariable('Continuous')

الإجابة الصحيحة: addVariable('Binary')

الشرح: يتم إضافة متغير قرار ثنائي (Binary) باستخدام دالة addVariable مع تحديد نوع المتغير كـ 'Binary' للإشارة إلى ما إذا كان الانتقال (i->j) مدرجًا في المسار

تلميح: ما هي الدالة المستخدمة لإضافة متغيرات في PuLP؟ وما هو نوع المتغير المناسب لمتغيرات ثنائية؟

سؤال 6: أَنشِئ خوارزمية حل برمجة الأعداد الصحيحة المختلطة لمشكلة البائع المتجول، من خلال إكمال المقطع البرمجي التالي، بحيث تنتقي متغيرات القرار وقيود الاتصال انتقاء صحيحًا:

الإجابة الصحيحة: انظر الأسئلة الفرعية

الشرح: هذا سؤال رئيسي يحتوي على أسئلة فرعية تتعلق بإكمال المقطع البرمجي لخوارزمية برمجة الأعداد الصحيحة المختلطة

تلميح: راجع الأسئلة الفرعية أدناه لإكمال الأجزاء الناقصة في الكود

سؤال 6: solver = ______________() # creates a solver

  • أ) pulp.LpProblem
  • ب) scipy.optimize
  • ج) numpy.array
  • د) itertools.permutations

الإجابة الصحيحة: pulp.LpProblem

الشرح: يتم إنشاء مشكلة برمجة خطية باستخدام pulp.LpProblem() من مكتبة PuLP لحل مشاكل البرمجة الخطية والأعداد الصحيحة

تلميح: ما هي المكتبة الشائعة لحل مشاكل البرمجة الخطية في بايثون؟

سؤال 6: transitions = list(____________________(location_ids, location_ids))

  • أ) product
  • ب) permutations
  • ج) combinations
  • د) zip

الإجابة الصحيحة: product

الشرح: يتم استخدام دالة product من مكتبة itertools لإنشاء جميع الانتقالات الممكنة بين المواقع (المنتج الديكارتي)

تلميح: ما هي الدالة التي تولد جميع التركيبات الممكنة من قائمتين؟

سؤال 6: solver.objective = ____________________(xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions))

  • أ) pulp.lpSum
  • ب) numpy.sum
  • ج) sum
  • د) min

الإجابة الصحيحة: pulp.lpSum

الشرح: يتم تعيين دالة الهدف باستخدام pulp.lpSum لحساب مجموع المسافات مضروبة في متغيرات القرار، بهدف تقليل المسافة الإجمالية

تلميح: ما هي الدالة المستخدمة في PuLP لتعيين مجموع كدالة هدف؟

سؤال 6: solver += xsum(____________________ for j in location_ids - {i}) == 1

  • أ) x[i, j]
  • ب) y[i]
  • ج) dist_matrix[i, j]
  • د) x[j, i]

الإجابة الصحيحة: x[i, j]

الشرح: هذا القيد يضمن أن كل موقع له انتقال واحد خارج منه (قيد المغادرة) باستخدام مجموع متغيرات القرار x[i, j] لجميع j المختلفة عن i

تلميح: ما هو المتغير الذي يمثل الانتقال من موقع i إلى موقع j؟

سؤال 6: solver += xsum(____________________ for j in location_ids - {i}) == 1

  • أ) x[j, i]
  • ب) x[i, j]
  • ج) y[j]
  • د) dist_matrix[j, i]

الإجابة الصحيحة: x[j, i]

الشرح: هذا القيد يضمن أن كل موقع له انتقال واحد وارد إليه (قيد الوصول) باستخدام مجموع متغيرات القرار x[j, i] لجميع j المختلفة عن i

تلميح: ما هو المتغير الذي يمثل الانتقال من موقع j إلى موقع i؟

سؤال 6: y = [solver.____________________(var_type=______________) for i in location_ids]

  • أ) addVariable('Integer')
  • ب) addVariable('Binary')
  • ج) createVar('Continuous')
  • د) newVariable('Real')

الإجابة الصحيحة: addVariable('Integer')

الشرح: يتم إنشاء متغيرات قرار صحيحة (Integer) y لكل موقع لفرض قيود الاتصال ومنع الحلقات الفرعية في المسار

تلميح: ما هي الدالة ونوع المتغير المستخدم لمتغيرات صحيحة في PuLP؟

سؤال 6: solver.____________________() # solves the problem

  • أ) solve
  • ب) optimize
  • ج) run
  • د) execute

الإجابة الصحيحة: solve

الشرح: يتم استدعاء دالة solve() لحل مشكلة البرمجة الخطية والأعداد الصحيحة المختلطة التي تم تعريفها

تلميح: ما هي الدالة التي تحل المشكلة في PuLP بعد تعريف المتغيرات والقيود والهدف؟

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

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

ما هي وظيفة `MIP_solver()` في هذا السياق؟

الإجابة: إنشاء وحل مشكلة برمجة الأعداد الصحيحة المختلطة (Mixed Integer Programming - MIP) لنمذجة وحل مشكلة البائع المتجول.

الشرح: الدالة `MIP_solver` تقوم بتهيئة وحل مشكلة برمجة الأعداد الصحيحة المختلطة، وهي طريقة رياضية شائعة لتمثيل وحل مشاكل التحسين التي تتضمن متغيرات قرار صحيحة.

تلميح: فكر في الدور الأساسي لأي 'solver' في سياق البرمجة الرياضية.

ماذا تمثل المتغيرات الثنائية `x[i, j]` التي يتم إنشاؤها بواسطة `solver.add_var(var_type=BINARY)`؟

الإجابة: تمثل المتغيرات الثنائية `x[i, j]` ما إذا كان الانتقال (المسار) من الموقع `i` إلى الموقع `j` مدرجاً في المسار النهائي للبائع المتجول أم لا. إذا كانت قيمتها 1، فهذا يعني أن الانتقال `i->j` جزء من المسار، وإذا كانت 0، فهو ليس كذلك.

الشرح: المتغيرات الثنائية (Binary) تكون قيمتها إما 0 أو 1، مما يجعلها مثالية لتمثيل القرارات التي تتطلب الاختيار بين خيارين، مثل تضمين أو عدم تضمين انتقال معين في مسار.

تلميح: ما هو نوع المتغير الذي تم تحديده؟ وكيف يرتبط ذلك باتخاذ قرار حول وجود انتقال معين في المسار؟

اشرح دالة الهدف `solver.objective = xsum(dist_matrix[i, j] * x[i][j] for i, j in transitions)`.

الإجابة: تهدف هذه الدالة إلى تقليل المسافة الكلية للمسار. يتم ذلك بضرب مصفوفة المسافات (`dist_matrix`) بين المواقع في المتغيرات الثنائية التي تحدد ما إذا كان الانتقال مستخدماً في المسار. ثم يتم جمع كل هذه القيم لتمثيل المسافة الإجمالية.

الشرح: الهدف هو إيجاد أقصر مسار ممكن يزور كل موقع مرة واحدة ويعود إلى نقطة البداية. ضرب المسافة بين موقعين `i` و `j` بعدد مرات استخدام هذا الانتقال (وهو ما يمثله `x[i][j]`) ثم جمع هذه النتائج يحقق ذلك.

تلميح: ما هو الهدف الأساسي لمشكلة البائع المتجول؟ وكيف يمكن التعبير عن المسافة الإجمالية باستخدام المتغيرات؟

ما الغرض من القيود التالية: `solver += xsum(x[i][j] for j in location_ids - {i}) == 1` و `solver += xsum(x[i][j] for j in location_ids - {i}) == 1` لكل موقع `i`؟

الإجابة: هذه القيود تضمن أن كل موقع `i` يتم دخوله مرة واحدة بالضبط والخروج منه مرة واحدة بالضبط. القيد الأول يضمن أن هناك انتقالاً واحداً فقط يخرج من الموقع `i` (Arrival Constraint)، والقيد الثاني يضمن أن هناك انتقالاً واحداً فقط يدخل إلى الموقع `i` (Depart Constraint).

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

تلميح: فكر في طبيعة المسار المطلوب في مشكلة البائع المتجول - كيف يجب أن يكون لكل موقع؟

ما الدور الذي تلعبه المتغيرات الثنائية `y[i]` في نموذج حل مشكلة البائع المتجول؟

الإجابة: تُستخدم المتغيرات الثنائية `y[i]` لتمثيل ترتيب زيارة المواقع (باستثناء نقطة البداية/النهاية `startstop`) ولمنع تكون الدورات الفرعية (subtours). إذا تم استخدام `y[i]`، فإنه يحدد أن الموقع `i` جزء من المسار الرئيسي.

الشرح: بدون قيود منع الدورات الفرعية، قد تجد الخوارزمية حلاً يتكون من مسارات متعددة قصيرة بدلاً من مسار واحد طويل. المتغيرات `y[i]` و القيود المرتبطة بها تمنع تكون هذه الدورات الفرعية غير المرغوبة.

تلميح: ما هي المشكلة الشائعة التي قد تنشأ في نماذج البائع المتجول غير المكتملة، وكيف يمكن لمتغيرات إضافية المساعدة في حلها؟