Mathematical Programming: Models, Complexity and Energy Applications
Programming in Power | البرمجة في الطاقة
Sixteen focused units: mathematical model foundations, optimization families, calculator-first energy examples, computational complexity and Python solver comparisons. Python basics are referenced in Part 03.
ست عشرة وحدة مركزة: أسس النموذج الرياضي، وعائلات التحسين، وأمثلة طاقية تحل أولا بالحاسبة، والتعقيد الحسابي، ومقارنة محللات Python. وتبقى أساسيات البرمجة في الجزء الثالث.
From Python to mathematical optimizationمن Python إلى التحسين الرياضي
Part 03 covers Python syntax, functions and numerical libraries. This part uses those skills to formulate and classify optimization models, calculate small energy examples by hand, then verify numerical solutions.
يشرح الجزء الثالث قواعد Python والتوابع والمكتبات العددية. نستخدم تلك المهارات هنا لصياغة نماذج التحسين وتصنيفها، وحل أمثلة طاقية صغيرة يدويا، ثم التحقق من حلولها العددية.
If you need a programming refresher, return to Part 03. Follow the units below in order: foundations once, then a distinct optimization family and energy example in each unit.
للمراجعة البرمجية ارجع إلى الجزء الثالث. اتبع الوحدات بالترتيب: المفاهيم الأساسية مرة واحدة، ثم عائلات التحسين وأمثلتها الطاقية.
Part 04 — Learning unitsالجزء الرابع — الوحدات
The optimization model — a single reference
النموذج الرياضي للتحسين — مرجع موحد
A parameter is fixed during one solve; a decision variable is selected by optimization. The objective evaluates each candidate; equality and inequality relations restrict feasible choices.
البارامتر معطى ثابت أثناء الحل الواحد؛ ومتغير القرار قيمة نبحث عنها. تقيم دالة الهدف كل حل مرشح، وتحدد علاقات المساواة وعدم المساواة الاختيارات الممكنة.
denotes parameters; is the decision vector. For discrete choices, declare the required components integer or binary. To maximize , minimize with the same feasible set.
يرمز إلى البارامترات و إلى شعاع القرار. نحدد مجال المتغير الصحيح أو الثنائي عند الحاجة. لتعظيم نصغر مع إبقاء المجموعة الممكنة نفسها.
A mathematical function assigns exactly one output to each input in its domain; a relation may allow several outputs. Thus is a function, while is a constraint relation. A Python function implements a calculation; its arguments are not automatically fixed model parameters.
يعطي التابع الرياضي خرجا وحيدا لكل مدخل في مجال تعريفه؛ وقد تسمح العلاقة بعدة مخارج. لذلك تابع، بينما علاقة قيد. وينفذ التابع البرمجي حسابا، ولا تكون وسائطه بالضرورة بارامترات ثابتة للنموذج.
Read, solve and verify the model by handقراءة النموذج وحله يدويا والتحقق منه
Demand is a fixed parameter; and are decision variables. Marginal costs are 20 and 35 currency units (CU) per MWh. Each generator has an 80 MW capacity; study one hour and neglect losses.
الطلب بارامتر ثابت، والاستطاعتان و متغيرا قرار. الكلفتان الحديتان هما 20 و35 وحدة نقدية لكل MWh، وسعة كل مولد 80 MW. ندرس التشغيل لمدة ساعة مع إهمال الفواقد.
Minimize [CU/], subject to power balance and the limits .
نريد تصغير الكلفة بوحدة نقدية/ساعة، مع تحقيق التوازن واحترام الحدين و.
Eliminate one unknown using the balance: .
نعزل المجهول الثاني باستخدام معادلة التوازن: .
Apply generator 2 limits: implies . Intersect with generator 1 limits : the feasible interval is .
نطبق حدود المولد الثاني: من نحصل على . بالتقاطع مع حدود المولد الأول يصبح المجال الممكن .
Substitute into the objective and collect terms: .
نعوض في دالة الهدف ونجمع الحدود المتشابهة: .
Each extra MW from generator 1 reduces total hourly cost by 15 CU/. Therefore choose the largest feasible : .
كل زيادة بمقدار 1 MW في استطاعة المولد الأول تخفض الكلفة بمقدار 15 وحدة نقدية/ساعة. لذلك نختار أكبر قيمة ممكنة: .
Calculate the remaining power: .
نحسب الاستطاعة المتبقية: .
Calculate the minimum cost with an ordinary calculator: . For one hour, the total cost is 2300 CU.
نحسب الكلفة الدنيا بالآلة الحاسبة العادية: وحدة نقدية/ساعة. ولمدة ساعة واحدة تكون الكلفة الكلية 2300 وحدة نقدية.
Verify: ; and . Balance and both capacities are satisfied. The decreasing linear cost proves this feasible endpoint is globally optimal.
نتحقق من التوازن ، ومن الحدود و. جميع القيود محققة، وتناقص الكلفة الخطية يثبت أن هذا الطرف الممكن هو الحل الأمثل العالمي.
Independent equalities, and degrees of freedom
المساواة المستقلة والرتبة ودرجات الحرية
is the number of decision variables. Rank counts independent equality equations, not the number of rows written. For consistent linear equalities , equality-based .
هو عدد متغيرات القرار. تقيس الرتبة عدد معادلات المساواة المستقلة، وليس عدد الصفوف المكتوبة. للمعادلات الخطية المتسقة تكون درجة الحرية بالنسبة للمساواة .
For nonlinear equalities , is the Jacobian: entry is . The nonlinear formula describes local dimension at regular points; singular points need extra analysis.
للمساواة غير الخطية ، تمثل مصفوفة يعقوبي؛ وعنصرها هو . تصف الصيغة غير الخطية البعد المحلي عند النقاط المنتظمة، وتحتاج النقاط الشاذة إلى تحليل إضافي.
Hand calculation — one example, three casesالحل اليدوي — مثال واحد وثلاث حالات
Take and . There are two unknowns; the coefficient row is [1,1], so and DOF=. Choosing determines .
نأخذ والتوازن . لدينا مجهولان وصف المعاملات [1,1]؛ إذن الرتبة 1 ودرجة الحرية . يحدد اختيار القيمة .
Add . Divide this row by 2: it is the first equation again. Rank remains 1 and DOF remains 1; writing an extra row did not add a new restriction.
نضيف . بقسمة الصف على 2 نحصل على المعادلة الأولى نفسها. تبقى الرتبة 1 ودرجة الحرية 1؛ لم يضف الصف المكرر قيدا مستقلا.
Instead add . Its row [1,0] is independent of [1,1]. The determinant is , so and DOF=.
بدلا من ذلك نضيف . الصف [1,0] مستقل عن [1,1]. محدد المصفوفة هو ؛ إذن الرتبة 2 ودرجة الحرية .
Substitute into the balance: . Check . The equalities determine the pair, but any additional capacity bounds must still be checked.
نعوض في التوازن فنحسب . نتحقق . تحدد المساواة زوج الاستطاعتين، لكن يجب فحص أي حدود إضافية للسعات.
Positive equality-based DOF does not guarantee feasibility, a bounded objective or a unique optimum. Inequalities and active bounds can restrict feasible directions. Even zero DOF requires checking consistency and all physical limits.
لا تضمن درجة الحرية الموجبة وجود حل ممكن أو هدف محدود أو حل أمثل وحيد. قد تقيد المتراجحات والحدود الفعالة اتجاهات الحركة الممكنة. وحتى عند انعدام درجة الحرية يجب فحص الاتساق والحدود الفيزيائية.
Classifying a mathematical program
تصنيف مسألة البرمجة الرياضية
Quick mathematics review: sets and decision-variable domainsمراجعة رياضية سريعة: مجموعات الأعداد ومجالات متغيرات القرار
Before classifying optimization models, recall which values a decision variable is permitted to take. The domain is part of the mathematical model, not just the variable's name.
قبل تصنيف مسائل الأمثلية، نتذكر القيم التي يسمح لكل متغير قرار باتخاذها. مجال المتغير جزء من النموذج الرياضي، وليس مجرد اسم أو رمز.
1. Natural numbers1. الأعداد الطبيعية
Count solar panels: 6 panels, not 6.5.
عدد الألواح الشمسية: 6 ألواح، وليس 6.5.
2. Integers2. الأعداد الصحيحة
Discrete signed controller adjustment: −2, 0, or 3 steps.
خطوات ضبط متقطعة للمتحكم: −2 أو 0 أو 3.
3. Rational numbers3. الأعداد النسبية
An exact fraction such as 3/4 of battery capacity; every ratio p/q of integers with q≠0.
كسر مثل 3/4 من سعة البطارية؛ كل نسبة p/q لعددين صحيحين بشرط q≠0.
4. Real numbers4. الأعداد الحقيقية
Generator output P=2.5 kW, varying continuously over a real interval; includes rational and irrational values.
استطاعة مولد P=2.5 kW قد تتغير باستمرار ضمن مجال حقيقي؛ وتشمل النسبية وغير النسبية.
5. Complex numbers5. الأعداد العقدية
AC phasor voltage V=230+j40 V; j²=−1. Standard LP decision variables are normally real, not complex.
تمثيل جهدي بالعقديات V=230+j40 V؛ حيث j²=−1. متغيرات LP المعتادة حقيقية وليست عقدية.
6. Binary values6. القيم الثنائية
Generator status u=0 (off) or u=1 (on). A subset of integers, not a separate number system.
حالة المولد u=0 (متوقف) أو u=1 (عامل). هي مجموعة جزئية من الصحيحة وليست نظام أعداد مستقلا.
7. Nonnegative numbers7. الأعداد غير السالبة
Battery energy E≥0; zero is allowed. Similarly restricts integers to nonnegative values.
طاقة البطارية E≥0؛ الصفر مسموح. وبالمثل تحدد الأعداد الصحيحة غير السالبة.
8. Intervals / finite sets8. المجالات والمجموعات المنتهية
Power P may take any real value from 0 to 10 kW, while n can only be 2, 4, or 6 units.
يمكن أن تأخذ P أي قيمة حقيقية بين 0 و10 kW، أما n فلا تقبل سوى 2 أو 4 أو 6 وحدات.
Each set on the left is contained in the next set. Here ℕ starts at 1; write ℕ₀ when including 0. The imaginary unit j is customary in electrical engineering.
كل مجموعة على اليسار محتواة في المجموعة التالية. هنا تبدأ ℕ من 1، ونكتب ℕ₀ عند إدراج الصفر. ويستخدم الرمز j للوحدة التخيلية في الهندسة الكهربائية.
Continuous versus discrete: A real-valued variable over an interval can vary continuously. An integer, binary or finite-choice variable is discrete. These are restrictions on a domain, not additional number systems. In standard LP, decision variables are continuous real values; ILP and MILP impose integer restrictions.
المتغير المستمر والمنفصل: المتغير الحقيقي في مجال متصل يمكن أن يأخذ قيما مستمرة، أما المتغير الصحيح أو الثنائي أو محدود الخيارات فهو منفصل. هذه قيود على مجال المتغير وليست مجموعات أعداد جديدة. في LP المعتادة تكون متغيرات القرار حقيقية مستمرة، بينما تفرض ILP وMILP قيودا صحيحة.
Classify the objective, constraints, and variable domains separately. Convexity is another axis: LP is convex; QP and QCQP may be convex or nonconvex. Stochastic, robust, multiobjective and multiperiod describe further features, not mutually exclusive algebraic classes.
نصنف دالة الهدف والقيود ومجالات المتغيرات كل على حدة. التحدب محور إضافي: LP محدبة، وقد تكون QP وQCQP محدبتين أو غير محدبتين. العشوائية والمتانة وتعدد الأهداف والفترات صفات إضافية يمكن أن تجتمع في نموذج واحد.
Convexity explained before classificationفهم التحدب قبل متابعة التصنيف
Imagine a bowl-shaped cost curve. It is convex when the straight segment joining any two points of its graph never goes below the curve. If the curve rises above such a segment, the function is nonconvex. For a minimization problem, a convex objective together with a convex feasible region guarantees that every local minimum is global.
تخيل منحنى كلفة يشبه الوعاء المفتوح إلى الأعلى. تكون الدالة محدبة إذا كان المستقيم الواصل بين أي نقطتين من منحناها يقع فوق المنحنى أو عليه. أما إذا ارتفع المنحنى فوق ذلك المستقيم في حالة ما، فالدالة غير محدبة. وفي مسألة التصغير، إذا كانت دالة الهدف محدبة ومجموعة الحلول المسموحة محدبة، فإن كل حد أدنى محلي هو حد أدنى عالمي.
Example 1 — Convexالمثال 1 — محدب
One bowl and one minimum at x = 0. The dashed chord stays above the curve.
منحنى على شكل وعاء وله قيمة صغرى واحدة عند . القطعة المستقيمة المتقطعة تقع فوق المنحنى.
Example 2 — Nonconvexالمثال 2 — غير محدب
Two valleys at x = −1 and x = 1. Between them, the curve rises above the dashed chord: convexity fails.
يوجد قاعان عند و. يرتفع المنحنى بينهما فوق القطعة المستقيمة المتقطعة، ولذلك لا يكون محدبا.
Important distinction: Convexity of a function is not the same as convexity of the whole optimization problem. Constraints must also define a convex feasible set; integer or binary decisions generally destroy that property. Nonconvex does not automatically mean that a global optimum is impossible to find.
ملاحظة مهمة: تحدب الدالة وحده لا يكفي لاعتبار مسألة الأمثلية محدبة؛ يجب أن تحدد القيود أيضا مجموعة حلول مسموحة محدبة. ووجود متغيرات صحيحة أو ثنائية يفقد المسألة هذه الخاصية عموما. كما أن عدم التحدب لا يعني استحالة إيجاد الحل الأمثل العالمي.
How to prove convexity by hand — derivative tests and solved examplesكيف نثبت التحدب يدويا؟ المشتقات وأمثلة محلولة خطوة بخطوة
Begin with the meaning. A set is convex when the entire straight segment between any two of its points stays inside it. A function is convex when its graph lies on or below the chord joining any two graph points. Let the weight be between 0 and 1.
نبدأ من المعنى. المجموعة محدبة عندما تقع القطعة المستقيمة الواصلة بين أي نقطتين منها داخل المجموعة بالكامل. والتابع محدب عندما يقع منحناه تحت القطعة الواصلة بين أي نقطتين من منحناه أو عليها. نرمز إلى وزن المزج بين النقطتين بالرمز ، وتتراوح قيمته بين الصفر والواحد.
Why does α appear in the convexity inequality?لماذا يظهر الرمز في متباينة التحدب؟
Choose two points. Take f(x)=x², x₁=−1 and x₂=2. Then f(x₁)=1 and f(x₂)=4, so the graph points are A=(−1,1) and B=(2,4).
نختار نقطتين. نأخذ التابع ، ونختار و. بالتعويض في التابع نحصل على و. إذن تقع النقطتان و على المنحنى.
Understand the weight. The expression αx₁+(1−α)x₂ gives an x-coordinate between x₁ and x₂. When α=1 it equals x₁; when α=0 it equals x₂; and when α=0.5 it is their midpoint.
نفهم وزن المزج. تحدد العبارة إحداثيا أفقيا بين و. عندما تكون نحصل على . وعندما تكون نحصل على . أما عندما تكون فنحصل على نقطة المنتصف.
Compute the curve height. At x=0.5 the function has height f(0.5)=(0.5)²=0.25, shown by point C.
نحسب ارتفاع المنحنى. عند الإحداثي نحسب قيمة التابع بالتعويض: . تمثل النقطة C هذه القيمة على المنحنى.
Compute the chord height. At the same horizontal coordinate, the straight line between A and B has height αf(x₁)+(1−α)f(x₂)=0.5(1)+0.5(4)=2.5, shown by point D.
نحسب ارتفاع القطعة المستقيمة. عند الإحداثي الأفقي نفسه نحسب ارتفاع القطعة الواصلة بين A وB من العبارة . بالتعويض نحصل على . تمثل النقطة D هذا الارتفاع على القطعة المستقيمة.
Compare. Since 0.25≤2.5, the curve lies below the chord at this chosen point. This illustrates the condition; to prove convexity we need it to hold for all x₁, x₂ and α∈[0,1], not only for this example. The later second-derivative test establishes that for f(x)=x².
نقارن. بما أن ، تقع نقطة المنحنى أسفل نقطة القطعة المستقيمة عند الإحداثي الأفقي نفسه. يوضح هذا المثال معنى الشرط فقط، ولا يكفي وحده لإثبات التحدب. يجب أن تتحقق المتباينة لكل نقطتين و ولكل قيمة . ويمكن إثبات تحدب التابع باستخدام المشتق الثاني.
Here and are any two points in the function's convex domain; selects a point between them. Strict convexity requires a strict inequality when the points differ and . The tests below are convenient sufficient or equivalent conditions when the derivatives exist.
حيث و نقطتان كيفيتان في مجال تعريف محدب، و يختار نقطة بينهما. في التحدب التام تكون المتباينة صارمة عند اختلاف النقطتين و. الاختبارات الآتية تسهل الإثبات عندما تكون المشتقات موجودة.
1. One-variable functions: first and second derivatives1. تابع بمتغير واحد: المشتق الأول والثاني
The first derivative gives the slope of the tangent. The second derivative tells us whether that slope is increasing or decreasing. First check that the domain is an interval, then differentiate twice and inspect the sign over the whole domain.
المشتق الأول يعطي ميل المماس. أما المشتق الثاني فيبين هل يزداد هذا الميل أم يتناقص. نتحقق أولا من أن المجال فترة متصلة، ثم نشتق مرتين وندرس إشارة المشتق الثاني على المجال كله.
| Second derivativeالمشتق الثاني | Conclusionالنتيجة |
| for every x in the intervalلكل قيمة ضمن المجال: | Convexمحدب |
| for every x in the intervalلكل قيمة ضمن المجال: | Strictly convex (sufficient condition)محدب تماما (شرط كاف) |
| for every x in the intervalلكل قيمة ضمن المجال: | Concaveمقعر |
| for every x in the intervalلكل قيمة ضمن المجال: | Strictly concave (sufficient condition)مقعر تماما (شرط كاف) |
Do not reverse the strict test: a strictly convex function need not have at every point. For example, is strictly convex although .
لا نعكس شرط التحدب التام: فقد يكون التابع محدبا تماما رغم أن مشتقه الثاني يساوي الصفر في بعض النقاط. مثلا محدب تماما رغم أن .
Solved example A — exponential functionمثال محلول أ — التابع الأسي
Given: for all real . We seek to prove whether it is convex or concave.
المعطى: لكل حقيقي. نريد إثبات ما إذا كان محدبا أو مقعرا.
Step 1 — Differentiate once. The derivative of the exponent −x is −1, so the chain rule gives:
الخطوة 1 — نشتق مرة واحدة. مشتق الأس −x هو −1، لذلك نستخدم قاعدة اشتقاق التابع المركب:
Break the calculation into three small parts: the outer function is the exponential; the inner function is −x; the derivative of the inner function is −1. Multiply the exponential by this derivative.
نفصل الاشتقاق إلى ثلاثة أجزاء: التابع الخارجي هو التابع الأسي؛ والتابع الداخلي هو −x؛ ومشتق التابع الداخلي −1. نضرب التابع الأسي في مشتق التابع الداخلي.
Step 2 — Differentiate again. The two negative signs cancel:
الخطوة 2 — نشتق مرة ثانية. إشارتا السالب تعطيان إشارة موجبة:
Start from the first derivative −e⁻ˣ. The outer minus sign remains outside the derivative. By the same chain rule, the derivative of e⁻ˣ is −e⁻ˣ. Thus minus times minus gives plus.
نبدأ من المشتق الأول −e⁻ˣ. تبقى إشارة السالب الخارجية خارج عملية الاشتقاق. وبقاعدة الاشتقاق نفسها يكون مشتق e⁻ˣ هو −e⁻ˣ، ولذلك فإن حاصل ضرب الإشارتين السالبتين موجب.
Step 3 — Check the sign. An exponential is positive for every real input. Thus the second derivative is positive everywhere and the function is strictly convex.
الخطوة 3 — ندرس الإشارة. التابع الأسي موجب لكل قيمة حقيقية، ولذلك المشتق الثاني موجب دائما؛ إذن التابع محدب تماما.
Solved example B — square-root functionمثال محلول ب — تابع الجذر التربيعي
Given: with . The positive-domain restriction matters because the second derivative contains a denominator.
المعطى: حيث . شرط المجال مهم لأن المشتق الثاني يحوي مقاما.
Step 1 — Rewrite the square root as a power and differentiate:
الخطوة 1 — نحول الجذر إلى أس ثم نشتق:
Recall the power rule: multiply by the exponent, then subtract 1 from that exponent. Here 1/2−1=−1/2. Finally use x⁻¹ᐟ²=1/√x to remove the negative exponent.
نتذكر قاعدة اشتقاق القوى: نضرب بالأس أولا، ثم نطرح 1 من الأس. هنا 1/2−1=−1/2. وبعد ذلك نستعمل x⁻¹ᐟ²=1/√x للتخلص من الأس السالب.
Step 2 — Differentiate the power once more; multiply the two exponents 1/2 and −1/2:
الخطوة 2 — نشتق الأس مرة ثانية ونضرب المعاملين 1/2 و−1/2:
Keep the constant factor 1/2. Differentiating x⁻¹ᐟ² multiplies it by −1/2 and reduces the exponent by another 1: −1/2−1=−3/2. Since (1/2)(−1/2)=−1/4, the sign is negative.
نحافظ على العامل الثابت 1/2. عند اشتقاق x⁻¹ᐟ² نضرب بالأس −1/2، ثم نطرح 1 من الأس: −1/2−1=−3/2. وبما أن (1/2)×(−1/2)=−1/4، تكون النتيجة سالبة.
Step 3 — As the denominator is positive when x is positive, the second derivative is strictly negative. Therefore the function is strictly concave, not convex.
الخطوة 3 — المقام موجب عندما يكون x موجبا، والمشتق الثاني سالب تماما. إذن التابع مقعر تماما وليس محدبا.
2. Two or more variables: Hessian matrix2. تابع بمتغيرين أو أكثر: مصفوفة هيس
For two variables, first differentiate with respect to each variable while holding the other fixed. Differentiate those results again to fill the 2×2 Hessian matrix. Its entries are the second partial derivatives.
عند وجود متغيرين، نشتق بالنسبة إلى كل متغير مع تثبيت الآخر. ثم نشتق النتائج مرة ثانية لنملأ مصفوفة هيس ذات الصفين والعمودين. عناصرها هي المشتقات الجزئية الثانية.
Before using eigenvalues — meaning and hand calculationقبل استخدام القيم الذاتية — التعريف وطريقة الحساب اليدوي
An eigenvalue (denoted by the Greek letter λ, lambda) is a number describing how a square matrix scales a special nonzero vector without changing its line of direction. That vector is called an eigenvector. For a real symmetric Hessian, the eigenvalues tell us the signs of the curvature in its principal directions.
القيمة الذاتية عدد نرمز إليه بالحرف اليوناني λ (لامبدا)، يبين مقدار تمدد متجه خاص غير صفري أو تقلصه عند ضربه بمصفوفة مربعة، مع بقائه على خط الاتجاه نفسه. يسمى هذا المتجه متجها ذاتيا. وفي مصفوفة هيس الحقيقية المتناظرة تساعدنا إشارات القيم الذاتية على معرفة اتجاهات انحناء التابع.
Here is the square Hessian matrix, is a nonzero eigenvector, and is its eigenvalue. To find the possible values of λ, move all terms to one side and write the identity matrix , whose diagonal entries are 1 and all other entries are 0.
هنا مصفوفة هيس المربعة، و متجه ذاتي غير صفري، و قيمته الذاتية. لإيجاد القيم المحتملة ننقل الحدود إلى طرف واحد، ونستعمل مصفوفة الوحدة التي عناصر قطرها الرئيسي تساوي 1 وبقية عناصرها تساوي 0.
The symbol means determinant. We set the determinant to zero because only then can the homogeneous system have a nonzero eigenvector. For a 2×2 matrix, compute the determinant by multiplying its main-diagonal entries and subtracting the product of the other two entries.
الرمز يعني المحدد. نساوي المحدد بالصفر لأن النظام المتجانس لا يقبل متجها ذاتيا غير صفري إلا في هذه الحالة. ولحساب محدد مصفوفة من صفين وعمودين، نضرب عنصري القطر الرئيسي ثم نطرح حاصل ضرب العنصرين الآخرين.
Hand calculation for the Hessian in our exampleالحساب اليدوي لمصفوفة هيس في مثالنا
Step 1 — Start with the Hessian that we will obtain in the example below. It is diagonal: 2 and 0.
الخطوة 1 — نبدأ بمصفوفة هيس التي سنحصل عليها في المثال التالي. عناصر قطرها الرئيسي هي 2 و0.
Step 2 — Subtract λ from each diagonal entry, because λI has λ on its diagonal.
الخطوة 2 — نطرح λ من كل عنصر على القطر الرئيسي، لأن مصفوفة λI تحوي λ على القطر الرئيسي.
Step 3 — Calculate the determinant: multiply (2−λ) by (−λ) and subtract 0×0. Then set the result equal to zero.
الخطوة 3 — نحسب المحدد: نضرب (2−λ) في (−λ)، ثم نطرح 0×0. وبعد ذلك نساوي النتيجة بالصفر.
Step 4 — A product equals zero when at least one factor is zero. Thus either −λ=0 or 2−λ=0. Solve both simple equations.
الخطوة 4 — يساوي حاصل الضرب صفرا عندما يساوي أحد العاملين صفرا على الأقل. لذلك إما −λ=0 وإما 2−λ=0. نحل كل معادلة على حدة.
Step 5 — Both eigenvalues are nonnegative, so this Hessian is positive semidefinite. One eigenvalue is zero, so it is not positive definite. We will use these results to classify the function as convex, then test strict convexity separately.
الخطوة 5 — كلتا القيمتين الذاتيتين غير سالبة، ولذلك مصفوفة هيس موجبة نصف التعريف. وبما أن إحداهما صفر، فهي ليست موجبة التعريف. سنستخدم ذلك لإثبات تحدب التابع، ثم نفحص التحدب التام بشكل مستقل.
Short rule for diagonal matrices: if every off-diagonal entry is zero, the eigenvalues are simply the diagonal entries. The determinant steps above explain why this shortcut works.
قاعدة مختصرة للمصفوفات القطرية: إذا كانت جميع العناصر خارج القطر الرئيسي صفرا، فإن القيم الذاتية هي عناصر القطر الرئيسي نفسها. وتوضح خطوات المحدد السابقة سبب صحة هذه القاعدة.
A symmetric Hessian is positive semidefinite if all its eigenvalues are nonnegative: then the function is convex on a convex domain. A positive definite Hessian (all eigenvalues positive) is a sufficient condition for strict convexity. If all eigenvalues are nonpositive, the function is concave. Mixed positive and negative eigenvalues indicate neither convex nor concave.
تكون مصفوفة هيس المتناظرة موجبة نصف التعريف إذا كانت قيمها الذاتية كلها غير سالبة؛ عندها يكون التابع محدبا على مجال محدب. وإذا كانت موجبة التعريف (القيم الذاتية كلها موجبة)، فهذا شرط كاف للتحدب التام. وإذا كانت القيم الذاتية كلها غير موجبة فالـتابع مقعر. ووجود قيم موجبة وسالبة معا يعني أنه ليس محدبا ولا مقعرا.
Solved example C — a quadratic cost plus a linear termمثال محلول ج — حد تربيعي وحد خطي
Given: . We will calculate every entry of the Hessian, then interpret the zero eigenvalue.
المعطى: . سنحسب جميع عناصر مصفوفة هيس ثم نفسر ظهور القيمة الذاتية الصفرية.
Step 1 — First partial derivatives. Treat x₂ as constant while differentiating x₁², and x₁ as constant while differentiating 2x₂:
الخطوة 1 — المشتقات الجزئية الأولى. نثبت x₂ عند اشتقاق x₁²، ونثبت x₁ عند اشتقاق 2x₂:
Differentiate term by term. With respect to x₁: x₁² becomes 2x₁ and 2x₂ becomes 0. With respect to x₂: x₁² becomes 0 and 2x₂ becomes 2. Add the two results in each direction.
نشتق كل حد على حدة. بالنسبة إلى x₁: يصبح مشتق x₁² مساويا 2x₁، ومشتق 2x₂ يساوي 0. وبالنسبة إلى x₂: مشتق x₁² يساوي 0، ومشتق 2x₂ يساوي 2. ثم نجمع النتيجتين في كل اتجاه.
Step 2 — Differentiate again. The derivative of 2x₁ with respect to x₁ is 2; the derivative of the constant 2 is zero; both mixed derivatives are zero.
الخطوة 2 — نشتق مرة ثانية. مشتق 2x₁ بالنسبة إلى x₁ هو 2، ومشتق الثابت 2 هو صفر، وكذلك المشتقان المختلطان يساويان صفرا.
Fill the four Hessian positions one by one: top-left, derivative of 2x₁ with respect to x₁ = 2; top-right, derivative of 2x₁ with respect to x₂ = 0; bottom-left, derivative of 2 with respect to x₁ = 0; bottom-right, derivative of 2 with respect to x₂ = 0.
نملأ مواقع مصفوفة هيس الأربعة واحدا بعد الآخر: أعلى اليسار مشتق 2x₁ بالنسبة إلى x₁ يساوي 2؛ أعلى اليمين مشتقه بالنسبة إلى x₂ يساوي 0؛ أسفل اليسار مشتق الثابت 2 بالنسبة إلى x₁ يساوي 0؛ وأسفل اليمين مشتقه بالنسبة إلى x₂ يساوي 0.
Step 3 — For a diagonal matrix the eigenvalues are its diagonal entries: 2 and 0. Both are nonnegative, so the Hessian is positive semidefinite and the function is convex.
الخطوة 3 — في المصفوفة القطرية تكون القيم الذاتية عناصر القطر: 2 و0. كلتاهما غير سالبة، فمصفوفة هيس موجبة نصف التعريف والتابع محدب.
Step 4 — Why not strictly convex? Take A=(0,0) and B=(0,2). Their midpoint M=(0,1). Direct substitution gives f(A)=0, f(B)=4, and f(M)=2, exactly the average (0+4)/2. Strict convexity would require a smaller value at M.
الخطوة 4 — لماذا ليس محدبا تماما؟ نختار A=(0,0) وB=(0,2)، فتكون نقطة المنتصف M=(0,1). بالتعويض نجد f(A)=0 وf(B)=4 وf(M)=2، وهي تساوي المتوسط (0+4)/2. لكن التحدب التام يشترط أن تكون القيمة عند المنتصف أصغر من هذا المتوسط.
Conclusion: convex, but not strictly convex. Replacing by gives the same Hessian, so has the same classification.
النتيجة: التابع محدب لكنه ليس محدبا تماما. وإذا استبدلنا بـ بقيت مصفوفة هيس نفسها، ولذلك فإن له التصنيف ذاته.
3. Quadratic programming and the whole optimization problem3. البرمجة التربيعية وتحدب مسألة الأمثلية كاملة
For a quadratic function with a symmetric matrix , the Hessian is . If , the function is convex; if , it is strictly convex. But a convex objective alone is not enough: the feasible set must also be convex. In the standard differentiable form, equality constraints should be affine and inequalities of the form should have convex . Integer/binary restrictions usually make the feasible set nonconvex.
في التابع التربيعي حيث المصفوفة متناظرة، تكون مصفوفة هيس . إذا كانت فالهدف محدب، وإذا كانت فالهدف محدب تماما. لكن تحدب الهدف وحده لا يكفي: يجب أن تكون مجموعة الحلول المسموحة محدبة أيضا. في الصياغة المعتادة، تكون قيود المساواة تآلفية (خطية مع ثابت)، وتكون توابع قيود عدم المساواة من الشكل محدبة. أما القيود الصحيحة والثنائية فتجعل المجموعة غير محدبة عموما.
Hand-check checklist: (1) Determine the domain; (2) compute derivatives or the Hessian; (3) check their signs or eigenvalues; (4) distinguish convex from strictly convex; (5) inspect the feasible constraints separately; (6) only then use convexity to justify global optimality.
قائمة التحقق اليدوي: (1) حدد مجال التعريف؛ (2) احسب المشتقات أو مصفوفة هيس؛ (3) افحص الإشارات أو القيم الذاتية؛ (4) ميز التحدب عن التحدب التام؛ (5) افحص قيود المنطقة المسموحة بصورة مستقلة؛ (6) بعدها فقط استخدم التحدب لتبرير عالمية الحل الأمثل.
Why every continuous LP is convex — solved exampleلماذا كل مسألة برمجة خطية مستمرة محدبة؟ مثال محلول
A continuous Linear Program (LP) has a linear objective, which is both convex and concave, and linear constraints whose feasible region is convex. Thus every local optimum is global, provided an optimum exists. This does not generally apply to Integer Linear Programming (ILP) or Mixed-Integer Linear Programming (MILP): discrete decision domains are usually nonconvex.
في البرمجة الخطية المستمرة، تكون دالة الهدف خطية، ولذلك فهي محدبة ومقعرة في الوقت نفسه. وتحدد القيود الخطية منطقة حلول محدبة. ومن ثم فإن كل حل أمثل محلي هو عالمي إذا وجد الحل. أما البرمجة الخطية بالأعداد الصحيحة ILP والبرمجة الخطية المختلطة بالأعداد الصحيحة MILP فلا تكونان محدبتين عموما بسبب مجالات المتغيرات المنفصلة.
Feasible region, convexity test and objective linesمنطقة الحل واختبار التحدب ومستقيمات دالة الهدف
The blue region continues beyond the drawing. The pink segment between A and B remains feasible: this illustrates convexity. Lower the cost from the purple line to the green line; its last contact with the feasible region is the green point ★ at (10,0), the global minimum.
تستمر المنطقة الزرقاء خارج حدود الرسم. تبقى القطعة الوردية بين A وB داخل المنطقة المسموحة، وهذا يوضح التحدب. نخفض الكلفة من المستقيم البنفسجي إلى المستقيم الأخضر، فنصل إلى النقطة الخضراء ★ عند (10,0)، وهي الحل الأمثل العالمي.
Solve it step by stepالحل خطوة بخطوة
Step 1 — Draw the boundary. Set ; the intercepts are (0,10) and (10,0).
الخطوة 1 — ارسم المستقيم الحدي. نحول القيد إلى مساواة لرسم المستقيم، فنحصل على نقطتي التقاطع (0,10) و(10,0).
Step 2 — Find feasible points. Apply and : shade the first-quadrant region above the boundary line.
الخطوة 2 — حدد منطقة الحلول المسموحة. نطبق و، فنظلل الجزء الواقع فوق المستقيم في الربع الأول. المنطقة تمتد إلى ما لا نهاية.
Step 3 — Check convexity on the graph. For example, A=(2,10) and B=(12,3) both satisfy the constraints. Every point of segment AB is feasible; this is true for any two feasible points because the constraints are linear.
الخطوة 3 — تحقق من التحدب على الشكل. النقطتان A=(2,10) وB=(12,3) تحققان القيود. القطعة AB كلها داخل المنطقة المسموحة، وينطبق ذلك على أي نقطتين مسموحتين لأن القيود خطية.
Step 4 — Move the objective line. Draw parallel lines . Slide from C=40 toward C=30 to reduce cost. C=30 touches the feasible region at (10,0).
الخطوة 4 — حرك مستقيم دالة الهدف. نرسم المستقيمات المتوازية ، ثم ننتقل من C=40 إلى C=30 لتصغير الكلفة. يلامس المستقيم C=30 المنطقة عند (10,0).
Step 5 — Verify global optimality. Comparing the two boundary corners gives C(10,0)=30 and C(0,10)=50. To prove the result for all feasible points (not just the corners), we derive a general lower bound.
الخطوة 5 — التحقق من الأمثلية العالمية. تعطينا المقارنة بين النقطتين الركنيتين الكلفتين 30 عند (10,0) و50 عند (0,10). لكن لإثبات النتيجة لجميع النقاط المسموحة، وليس فقط للنقاط الركنية، نشتق حدا أدنى عاما للكلفة.
1. Start from the objective and rearrange it to expose the known constraint
1. نبدأ من دالة الهدف ونعيد ترتيبها لإظهار القيد المعروف
Why split 5 into 3+2? Because the first constraint contains x+y. Taking 3y alongside 3x produces 3(x+y), while the remaining term is 2y. This is an intentional algebraic rearrangement, not a new assumption.
لماذا نفكك العدد 5 إلى 3+2؟ لأن القيد الأول يتضمن مجموع المتغيرين x+y. نأخذ 3y مع 3x لنحصل على 3(x+y)، ويبقى لدينا 2y. هذه إعادة ترتيب جبرية مقصودة وليست فرضا جديدا.
2. Apply the first constraint
2. نطبق القيد الأول
The first constraint says x+y≥10. Multiplying both sides by positive 3 preserves the inequality direction.
ينص القيد الأول على أن x+y≥10. نضرب طرفي المتباينة بالعدد الموجب 3، لذلك يبقى اتجاه المتباينة كما هو.
3. Apply nonnegativity of y
3. نستخدم شرط عدم سلبية المتغير y
Because y≥0, multiplying by positive 2 gives 2y≥0. The leftover term in the objective cannot decrease the cost.
بما أن y≥0، فإن ضرب الطرفين بالعدد الموجب 2 يعطينا 2y≥0. أي إن الحد المتبقي في دالة الهدف لا يستطيع إنقاص الكلفة.
4. Combine the two bounds
4. نجمع الحدين الأدنى
We now know that the first term is at least 30 and the second is at least zero. Their sum is therefore at least 30 for every feasible point.
أثبتنا أن الحد الأول لا يقل عن 30 وأن الحد الثاني لا يقل عن الصفر. لذلك لا يمكن أن يقل مجموعهما، أي الكلفة، عن 30 عند أي نقطة مسموحة.
5. Show that the lower bound is attained
5. نثبت إمكانية بلوغ الحد الأدنى
At (x,y)=(10,0), the constraints hold: 10+0≥10, x≥0 and y≥0. Substituting in the original objective yields C=30, exactly the proven lower bound. Therefore this is the global minimum.
عند النقطة (x,y)=(10,0) تتحقق جميع القيود: 10+0≥10، وx≥0، وy≥0. وبالتعويض في دالة الهدف الأصلية نجد C=30، وهي بالضبط الحد الأدنى الذي أثبتناه. إذن هذا هو الحل الأمثل العالمي.
The optimum is global. Here it is unique and occurs at a corner, although a general LP may have multiple optima or no attained optimum.
الحل أمثل عالمي ووحيد في هذا المثال ويقع عند نقطة ركنية. وقد يكون لمسألة برمجة خطية أخرى عدة حلول مثلى، أو لا يتحقق فيها حل أمثل أصلا.
| Typeالنوع | Structureالبنية | Energy applicationتطبيق طاقي | Illustrative modelمثال يوضح البنية |
|---|---|---|---|
| Linear Programming (LP)البرمجة الخطية | Linear objective + linear constraints; continuous هدف وقيود خطية ومتغيرات مستمرة | Linear dispatchتوزيع خطي | Linear dispatch of two generatorsتوزيع إنتاج مولدين بكلفة خطية |
| Quadratic Programming (QP) / Quadratically Constrained Quadratic Programming (QCQP)البرمجة التربيعية / البرمجة التربيعية ذات القيود التربيعية | Quadratic objective / quadratic constraintsهدف تربيعي / قيود تربيعية | Economic dispatch / inverter limitsالتوزيع الاقتصادي / حدود العاكس | QP: quadratic generation cost. QCQP: inverter apparent-power limit.QP: كلفة توليد تربيعية. QCQP: حد الاستطاعة الظاهرية للعاكس. |
| Nonlinear Programming (NLP)البرمجة غير الخطية | General nonlinear expressionsتعابير غير خطية عامة | AC-OPFالتدفق الأمثل المتناوب | Nonlinear model with a sine termنموذج غير خطي يحوي تابع الجيب |
| Integer Programming (IP) / Binary Integer Programming (BIP)البرمجة الصحيحة / البرمجة الصحيحة الثنائية | Integer / binary decisionsقرارات صحيحة / ثنائية | Equipment counts / selectionعدد التجهيزات / اختيارها | IP: battery-module count; BIP: on/off selectionIP: عدد وحدات البطاريات؛ BIP: قرار تشغيل أو إيقاف |
| Mixed-Integer Linear Programming (MILP) / Mixed-Integer Quadratic Programming (MIQP) / Mixed-Integer Nonlinear Programming (MINLP)البرمجة المختلطة بمتغيرات صحيحة: خطية / تربيعية / غير خطية | Mixed discrete + continuous variablesمتغيرات متقطعة ومستمرة مختلطة | Unit commitmentجدولة تشغيل المولدات | Continuous power p and binary state uاستطاعة مستمرة وقرار تشغيل ثنائي |
| Dynamic Mixed-Integer Nonlinear Programming (Dynamic MINLP)البرمجة الديناميكية غير الخطية المختلطة | Nonlinear AC equations + binary decisions + time-coupled statesعلاقات متناوبة غير خطية وقرارات ثنائية وحالات مقترنة زمنيا | A-R-OPF with wind power stations and BSSالتدفق الأمثل A-R-OPF مع محطات الرياح وبطاريات التخزين | Illustrative structure: state evolves over time; network constraints are nonlinear.بنية توضيحية: تتغير حالة التخزين مع الزمن مع قيود شبكة غير خطية. |
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
In , each decision variable has power one and is not multiplied by another decision: linear.
في أس كل متغير قرار 1 ولا يوجد جداء بين متغيري قرار؛ إذن الهدف خطي.
Adding makes the objective quadratic. is also quadratic, not linear.
إضافة تجعل الهدف تربيعيا. الجداء تربيعي أيضا، وليس خطيا.
The product remains linear when is a fixed efficiency parameter; it is bilinear when both and are decisions.
يبقى الجداء خطيا إذا كانت كفاءة ثابتة؛ ويصبح ثنائي الخطية إذا كانت و متغيري قرار.
Add : linear expressions give MILP, a quadratic objective with linear constraints gives MIQP.
نضيف : التعابير الخطية تعطي MILP، والهدف التربيعي مع القيود الخطية يعطي MIQP.
An algorithm (simplex, interior point, branch-and-bound, genetic algorithm) is a solution method, not a model class. A modeling library is also distinct from a solver.
الخوارزمية مثل السمبلكس والنقطة الداخلية والتفرع والتحديد والخوارزمية الجينية طريقة حل وليست نوع النموذج. كما أن مكتبة النمذجة تختلف عن المحلل.
Linear Programming (LP) — linear economic dispatch
البرمجة الخطية — التوزيع الاقتصادي
Two generators supply 100 MW for one hour. Costs are 20 and 35 CU/MWh; each capacity is 80 MW. Assume no losses, startup costs or minimum output.
يغذي مولدان حملا 100 MW لمدة ساعة. الكلفتان 20 و35 وحدة نقدية/MWh، وسعة كل مولد 80 MW. نهمل الفواقد وكلفة الإقلاع والحد الأدنى للتوليد.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
Use balance to eliminate : .
نحذف باستخدام التوازن: .
From : , hence . Intersect with : .
من نحصل على . بالتقاطع مع يصبح المجال .
Substitute into cost: .
نعوض في الكلفة: .
The coefficient is negative: cost falls as rises. Choose , so .
المعامل سالب؛ تنخفض الكلفة بزيادة . نختار ، ومنه .
Calculate CU/; for 1 , energy cost=2300 CU.
نحسب وحدة نقدية/ساعة، ولمدة ساعة الكلفة 2300 وحدة نقدية.
Check both interval endpoints: ; . A linear function on this interval has its minimum at the cheaper endpoint. Balance .
نفحص الطرفين: و. التابع الخطي على هذا المجال يحقق أدناه عند الطرف الأقل كلفة. باقي التوازن .
See the linear cost and its feasible minimumمنحنى الكلفة الخطية وأدناها الممكن
Axes: horizontal (MW); vertical total cost (CU/).
المحور الأفقي: استطاعة المولد الأول بالميغاواط. المحور الرأسي: الكلفة الكلية بوحدة نقدية/ساعة.
The green-tinted interval is feasible: . Grey regions are excluded by generator limits. Some excluded points have lower cost, but they are not admissible solutions.
المجال المظلل بالأخضر ممكن: . تستبعد حدود المولدين المجالين الرماديين. قد تظهر فيهما قيم كلفة أقل، لكنها لا تمثل حلولا مسموحة.
Proof of globality: for every feasible , . Equality holds at . Thus no feasible decision is cheaper.
برهان العالمية: لكل قيمة ممكنة من يكون . تتحقق المساواة عند ؛ لذلك لا توجد قيمة قرار ممكنة أقل كلفة.
import numpy as np
from scipy.optimize import linprog
for method in ['highs-ds', 'highs-ipm']:
r = linprog([20, 35], A_eq=[[1, 1]], b_eq=[100],
bounds=[(0, 80), (0, 80)], method=method)
if not r.success:
raise RuntimeError(r.message)
assert abs(sum(r.x)-100) < 1e-6
assert np.all(r.x >= -1e-6) and np.all(r.x <= 80+1e-6)
assert abs(r.fun-2300) < 1e-5
print(method, np.round(r.x, 4), round(r.fun, 4))Read the codeشرح الكود
The loop runs dual simplex and interior-point algorithms within the same HiGHS solver; these are two algorithms, not two independent solver packages.
تشغل الحلقة خوارزميتي السمبلكس الثنائي والنقطة الداخلية ضمن محلل HiGHS نفسه؛ هما خوارزميتان وليستا حزمتين مستقلتين.
linprog receives cost coefficients, balance coefficients, demand and bounds in the same variable order .
تستقبل linprog معاملات الكلفة والتوازن والطلب والحدود بالترتيب نفسه .
success checks termination; assert verifies balance, bounds and the hand-calculated cost.
تفحص success انتهاء الحل، وتتحقق assert من التوازن والحدود والكلفة المحسوبة يدويا.
Quadratic Programming (QP) — quadratic generator costs
البرمجة التربيعية — كلف توليد تربيعية
Keep the same demand and capacities as Unit 04, but use strictly convex quadratic cost curves. Coefficients carry the units needed to produce CU/.
نحتفظ بطلب الوحدة 04 وسعاتها ونستخدم منحنيات كلفة تربيعية محدبة تماما. تحمل المعاملات الوحدات اللازمة لإعطاء الكلفة بوحدة نقدية/ساعة.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
As before, and .
كما سبق والمجال .
Expand ; substitute to get .
نفك المربع ؛ ثم نجمع الحدود فنحصل على .
Complete the square: . A nonnegative square is minimized at zero.
نكمل المربع: . المربع غير السالب يحقق أدناه عند الصفر.
Thus and =100−68.75=31.25 MW; both respect [0,80]. Alternatively gives .
إذن و، وكلتاهما ضمن الحدود. وبالمشتقة نحصل على .
Calculate ²=4726.5625 and ²=976.5625. .
نحسب مربعي الاستطاعتين 4726.5625 و976.5625. الكلفة وحدة نقدية/ساعة.
Compare endpoints: and . Positive curvature proves the feasible stationary point is the unique global minimum.
نفحص الطرفين: و. المشتقة الثانية موجبة، فتثبت أن النقطة الداخلية الممكنة هي الأدنى العالمي الوحيد.
See and test the global quadratic minimumمشاهدة الأدنى التربيعي العالمي واختباره
Axes: horizontal (MW); vertical total cost (CU/).
المحور الأفقي: استطاعة المولد الأول بالميغاواط. المحور الرأسي: الكلفة الكلية بوحدة نقدية/ساعة.
Enforce balance first: . The plotted curve is the total system cost , not the isolated cost of one generator. Green marks the unique global optimum; grey shading is infeasible.
نطبق التوازن أولا: . يمثل المنحنى الكلفة الكلية ، وليس كلفة مولد منفرد. تحدد النقطة الخضراء الحل الأمثل العالمي الوحيد، ويمثل التظليل الرمادي المجال غير الممكن.
Exact proof: . A square cannot be negative, so every feasible point has . Equality holds only at , which also satisfies all limits.
البرهان الدقيق: . لا يكون المربع سالبا؛ لذلك تحقق كل نقطة ممكنة . تتحقق المساواة فقط عند ، وهي قيمة تحترم جميع الحدود.
The orange ring shows your trial point. is the extra hourly cost above the green optimum. Move the slider to either side: the extra cost stays nonnegative. Component costs are rounded for display; the total uses unrounded values.
تمثل الحلقة البرتقالية نقطة تجربتك. ويعني فرق الكلفة عن الحل الأخضر. حرك المؤشر إلى الجهتين؛ يبقى الفرق غير سالب. تعرض الكلفتان الجزئيتان بعد التقريب، وتحسب الكلفة الكلية من القيم غير المقربة.
| (MW) | (MW) | (CU/) | Δ (CU/) |
|---|---|---|---|
| 20 | 80 | 4560.000 | 950.625 |
| 40 | 60 | 3940.000 | 330.625 |
| 60 | 40 | 3640.000 | 30.625 |
| 68.75 | 31.25 | 3609.375 | 0.000 |
| 80 | 20 | 3660.000 | 50.625 |
Calculator exercise: recompute one row by hand, then use the slider to verify it. The graph illustrates globality; the nonnegative-square identity proves it for the entire feasible interval, including points between sampled rows.
تمرين بالحاسبة: أعد حساب أحد الصفوف يدويا، ثم تحقق منه بالمؤشر. يوضح الرسم العالمية، وتثبتها هوية المربع غير السالب لكل المجال الممكن، بما فيه القيم الواقعة بين صفوف الجدول.
import numpy as np
from scipy.optimize import minimize
def cost(x):
return .2*x[0]**2+20*x[0]+.2*x[1]**2+35*x[1]
r = minimize(cost, [50., 50.], method='SLSQP',
bounds=[(0, 80), (0, 80)],
constraints=[{'type':'eq', 'fun':lambda x: sum(x)-100}],
options={'ftol':1e-10})
if not r.success:
raise RuntimeError(r.message)
assert np.allclose(r.x, [68.75, 31.25], atol=1e-3)
assert abs(sum(r.x)-100) < 1e-6
assert np.all(r.x >= -1e-6) and np.all(r.x <= 80+1e-6)
print(np.round(r.x, 4), round(cost(r.x), 4))Read the codeشرح الكود
cost implements the original two-variable objective, not the reduced hand formula.
ينفذ cost الهدف الأصلي بمتغيرين، وليس الصيغة المختزلة للحل اليدوي.
The equality callback must return zero; bounds enforce both generator capacities.
يجب أن يعيد تابع المساواة صفرا، وتطبق bounds سعات المولدين.
SLSQP is a numerical optimization algorithm. Globality here follows from convexity and the hand proof, not merely from success.
SLSQP خوارزمية أمثلية عددية؛ والعالمية هنا ناتجة عن التحدب والبرهان اليدوي، وليست مستنتجة من success وحدها.
Unit 16 solves this same QP with the independent OSQP and CLARABEL solvers through CVXPY. Compare numbers only for the same model: LP and QP above have different cost functions.
تحل الوحدة 16 مسألة QP نفسها بالمحللين المستقلين OSQP وCLARABEL عبر CVXPY. نقارن النتائج للنموذج نفسه؛ فهدفا LP وQP السابقان مختلفان.
Final check — prove convexity of this QP and global optimalityالتحقق النهائي — إثبات تحدب المسألة التربيعية وعالمية الحل
We now apply the derivative and Hessian tests learned earlier to the same cost function used in this example. This is a mathematical proof independent of the numerical solver status.
نطبق الآن اختباري المشتق الثاني ومصفوفة هيس اللذين شرحناهما سابقا على تابع الكلفة نفسه في هذا المثال. هذا إثبات رياضي لا يعتمد على رسالة نجاح المحلل العددي.
Step 1 — Write the original objective in the two generator powers. Each quadratic coefficient is 0.2; the linear coefficients are 20 and 35.
الخطوة 1 — نكتب تابع الكلفة الأصلي باستطاعتي المولدين. معامل كل حد تربيعي يساوي 0.2، ومعاملا الحدين الخطيين هما 20 و35.
Step 2 — Take first partial derivatives. When differentiating with respect to one generator power, treat the other power as constant. The derivative of 0.2P² is 0.4P, while the derivative of 20P or 35P is its constant coefficient.
الخطوة 2 — نحسب المشتقات الجزئية الأولى. عند الاشتقاق بالنسبة إلى استطاعة أحد المولدين نعامل استطاعة المولد الآخر كثابت. مشتق الحد التربيعي ذي المعامل 0.2 هو 0.4 مضروبا في الاستطاعة، ومشتق الحد الخطي يساوي معامله الثابت.
Step 3 — Differentiate each first derivative once more. The own second derivatives equal 0.4; the mixed derivatives equal zero because the objective contains no product of the two power variables.
الخطوة 3 — نشتق كل مشتق أول مرة ثانية. المشتقان الثانيان بالنسبة إلى المتغير نفسه يساويان 0.4، والمشتقان المختلطان يساويان صفرا لأن تابع الكلفة لا يحتوي على جداء استطاعتي المولدين.
Step 4 — Compute the eigenvalues using the determinant. Because this Hessian is diagonal, they are also its diagonal entries. Both eigenvalues are strictly positive.
الخطوة 4 — نحسب القيم الذاتية باستخدام المحدد. وبما أن مصفوفة هيس قطرية، فإن القيمتين الذاتيتين تساويان عنصري القطر الرئيسي. كلتاهما موجبة تماما.
Step 5 — A positive-definite Hessian means the original cost is strictly convex. The demand equality and generator limits are affine constraints, so their feasible region is convex. Therefore the computed feasible point is the unique global minimum of this QP.
الخطوة 5 — بما أن مصفوفة هيس موجبة التعريف، فإن تابع الكلفة محدب تماما. وقيد توازن الطلب وحدود استطاعات المولدين قيود تآلفية، ولذلك تكون منطقة الحلول المسموحة محدبة. وبناء على ذلك، فإن نقطة الحل المسموحة التي حسبناها هي الحل الأمثل العالمي الوحيد لهذه المسألة.
An equivalent one-variable check follows from P₂=100−P₁: after substitution, the coefficient of P₁² is 0.4, so the second derivative of the reduced objective is 0.8>0 throughout the feasible interval 20≤P₁≤80. Both tests therefore agree.
يمكن إجراء تحقق مكافئ بمتغير واحد بتعويض استطاعة المولد الثاني من علاقة توازن الطلب. بعد التعويض يصبح معامل مربع استطاعة المولد الأول 0.4، فيكون المشتق الثاني للهدف المختزل مساويا 0.8، وهو موجب على المجال المسموح من 20 إلى 80 ميغاواط. وهكذا يتفق الاختباران.
Quadratically Constrained Quadratic Programming (QCQP) — inverter capability
البرمجة التربيعية ذات القيود التربيعية — حدود العاكس
In QCQP, at least one constraint is quadratic. Here an inverter has a nominal apparent-power limit of 5 kVA, must supply 3 kvar of reactive power, and should maximize its nonnegative active power.
في البرمجة التربيعية ذات القيود التربيعية يكون أحد القيود على الأقل تربيعيا. عاكس استطاعته الظاهرية الاسمية 5 kVA، ويجب أن يقدم استطاعة ردية 3 kvar. نبحث عن أكبر استطاعة فعالة غير سالبة.
Model and unitsالنموذج والوحدات
Solve by handالحل اليدوي خطوة بخطوة
Insert Q=3 in the inverter capacity constraint.
نعوض عن الاستطاعة الردية Q=3 في قيد استطاعة العاكس.
P² + 3² ≤ 5²Subtract 9 from both sides.
نطرح 9 من طرفي المتباينة.
P² ≤ 25 − 9 = 16Because active power is nonnegative, the largest feasible P is 4 kW.
لأن الاستطاعة الفعالة غير سالبة، فإن أكبر قيمة مسموحة هي 4 kW.
P* = √16 = 4 kW
The nonlinear quadratic inequality defines the inverter capability. This example is a convex QCQP; not every QCQP is convex. A lower bound P²+Q²≥25 would generally define a nonconvex region.
المتباينة التربيعية غير الخطية تعبر عن حدود العاكس. هذا المثال QCQP محدبة، لكن ليست كل QCQP محدبة؛ إذ إن القيد المعاكس P²+Q²≥25 يحدد عموما منطقة غير محدبة.
from math import sqrt
S, Q = 5.0, 3.0
P_max = sqrt(S**2 - Q**2)
assert abs(P_max - 4.0) < 1e-12
print("Maximum active power:", P_max, "kW")
print("Power check:", P_max**2 + Q**2, "<=", S**2)Nonlinear Programming (NLP) — local and global optima
البرمجة غير الخطية — الحل المحلي والعالمي
Illustrative normalized operating penalty: an energy device has two preferred power levels, 2 and 6 kW. This fourth-degree toy objective is not a measured equipment cost curve.
مثال توضيحي لدالة جزاء تشغيلية دون وحدات: لجهاز طاقي مستويان مفضلان للاستطاعة، هما 2 kW و6 kW. دالة الهدف من الدرجة الرابعة مصممة للتعليم، وليست منحنى كلفة مقاسا لجهاز حقيقي.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
Every squared factor is nonnegative; therefore for all .
كل عامل مربع غير سالب؛ لذلك لكل .
At : ; at : . Both are feasible, hence both are global minima.
عند نحصل على ، وعند نحصل على . النقطتان ممكنتان؛ لذلك كلتاهما أدنى عالمي.
Differentiate the fourth-degree objective — every stepاشتقاق تابع الهدف من الدرجة الرابعة — جميع الخطوات
Before searching for stationary points, let us derive the first derivative by hand. We start from the product of two squared expressions. The variable is the active power P; the numbers 2 and 6 are constants.
قبل البحث عن النقاط الساكنة، نشتق تابع الهدف يدويا. نبدأ بجداء تعبيرين مربعين. المتغير هو الاستطاعة الفعالة P، أما العددان 2 و6 فهما ثابتان.
Step 1 — Name the two factors A(P)=(P−2)² and B(P)=(P−6)². For a product, differentiate the first factor and keep the second, then keep the first and differentiate the second.
الخطوة 1 — نسمي العامل الأول A(P)=(P−2)² والثاني B(P)=(P−6)². في اشتقاق الجداء نشتق العامل الأول ونبقي الثاني، ثم نبقي العامل الأول ونشتق الثاني، وبعد ذلك نجمع.
Step 2 — Differentiate each squared factor using the chain rule. The derivative of (P−a)² is 2(P−a) because the derivative of the inner expression P−a equals 1.
الخطوة 2 — نشتق كل عامل مربع باستخدام قاعدة السلسلة. مشتق مربع الفرق يساوي ضعفي الفرق نفسه، لأن مشتق التعبير الداخلي هو 1.
Step 3 — Substitute these two derivatives into the product rule. At this stage we have two terms; do not multiply all brackets yet.
الخطوة 3 — نعوض المشتقين في قاعدة اشتقاق الجداء. نحصل هنا على حدين، ولا داعي لنشر جميع الأقواس.
Step 4 — Both terms contain the common factor 2(P−2)(P−6). Take it outside the brackets; one (P−6) remains in the first term, and one (P−2) remains in the second.
الخطوة 4 — يحتوي الحدان على العامل المشترك 2(P−2)(P−6). نخرجه خارج القوس، فيبقى عامل (P−6) من الحد الأول وعامل (P−2) من الحد الثاني.
Step 5 — Simplify the sum inside the brackets: (P−6)+(P−2)=2P−8=2(P−4). Multiply the outside factor 2 by this new factor 2 to obtain 4.
الخطوة 5 — نبسط مجموع التعبيرين داخل القوس: (P−6)+(P−2)=2P−8=2(P−4). نضرب العامل الخارجي 2 بالعامل الجديد 2 فنحصل على 4.
Now set F′(P)=0. A product equals zero if at least one factor is zero, which gives P=2, P=4 or P=6. These are stationary points, not automatically all minima; we must compare their objective values and the interval endpoints, as in the next step.
نساوي الآن المشتق الأول بالصفر. يساوي الجداء صفرا إذا كان أحد عوامله صفرا على الأقل، فنحصل على النقاط الساكنة P=2 أو P=4 أو P=6. ليست جميع النقاط الساكنة نقاط حد أدنى بالضرورة؛ لذلك نقارن قيم تابع الهدف عندها وعند طرفي المجال كما في الخطوة التالية.
For a stationary-point check, gives ,4,6.
لفحص النقاط الساكنة نكتب ، فنحصل على و4 و6.
Evaluate candidates and endpoints: ; ; ; ; . is stationary but not a minimum.
نحسب عند المرشحين والطرفين: و و و و. النقطة ساكنة وليست دنيا.
The midpoint of the two minima has , demonstrating nonconvexity. Different returned values can have the same optimal objective.
منتصف نقطتي الأدنى يعطي ، وهذا يبين عدم التحدب. قد يعيد محللان قيمتين مختلفتين لـ مع قيمة هدف مثلى واحدة.
Two global optima in the nonlinear exampleحلان أمثلان عالميان في المثال غير الخطي
Axes: horizontal device power (kW); vertical normalized penalty F.
المحور الأفقي: استطاعة الجهاز بالكيلوواط. المحور الرأسي: قيمة دالة الجزاء دون وحدات.
Both green points are global: the penalty is a product of squares, so it can never fall below zero. A nonconvex problem may have more than one global optimizer. A zero derivative alone does not establish a minimum, as the red point demonstrates.
النقطتان الخضراوان عالميتان؛ فالهدف جداء مربعين ولا يمكن أن يقل عن الصفر. قد تملك المسألة غير المحدبة أكثر من حل أمثل عالمي. ولا يكفي انعدام المشتقة لإثبات وجود أدنى، كما توضح النقطة الحمراء.
Compare a local minimum with the global minimumمقارنة الأدنى المحلي بالأدنى العالمي
Axes: horizontal normalized decision ; vertical penalty G().
المحور الأفقي: قرار التشغيل المطبع دون وحدات. المحور الرأسي: قيمة دالة الجزاء.
Local minimum: no nearby feasible point is cheaper. Global minimum: no point anywhere in the feasible set is cheaper. This additional polynomial is only a teaching illustration for a dimensionless normalized operating decision, not a measured equipment cost curve.
الأدنى المحلي: لا توجد نقطة ممكنة مجاورة أقل كلفة. الأدنى العالمي: لا توجد أي نقطة في المجموعة الممكنة كلها أقل كلفة. هذا كثير حدود إضافي للتوضيح التعليمي، بقرار تشغيل مطبع دون وحدات، وليس منحنى كلفة مقاسا لجهاز حقيقي.
Differentiate and factor: . Thus stationary candidates are 1, 3 and 6.
نشتق ونحلل: . إذن القيم الساكنة المرشحة هي 1, 3, 6.
Also include the feasible interval endpoints 0 and 7. A continuous differentiable function on this closed interval attains a minimum at an endpoint or an interior stationary point.
نضيف طرفي المجال الممكن 0 و7. تبلغ الدالة المتصلة القابلة للاشتقاق أدناها على هذا المجال المغلق عند أحد الطرفين أو عند نقطة ساكنة داخلية.
Evaluate all five candidates with a calculator, as shown in the table. For : .
نحسب قيم المرشحين الخمسة بالحاسبة كما في الجدول. عند نحصل على .
The derivative changes from negative to positive at and : both are local minima. Comparing every candidate proves that only attains the global minimum 12 on [0,7].
تتغير إشارة المشتقة من سالبة إلى موجبة عند و، فتكون النقطتان أدنيين محليين. وتثبت مقارنة جميع المرشحين أن وحدها تحقق الأدنى العالمي 12 على المجال [0,7].
| G() | Interpretationالتفسير | |
|---|---|---|
| 0 | 30.000000 | Left endpointالطرف الأيسر |
| 1 | 22.416667 | Local minimum, not globalأدنى محلي وليس عالميا |
| 3 | 27.750000 | Local maximumأعلى محلي |
| 6 | 12.000000 | Global minimum (also local)أدنى عالمي، وهو محلي أيضا |
| 7 | 22.416667 | Right endpointالطرف الأيمن |
A local algorithm starting near may stop at the higher local minimum. A plot or several starting guesses alone cannot certify a general global optimum; this example is certified by checking the complete candidate set.
قد تتوقف خوارزمية محلية تبدأ قرب عند الأدنى المحلي الأعلى كلفة. لا يكفي الرسم أو تعدد التخمينات الابتدائية وحدهما لإثبات العالمية في المسألة العامة؛ أما هنا فيوفر فحص جميع المرشحين برهانا كاملا.
from scipy.optimize import minimize
def f(x):
return (x[0]-2)**2*(x[0]-6)**2
for start in [1., 7.]:
r = minimize(f, [start], bounds=[(0, 8)], method='SLSQP',
options={'ftol':1e-12})
if not r.success:
raise RuntimeError(r.message)
assert 0 <= r.x[0] <= 8 and r.fun < 1e-6
print('start=', start, 'P=', round(r.x[0], 4), 'F=', round(r.fun, 6))Read the codeشرح الكود
Run the same model from two initial guesses. The solutions should approach 2 and 6 respectively.
نشغل النموذج من تخمينين ابتدائيين؛ يفترض أن يقترب الحلان من 2 و6 على الترتيب.
Multiple starts alone do not certify a global optimum in general. Here supplies the certificate.
لا تكفي عدة بدايات لإثبات العالمية عموما. في هذا المثال يوفر البرهان.
Integer Programming (IP) and Binary Integer Programming (BIP) — equipment selection
البرمجة الصحيحة والثنائية — اختيار التجهيزات
A battery module stores 3 kWh of usable energy and costs 400 CU. Supply at least 8 kWh. Energy is usable DC energy; conversion losses are already excluded from this small sizing exercise.
تخزن وحدة بطارية 3 kWh من الطاقة القابلة للاستخدام وتكلف 400 وحدة نقدية. المطلوب 8 kWh على الأقل. الطاقة هنا طاقة DC صالحة للاستخدام؛ لم نضف خسائر تحويل في هذا المثال المبسط.
Example 1 — Integer Programming: battery-module countالمثال الأول — البرمجة الصحيحة: عدد وحدات البطاريات
For the integer model, the blue line is the continuous cost function. The green shaded region satisfies the energy requirement, but only the isolated integer dots are allowed decisions. Red dots are infeasible; the first green dot is the minimum-cost feasible solution.
في النموذج الصحيح، يمثل الخط الأزرق تابع الكلفة المستمر. تحقق المنطقة المظللة بالأخضر شرط الطاقة، لكن النقاط المنفصلة ذات القيم الصحيحة فقط هي قرارات مسموحة. النقاط الحمراء غير ممكنة، وأول نقطة خضراء هي الحل المسموح الأقل كلفة.
Example 1 — Integer decisions: hand calculationالمثال الأول — الحل اليدوي للقرارات الصحيحة
Divide the energy requirement by module energy: .
نقسم الطاقة المطلوبة على طاقة الوحدة: .
must be integer, so the smallest feasible value is the ceiling .
يجب أن تكون صحيحة؛ أصغر قيمة ممكنة هي التقريب للأعلى .
Check : energy=6<8, infeasible. Check : energy= and cost= CU.
نفحص : الطاقة 6<8، فهي غير ممكنة. وعند : الطاقة والكلفة وحدة نقدية.
The cost increases with , so all >3 cost more. Rounding upward is valid for this one-variable monotone model, not a general integer-solving method.
الكلفة متزايدة مع ، فتكون كل >3 أغلى. يصلح التقريب للأعلى لهذا النموذج الرتيب بمتغير واحد؛ وليس طريقة عامة لحل البرمجة الصحيحة.
Example 2 — Binary Integer Programming: pack selectionالمثال الثاني — البرمجة الثنائية: اختيار حزم البطاريات
Separate input data: pack A supplies 5 kWh and costs 700 CU; pack B supplies 8 kWh and costs 1000 CU. Each pack is either selected or not selected. Choose the least-cost combination supplying at least 8 kWh.
معطيات مستقلة: توفر الحزمة A طاقة 5 kWh وكلفتها 700 وحدة نقدية، وتوفر الحزمة B طاقة 8 kWh وكلفتها 1000 وحدة نقدية. نقرر اختيار كل حزمة أو عدم اختيارها، ونبحث عن أقل كلفة تحقق طاقة لا تقل عن 8 kWh.
Binary variable domains: each variable can take exactly one of two values. A value of 1 means the corresponding battery pack is selected; 0 means it is not selected. No fractions such as 0.5 are permitted.
مجال تعريف المتغيرات الثنائية: يأخذ كل متغير إحدى قيمتين فقط. تعني القيمة 1 اختيار حزمة البطاريات المقابلة، وتعني القيمة 0 عدم اختيارها. ولا يسمح بقيم كسرية مثل 0.5.
For the binary-pack variant there is no continuous choice curve: only four combinations exist. The second graph plots their total energy against cost. The green region begins at 8 kWh; the cheapest feasible decision is (yA,yB)=(0,1).
في النموذج الثنائي لا يوجد منحنى قرارات مستمر، بل توجد أربع حالات فقط. يوضح الرسم الثاني طاقة كل حالة وكلفتها. تبدأ المنطقة المسموحة عند 8 kWh، وأقل القرارات الممكنة كلفة هو (yA,yB)=(0,1).
Important distinction: the shaded interval is the feasible region of the relaxed continuous model. In integer and binary programming, the actual feasible set consists only of the marked discrete points.
تمييز مهم: تمثل المنطقة المظللة المجال المسموح في النموذج المستمر بعد إلغاء شرط الأعداد الصحيحة. أما في البرمجة الصحيحة والثنائية فتتكون مجموعة الحلول المسموحة الفعلية من النقاط المنفصلة المحددة فقط.
Example 2 — Binary decisions: hand calculationالمثال الثاني — الحل اليدوي للقرارات الثنائية
Binary variant: two candidate packs A=(5 kWh,700 CU), =(8 kWh,1000 CU). Enumerate : energies 0,5,8,13; costs 0,700,1000,1700.
في المثال الثنائي نفترض حزمتين مرشحتين: طاقة الحزمة A هي 5 kWh وكلفتها 700 وحدة نقدية؛ وطاقة الحزمة هي 8 kWh وكلفتها 1000 وحدة نقدية. الحالات الممكنة للقرارين (yA,yB) هي (0,0), (1,0), (0,1), (1,1). تقابلها الطاقات 0, 5, 8, 13 kWh والكلف 0, 700, 1000, 1700 وحدة نقدية، على الترتيب.
For with and binary y, choose (0,1): 8 kWh at 1000 CU.
في النموذج مع ومتغيرين ثنائيين، نختار (0,1): طاقة 8 kWh بكلفة 1000.
from scipy.optimize import milp, Bounds, LinearConstraint
r = milp(c=[400.], integrality=[1], bounds=Bounds([0], [10]),
constraints=LinearConstraint([[3.]], [8.], [float('inf')]))
if not r.success:
raise RuntimeError(r.message)
assert abs(r.x[0]-3) < 1e-6 and 3*r.x[0] >= 8-1e-6
print('modules=', round(r.x[0]), 'cost=', round(r.fun))
candidates = [(700*a+1000*b, a, b) for a in [0,1] for b in [0,1]
if 5*a+8*b >= 8]
print('binary (cost,yA,yB)=', min(candidates))Read the codeشرح الكود
integrality=[1] declares an integer; a binary variable additionally needs bounds 0 and 1. The upper bound 10 is harmless here because larger cannot improve cost.
تحدد integrality=[1] متغيرا صحيحا؛ والثنائي يحتاج أيضا الحدين 0 و1. الحد 10 لا يستبعد الحل الأمثل هنا لأن زيادة ترفع الكلفة.
The comprehension enumerates all four binary choices, retaining only feasible ones.
يعدد التعبير كل الخيارات الثنائية الأربعة، ويحتفظ بالممكنة فقط.
Mixed-Integer Linear Programming (MILP) — generator on/off decisions
البرمجة الخطية المختلطة — تشغيل المولد
One-hour demand is 6 kW. Grid energy costs 3 CU/kWh. A generator costs 1 CU/kWh plus a one-period fixed on-cost of 5 CU, and has capacity 8 kW.
الحمل 6 kW لمدة ساعة. كلفة الشبكة 3 وحدات نقدية/kWh، وكلفة المولد 1 وحدة نقدية/kWh مع كلفة ثابتة للتشغيل في الفترة قدرها 5، وسعة 8 kW.
Network diagram — power directions and mathematical modelمخطط الشبكة — اتجاه الاستطاعات وربطها بالنموذج الرياضي
Both sources inject active power into one common bus: g comes from the grid and p comes from the generator. The bus supplies the 6 kW load, so the arrows express p+g=6. The binary variable u controls whether the generator is allowed to produce power.
تحقن الشبكة استطاعة فعالة g، ويحقن المولد استطاعة فعالة p في عقدة مشتركة تغذي حملا مقداره 6 kW. يوضح اتجاه الأسهم معادلة التوازن p+g=6. ويحدد المتغير الثنائي u إن كان المولد مسموحا له بإنتاج الاستطاعة أم لا.
Graphical solution — compare both binary operating casesرسم الحل — مقارنة حالتي التشغيل الثنائيتين
Case 1: u = 0 — Generator OFF
Case 2: u = 1 — Generator ON
The generator has capacity 8 kW, but produces only 6 kW at the optimum because the load is 6 kW and grid export is not allowed. Comparing 11 and 18 CU proves a saving of 7 CU for this one-period model.
سعة المولد 8 kW، لكنه ينتج 6 kW فقط في الحل الأمثل لأن الحمل مقداره 6 kW ولا يسمح النموذج بتصدير الاستطاعة إلى الشبكة. بمقارنة الكلفتين 11 و18 وحدة نقدية، نجد وفرا مقداره 7 وحدات نقدية.
Cost curve — linear on-case and discrete off-caseمنحنى الكلفة — حالة التشغيل الخطية وحالة الإيقاف المنفصلة
This is a linear cost segment, not a quadratic parabola: unlike Unit 10, increasing generator output always reduces the on-case cost in this example. The entire feasible MILP set is the blue segment for u=1 plus the yellow off-case point.
هذا منحنى كلفة خطي وليس قطعا مكافئا تربيعيا، بخلاف الوحدة العاشرة. في هذا المثال يؤدي رفع استطاعة المولد إلى خفض الكلفة في حالة التشغيل. تتكون مجموعة الحلول الممكنة للنموذج المختلط من القطعة الزرقاء عندما يعمل المولد ومن النقطة الصفراء عندما يكون متوقفا.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
Case : , hence . Balance gives . Cost = CU.
الحالة : القيد يفرض ، ومن التوازن . الكلفة .
Case : and because grid import cannot be negative.
الحالة : والمجال لأن الاستجرار من الشبكة غير سالب.
Substitute: . It decreases with p, so choose , .
نعوض . التابع متناقص؛ فنختار و.
On-cost total = CU. Compare 11<18: is best; saving=7 CU.
كلفة التشغيل . بالمقارنة 11<18 نختار ، والتوفير 7.
Check p≤8u: ; balance ; is binary. This complete enumeration proves optimality for the two cases.
نفحص والتوازن ، و ثنائية. تعداد الحالتين بالكامل يثبت الأمثلية لهذا المثال.
import numpy as np
from scipy.optimize import milp, Bounds, LinearConstraint
# Variable order: [p, g, u]
A = [[1,1,0], [1,0,-8]]
rules = LinearConstraint(A, [6,-np.inf], [6,0])
r = milp(c=[1,3,5], integrality=[0,0,1],
bounds=Bounds([0,0,0], [8,np.inf,1]), constraints=rules)
if not r.success:
raise RuntimeError(r.message)
p,g,u = r.x
assert abs(p+g-6) < 1e-6 and p <= 8*u+1e-6
assert min(p,g,u) >= -1e-6 and abs(u-round(u)) < 1e-6
assert abs(r.fun-11) < 1e-6
print('p,g,u=', np.round(r.x,4), 'C=', round(r.fun,4))Read the codeشرح الكود
The first matrix row encodes balance; the second encodes . Lower −infinity leaves this inequality without an artificial lower bound.
يمثل الصف الأول التوازن، والثاني . نستخدم لانهاية لتجنب فرض حد أدنى مصطنع على هذا القيد.
integrality=[0,0,1] leaves p and g continuous and makes integer; its [0,1] bounds make it binary.
تبقي integrality=[0,0,1] الاستطاعتين مستمرتين وتجعل صحيحة؛ وحدود [0,1] تجعلها ثنائية.
This fixed cost is charged for the period, not a temporal startup-transition model; real unit commitment adds transition and minimum up/down-time constraints.
هذه كلفة ثابتة للفترة، وليست نموذجا زمنيا لانتقال الإقلاع؛ الجدولة الواقعية تضيف قيود الانتقال وأزمنة التشغيل والتوقف الدنيا.
Mixed-Integer Quadratic Programming (MIQP) — on/off plus quadratic costs
البرمجة التربيعية المختلطة — التشغيل والكلفة
Use the previous 6 kW demand and 8 kW generator, but quadratic generator cost and fixed cost 2 CU. Grid cost remains 3 CU/kWh for one hour.
نستخدم الحمل 6 kW وسعة المولد 8 kW، لكن كلفة المولد والكلفة الثابتة 2. تبقى كلفة الشبكة 3 وحدات نقدية/kWh لمدة ساعة.
Network diagram and power directions — MIQP modelمخطط الشبكة واتجاه الاستطاعات — النموذج التربيعي المختلط
The network topology is the same as Unit 09: grid power g and generator power p enter a common bus and supply the 6 kW load. What changes is the objective: generator cost is quadratic in p, so it may be cheaper to share the load between the two sources.
بنية الشبكة هي نفسها في الوحدة التاسعة: تدخل استطاعة الشبكة g واستطاعة المولد p إلى عقدة مشتركة تغذي حملا مقداره 6 kW. الاختلاف هنا في تابع الهدف؛ لأن كلفة المولد تربيعية، فقد يكون توزيع الحمل بين المصدرين أقل كلفة من الاعتماد على مصدر واحد.
Case 1 — Generator OFF / المولد متوقف
Case 2 — Generator ON / المولد يعمل
Cost curve and optimal operating pointمنحنى الكلفة ونقطة التشغيل المثلى
The feasible active-power operating range with the generator on is 0≤p≤6 kW and g=6−p. Convexity of this quadratic on-case means its local minimum at p=3 is global within that case. Comparing it with the off-case cost shows the full MIQP optimum is u=1, p=g=3.
عندما يعمل المولد يكون المجال المسموح لاستطاعته 0≤p≤6 kW، وتحدد استطاعة الشبكة بالعلاقة g=6−p. تحدب تابع الكلفة التربيعي يجعل الحل الأدنى عند p=3 عالميا في حالة التشغيل. وبمقارنته بكلفة الإيقاف نحصل على الحل الأمثل للنموذج المختلط: u=1 وp=g=3 kW.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
forces , , =18.
عند يكون و والكلفة 18.
: substitute to get on .
عند نعوض فنحصل على ضمن .
Complete the square: . Thus , is the on-case minimum.
نكمل المربع ؛ إذن و يحققان أدنى كلفة في حالة التشغيل.
Check =0.5×9+3×3+2=4.5+9+2=15.5 CU. Endpoints give and .
نتحقق من الكلفة . ويعطي الطرفان و.
Compare cases: 15.5<18; choose , .
نقارن الحالتين: 15.5<18؛ نختار و.
from scipy.optimize import minimize_scalar
answers = [(18., 0., 6., 0)]
r = minimize_scalar(lambda p: .5*p*p+3*(6-p)+2,
bounds=(0,6), method='bounded')
if not r.success:
raise RuntimeError(r.message)
answers.append((r.fun, r.x, 6-r.x, 1))
best = min(answers)
assert abs(best[0]-15.5) < 1e-5
print('(C,p,g,u)=', tuple(round(v,4) for v in best))Read the codeشرح الكود
We enumerate the binary variable and solve the continuous convex on-case. This is an exact decomposition for this tiny model, not a general MIQP solver.
نعدد المتغير الثنائي ونحل مسألة التشغيل المستمرة المحدبة. هذا تفكيك تام لهذا النموذج الصغير، وليس محللا عاما لـMIQP.
minimize_scalar solves only the one-dimensional continuous subproblem; answers keeps both cases for comparison.
تحل minimize_scalar المسألة الجزئية المستمرة أحادية المتغير فقط؛ وتحتفظ answers بالحالتين للمقارنة.
Mixed-Integer Nonlinear Programming (MINLP) — a nonlinear mixed decision
البرمجة غير الخطية المختلطة — قرار مختلط
For a one-hour toy dispatch, demand is 4 kW, grid price 4 CU/kWh, generator cost and period fixed cost 2 CU. The cubic coefficient has units CU/ for this period.
في مثال توزيع مبسط لمدة ساعة، الطلب 4 kW وسعر الشبكة 4 وحدات نقدية/kWh وكلفة المولد مع كلفة ثابتة 2. وحدة المعامل التكعيبي هي وحدة نقدية/ لهذه الفترة.
Power-flow network and cubic cost — MINLPالشبكة واتجاه الاستطاعات ومنحنى الكلفة التكعيبي — البرمجة غير الخطية المختلطة
The grid and generator feed the 4 kW load through a common bus. The binary variable u switches the generator off or on. Arrows indicate the direction of supplied active power.
تغذي الشبكة والمولد حملا مقداره 4 kW عبر عقدة مشتركة. يحدد المتغير الثنائي u تشغيل المولد أو إيقافه، وتبين الأسهم اتجاه تدفق الاستطاعة الفعلية.
Case 1 — Generator OFF / المولد متوقف
Case 2 — Optimal dispatch / الحل الأمثل
Cost curve and global comparisonمنحنى الكلفة والمقارنة لتحديد الحل الأفضل
When the generator is on, substitute g=4−p to obtain C(p)=p³−4p+18 for 0≤p≤4. The green point is the minimum of this on-case curve. The yellow point is the separate off-case (u=0, p=0, C=16). Compare both: 14.921 < 16, so the on-case minimum is globally optimal across the two binary cases.
عند تشغيل المولد نعوض g = 4 − p فنحصل على تابع الكلفة C(p) = p³ − 4p + 18 ضمن المجال 0 ≤ p ≤ 4. تمثل النقطة الخضراء أصغر كلفة في حالة التشغيل، بينما تمثل النقطة الصفراء حالة الإيقاف المستقلة عند u = 0 وكلفة 16. بما أن 14.921 < 16 فإن الحل الموضح هو الأفضل بين الحالتين.
Full feasible range / المجال الكامل
Zoom near optimum / تكبير قرب الحل الأمثل
The left plot includes the complete feasible interval, while the right plot enlarges the lower-cost portion to show the minimum clearly. The cubic curve is not a parabola; on this nonnegative interval its second derivative is 6p≥0.
يعرض الرسم الأول مجال الحل الكامل، ويكبر الرسم الثاني منطقة الكلفة المنخفضة لإظهار نقطة الحد الأدنى بوضوح. المنحنى تكعيبي وليس قطعا مكافئا، ومشتقته الثانية 6p ≥ 0 على المجال غير السالب.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
: .
عند : و والكلفة 16.
: ; .
عند : ؛ والكلفة ضمن .
Differentiate: . Thus and . The negative root is infeasible.
نشتق . إذن ، والجذر الممكن . الجذر السالب خارج المجال.
. Compute CU.
نحسب . ثم .
The derivative changes from negative to positive at this root. Endpoints and are larger. Compare on-case 14.920799 with off-case 16: operating the generator is cheaper.
تتغير إشارة المشتقة من سالبة إلى موجبة عند هذا الجذر. كلفتا الطرفين و أكبر. نقارن كلفة التشغيل 14.920799 بكلفة الإيقاف 16؛ لذا يكون تشغيل المولد أقل كلفة.
from scipy.optimize import minimize_scalar
r = minimize_scalar(lambda p:p**3+4*(4-p)+2,
bounds=(0,4), method='bounded')
if not r.success:
raise RuntimeError(r.message)
best = min([(16.,0.,4.,0), (r.fun,r.x,4-r.x,1)])
assert abs(best[0]-14.9207985643) < 1e-5
print('(C,p,g,u)=', tuple(round(v,6) for v in best))Read the codeشرح الكود
Enumeration plus a one-variable solve is complete here because both binary states are covered and the on-case is convex on .
التعداد مع حل متغير واحد تام هنا لأننا غطينا الحالتين، ومسألة التشغيل محدبة على .
A general nonconvex MINLP needs more than this small enumeration. A local NLP result in each branch does not generally prove global optimality.
تحتاج MINLP العامة غير المحدبة إلى أكثر من هذا التعداد الصغير؛ والحل المحلي لكل فرع لا يثبت العالمية عموما.
Dynamic MINLP — A-R-OPF with wind power stations and battery storage
البرمجة الديناميكية غير الخطية المختلطة — A-R-OPF مع محطات الرياح وبطاريات التخزين
Dynamic MINLP — Dynamic Mixed-Integer Nonlinear Programming combines several time periods, nonlinear equations and integer or binary decisions. The time coupling comes from storage state equations. Unlike Dynamic Programming (DP) in Unit 13, this describes the optimization model, not a particular solution algorithm.
Dynamic MINLP — البرمجة غير الخطية المختلطة متعددة الفترات والمقترنة زمنيا. يجمع النموذج فترات زمنية متتابعة، وعلاقات غير خطية، وقرارات صحيحة أو ثنائية. ينشأ الاقتران الزمني من معادلات حالة تخزين الطاقة. وهذا وصف لنوع النموذج وليس خوارزمية البرمجة الديناميكية DP التي تدرس في الوحدة 13.
As an energy-systems application, consider A-R-OPF (Active-Reactive Optimal Power Flow), incorporating wind power stations and Battery Storage Systems (BSS). An AC-OPF model includes nonlinear active/reactive power-flow equations. With discrete operating decisions, its multi-period extension is a Dynamic MINLP. Without integer or binary decisions, the same continuous formulation is a dynamic nonlinear program, not MINLP.
نأخذ تطبيقا على A-R-OPF أي التدفق الأمثل للاستطاعتين الفعالة والردية، مع محطات توليد الكهرباء من الرياح وأنظمة تخزين البطاريات BSS. تحتوي معادلات تدفق الاستطاعة المتناوبة على علاقات غير خطية، وتصبح المسألة Dynamic MINLP عند إضافة قرارات تشغيل منفصلة عبر الزمن. أما إذا كانت جميع القرارات مستمرة، فتصنف المسألة بوصفها برمجة غير خطية ديناميكية، وليست MINLP.
Research context: A-R-OPF was developed for coordinated active/reactive dispatch with wind power stations and battery storage. A later flexible battery-operation extension explicitly leads to a MINLP when charging/discharging period lengths become discrete decisions. This lesson uses a simplified two-period example to illustrate the structure, not reproduce the full published model.
السياق البحثي: طورت طريقة A-R-OPF لتنسيق الاستطاعتين الفعالة والردية مع محطات الرياح والتخزين بالبطاريات. وأدت توسعة التشغيل المرن للبطاريات إلى نموذج MINLP عند اعتبار أطوال فترات الشحن والتفريغ قرارات منفصلة. يستخدم هذا الدرس مثالا مبسطا لفترتين لتوضيح البنية، ولا يعيد إنتاج النموذج البحثي الكامل.
Three ingredients in the modelثلاثة مكونات أساسية في النموذج
Compact nonlinear network model: instead of expanding the AC active/reactive power-flow equations, collect the system equations and operating limits into two functions that explicitly depend on time:
الصياغة المضغوطة لنموذج الشبكة غير الخطي: بدلا من كتابة معادلات جريان الاستطاعة الفعالة والردية بالتفصيل، نجمع معادلات النظام وحدود التشغيل في تابعين مع إظهار الاعتماد على الزمن صراحة:
Symbols: x is the state-variable vector (such as bus-voltage magnitudes and phase angles); u is the control/decision-variable vector (such as controllable active/reactive injections and battery operating decisions); t is the time period. f(x,u,t)=0 collects equality equations, including active/reactive power balances; g(x,u,t)≤0 collects inequality limits, including voltage, current and equipment ratings. Fixed network parameters are implicit in these functions. Their compact form does not make the underlying equations linear.
تعريف الرموز: x متجه متغيرات الحالة، مثل مقادير جهود العقد وزواياها؛ وu متجه متغيرات التحكم والقرار، مثل الاستطاعات الفعالة والردية القابلة للتحكم وقرارات تشغيل البطارية؛ وt الفترة الزمنية. يجمع f(x,u,t)=0 معادلات المساواة، ومنها توازن الاستطاعتين الفعالة والردية، بينما يجمع g(x,u,t)≤0 قيود عدم المساواة، ومنها حدود الجهد والتيار والقيم الاسمية للتجهيزات. تعد معاملات الشبكة الثابتة مضمنة ضمن التابعين. ولا تعني هذه الكتابة المختصرة أن المعادلات أصبحت خطية.
Time coupling: battery energy in the next period depends on charging and discharging in the current period. For a one-hour time step, energy is in kWh and powers are in kW.
الاقتران الزمني: تعتمد طاقة البطارية في الفترة التالية على شحنها وتفريغها في الفترة الحالية. عند خطوة زمنية مقدارها ساعة واحدة تكون الطاقة بوحدة kWh والاستطاعات بوحدة kW.
Eₜ₊₁ = Eₜ + ηc Pch,ₜ Δt − (Pdch,ₜ / ηd) ΔtBinary decisions: uₜ=1 selects charging and uₜ=0 permits discharging; both cannot occur at the same time.
القرارات الثنائية: تشير uₜ=1 إلى نمط الشحن، وتسمح uₜ=0 بنمط التفريغ، لمنع الشحن والتفريغ المتزامنين.
0 ≤ Pch,ₜ ≤ P̅ch uₜ ; 0 ≤ Pdch,ₜ ≤ P̅dch (1−uₜ) ; uₜ ∈ {0,1}
Important — the distinguishing feature of A-R-OPF: Coordinated control of reactive power, alongside active power, is a central distinguishing feature of this approach. To keep the hand calculation accessible, the following simplified example illustrates only active-power control through battery charging and discharging; it does not model reactive-power control or represent the full A-R-OPF formulation.
ملاحظة مهمة — ما يميز طريقة A-R-OPF: يعد التحكم المنسق بالاستطاعة الردية إلى جانب الاستطاعة الفعالة من أهم ما يميز هذه الطريقة. ولتسهيل الحسابات اليدوية على الطلاب، يوضح المثال المبسط التالي التحكم بالاستطاعة الفعالة فقط من خلال شحن البطاريات وتفريغها، دون نمذجة التحكم بالاستطاعة الردية أو تمثيل نموذج A-R-OPF الكامل.
Two-period numerical example — a feasible schedule, not a claimed optimumمثال عددي لفترتين — برنامج تشغيل مسموح وليس حلا أمثل مثبتا
Consider a simplified single-bus balance to teach the storage/time coupling before solving the full AC network. Demand is (6,8) kW, wind generation is (2,0) kW, time step is 1 h, battery initial energy E₁=2 kWh, capacity is 4 kWh, and charging/discharging limits are 2 kW. Take charging efficiency ηc=1 and discharging efficiency ηd=0.9.
نبدأ بميزان استطاعة مبسط لعقدة واحدة لتعليم الاقتران الزمني قبل الانتقال إلى شبكة التيار المتناوب الكاملة. الطلب (6,8) kW، وتوليد الرياح (2,0) kW، والخطوة الزمنية 1 h، وطاقة البطارية الابتدائية E₁=2 kWh وسعتها 4 kWh، وحد الشحن والتفريغ 2 kW. نأخذ كفاءة الشحن ηc=1 وكفاءة التفريغ ηd=0.9.
| Period / الفترة | Wind (kW) | Load (kW) | u | Charge (kW) | Discharge (kW) | Grid (kW) | Battery at end (kWh) |
|---|---|---|---|---|---|---|---|
| 1 | 2 | 6 | 1 | 2 | 0 | 6 | 4 |
| 2 | 0 | 8 | 0 | 0 | 1.8 | 6.2 | 2 |
Two-period network — power directions and battery stateمخطط الشبكة لفترتين — اتجاه الاستطاعات وحالة البطارية
The arrows follow the active-power balance for this simplified single-bus example. In period 1, power enters the battery (charging); in period 2, it leaves the battery (discharging). This illustration does not depict the full reactive-power network.
تتبع الأسهم ميزان الاستطاعة الفعالة في هذا المثال المبسط ذي العقدة الواحدة. تدخل الاستطاعة إلى البطارية خلال الفترة الأولى عند الشحن، وتخرج منها خلال الفترة الثانية عند التفريغ. لا تمثل الرسومات شبكة التحكم بالاستطاعة الردية الكاملة.
Period 1 — Charging / الشحن
Period 2 — Discharging / التفريغ
Illustrative nonlinear cost curve — feasibility onlyمنحنى الكلفة غير الخطية التوضيحي — للتحقق من إمكانية التشغيل فقط
For each one-hour interval, the assumed cost is Jₜ(gₜ)=gₜ+0.01gₜ². The curve shows cost versus purchased grid power, and the two points show the selected dispatch. It is not a global-optimality plot: the battery energy equations couple the two time periods and constrain which pairs of points can be selected.
نفترض أن كلفة كل فترة مدتها ساعة واحدة هي Jₜ(gₜ) = gₜ + 0.01gₜ². يبين المنحنى تغير الكلفة مع استطاعة الشبكة المشتراة، وتحدد النقطتان برنامج التشغيل المختار. ليس هذا رسما يثبت الأمثلية العالمية؛ إذ تقرن معادلات طاقة البطارية الفترتين وتحد من مجموعات نقاط التشغيل الممكنة.
Illustrative cost vs grid power / الكلفة التوضيحية تبعا لاستطاعة الشبكة
Verify each period by handالتحقق اليدوي في كل فترة
Period 1: grid purchase = demand − wind + charge = 6−2+2=6 kW. The battery energy increases from 2 to 4 kWh.
الفترة الأولى: نحسب الاستطاعة المستجرة من الشبكة بطرح توليد الرياح من الطلب ثم إضافة استطاعة شحن البطارية.
تزداد الطاقة المخزنة في البطارية من 2 kWh إلى 4 kWh عند نهاية الفترة.
E₂ = 2 + 1×2×1 = 4 kWhPeriod 2: the battery discharges 1.8 kW; grid purchase is 8−1.8=6.2 kW. The energy decreases by 1.8/0.9=2 kWh.
الفترة الثانية: تفرغ البطارية باستطاعة 1.8 kW، ولذلك نطرح استطاعة التفريغ من الطلب لحساب استطاعة الشبكة.
نحسب انخفاض الطاقة المخزنة بقسمة استطاعة التفريغ على كفاءة التفريغ وضرب الناتج في مدة الفترة.
E₃ = 4 − (1.8/0.9)×1 = 2 kWhThe two decisions satisfy the energy capacity and charging/discharging mode constraints. A simple illustrative nonlinear cost is grid energy plus 0.01 times the square of purchased grid power, summed over both one-hour intervals.
يحقق القراران حدود سعة البطارية وشرط نمط الشحن والتفريغ. ويمكن استخدام تابع كلفة توضيحي غير خطي يساوي مجموع طاقة الشبكة المشتراة مع إضافة 0.01 من مربع استطاعة الشبكة في كل فترة مدتها ساعة.
J = 6+6.2+0.01(6²+6.2²) = 12.9444 (illustrative cost units)
This calculation checks feasibility only. The full A-R-OPF optimization also enforces AC network balances, voltage, current and equipment limits, and searches decision variables to minimize its objective. The illustrative penalty and two-period values here are teaching assumptions, not experimental results attributed to the A-R-OPF research.
هذا الحساب يثبت إمكانية التشغيل فقط، ولا يثبت الحل الأمثل. تتضمن مسألة A-R-OPF الكاملة موازين تدفق الاستطاعة في الشبكة وحدود الجهد والتيار والتجهيزات، ثم تبحث عن متغيرات القرار التي تصغر دالة الهدف. الأرقام وتابع الجزاء هنا افتراضات تعليمية، وليست نتائج تجريبية من أبحاث A-R-OPF.
# Feasibility verification, NOT a MINLP solver
load = [6.0, 8.0]
wind = [2.0, 0.0]
mode = [1, 0] # 1: charge, 0: discharge
charge = [2.0, 0.0]
discharge = [0.0, 1.8]
grid = [6.0, 6.2]
energy = 2.0
for t in range(2):
assert mode[t] in (0, 1)
assert 0 <= charge[t] <= 2*mode[t]
assert 0 <= discharge[t] <= 2*(1-mode[t])
assert abs(grid[t]+wind[t]+discharge[t]
-load[t]-charge[t]) < 1e-10
energy += charge[t] - discharge[t]/0.9
assert 0 <= energy <= 4
print("Period", t+1, "grid =", grid[t], "battery =", energy)
assert abs(energy-2.0) < 1e-10
cost = sum(g+0.01*g*g for g in grid)
assert abs(cost-12.9444) < 1e-10
print("Illustrative cost =", round(cost, 4))Dynamic Programming (DP) — battery scheduling
البرمجة الديناميكية — جدولة البطارية
Two one-hour periods, prices 1 and 4 CU/kWh, load 1 kWh each. Battery states are 0 or 1 kWh, initially and finally empty; ideal efficiency, no export, maximum charging/discharging 1 kW.
فترتان مدة كل منهما ساعة، بسعري 1 و4 وحدات نقدية/kWh وحمل 1 kWh في كل فترة. حالتا البطارية 0 أو1 kWh، وهي فارغة في البداية والنهاية، بكفاءة مثالية ومنع التصدير وحد شحن وتفريغ 1 kW.
Power-flow diagrams for the optimal battery scheduleمخططات تدفق الاستطاعة لبرنامج البطارية الأمثل
The arrows show the direction of active power in each period. Since each interval lasts one hour, 1 kW corresponds to 1 kWh of energy. The first period has the low price of 1 CU/kWh; the second period has the high price of 4 CU/kWh.
توضح الأسهم اتجاه الاستطاعة الفعالة في كل فترة. بما أن مدة الفترة ساعة واحدة، تعادل استطاعة مقدارها 1 kW طاقة مقدارها 1 kWh. سعر الفترة الأولى منخفض، بينما سعر الفترة الثانية مرتفع.
Period 1 — charge / شحن
Period 2 — discharge / تفريغ
Battery-state plot and discrete cost comparisonمنحنى طاقة البطارية ومقارنة الكلفة للقرارات المتقطعة
Optimal battery energy trajectory / مسار طاقة البطارية الأمثل
Two feasible schedules / مقارَنة برنامجي التشغيل
No storage gives total cost 5 CU. Charge then discharge gives cost 2 CU and saves 3 CU. Because the model permits only two intermediate states, comparing both establishes the global optimum of this teaching example.
من دون التخزين تبلغ الكلفة الكلية 5 CU. وعند الشحن ثم التفريغ تنخفض إلى 2 CU، فيكون التوفير 3 CU. ولأن النموذج يسمح بحالتين فقط لطاقة البطارية بعد الفترة الأولى، فإن المقارنة بينهما تثبت الحل الأمثل لهذا المثال التعليمي.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
The state is stored energy. In the last period , incoming gives and remaining cost .
الحالة هي الطاقة المخزنة. في الفترة الأخيرة ، إذا كانت فاستجرار الشبكة ، والكلفة المتبقية .
If , discharge to : and .
إذا كانت نفرغ إلى ؛ فيكون والكلفة .
Step backward to , . Choose : and total=.
نرجع إلى و. إذا اخترنا فإن والكلفة الكلية .
Choose : , total= CU.
إذا اخترنا فإن والكلفة الكلية .
Select : buy 2 kWh in period 1, buy zero in period 2; saving= CU. Check g₁+g₂=2 equals total load and final energy is zero.
نختار : نشتري 2 kWh أولا، وصفرا ثانيا؛ التوفير . ومجموع الاستجرار 2 يساوي الحمل الكلي والبطارية فارغة في النهاية.
prices = [1., 4.]
states = [0, 1]
J = {0:0., 1:float('inf')} # Terminal empty state
policy = {}
for t in [1, 0]:
current = {}
for old in states:
choices = []
for new in states:
grid = 1+new-old
if grid >= 0 and abs(new-old) <= 1:
choices.append((prices[t]*grid+J[new], new))
value, new = min(choices)
current[old] = value
policy[t, old] = new
J = current
assert J[0] == 2 and policy[0,0] == 1
print('Minimum cost=', J[0], 'CU; charge then discharge')Read the codeشرح الكود
starts with zero terminal cost for empty storage and infinity for a nonempty terminal battery.
نبدأ بكلفة نهائية صفر للبطارية الفارغة، ولانهاية للبطارية غير الفارغة.
The backward loop calculates every allowed transition and adds its immediate cost to future cost [new].
تحسب الحلقة العكسية كل انتقال مسموح وتضيف كلفته الآنية إلى كلفة المستقبل [new].
This discrete-state method is DP. A multiperiod model can instead be formulated as LP or MILP; time indexing alone does not make it DP.
هذه طريقة DP بحالات متقطعة. ويمكن بدلا منها صياغة نموذج متعدد الفترات كـLP أوMILP؛ لا يكفي وجود فهرس زمني لتسميته DP.
Stochastic and robust optimization — uncertain Photovoltaic (PV) generation
التحسين العشوائي والمتين — عدم يقين الشمس
Demand is 4 kWh. PV is 0 or 4 kWh, each with probability 0.5. Before observing PV, buy kWh at 2 CU/kWh; afterward buy any shortfall at 5 CU/kWh. Surplus has zero value and may be curtailed.
الطلب 4 kWh، والإنتاج الشمسي إما 0 أو4 kWh باحتمال 0.5 لكل منهما. نشتري مسبقا بسعر 2؛ وبعد معرفة الشمس نشتري العجز بسعر 5 وحدات نقدية/kWh. يمكن هدر الفائض ولا قيمة له.
All mathematical symbols — meaning and unitsشرح جميع الرموز الرياضية ومعانيها ووحداتها
Read this table before the model and graphs. The subscript s identifies a scenario; L means low photovoltaic generation and H means high photovoltaic generation. The star indicates an optimal decision.
اقرأ الجدول قبل البدء بالنموذج والمنحنيات. يشير الدليل السفلي إلى السيناريو، ويرمز الحرفان إلى حالتي التوليد الشمسي المنخفض والمرتفع. وتدل النجمة على القيمة المثلى لمتغير القرار.
| Symbol / الرمز | Meaning / المعنى | الشرح بالعربية | Unit / الوحدة |
|---|---|---|---|
| Scenario index. | فهرس السيناريو الذي يحدد حالة التوليد الشمسي. | — | |
| Low-PV and high-PV scenarios. | الحالة منخفضة التوليد الشمسي والحالة مرتفعة التوليد الشمسي. | — | |
| Available photovoltaic energy in scenario s: 0 or 4. | الطاقة الشمسية المتاحة في السيناريو المعني، وقيمتها في المثال إما 0 أو 4. | kWh | |
| Fixed demand; D=4 in this example. | طاقة الحمل المطلوبة، وهي ثابتة في المثال وتساوي 4. | kWh | |
| First-stage grid energy purchased before PV is known; 0≤q≤4. | الطاقة المشتراة مسبقا من الشبكة قبل معرفة التوليد الشمسي، وهي متغير القرار الأول. | kWh | |
| Extra energy purchased after observing scenario s to cover any shortfall. | الطاقة الإضافية المشتراة بعد ظهور السيناريو لتغطية العجز، ولا يمكن أن تكون سالبة. | kWh | |
| Total operating cost in scenario s for advance decision q. | الكلفة الإجمالية في سيناريو معين عند اتخاذ قرار شراء مسبق محدد. | CU | |
| Costs of the low-PV and high-PV scenarios, respectively. | كلفة حالة غياب الشمس وكلفة حالة وفرة الشمس، على الترتيب. | CU | |
| Probability of scenario s; probabilities sum to 1. | احتمال حدوث السيناريو، ومجموع احتمالات السيناريوهات يساوي 1. | — | |
| Expected cost: sum of probability-weighted scenario costs. E denotes expectation, not energy. | الكلفة المتوقعة الناتجة عن ضرب كلفة كل سيناريو باحتماله ثم جمع القيم. هنا يرمز E إلى التوقع الرياضي وليس الطاقة. | CU | |
| Upper bound on every scenario cost in the robust linear reformulation. | متغير مساعد يمثل حدا أعلى لكلفة جميع السيناريوهات في الصياغة الخطية المتينة. | CU | |
| Optimal advance-purchase decision. | قيمة الشراء المسبق التي تحقق أقل كلفة وفق معيار التحسين المختار. | kWh | |
| min selects the smallest value; max selects the largest. | ترمز الأولى إلى إيجاد أصغر قيمة، والثانية إلى إيجاد أكبر قيمة أو أسوأ كلفة. | — | |
| Advance and later emergency purchase prices, respectively. | سعر الطاقة المشتراة مسبقا وسعر الطاقة الإضافية بعد ظهور السيناريو، على الترتيب. | CU/kWh | |
| Cost Unit: illustrative monetary unit. | وحدة نقدية افتراضية للمثال التعليمي. | — |
For example, if q=1 kWh and no solar energy is available, the shortfall is r_L=max(4−0−1,0)=3 kWh and the cost is C_L(1)=2×1+5×3=17 CU. This illustrates the difference between first-stage q and scenario-dependent r_s.
مثال توضيحي: إذا اشترينا مسبقا 1 kWh ولم تتوافر الطاقة الشمسية، نحتاج إلى شراء 3 kWh إضافية لتغطية الحمل. تصبح الكلفة 17 CU. وهكذا نميز بين قرار الشراء المسبق والشراء الإضافي الذي يتغير حسب السيناريو.
PV scenarios — network and energy-flow directionsسيناريوهات التوليد الشمسي — مخطط الشبكة واتجاهات الطاقة
The following diagrams use the optimal advance purchase q=4 kWh under the original 0.5/0.5 probabilities and under the robust criterion. They show two possible PV realizations. Since each period is one hour, the illustrated energy quantities also correspond numerically to kW of average active power.
يعرض المخططان قرار الشراء المسبق الأمثل بمقدار 4 kWh عند الاحتمالين الأصليين المتساويين، وكذلك وفق معيار المتانة. يوضح كل مخطط سيناريو مختلفا للتوليد الشمسي. ولأن مدة الفترة ساعة واحدة، تساوي قيم الطاقة عدديا متوسط الاستطاعة الفعالة بوحدة kW خلال تلك الساعة.
Scenario L — No sun / غياب الشمس
Scenario H — High PV / وفرة الشمس
Scenario, robust and expected-cost curvesمنحنيات كلفة السيناريوهات والمتانة والكلفة المتوقعة
Scenario costs and robust envelope / كلف السيناريوهات وأسوأ حالة
Expected cost and probability / الكلفة المتوقعة والاحتمال
The horizontal axis represents the first-stage purchase q from 0 to 4 kWh, not time. The scenario cost functions are C_L=20−3q and C_H=2q. With equal probabilities, expected cost decreases to 8 CU at q=4, and robust worst-case cost also minimizes at q=4. If low-PV probability is only 0.2, the expected-cost minimum instead occurs at q=0, with cost 4 CU.
يمثل المحور الأفقي الطاقة المشتراة مسبقا ضمن المجال من 0 إلى 4 kWh، ولا يمثل الزمن. يتناقص تابع الكلفة المتوقعة عند تساوي احتمالي السيناريوهين حتى تبلغ الكلفة الدنيا 8 CU عند شراء كامل الحمل مسبقا. ويتحقق القرار نفسه لمسألة المتانة. أما عندما يصبح احتمال غياب الشمس 0.2 فتبلغ الكلفة المتوقعة الدنيا 4 CU دون شراء مسبق.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
Low PV: and .
عند غياب الشمس والكلفة .
High PV: and .
عند الشمس العالية والكلفة .
Expected cost: . It decreases, so and expected cost=8 CU.
الكلفة المتوقعة . وهي متناقصة؛ إذن والكلفة المتوقعة 8.
Worst-case cost is . Lines intersect at , so ; worst cost=8. On [0,4], low-PV cost is the larger line and decreases to 8.
كلفة أسوأ حالة . يتقاطع الخطان عند ، أي ؛ وأسوأ كلفة 8. على [0,4] خط الشمس المنخفضة هو الأعلى ويتناقص حتى 8.
Change low-PV probability to 0.2: . The stochastic optimum becomes , expected cost=4; the robust optimum stays .
نغير احتمال غياب الشمس إلى 0.2: الكلفة المتوقعة . يصبح الحل العشوائي وكلفته المتوقعة 4، بينما يبقى الحل المتين .
from scipy.optimize import linprog
for prob in [0.5, 0.2]:
# Variables [q,r_low,r_high]; q+r_low >= 4
r = linprog([2,5*prob,5*(1-prob)],
A_ub=[[-1,-1,0]], b_ub=[-4],
bounds=[(0,4),(0,None),(0,None)], method='highs')
if not r.success:
raise RuntimeError(r.message)
assert abs(r.x[0]-(4 if prob == .5 else 0)) < 1e-6
print('prob=',prob,'q=',round(r.x[0],4),'E[C]=',round(r.fun,4))
# Robust variables [q,z]: z >= 20-3q; z >= 2q
r = linprog([0,1], A_ub=[[-3,-1],[2,-1]], b_ub=[-20,0],
bounds=[(0,4),(0,None)], method='highs')
if not r.success:
raise RuntimeError(r.message)
assert abs(r.x[0]-4) < 1e-6 and abs(r.fun-8) < 1e-6
print('robust q,z=',r.x)Read the codeشرح الكود
is shared across scenarios because it is chosen before observing PV; recourse variables can depend on the observed scenario.
مشتركة بين السيناريوهات لأنها تختار قبل معرفة الشمس؛ ويمكن أن تعتمد متغيرات التعويض على السيناريو المشاهد.
Negative coefficients convert the required inequality to linprog’ convention. bounds every scenario cost from above.
تحول الإشارات السالبة قيد إلى الصيغة المطلوبة في linprog. والمتغير حد أعلى لكلفة كل سيناريو.
These examples remain LP after reformulation. Robust does not mean stochastic with an arbitrarily chosen probability.
يبقى المثالان LP بعد إعادة الصياغة. ولا تعني المتانة مسألة عشوائية باحتمال نختاره اعتباطيا.
Multiobjective optimization — cost and emissions
التحسين متعدد الأهداف — الكلفة والانبعاثات
One-hour demand 10 kW. Generator 1 costs 1 CU/kWh and emits 0.8 kg/kWh; generator 2 costs 3 CU/kWh and emits 0.2 kg/kWh. Both have capacity 10 kW.
طلب لمدة ساعة 10 kW. كلفة المولد الأول 1 وحدة نقدية/kWh وانبعاثه 0.8 kg/kWh؛ والثاني 3 و0.2 على الترتيب. سعة كل مولد 10 kW.
Symbols and physical units — two objectivesالرموز والوحدات الفيزيائية — هدفان مختلفان
| Symbol / الرمز | Meaning / English | المعنى بالعربية | Unit / الوحدة |
|---|---|---|---|
| Power from generator 1, cheaper but more polluting. | استطاعة المولد الأول، الأقل كلفة والأعلى انبعاثا. | kW | |
| Power from generator 2, costlier but cleaner. | استطاعة المولد الثاني، الأعلى كلفة والأقل انبعاثا. | kW | |
| Demand, fixed at 10 kW for one hour. | استطاعة الحمل المطلوبة، وتساوي 10 في المثال. | kW | |
| Total monetary cost of generation over one hour. | الكلفة النقدية الكلية للتوليد خلال ساعة واحدة. | CU | |
| Total emitted mass in the one-hour period; not the expectation operator of Unit 14. | كتلة الانبعاثات الكلية خلال الساعة، ولا تعني هنا مؤثر القيمة المتوقعة في الوحدة 14. | kg | |
| Energy price of generator i: 1 or 3. | سعر طاقة المولد ذي الفهرس المعني، وتبلغ قيمته 1 أو 3. | CU/kWh | |
| Emission factor of generator i: 0.8 or 0.2. | معامل الانبعاث للمولد المعني، وقيمته 0.8 أو 0.2. | kg/kWh | |
| Generator index, i=1 or i=2. | دليل المولد الذي يميز المولد الأول عن الثاني. | — | |
| Upper limit on total emissions, set to 5 in the example. | الحد الأعلى المسموح لكتلة الانبعاثات، ويساوي 5 في المثال. | kg | |
| Carbon price converting an emitted kilogram into monetary cost. | سعر الكربون المستخدم لتحويل كتلة الانبعاثات إلى كلفة نقدية مكافئة. | CU/kg | |
| Weighted scalar objective combining cost and monetized emissions. | تابع هدف مركب يجمع الكلفة الأصلية وكلفة الانبعاثات المحولة نقديا. | CU | |
| Emissions cap in the epsilon-constraint method, such as ε=5. | رمز الحد المفروض على الانبعاثات عند استخدام طريقة قيد إبسيلون. | kg | |
| A feasible solution is Pareto-efficient if no other solution improves one objective without worsening the other. | الحل الكفء وفق باريتو هو حل لا يمكن تحسين أحد هدفيه دون زيادة سوء الهدف الآخر. | — | |
| Illustrative currency units and kilograms of emitted mass. | وحدة نقدية افتراضية وكيلوغرام لكتلة الانبعاث. | — |
In the example, p₁=p₂=5 kW gives C=20 CU and E=5 kg. Multiplying λ [CU/kg] by E [kg] gives CU, so C+λE has consistent units.
في المثال يعطي تشغيل كل مولد باستطاعة 5 kW كلفة 20 CU وانبعاثات 5 kg. يؤدي ضرب سعر الكربون بوحدة نقدية لكل كيلوغرام في كتلة الانبعاثات إلى كلفة نقدية، فتتوافق الوحدات في تابع الهدف المركب.
Power network and competing optimization objectivesمخطط الشبكة وتعارض هدفي الكلفة والانبعاثات
The two generators supply a common 10 kW load. At the optimal cost under the emissions limit of 5 kg, both deliver 5 kW. Generator 1 is cheaper but emits more, whereas generator 2 is cleaner but more expensive.
يغذي المولدان حملا مشتركا مقداره 10 kW. عند أصغر كلفة مع الالتزام بحد الانبعاثات البالغ 5 kg ينتج كل مولد استطاعة مقدارها 5 kW. المولد الأول أرخص وأكثر انبعاثا، بينما الثاني أعلى كلفة وأقل انبعاثا.
Optimal dispatch with emission cap / الحل مع قيد الانبعاثات
Cost and emissions plots — the Pareto trade-offمنحنيات الكلفة والانبعاثات — المفاضلة وفق باريتو
Cost–emissions Pareto frontier / جبهة باريتو للكلفة والانبعاثات
Objectives versus generator 1 / الهدفان تبعا لاستطاعة المولد الأول
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
Set : and . Cost falls while emissions rise.
نعوض فنحصل على و. تنخفض الكلفة بينما ترتفع الانبعاثات.
At : . At : . Neither dominates the other; every point on this segment is Pareto efficient.
عند يكون ، وعند يكون (10,8). لا يتفوق أحدهما في الهدفين معا؛ وكل نقطة على القطعة حل كفء وفق باريتو.
Use an emissions cap : ⇒ .
نفرض حدا للانبعاث : إذن ومنه .
Minimize cost under that cap: maximize within [0,5], giving , and .
لتصغير الكلفة ضمن الحد نختار أكبر ضمن [0,5]؛ فنحصل على والكلفة 20 والانبعاث 5 kg.
A carbon price [CU/kg] gives , with slope . The switch is =2÷0.6≈3.3333 CU/kg; at exactly this price all feasible dispatches tie.
يعطي سعر الكربون بوحدة نقدية/kg الهدف وميله . قيمة التحول ؛ وعندها تتساوى كل التوزيعات الممكنة في الهدف المركب.
from scipy.optimize import linprog
r = linprog([1,3], A_eq=[[1,1]], b_eq=[10],
A_ub=[[.8,.2]], b_ub=[5],
bounds=[(0,10),(0,10)], method='highs')
if not r.success:
raise RuntimeError(r.message)
p1,p2 = r.x
emissions = .8*p1+.2*p2
assert abs(p1+p2-10) < 1e-6 and emissions <= 5+1e-6
print('p=',r.x,'C=',r.fun,'E=',emissions)Read the codeشرح الكود
This is the epsilon-constraint approach: optimize cost while limiting emissions. The resulting subproblem is LP.
هذه طريقة قيد إبسيلون: نصغر الكلفة مع تقييد الانبعاثات، وتكون المسألة الناتجة LP.
Never add currency and kilograms without a conversion coefficient or a clearly defined normalization.
لا نجمع الوحدات النقدية والكيلوغرامات دون معامل تحويل أو تطبيع معرف بوضوح.
Complexity, solver selection and comparison
التعقيد واختيار المحللات ومقارنة النتائج
A model class does not determine an exact running time. Compare mathematical structure, size, precision and optimality guarantees. The code below uses free solver packages and solves the same models already calculated by hand.
لا يحدد نوع النموذج زمن تشغيل دقيقا. نقارن البنية والحجم والدقة وضمانات الأمثلية. يستخدم الكود التالي محللات مجانية لحل النماذج نفسها المحسوبة يدويا.
Convexity: the straight segment between any two feasible points stays feasible; for a convex minimization objective, every local minimum is global. With linear constraints, a QP is convex when its quadratic matrix is positive semidefinite. Convex does not mean linear.
التحدب: تبقى القطعة المستقيمة بين أي نقطتين ممكنتين داخل المجموعة الممكنة؛ ومع هدف تصغير محدب يكون كل أدنى محلي أدنى عالميا. ومع قيود خطية تكون المسألة التربيعية محدبة عندما تكون مصفوفتها التربيعية موجبة شبه محددة. التحدب لا يعني الخطية.
Complexity: polynomial growth, such as or ³, differs from exponential growth such as 2ⁿ. These are operation-count illustrations, not exact bounds for every solver. For LP, theoretical polynomial bounds account for input encoding length. NP-hard means at least as hard as every problem in NP under polynomial reductions; no general polynomial-time exact algorithm is known. It does not mean every instance is slow or unsolvable.
التعقيد: يختلف النمو متعدد الحدود مثل مربع الحجم أو مكعبه عن النمو الأسي مثل 2 مرفوعة لعدد القرارات. هذه أمثلة لعدد العمليات وليست حدودا دقيقة لكل محلل. تراعي الحدود النظرية للبرمجة الخطية طول تمثيل بيانات المسألة. وتعني الصعوبة من فئة NP-hard أن كل مسألة في NP يمكن تحويلها إليها خلال زمن متعدد الحدود؛ ولا تعرف خوارزمية عامة دقيقة متعددة الحدود لحلها. لا يعني ذلك أن كل مثال بطيء أو غير قابل للحل.
Complexity, numerical verification and solver symbolsرموز التعقيد والتحقق العددي والمحللات
| Symbol / الرمز | Meaning / English | المعنى بالعربية | Unit / الوحدة |
|---|---|---|---|
| Number of variables or a generic problem-size measure in the growth examples. | عدد المتغيرات أو مقياس عام لحجم المسألة في أمثلة النمو. | count | |
| Illustrative polynomial operation counts. | مثالان لعدد عمليات ينمو وفق مربع حجم المسألة أو مكعبه. | operations | |
| Illustrative exponential growth in problem size. | مثال على نمو عدد العمليات أسيا مع حجم المسألة. | operations | |
| Number of binary decision variables. | عدد متغيرات القرار الثنائية التي تأخذ 0 أو 1. | count | |
| Number of all possible binary assignments before pruning. | عدد جميع تركيبات المتغيرات الثنائية قبل أي استبعاد ذكي للحالات. | cases | |
| Number of time steps; 24 when one day uses hourly periods. | عدد الفترات الزمنية، ويساوي 24 عند تقسيم اليوم إلى ساعات. | periods | |
| Two continuous dispatch decisions per time period. | عدد متغيرات التشغيل المستمرة عند وجود متغيرين في كل فترة. | variables | |
| Linear, quadratic and quadratically constrained quadratic programming. | البرمجة الخطية والتربيعية والبرمجة التربيعية ذات القيود التربيعية. | — | |
| Mixed-integer linear, quadratic and nonlinear programming. | البرمجة الخطية والتربيعية وغير الخطية المختلطة بمتغيرات صحيحة. | — | |
| Nonlinear Programming and Dynamic Programming. | البرمجة غير الخطية والبرمجة الديناميكية. | — | |
| Complexity classes; NP-hard is a hardness classification, not a runtime formula. | تصنيفات نظرية للتعقيد؛ ولا تعني NP-hard معادلة مباشرة لزمن التنفيذ. | — | |
| Matrix of second partial derivatives used to test convexity. | مصفوفة المشتقات الجزئية الثانية المستخدمة لفحص التحدب. | depends | |
| Numerical solution vector x and power-decision vector p in the QP code. | متجه الحل العددي ومتجه استطاعات المولدات في مثال البرمجة التربيعية. | MW for p | |
| Quadratic optimization objective and the reported total generation cost. | تابع الهدف التربيعي والكلفة الإجمالية المحسوبة للتوليد. | CU/h | |
| Small numerical tolerance used to accept floating-point residuals, such as 10⁻³. | هامش خطأ عددي صغير للتحقق من بواقي المعادلات وحدود المتغيرات. | as checked quantity | |
| Python modeling package and its short import alias. | مكتبة بناء نماذج الأمثلية في بايثون واسمها المختصر في الكود. | — | |
| Solver names used for the convex quadratic example. | اسما محللين عدديين للمثال التربيعي المحدب. | — | |
| Solver status asserting a solution meets its optimality criteria. | حالة يعلنها المحلل عندما يستوفي الحل معايير الأمثلية المعتمدة فيه. | — | |
| The question whether P equals NP is open; this row is not a claim that they differ. | العلاقة بين فئتي P وNP مسألة رياضية مفتوحة، ولا نفترض هنا أنهما مختلفتان. | — |
For 10 binary decisions, exhaustive enumeration has 2¹⁰=1024 cases; for 20 it has 2²⁰=1,048,576. These counts do not predict the actual solver runtime.
مثال: مع 10 متغيرات ثنائية يوجد 1024 تعيينا ممكنا، ومع 20 يوجد 1048576 تعيينا. هذه أعداد حالات البحث المباشر وليست تنبؤا بزمن تشغيل المحلل.
| Classالفئة | Complexity and guaranteesالتعقيد والضمانات |
|---|---|
| LP | Polynomial-time algorithms exist (input bit length matters). Simplex itself has exponential worst cases.توجد خوارزميات متعددة الحدود؛ ويؤثر طول تمثيل البيانات. للسمبلكس حالات أسوأ أسية. |
| Convex QP / QCQP | Broad standard classes admit efficient approximation algorithms under appropriate assumptions. Precision, conditioning and matrix size matter.تتوفر خوارزميات تقريب فعالة لفئات معيارية واسعة ضمن شروط مناسبة؛ الدقة والحالة العددية وحجم المصفوفات مؤثرة. |
| MILP / MIQP / MINLP | NP-hard in general; small special cases can be easy. A convex continuous relaxation does not remove integer difficulty.صعبة من فئة NP-hard في الحالة العامة؛ وقد تسهل حالات خاصة صغيرة. تحدب الجزء المستمر لا يزيل صعوبة المتغيرات الصحيحة. |
| Nonconvex NLP / QCQP | Global optimization is generally hard. Local convergence does not certify global optimality.الأمثلية العالمية صعبة عموما. التقارب المحلي لا يثبت العالمية. |
| DP / stochastic / robust / multiobjective | Complexity depends on the resulting model. DP can suffer from the curse of dimensionality; scenarios enlarge models.يتبع التعقيد بنية المسألة الناتجة. قد تعاني DP من لعنة الأبعاد وتزيد السيناريوهات الحجم. |
The same QP solved by two solvers — power networkمخطط الشبكة التربيعية التي يحلها محللان عدديان
Unit 16 reuses the convex QP from Unit 05: two generators feed a total 100 MW load. OSQP and CLARABEL independently solve the same mathematical problem. The diagram shows the verified dispatch, not two separate physical networks.
تعيد الوحدة 16 استخدام المسألة التربيعية المحدبة من الوحدة 05: يغذي مولدان حملا كليا قدره 100 MW. يحل المحللان العدديان المسألة الرياضية نفسها بصورة مستقلة. يعرض المخطط نتيجة التشغيل المتحقق منها، ولا يمثل شبكتين كهربائيتين مختلفتين.
Verified QP dispatch / تشغيل المولدين
Quadratic optimum and complexity growthمنحنى الحل التربيعي ونمو التعقيد
Convex QP cost curve / منحنى كلفة البرمجة التربيعية المحدبة
Illustrative growth rates / أمثلة على معدلات نمو التعقيد
The QP cost decreases toward its global minimum and rises afterward. Complexity growth is a separate theoretical comparison: the number of binary assignments can grow quickly, but solver runtimes depend on model structure, algorithms, numerical precision and hardware.
تنخفض كلفة المسألة التربيعية حتى تبلغ الحد الأدنى العالمي ثم ترتفع بعده. أما نمو التعقيد فهو مقارنة نظرية مستقلة؛ فقد يزداد عدد التركيبات الثنائية بسرعة، لكن زمن الحل الفعلي يعتمد على بنية النموذج والخوارزمية والدقة العددية والحاسوب المستخدم.
Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية
With binary choices there are assignments: gives 2; gives 1024; gives 1048576. This counts brute-force cases, not the actual runtime of branch-and-bound.
مع متغيرات ثنائية يوجد تعيينا: عند 1 يوجد 2، وعند 10 يوجد 1024، وعند 20 يوجد 1048576. هذا تعداد مباشر للحالات وليس زمنا فعليا لخوارزمية التفرع والتحديد.
For hours with 2 dispatch variables per hour, there are 2T continuous variables before other states: gives 48. Adding 2 binary statuses per hour adds 48 binaries, whose naive enumeration has cases.
مع ساعة ومتغيري استطاعة لكل ساعة، يوجد 2T متغيرا مستمرا قبل إضافة الحالات؛ عند يوجد 48. وإضافة حالتي تشغيل ثنائيتين لكل ساعة تعطي 48 متغيرا ثنائيا، وتعدادها المباشر حالة.
For each solver result compare objective, balance residuals, bound violations, integrality and status. Compare runtime only on the same model and environment; a single tiny benchmark does not solvers.
لكل نتيجة نقارن الهدف وبواقي التوازن ومخالفات الحدود والصحة العددية وحالة الحل. لا نقارن الزمن إلا للنموذج والبيئة نفسيهما؛ ولا تصنف تجربة صغيرة واحدة المحللات.
# If missing on Kaggle: run %pip install cvxpy in a SEPARATE cell.
# Installing missing packages requires notebook Internet access.
import numpy as np
import cvxpy as cp
print('Available solvers:', cp.installed_solvers())
# QP from Unit 05: independent solver packages
p = cp.Variable(2)
C = .2*cp.sum_squares(p)+np.array([20.,35.])@p
qp = cp.Problem(cp.Minimize(C), [cp.sum(p)==100, p>=0, p<=80])
for solver in ['OSQP','CLARABEL']:
if solver not in cp.installed_solvers():
print(solver, 'not installed; skipped')
continue
qp.solve(solver=solver)
if qp.status != cp.OPTIMAL:
raise RuntimeError(qp.status)
x = p.value
assert abs(sum(x)-100) < 1e-3
assert np.min(x) >= -1e-3 and np.max(x) <= 80+1e-3
assert np.allclose(x,[68.75,31.25],atol=1e-2)
print(solver, np.round(x,4), round(qp.value,4))
Read the codeشرح الكود
The code solves the same QP from Unit 05 using independently available solvers. It verifies power balance, generator limits and the expected result. This is not a general Dynamic MINLP solver.
يحل الكود مسألة QP نفسها من الوحدة 05 باستخدام المحللات المتاحة، ويتحقق من توازن الاستطاعة وحدود المولدات والنتيجة المتوقعة. وهو ليس محللا عاما لمسائل Dynamic MINLP.
| Modelالنموذج | Suitable free toolsأدوات مجانية مناسبة |
|---|---|
| LP / MILP | SciPy → HiGHS |
| Convex QP | CVXPY → OSQP / CLARABEL |
| Dynamic MINLP (A-R-OPF) | Pyomo / compatible MINLP solvers; verify feasibility and solver guarantees |
| NLP | SciPy → SLSQP (local method) |
| MIQP / MINLP teaching cases | Explicit binary enumeration + continuous subproblem; not a general-purpose solverتعداد ثنائي صريح ومسألة مستمرة جزئية، وليس محللا عاما |
Interactive assessment — 10 questionsاختبار تفاعلي — 10 أسئلة
Choose one answer per question. Suggested mastery: at least 8/10.اختر جوابا واحدا لكل سؤال. معيار الإتقان المقترح: 8 من 10.
Self-assessmentالتقييم الذاتي
- 1. Which quantity is chosen by the optimizer?أي كمية يختارها المحلل؟
- 2. Two variables and one independent equality: equality-based DOF?متغيران ومعادلة مستقلة واحدة: ما درجة الحرية بالنسبة للمساواة؟
- 3. What makes the dispatch in Unit 04 an LP?لماذا يعد توزيع الوحدة 04 مسألة LP؟
- 4. Unit 05: optimal powers in MW?ما استطاعتا الحل الأمثل في الوحدة 05؟
- 5. An inverter has and . Maximum ?عاكس و؛ ما أكبر ؟
- 6. Linear model with continuous power and binary on/off: class?نموذج خطي باستطاعة مستمرة وقرار تشغيل ثنائي: ما نوعه؟
- 7. Does local solver success prove global optimality for a nonconvex NLP?هل نجاح المحلل المحلي يثبت العالمية في NLP غير محدبة؟
- 8. Three kWh per battery module, requirement 8 kWh: minimum count?طاقة الوحدة 3 kWh والمطلوب 8 kWh؛ كم وحدة نحتاج على الأقل؟
- 9. Robust optimization in Unit 14 minimizes what?ماذا يصغر التحسين المتين في الوحدة 14؟
- 10. Which statement about CVXPY and OSQP is correct?أي عبارة صحيحة عن CVXPY وOSQP؟