PART 04الجزء الرابع

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. وتبقى أساسيات البرمجة في الجزء الثالث.

StartابدأAll unitsالوحداتOpen Kaggleافتح Kaggle ↗

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.

للمراجعة البرمجية ارجع إلى الجزء الثالث. اتبع الوحدات بالترتيب: المفاهيم الأساسية مرة واحدة، ثم عائلات التحسين وأمثلتها الطاقية.

Progress: 0 / 16الإنجاز: 0 / 16

Part 04 — Learning unitsالجزء الرابع — الوحدات

01The optimization model — a single referenceالنموذج الرياضي للتحسين — مرجع موحد02Independent equalities, rank and degrees of freedomالمساواة المستقلة والرتبة ودرجات الحرية03Classifying a mathematical programتصنيف مسألة البرمجة الرياضية04Linear Programming (LP) — linear economic dispatchالبرمجة الخطية — التوزيع الاقتصادي05Quadratic Programming (QP) — quadratic generator costsالبرمجة التربيعية — كلف توليد تربيعية06Quadratically Constrained Quadratic Programming (QCQP) — inverter capabilityالبرمجة ذات القيود التربيعية — حدود العاكس07Nonlinear Programming (NLP) — local and global optimaالبرمجة غير الخطية — الحل المحلي والعالمي08Integer Programming (IP) and Binary Integer Programming (BIP) — equipment selectionالبرمجة الصحيحة والثنائية — اختيار التجهيزات09Mixed-Integer Linear Programming (MILP) — generator on/off decisionsالبرمجة الخطية المختلطة — تشغيل المولد10Mixed-Integer Quadratic Programming (MIQP) — on/off plus quadratic costsالبرمجة التربيعية المختلطة — التشغيل والكلفة11Mixed-Integer Nonlinear Programming (MINLP) — a nonlinear mixed decisionالبرمجة غير الخطية المختلطة — قرار مختلط12Dynamic MINLP — A-R-OPF with wind power stations and battery storageالبرمجة غير الخطية المختلطة الديناميكية — A-R-OPF مع محطات الرياح وبطاريات التخزين13Dynamic Programming (DP) — battery schedulingالبرمجة الديناميكية — جدولة البطارية14Stochastic and robust optimization — uncertain Photovoltaic (PV) generationالتحسين العشوائي والمتين — عدم يقين التوليد الشمسي الكهروضوئي15Multiobjective optimization — cost and emissionsالتحسين متعدد الأهداف — الكلفة والانبعاثات16Complexity, solver selection and comparisonالتعقيد واختيار المحللات ومقارنة النتائج
01

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.

البارامتر معطى ثابت أثناء الحل الواحد؛ ومتغير القرار قيمة نبحث عنها. تقيم دالة الهدف كل حل مرشح، وتحدد علاقات المساواة وعدم المساواة الاختيارات الممكنة.

General modelالنموذج العام
min f(x;θ), subject to h(x;θ)=0, g(x;θ)≤0, ℓ≤x≤u.

θ denotes parameters; x is the decision vector. For discrete choices, declare the required components integer or binary. To maximize B, minimize −B with the same feasible set.

يرمز θ إلى البارامترات وx إلى شعاع القرار. نحدد مجال المتغير الصحيح أو الثنائي عند الحاجة. لتعظيم B نصغر −B مع إبقاء المجموعة الممكنة نفسها.

A mathematical function assigns exactly one output to each input in its domain; a relation may allow several outputs. Thus f(x) is a function, while h(x)=0 is a constraint relation. A Python function implements a calculation; its arguments are not automatically fixed model parameters.

يعطي التابع الرياضي خرجا وحيدا لكل مدخل في مجال تعريفه؛ وقد تسمح العلاقة بعدة مخارج. لذلك f(x) تابع، بينما h(x)=0 علاقة قيد. وينفذ التابع البرمجي حسابا، ولا تكون وسائطه بالضرورة بارامترات ثابتة للنموذج.

Read, solve and verify the model by handقراءة النموذج وحله يدويا والتحقق منه

  1. Demand D=100MW is a fixed parameter; P1 and P2 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.

    الطلب D=100MW بارامتر ثابت، والاستطاعتان P1 وP2 متغيرا قرار. الكلفتان الحديتان هما 20 و35 وحدة نقدية لكل MWh، وسعة كل مولد 80 MW. ندرس التشغيل لمدة ساعة مع إهمال الفواقد.

  2. Minimize C=20P1+35P2 [CU/h], subject to power balance P1+P2=100MW and the limits 0≤P1,P2≤80MW.

    نريد تصغير الكلفة C=20P1+35P2 بوحدة نقدية/ساعة، مع تحقيق التوازن P1+P2=100MW واحترام الحدين 0≤P1≤80MW و0≤P2≤80MW.

  3. Eliminate one unknown using the balance: P2=100−P1.

    نعزل المجهول الثاني باستخدام معادلة التوازن: P2=100−P1.

  4. Apply generator 2 limits: 0≤100−P1≤80 implies 20≤P1≤100. Intersect with generator 1 limits 0≤P1≤80: the feasible interval is 20≤P1≤80MW.

    نطبق حدود المولد الثاني: من 0≤100−P1≤80 نحصل على 20≤P1≤100. بالتقاطع مع حدود المولد الأول 0≤P1≤80 يصبح المجال الممكن 20≤P1≤80MW.

  5. Substitute into the objective and collect terms: C=20P1+35(100−P1)=20P1+3500−35P1=3500−15P1.

    نعوض في دالة الهدف ونجمع الحدود المتشابهة: C=20P1+35(100−P1)=20P1+3500−35P1=3500−15P1.

  6. Each extra MW from generator 1 reduces total hourly cost by 15 CU/h. Therefore choose the largest feasible P1: P1∗=80MW.

    كل زيادة بمقدار 1 MW في استطاعة المولد الأول تخفض الكلفة بمقدار 15 وحدة نقدية/ساعة. لذلك نختار أكبر قيمة ممكنة: P1∗=80MW.

  7. Calculate the remaining power: P2∗=100−80=20MW.

    نحسب الاستطاعة المتبقية: P2∗=100−80=20MW.

  8. Calculate the minimum cost with an ordinary calculator: C∗=20×80+35×20=1600+700=2300CUh. For one hour, the total cost is 2300 CU.

    نحسب الكلفة الدنيا بالآلة الحاسبة العادية: C∗=20×80+35×20=1600+700=2300 وحدة نقدية/ساعة. ولمدة ساعة واحدة تكون الكلفة الكلية 2300 وحدة نقدية.

  9. Verify: 80+20=100MW; 0≤80≤80 and 0≤20≤80. Balance and both capacities are satisfied. The decreasing linear cost proves this feasible endpoint is globally optimal.

    نتحقق من التوازن 80+20=100MW، ومن الحدود 0≤80≤80 و0≤20≤80. جميع القيود محققة، وتناقص الكلفة الخطية يثبت أن هذا الطرف الممكن هو الحل الأمثل العالمي.

Verified optimumالحل الأمثل بعد التحقق
P1∗=80MW; P2∗=20MW; C∗=2300CUh.
Continue with Python solvers — Unit 04تابع الحل بمحللات بايثون في الوحدة 04
02

Independent equalities, rank and degrees of freedom

المساواة المستقلة والرتبة ودرجات الحرية

n is the number of decision variables. Rank counts independent equality equations, not the number of rows written. For consistent linear equalities Ax=b, equality-based DOF=n−rank(A).

n هو عدد متغيرات القرار. تقيس الرتبة عدد معادلات المساواة المستقلة، وليس عدد الصفوف المكتوبة. للمعادلات الخطية المتسقة Ax=b تكون درجة الحرية بالنسبة للمساواة DOF=n−rank(A).

Linear: DOF=n−rank(A). Regular nonlinear equalities: localDOF=n−rank(Jh).

For nonlinear equalities h(x)=0, Jh is the Jacobian: entry (i,j) is ∂hi∂xj. The nonlinear formula describes local dimension at regular points; singular points need extra analysis.

للمساواة غير الخطية h(x)=0، تمثل Jh مصفوفة يعقوبي؛ وعنصرها (i,j) هو ∂hi∂xj. تصف الصيغة غير الخطية البعد المحلي عند النقاط المنتظمة، وتحتاج النقاط الشاذة إلى تحليل إضافي.

Hand calculation — one example, three casesالحل اليدوي — مثال واحد وثلاث حالات

  1. Take x=[P1,P2] and P1+P2=10kW. There are two unknowns; the coefficient row is [1,1], so rank=1 and DOF=2−1=1. Choosing P1 determines P2=10−P1.

    نأخذ x=[P1,P2] والتوازن P1+P2=10kW. لدينا مجهولان وصف المعاملات [1,1]؛ إذن الرتبة 1 ودرجة الحرية 2−1=1. يحدد اختيار P1 القيمة P2=10−P1.

  2. Add 2P1+2P2=20. 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.

    نضيف 2P1+2P2=20. بقسمة الصف على 2 نحصل على المعادلة الأولى نفسها. تبقى الرتبة 1 ودرجة الحرية 1؛ لم يضف الصف المكرر قيدا مستقلا.

  3. Instead add P1=6kW. Its row [1,0] is independent of [1,1]. The 2×2 determinant is 1×0−1×1=−1≠0, so rank=2 and DOF=2−2=0.

    بدلا من ذلك نضيف P1=6kW. الصف [1,0] مستقل عن [1,1]. محدد المصفوفة 2×2 هو 1×0−1×1=−1≠0؛ إذن الرتبة 2 ودرجة الحرية 2−2=0.

  4. Substitute P1=6 into the balance: P2=10−6=4kW. Check 6+4−10=0. The equalities determine the pair, but any additional capacity bounds must still be checked.

    نعوض P1=6 في التوازن فنحسب P2=10−6=4kW. نتحقق 6+4−10=0. تحدد المساواة زوج الاستطاعتين، لكن يجب فحص أي حدود إضافية للسعات.

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.

لا تضمن درجة الحرية الموجبة وجود حل ممكن أو هدف محدود أو حل أمثل وحيد. قد تقيد المتراجحات والحدود الفعالة اتجاهات الحركة الممكنة. وحتى عند انعدام درجة الحرية يجب فحص الاتساق والحدود الفيزيائية.

03

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. الأعداد الطبيعية

N={1,2,3,…}

Count solar panels: 6 panels, not 6.5.

عدد الألواح الشمسية: 6 ألواح، وليس 6.5.

2. Integers2. الأعداد الصحيحة

Z:…,−2,−1,0,1,2,…

Discrete signed controller adjustment: −2, 0, or 3 steps.

خطوات ضبط متقطعة للمتحكم: −2 أو 0 أو 3.

3. Rational numbers3. الأعداد النسبية

Q:34

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. الأعداد الحقيقية

R:2.5,−1,√2

Generator output P=2.5 kW, varying continuously over a real interval; includes rational and irrational values.

استطاعة مولد P=2.5 kW قد تتغير باستمرار ضمن مجال حقيقي؛ وتشمل النسبية وغير النسبية.

5. Complex numbers5. الأعداد العقدية

C:3+j4

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. القيم الثنائية

B={0,1}

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. الأعداد غير السالبة

R≥0=[0,∞)

Battery energy E≥0; zero is allowed. Similarly Z≥0 restricts integers to nonnegative values.

طاقة البطارية E≥0؛ الصفر مسموح. وبالمثل تحدد Z≥0 الأعداد الصحيحة غير السالبة.

8. Intervals / finite sets8. المجالات والمجموعات المنتهية

P∈[0,10];n∈{2,4,6}

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 وحدات.

N⊂Z⊂Q⊂R⊂C;B⊂Z

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 — محدب

f(x)=x2
0xf(x)

One bowl and one minimum at x = 0. The dashed chord stays above the curve.

منحنى على شكل وعاء وله قيمة صغرى واحدة عند x=0. القطعة المستقيمة المتقطعة تقع فوق المنحنى.

Example 2 — Nonconvexالمثال 2 — غير محدب

g(x)=(x2−1)2
−11xg(x)

Two valleys at x = −1 and x = 1. Between them, the curve rises above the dashed chord: convexity fails.

يوجد قاعان عند x=−1 وx=1. يرتفع المنحنى بينهما فوق القطعة المستقيمة المتقطعة، ولذلك لا يكون محدبا.

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.

نبدأ من المعنى. المجموعة محدبة عندما تقع القطعة المستقيمة الواصلة بين أي نقطتين منها داخل المجموعة بالكامل. والتابع محدب عندما يقع منحناه تحت القطعة الواصلة بين أي نقطتين من منحناه أو عليها. نرمز إلى وزن المزج بين النقطتين بالرمز α، وتتراوح قيمته بين الصفر والواحد.

f(αx1+(1−α)x2)≤αf(x1)+(1−α)f(x2)
A (−1, 1)B (2, 4)C (0.5, 0.25)D (0.5, 2.5)xf(x)f(x) = x²Chord / الوتر0.5
Blue: the curve. Yellow: the straight chord between A and B. At α=0.5, C on the curve lies below D on the chord.بالأزرق: منحنى التابع. بالأصفر: القطعة المستقيمة بين النقطتين A وB. عندما تكون α=0.5، تقع النقطة C على المنحنى أسفل النقطة D على القطعة المستقيمة.

Why does α appear in the convexity inequality?لماذا يظهر الرمز α في متباينة التحدب؟

  1. 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).

    نختار نقطتين. نأخذ التابع f(x)=x2، ونختار x1=−1 وx2=2. بالتعويض في التابع نحصل على f(x1)=1 وf(x2)=4. إذن تقع النقطتان A=(−1,1) وB=(2,4) على المنحنى.

  2. 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.

    نفهم وزن المزج. تحدد العبارة αx1+(1−α)x2 إحداثيا أفقيا بين x1 وx2. عندما تكون α=1 نحصل على x1. وعندما تكون α=0 نحصل على x2. أما عندما تكون α=0.5 فنحصل على نقطة المنتصف.

    x=0.5(−1)+(1−0.5)(2)=0.5
  3. Compute the curve height. At x=0.5 the function has height f(0.5)=(0.5)²=0.25, shown by point C.

    نحسب ارتفاع المنحنى. عند الإحداثي x=0.5 نحسب قيمة التابع بالتعويض: f(0.5)=(0.5)²=0.25. تمثل النقطة C هذه القيمة على المنحنى.

    f(αx1+(1−α)x2)=f(0.5)=0.25
  4. 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 من العبارة αf(x1)+(1−α)f(x2). بالتعويض نحصل على 0.5(1)+0.5(4)=2.5. تمثل النقطة D هذا الارتفاع على القطعة المستقيمة.

    αf(x1)+(1−α)f(x2)=2.5
  5. 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².

    نقارن. بما أن 0.25≤2.5، تقع نقطة المنحنى أسفل نقطة القطعة المستقيمة عند الإحداثي الأفقي نفسه. يوضح هذا المثال معنى الشرط فقط، ولا يكفي وحده لإثبات التحدب. يجب أن تتحقق المتباينة لكل نقطتين x1 وx2 ولكل قيمة α∈[0,1]. ويمكن إثبات تحدب التابع f(x)=x2 باستخدام المشتق الثاني.

    0.25≤2.5

Here x1 and x2 are any two points in the function's convex domain; α∈[0,1] selects a point between them. Strict convexity requires a strict inequality when the points differ and 0<α<1. The tests below are convenient sufficient or equivalent conditions when the derivatives exist.

حيث x1 وx2 نقطتان كيفيتان في مجال تعريف محدب، وα∈[0,1] يختار نقطة بينهما. في التحدب التام تكون المتباينة صارمة عند اختلاف النقطتين و0<α<1. الاختبارات الآتية تسهل الإثبات عندما تكون المشتقات موجودة.

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النتيجة
f″(x)≥0 for every x in the intervalلكل قيمة x ضمن المجال: f″(x)≥0Convexمحدب
f″(x)>0 for every x in the intervalلكل قيمة x ضمن المجال: f″(x)>0Strictly convex (sufficient condition)محدب تماما (شرط كاف)
f″(x)≤0 for every x in the intervalلكل قيمة x ضمن المجال: f″(x)≤0Concaveمقعر
f″(x)<0 for every x in the intervalلكل قيمة x ضمن المجال: f″(x)<0Strictly concave (sufficient condition)مقعر تماما (شرط كاف)

Do not reverse the strict test: a strictly convex function need not have f″(x)>0 at every point. For example, f(x)=x4 is strictly convex although f″(0)=0.

لا نعكس شرط التحدب التام: فقد يكون التابع محدبا تماما رغم أن مشتقه الثاني يساوي الصفر في بعض النقاط. مثلا f(x)=x4 محدب تماما رغم أن f″(0)=0.

Solved example A — exponential functionمثال محلول أ — التابع الأسي

Given: f(x)=e−x for all real x. We seek to prove whether it is convex or concave.

المعطى: f(x)=e−x لكل x حقيقي. نريد إثبات ما إذا كان محدبا أو مقعرا.

  1. 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. نضرب التابع الأسي في مشتق التابع الداخلي.

    v(x)=−x,v′(x)=−1
    ddxev=ev×v′=e−x×(−1)
    f′(x)=−e−x
  2. 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⁻ˣ، ولذلك فإن حاصل ضرب الإشارتين السالبتين موجب.

    f″(x)=−[−e−x]=e−x
    f″(x)=e−x
  3. 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 — ندرس الإشارة. التابع الأسي موجب لكل قيمة حقيقية، ولذلك المشتق الثاني موجب دائما؛ إذن التابع محدب تماما.

    f″(x)>0⇒strictly convex

Solved example B — square-root functionمثال محلول ب — تابع الجذر التربيعي

Given: f(x)=x with x>0. The positive-domain restriction matters because the second derivative contains a denominator.

المعطى: f(x)=x حيث x>0. شرط المجال مهم لأن المشتق الثاني يحوي مقاما.

  1. 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 للتخلص من الأس السالب.

    ddxxn=nxn−1
    f′(x)=12x−12=12x
    f(x)=x12⇒f′(x)=12x
  2. 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، تكون النتيجة سالبة.

    f″(x)=12×(−12)x−32=−14x−32
    f″(x)=−14x32
  3. 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.

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

H=∇2f=[∂2f∂x12∂2f∂x1∂x2∂2f∂x2∂x1∂2f∂x22]

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.

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

Hv=λv,v≠0

Here H is the square Hessian matrix, v 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 I, whose diagonal entries are 1 and all other entries are 0.

هنا H مصفوفة هيس المربعة، وv متجه ذاتي غير صفري، وλ قيمته الذاتية. لإيجاد القيم المحتملة ننقل الحدود إلى طرف واحد، ونستعمل مصفوفة الوحدة I التي عناصر قطرها الرئيسي تساوي 1 وبقية عناصرها تساوي 0.

(H−λI)v=0⇒det(H−λI)=0

The symbol det 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.

الرمز det يعني المحدد. نساوي المحدد بالصفر لأن النظام المتجانس لا يقبل متجها ذاتيا غير صفري إلا في هذه الحالة. ولحساب محدد مصفوفة من صفين وعمودين، نضرب عنصري القطر الرئيسي ثم نطرح حاصل ضرب العنصرين الآخرين.

det[abcd]=ad−bc

Hand calculation for the Hessian in our exampleالحساب اليدوي لمصفوفة هيس في مثالنا

  1. Step 1 — Start with the Hessian that we will obtain in the example below. It is diagonal: 2 and 0.

    الخطوة 1 — نبدأ بمصفوفة هيس التي سنحصل عليها في المثال التالي. عناصر قطرها الرئيسي هي 2 و0.

    H=[2000]
  2. Step 2 — Subtract λ from each diagonal entry, because λI has λ on its diagonal.

    الخطوة 2 — نطرح λ من كل عنصر على القطر الرئيسي، لأن مصفوفة λI تحوي λ على القطر الرئيسي.

    H−λI=[2−λ00−λ]
  3. Step 3 — Calculate the determinant: multiply (2−λ) by (−λ) and subtract 0×0. Then set the result equal to zero.

    الخطوة 3 — نحسب المحدد: نضرب (2−λ) في (−λ)، ثم نطرح 0×0. وبعد ذلك نساوي النتيجة بالصفر.

    (2−λ)(−λ)−0×0=0
  4. 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. نحل كل معادلة على حدة.

    λ1=0,λ2=2
  5. 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: f(x1,x2)=x12+2x2. We will calculate every entry of the Hessian, then interpret the zero eigenvalue.

المعطى: f(x1,x2)=x12+2x2. سنحسب جميع عناصر مصفوفة هيس ثم نفسر ظهور القيمة الذاتية الصفرية.

  1. 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. ثم نجمع النتيجتين في كل اتجاه.

    ∂f∂x1=2x1+0;∂f∂x2=0+2
    ∂f∂x1=2x1;∂f∂x2=2
  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.

    H11=2,H12=0,H21=0,H22=0
    H=[2000]
  3. 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. كلتاهما غير سالبة، فمصفوفة هيس موجبة نصف التعريف والتابع محدب.

    λ1=2;λ2=0
  4. 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. لكن التحدب التام يشترط أن تكون القيمة عند المنتصف أصغر من هذا المتوسط.

    f(M)=2=0+42

Conclusion: convex, but not strictly convex. Replacing +2x2 by −2x2 gives the same Hessian, so x12−2x2 has the same classification.

النتيجة: التابع محدب لكنه ليس محدبا تماما. وإذا استبدلنا +2x2 بـ −2x2 بقيت مصفوفة هيس نفسها، ولذلك فإن x12−2x2 له التصنيف ذاته.

3. Quadratic programming and the whole optimization problem3. البرمجة التربيعية وتحدب مسألة الأمثلية كاملة

For a quadratic function f(x)=xTQx with a symmetric matrix Q, the Hessian is 2Q. If Q⪰0, the function is convex; if Q≻0, 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 gi(x)≤0 should have convex gi. Integer/binary restrictions usually make the feasible set nonconvex.

في التابع التربيعي f(x)=xTQx حيث المصفوفة Q متناظرة، تكون مصفوفة هيس 2Q. إذا كانت Q⪰0 فالهدف محدب، وإذا كانت Q≻0 فالهدف محدب تماما. لكن تحدب الهدف وحده لا يكفي: يجب أن تكون مجموعة الحلول المسموحة محدبة أيضا. في الصياغة المعتادة، تكون قيود المساواة تآلفية (خطية مع ثابت)، وتكون توابع قيود عدم المساواة من الشكل gi(x)≤0 محدبة. أما القيود الصحيحة والثنائية فتجعل المجموعة غير محدبة عموما.

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 فلا تكونان محدبتين عموما بسبب مجالات المتغيرات المنفصلة.

minC=3x+5y
x+y≥10
x≥0,y≥0

Feasible region, convexity test and objective linesمنطقة الحل واختبار التحدب ومستقيمات دالة الهدف

Convex feasible region and global optimum of a linear programThe shaded first quadrant above x+y=10 is feasible. A and B are feasible points with a connecting segment inside the region. The two parallel objective lines C=40 and C=30 are shown. The global optimum is x=10, y=0. A B ★ 0 10 10 x y Feasible region
Feasible regionx + y = 10C = 40C = 30A–B★ (10, 0)

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الحل خطوة بخطوة

  1. Step 1 — Draw the boundary. Set x+y=10; the intercepts are (0,10) and (10,0).

    الخطوة 1 — ارسم المستقيم الحدي. نحول القيد إلى مساواة لرسم المستقيم، فنحصل على نقطتي التقاطع (0,10) و(10,0).

  2. Step 2 — Find feasible points. Apply x+y≥10 and x≥0,y≥0: shade the first-quadrant region above the boundary line.

    الخطوة 2 — حدد منطقة الحلول المسموحة. نطبق x+y≥10 وx≥0,y≥0، فنظلل الجزء الواقع فوق المستقيم في الربع الأول. المنطقة تمتد إلى ما لا نهاية.

  3. 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 كلها داخل المنطقة المسموحة، وينطبق ذلك على أي نقطتين مسموحتين لأن القيود خطية.

  4. Step 4 — Move the objective line. Draw parallel lines 3x+5y=k. Slide from C=40 toward C=30 to reduce cost. C=30 touches the feasible region at (10,0).

    الخطوة 4 — حرك مستقيم دالة الهدف. نرسم المستقيمات المتوازية 3x+5y=k، ثم ننتقل من C=40 إلى C=30 لتصغير الكلفة. يلامس المستقيم C=30 المنطقة عند (10,0).

  5. 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. هذه إعادة ترتيب جبرية مقصودة وليست فرضا جديدا.

    C=3x+5y
    C=3x+(3+2)y
    C=3x+3y+2y
    C=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، لذلك يبقى اتجاه المتباينة كما هو.

    x+y≥103(x+y)≥30

    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. أي إن الحد المتبقي في دالة الهدف لا يستطيع إنقاص الكلفة.

    y≥02y≥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 عند أي نقطة مسموحة.

    C=3(x+y)+2y≥30+0=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، وهي بالضبط الحد الأدنى الذي أثبتناه. إذن هذا هو الحل الأمثل العالمي.

    C(10,0)=3×10+5×0=30
x★=10,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.

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

Model and unitsالنموذج والوحدات
TypeالنوعStructureالبنيةEnergy applicationتطبيق طاقيIllustrative modelمثال يوضح البنية
Linear Programming (LP)البرمجة الخطيةLinear objective + linear constraints; continuous xهدف وقيود خطية ومتغيرات مستمرةLinear dispatchتوزيع خطي
min(2x1+3x2)
x1+x2≥5,x1,x2≥0
Linear dispatch of two generatorsتوزيع إنتاج مولدين بكلفة خطية
Quadratic Programming (QP) / Quadratically Constrained Quadratic Programming (QCQP)البرمجة التربيعية / البرمجة التربيعية ذات القيود التربيعيةQuadratic objective / quadratic constraintsهدف تربيعي / قيود تربيعيةEconomic dispatch / inverter limitsالتوزيع الاقتصادي / حدود العاكس
QP:min(x2+y2)
QCQP:x2+y2≤25
QP: quadratic generation cost. QCQP: inverter apparent-power limit.QP: كلفة توليد تربيعية. QCQP: حد الاستطاعة الظاهرية للعاكس.
Nonlinear Programming (NLP)البرمجة غير الخطيةGeneral nonlinear expressionsتعابير غير خطية عامةAC-OPFالتدفق الأمثل المتناوب
min(x2+sin(x))
x≥0
Nonlinear model with a sine termنموذج غير خطي يحوي تابع الجيب
Integer Programming (IP) / Binary Integer Programming (BIP)البرمجة الصحيحة / البرمجة الصحيحة الثنائيةInteger / binary decisionsقرارات صحيحة / ثنائيةEquipment counts / selectionعدد التجهيزات / اختيارها
IP:min(n),3n≥8
n∈Z
BIP:u∈{0,1}
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جدولة تشغيل المولدات
MILP:min(2p+5u)
MIQP:min(p2+5u)
MINLP:min(p2+sin(p)+5u)
0≤p≤10u,u∈{0,1}
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 مع محطات الرياح وبطاريات التخزين
minΣct(Pt)
Et+1=Et+PchtΔt−PdchtΔt
f(x,u,t)=0;g(x,u,t)≤0
Illustrative structure: state evolves over time; network constraints are nonlinear.بنية توضيحية: تتغير حالة التخزين مع الزمن مع قيود شبكة غير خطية.

Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية

  1. In C=20P1+35P2, each decision variable has power one and is not multiplied by another decision: linear.

    في C=20P1+35P2 أس كل متغير قرار 1 ولا يوجد جداء بين متغيري قرار؛ إذن الهدف خطي.

  2. Adding 0.2P12 makes the objective quadratic. P1P2 is also quadratic, not linear.

    إضافة 0.2P12 تجعل الهدف تربيعيا. الجداء P1P2 تربيعي أيضا، وليس خطيا.

  3. The product η⁢P remains linear when η is a fixed efficiency parameter; it is bilinear when both η and P are decisions.

    يبقى الجداء η⁢P خطيا إذا كانت η كفاءة ثابتة؛ ويصبح ثنائي الخطية إذا كانت η وP متغيري قرار.

  4. Add u∈{0,1}: linear expressions give MILP, a quadratic objective with linear constraints gives MIQP.

    نضيف u∈{0,1}: التعابير الخطية تعطي 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.

الخوارزمية مثل السمبلكس والنقطة الداخلية والتفرع والتحديد والخوارزمية الجينية طريقة حل وليست نوع النموذج. كما أن مكتبة النمذجة تختلف عن المحلل.

04

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. نهمل الفواقد وكلفة الإقلاع والحد الأدنى للتوليد.

Model and unitsالنموذج والوحدات
minC=20P1+35P2 [CU/h]; P1+P2=100MW; 0≤P1,P2≤80MW

Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية

  1. Use balance to eliminate P2: P2=100−P1.

    نحذف P2 باستخدام التوازن: P2=100−P1.

  2. From 0≤P2≤80: 0≤100−P1≤80, hence 20≤P1≤100. Intersect with 0≤P1≤80: 20≤P1≤80.

    من 0≤P2≤80 نحصل على 20≤P1≤100. بالتقاطع مع 0≤P1≤80 يصبح المجال 20≤P1≤80.

  3. Substitute into cost: C=20P1+35(100−P1)=3500−15P1.

    نعوض في الكلفة: C=20P1+35(100−P1)=3500−15P1.

  4. The coefficient −15 is negative: cost falls as P1 rises. Choose P1=80MW, so P2=100−80=20MW.

    المعامل −15 سالب؛ تنخفض الكلفة بزيادة P1. نختار P1=80MW، ومنه P2=20MW.

  5. Calculate C=20×80+35×20=1600+700=2300 CU/h; for 1 h, energy cost=2300 CU.

    نحسب C=20×80+35×20=1600+700=2300 وحدة نقدية/ساعة، ولمدة ساعة الكلفة 2300 وحدة نقدية.

  6. Check both interval endpoints: C(20)=3200; C(80)=2300. A linear function on this interval has its minimum at the cheaper endpoint. Balance 80+20−100=0.

    نفحص الطرفين: C(20)=3200 وC(80)=2300. التابع الخطي على هذا المجال يحقق أدناه عند الطرف الأقل كلفة. باقي التوازن 80+20−100=0.

See the linear cost and its feasible minimumمنحنى الكلفة الخطية وأدناها الممكن

Linear cost: the minimum is a feasible endpointC=3500−15P1 decreases over the feasible interval 20≤P1≤80. Its constrained global minimum is C=2300 at P1=80. Grey regions violate generator limits. 2026-11-01T20:10:09.486860 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 20 40 60 80 100 Generator 1 power P1 (MW) 2000 2200 2400 2600 2800 3000 3200 3400 3600 Total cost C (CU/h) Global minimum P1=80, C=2300 Infeasible Linear cost: the minimum is a feasible endpoint Outside feasible interval Feasible cost curve
P1∗=80MW; P2∗=20MW; Cmin=2300CUh.

Axes: horizontal P1 (MW); vertical total cost C (CU/h).

المحور الأفقي: استطاعة المولد الأول بالميغاواط. المحور الرأسي: الكلفة الكلية بوحدة نقدية/ساعة.

The green-tinted interval is feasible: 20≤P1≤80MW. Grey regions are excluded by generator limits. Some excluded points have lower cost, but they are not admissible solutions.

المجال المظلل بالأخضر ممكن: 20≤P1≤80MW. تستبعد حدود المولدين المجالين الرماديين. قد تظهر فيهما قيم كلفة أقل، لكنها لا تمثل حلولا مسموحة.

Proof of globality: for every feasible P1, C(P1)−2300=15(80−P1)≥0. Equality holds at P1=80. Thus no feasible decision is cheaper.

برهان العالمية: لكل قيمة ممكنة من P1 يكون C(P1)−2300=15(80−P1)≥0. تتحقق المساواة عند P1=80؛ لذلك لا توجد قيمة قرار ممكنة أقل كلفة.

Python · Kaggle · 01
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شرح الكود

  1. The loop runs dual simplex and interior-point algorithms within the same HiGHS solver; these are two algorithms, not two independent solver packages.

    تشغل الحلقة خوارزميتي السمبلكس الثنائي والنقطة الداخلية ضمن محلل HiGHS نفسه؛ هما خوارزميتان وليستا حزمتين مستقلتين.

  2. linprog receives cost coefficients, balance coefficients, demand and bounds in the same variable order [P1,P2].

    تستقبل linprog معاملات الكلفة والتوازن والطلب والحدود بالترتيب نفسه [P1,P2].

  3. success checks termination; assert verifies balance, bounds and the hand-calculated cost.

    تفحص success انتهاء الحل، وتتحقق assert من التوازن والحدود والكلفة المحسوبة يدويا.

Expected resultالنتيجة المتوقعة
P1=80MW; P2=20MW; C=2300CUh (both algorithms)
05

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/h.

نحتفظ بطلب الوحدة 04 وسعاتها ونستخدم منحنيات كلفة تربيعية محدبة تماما. تحمل المعاملات الوحدات اللازمة لإعطاء الكلفة بوحدة نقدية/ساعة.

Model and unitsالنموذج والوحدات
minC=0.2P12+20P1+0.2P22+35P2; P1+P2=100; 0≤P1,P2≤80 [MW]

Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية

  1. As before, P2=100−P1 and 20≤P1≤80MW.

    كما سبق P2=100−P1 والمجال 20≤P1≤80MW.

  2. Expand (100−P1)2=10000−200P1+P12; substitute to get C=0.4P12−55P1+5500.

    نفك المربع (100−P1)2=10000−200P1+P12؛ ثم نجمع الحدود فنحصل على C=0.4P12−55P1+5500.

  3. Complete the square: C=0.4(P1−68.75)2+3609.375. A nonnegative square is minimized at zero.

    نكمل المربع: C=0.4(P1−68.75)2+3609.375. المربع غير السالب يحقق أدناه عند الصفر.

  4. Thus P1=68.75 and P2=100−68.75=31.25 MW; both respect [0,80]. Alternatively C′=0.8P1−55=0 gives 55÷0.8=68.75.

    إذن P1=68.75 وP2=31.25MW، وكلتاهما ضمن الحدود. وبالمشتقة C′=0.8P1−55=0 نحصل على 55÷0.8=68.75.

  5. Calculate P1²=4726.5625 and P2²=976.5625. C=945.3125+1375+195.3125+1093.75=3609.375CUh.

    نحسب مربعي الاستطاعتين 4726.5625 و976.5625. الكلفة 945.3125+1375+195.3125+1093.75=3609.375 وحدة نقدية/ساعة.

  6. Compare endpoints: C(20)=4560 and C(80)=3660. Positive curvature C″=0.8 proves the feasible stationary point is the unique global minimum.

    نفحص الطرفين: C(20)=4560 وC(80)=3660. المشتقة الثانية C″=0.8 موجبة، فتثبت أن النقطة الداخلية الممكنة هي الأدنى العالمي الوحيد.

See and test the global quadratic minimumمشاهدة الأدنى التربيعي العالمي واختباره

Quadratic cost: a unique global minimumC=0.4(P1−68.75)²+3609.375. The exact lower bound 3609.375 is attained only at P1=68.75, inside the feasible interval [20,80]. 2026-11-01T20:07:31.722198 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 20 40 60 80 100 Generator 1 power P1 (MW) 3500 3750 4000 4250 4500 4750 5000 5250 5500 Total cost C (CU/h) P1=68.75 MW Cmin=3609.375 CU/h C(20)=4560 C(80)=3660 Quadratic cost: a unique global minimum Feasible total cost Global lower bound
P1∗=68.75MW; P2∗=31.25MW; Cmin=3609.375CUh.

Axes: horizontal P1 (MW); vertical total cost C (CU/h).

المحور الأفقي: استطاعة المولد الأول بالميغاواط. المحور الرأسي: الكلفة الكلية بوحدة نقدية/ساعة.

Enforce balance first: P2=100−P1. The plotted curve is the total system cost C1(P1)+C2(100−P1), not the isolated cost of one generator. Green marks the unique global optimum; grey shading is infeasible.

نطبق التوازن أولا: P2=100−P1. يمثل المنحنى الكلفة الكلية C1(P1)+C2(100−P1)، وليس كلفة مولد منفرد. تحدد النقطة الخضراء الحل الأمثل العالمي الوحيد، ويمثل التظليل الرمادي المجال غير الممكن.

Exact proof: C(P1)=3609.375+0.4(P1−68.75)2. A square cannot be negative, so every feasible point has C≥3609.375. Equality holds only at P1=68.75MW, which also satisfies all limits.

البرهان الدقيق: C(P1)=3609.375+0.4(P1−68.75)2. لا يكون المربع سالبا؛ لذلك تحقق كل نقطة ممكنة C≥3609.375. تتحقق المساواة فقط عند P1=68.75MW، وهي قيمة تحترم جميع الحدود.

P1 (MW)68.75
P2 (MW)31.25
C1 (CU/h)2320.313
C2 (CU/h)1289.063
Total C (CU/h)3609.375
ΔC (CU/h)0.000

The orange ring shows your trial point. ΔC=C−Cmin 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.

تمثل الحلقة البرتقالية نقطة تجربتك. ويعني ΔC=C−Cmin فرق الكلفة عن الحل الأخضر. حرك المؤشر إلى الجهتين؛ يبقى الفرق غير سالب. تعرض الكلفتان الجزئيتان بعد التقريب، وتحسب الكلفة الكلية من القيم غير المقربة.

P1 (MW)P2 (MW)C (CU/h)ΔC (CU/h)
20804560.000950.625
40603940.000330.625
60403640.00030.625
68.7531.253609.3750.000
80203660.00050.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.

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

Python · Kaggle · 02
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شرح الكود

  1. cost implements the original two-variable objective, not the reduced hand formula.

    ينفذ cost الهدف الأصلي بمتغيرين، وليس الصيغة المختزلة للحل اليدوي.

  2. The equality callback must return zero; bounds enforce both generator capacities.

    يجب أن يعيد تابع المساواة صفرا، وتطبق bounds سعات المولدين.

  3. SLSQP is a numerical optimization algorithm. Globality here follows from convexity and the hand proof, not merely from success.

    SLSQP خوارزمية أمثلية عددية؛ والعالمية هنا ناتجة عن التحدب والبرهان اليدوي، وليست مستنتجة من success وحدها.

Expected resultالنتيجة المتوقعة
P=[68.75,31.25]MW; C=3609.375CUh

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.

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

  1. 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.

    C(P1,P2)=0.2P12+20P1+0.2P22+35P2
  2. 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 مضروبا في الاستطاعة، ومشتق الحد الخطي يساوي معامله الثابت.

    ∂C∂P1=0.4P1+20;∂C∂P2=0.4P2+35
  3. 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، والمشتقان المختلطان يساويان صفرا لأن تابع الكلفة لا يحتوي على جداء استطاعتي المولدين.

    H=[0.4000.4]
  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 — نحسب القيم الذاتية باستخدام المحدد. وبما أن مصفوفة هيس قطرية، فإن القيمتين الذاتيتين تساويان عنصري القطر الرئيسي. كلتاهما موجبة تماما.

    det(H−λI)=(0.4−λ)2=0
    λ1=λ2=0.4>0
  5. 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 — بما أن مصفوفة هيس موجبة التعريف، فإن تابع الكلفة محدب تماما. وقيد توازن الطلب وحدود استطاعات المولدين قيود تآلفية، ولذلك تكون منطقة الحلول المسموحة محدبة. وبناء على ذلك، فإن نقطة الحل المسموحة التي حسبناها هي الحل الأمثل العالمي الوحيد لهذه المسألة.

    P1+P2=100;0≤P1,P2≤80

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 ميغاواط. وهكذا يتفق الاختباران.

d2CdP12=0.8>0
06

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النموذج والوحدات

maxPP2+Q2≤25Q=3,P≥0

Solve by handالحل اليدوي خطوة بخطوة

  1. Insert Q=3 in the inverter capacity constraint.

    نعوض عن الاستطاعة الردية Q=3 في قيد استطاعة العاكس.

    P² + 3² ≤ 5²
  2. Subtract 9 from both sides.

    نطرح 9 من طرفي المتباينة.

    P² ≤ 25 − 9 = 16
  3. Because 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 يحدد عموما منطقة غير محدبة.

Python · Kaggle · QCQP
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)
07

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. دالة الهدف من الدرجة الرابعة مصممة للتعليم، وليست منحنى كلفة مقاسا لجهاز حقيقي.

Model and unitsالنموذج والوحدات
minF(P)=(P−2)2(P−6)2; 0≤P≤8kW

Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية

  1. Every squared factor is nonnegative; therefore F(P)≥0 for all P.

    كل عامل مربع غير سالب؛ لذلك F(P)≥0 لكل P.

  2. At P=2: F=02×(−4)2=0; at P=6: F=42×02=0. Both are feasible, hence both are global minima.

    عند P=2 نحصل على 02×(−4)2=0، وعند P=6 نحصل على 42×02=0. النقطتان ممكنتان؛ لذلك كلتاهما أدنى عالمي.

  3. 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 فهما ثابتان.

    F(P)=(P−2)2(P−6)2
    1. 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)². في اشتقاق الجداء نشتق العامل الأول ونبقي الثاني، ثم نبقي العامل الأول ونشتق الثاني، وبعد ذلك نجمع.

      F′=A′B+AB′
    2. 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.

      A′=2(P−2)×1=2(P−2)
      B′=2(P−6)×1=2(P−6)
    3. Step 3 — Substitute these two derivatives into the product rule. At this stage we have two terms; do not multiply all brackets yet.

      الخطوة 3 — نعوض المشتقين في قاعدة اشتقاق الجداء. نحصل هنا على حدين، ولا داعي لنشر جميع الأقواس.

      F′=2(P−2)(P−6)2+(P−2)22(P−6)
    4. 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) من الحد الثاني.

      F′=2(P−2)(P−6)[(P−6)+(P−2)]
    5. 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.

      (P−6)+(P−2)=2P−8=2(P−4)
      F′=4(P−2)(P−4)(P−6)

    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. ليست جميع النقاط الساكنة نقاط حد أدنى بالضرورة؛ لذلك نقارن قيم تابع الهدف عندها وعند طرفي المجال كما في الخطوة التالية.

  4. For a stationary-point check, F′=4(P−2)(P−4)(P−6)=0 gives P=2,4,6.

    لفحص النقاط الساكنة نكتب F′=4(P−2)(P−4)(P−6)=0، فنحصل على P=2 و4 و6.

  5. Evaluate candidates and endpoints: F(0)=144; F(2)=0; F(4)=16; F(6)=0; F(8)=144. P=4 is stationary but not a minimum.

    نحسب عند المرشحين والطرفين: F(0)=144 وF(2)=0 وF(4)=16 وF(6)=0 وF(8)=144. النقطة P=4 ساكنة وليست دنيا.

  6. The midpoint of the two minima has F(4)=16>0, demonstrating nonconvexity. Different returned P values can have the same optimal objective.

    منتصف نقطتي الأدنى يعطي F(4)=16>0، وهذا يبين عدم التحدب. قد يعيد محللان قيمتين مختلفتين لـP مع قيمة هدف مثلى واحدة.

Two global optima in the nonlinear exampleحلان أمثلان عالميان في المثال غير الخطي

Nonconvex does not mean a unique optimumF=(P−2)²(P−6)² is nonnegative everywhere. Both P=2 and P=6 attain the global lower bound zero. P=4 is stationary but is not a minimum. 2026-11-01T20:07:31.826897 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 1 2 3 4 5 6 7 8 Device power P (kW) 0 20 40 60 80 100 120 140 Normalized penalty F(P) Global minimum P=2, F=0 Global minimum P=6, F=0 Stationary, not minimum Nonconvex does not mean a unique optimum Exact lower bound F=0
P∗=2kW or P*=6 kW; Fmin=0.

Axes: horizontal device power P (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مقارنة الأدنى المحلي بالأدنى العالمي

A local minimum can be more expensiveA teaching polynomial G has derivative (x−1)(x−3)(x−6). On [0,7], x=1 is a local minimum with G=22.4167, while x=6 is the global minimum with G=12. All stationary points and endpoints are compared analytically. 2026-11-01T20:07:31.925092 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 1 2 3 4 5 6 7 Normalized operating decision x 10 15 20 25 30 Illustrative penalty G(x) Local minimum x=1, G=22.4167 Global minimum x=6, G=12 Local maximum A local minimum can be more expensive
Local: x=1,G≈22.416667. Global: x=6, Gmin=12.

Axes: horizontal normalized decision x; vertical penalty G(x).

المحور الأفقي: قرار التشغيل المطبع دون وحدات. المحور الرأسي: قيمة دالة الجزاء.

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.

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

G(x)=x44−(103)x3+(272)x2−18x+30,0≤x≤7.
  1. Differentiate and factor: G′(x)=x3−10x2+27x−18=(x−1)(x−3)(x−6). Thus stationary candidates are 1, 3 and 6.

    نشتق ونحلل: G′(x)=x3−10x2+27x−18=(x−1)(x−3)(x−6). إذن القيم الساكنة المرشحة هي 1, 3, 6.

  2. 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. تبلغ الدالة المتصلة القابلة للاشتقاق أدناها على هذا المجال المغلق عند أحد الطرفين أو عند نقطة ساكنة داخلية.

  3. Evaluate all five candidates with a calculator, as shown in the table. For x=6: 12964−(103)×216+(272)×36−108+30=324−720+486−108+30=12.

    نحسب قيم المرشحين الخمسة بالحاسبة كما في الجدول. عند x=6 نحصل على 12964−(103)×216+(272)×36−108+30=324−720+486−108+30=12.

  4. The derivative changes from negative to positive at x=1 and x=6: both are local minima. Comparing every candidate proves that only x=6 attains the global minimum 12 on [0,7].

    تتغير إشارة المشتقة من سالبة إلى موجبة عند x=1 وx=6، فتكون النقطتان أدنيين محليين. وتثبت مقارنة جميع المرشحين أن x=6 وحدها تحقق الأدنى العالمي 12 على المجال [0,7].

xG(x)Interpretationالتفسير
030.000000Left endpointالطرف الأيسر
122.416667Local minimum, not globalأدنى محلي وليس عالميا
327.750000Local maximumأعلى محلي
612.000000Global minimum (also local)أدنى عالمي، وهو محلي أيضا
722.416667Right endpointالطرف الأيمن

A local algorithm starting near x=1 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.

قد تتوقف خوارزمية محلية تبدأ قرب x=1 عند الأدنى المحلي الأعلى كلفة. لا يكفي الرسم أو تعدد التخمينات الابتدائية وحدهما لإثبات العالمية في المسألة العامة؛ أما هنا فيوفر فحص جميع المرشحين برهانا كاملا.

Python · Kaggle · 04
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شرح الكود

  1. Run the same model from two initial guesses. The solutions should approach 2 and 6 respectively.

    نشغل النموذج من تخمينين ابتدائيين؛ يفترض أن يقترب الحلان من 2 و6 على الترتيب.

  2. Multiple starts alone do not certify a global optimum in general. Here F≥0 supplies the certificate.

    لا تكفي عدة بدايات لإثبات العالمية عموما. في هذا المثال يوفر F≥0 البرهان.

Expected resultالنتيجة المتوقعة
start 1 → P≈2; start 7 → P≈6; F≈0 for both
08

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 صالحة للاستخدام؛ لم نضف خسائر تحويل في هذا المثال المبسط.

Model and unitsالنموذج والوحدات
minC=400n; 3n≥8kWh; n∈{0,1,2,...}

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.

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

0123456n=3, C=1200n=2: 6<8 kWhn (modules)C (CU)012002400Feasible n ≥ 3C = 400n8/3
Energy constraint: 3n ≥ 8 kWh, therefore n ≥ 8/3. With integer n, the feasible values are 3, 4, 5, ... . The optimum is n = 3 modules and cost = 1200 CU.شرط الطاقة هو 3n ≥ 8 kWh، ومنه n ≥ 8/3. وبما أن عدد الوحدات صحيح، تكون القيم المسموحة 3، 4، 5، .... الحل الأمثل هو n = 3 وحدات، بكلفة 1200 وحدة نقدية.

Example 1 — Integer decisions: hand calculationالمثال الأول — الحل اليدوي للقرارات الصحيحة

  1. Divide the energy requirement by module energy: n≥8÷3=2.6667.

    نقسم الطاقة المطلوبة على طاقة الوحدة: n≥8÷3=2.6667.

  2. n must be integer, so the smallest feasible value is the ceiling n=3.

    يجب أن تكون n صحيحة؛ أصغر قيمة ممكنة هي التقريب للأعلى n=3.

  3. Check n=2: energy=6<8, infeasible. Check n=3: energy=9≥8 and cost=3×400=1200 CU.

    نفحص n=2: الطاقة 6<8، فهي غير ممكنة. وعند n=3: الطاقة 9≥8 والكلفة 3×400=1200 وحدة نقدية.

  4. The cost increases with n, so all n>3 cost more. Rounding upward is valid for this one-variable monotone model, not a general integer-solving method.

    الكلفة متزايدة مع n، فتكون كل n>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.

min(700yA+1000yB);5yA+8yB≥8

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.

yA∈{0,1},yB∈{0,1}

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).

(0,0)(1,0)(0,1)(1,1)Energy (kWh)Cost (CU)8Not feasibleFeasible ≥ 8 kWhOptimal: (0,1)
Red: infeasible choices (0,0) and (1,0). Green: optimal feasible choice (0,1). Yellow: feasible but more expensive (1,1).بالأحمر: الحالتان غير المسموحتين (0,0) و(1,0). بالأخضر: الحل الأمثل (0,1). بالأصفر: الحالة المسموحة الأعلى كلفة (1,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المثال الثاني — الحل اليدوي للقرارات الثنائية

  1. Binary variant: two candidate packs A=(5 kWh,700 CU), B=(8 kWh,1000 CU). Enumerate (yA,yB)=(0,0),(1,0),(0,1),(1,1): energies 0,5,8,13; costs 0,700,1000,1700.

    في المثال الثنائي نفترض حزمتين مرشحتين: طاقة الحزمة A هي 5 kWh وكلفتها 700 وحدة نقدية؛ وطاقة الحزمة B هي 8 kWh وكلفتها 1000 وحدة نقدية. الحالات الممكنة للقرارين (yA,yB) هي (0,0), (1,0), (0,1), (1,1). تقابلها الطاقات 0, 5, 8, 13 kWh والكلف 0, 700, 1000, 1700 وحدة نقدية، على الترتيب.

  2. For min700yA+1000yB with 5yA+8yB≥8 and binary y, choose (0,1): 8 kWh at 1000 CU.

    في النموذج min700yA+1000yB مع 5yA+8yB≥8 ومتغيرين ثنائيين، نختار (0,1): طاقة 8 kWh بكلفة 1000.

Python · Kaggle · 05
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شرح الكود

  1. integrality=[1] declares an integer; a binary variable additionally needs bounds 0 and 1. The upper bound 10 is harmless here because larger n cannot improve cost.

    تحدد integrality=[1] متغيرا صحيحا؛ والثنائي يحتاج أيضا الحدين 0 و1. الحد 10 لا يستبعد الحل الأمثل هنا لأن زيادة n ترفع الكلفة.

  2. The comprehension enumerates all four binary choices, retaining only feasible ones.

    يعدد التعبير كل الخيارات الثنائية الأربعة، ويحتفظ بالممكنة فقط.

Expected resultالنتيجة المتوقعة
Integer: n=3,C=1200CU; binary: (yA,yB)=(0,1),C=1000CU
09

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.

Model and unitsالنموذج والوحدات
minC=p+3g+5u [CU for 1h]; p+g=6kW; 0≤p≤8u; g≥0; u∈{0,1}

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 إن كان المولد مسموحا له بإنتاج الاستطاعة أم لا.

Grid / الشبكةg ≥ 0Generator / مولد0 ≤ p ≤ 8uBusعقدةLoad / حمل6 kWPower balance: p + g = 6 kWu ∈ {0, 1}C = p + 3g + 5u
Blue arrow: imported grid power g. Green arrow: generator power p. Yellow arrow: 6 kW delivered to the load. These are directions of positive active power, not a detailed AC power-flow model.السهم الأزرق: الاستطاعة المستجرة من الشبكة g. السهم الأخضر: استطاعة المولد p. السهم الأصفر: استطاعة الحمل 6 kW. الأسهم تبين الاتجاه الموجب للاستطاعة الفعالة، ولا تمثل نموذجا تفصيليا لجريان الاستطاعة المتناوبة.

Graphical solution — compare both binary operating casesرسم الحل — مقارنة حالتي التشغيل الثنائيتين

Case 1: u = 0 — Generator OFF

Grid / الشبكةg = 6 kW Generator / مولدp = 0 kW Busعقدة Load / حمل6 kW u = 0 (OFF)g + p = 6 kWC = 18 CUFeasible / حل مسموح
All 6 kW comes from the grid; total cost is 18 CU.تأتي الاستطاعة كاملة من الشبكة، أي 6 kW، وتبلغ الكلفة 18 وحدة نقدية.

Case 2: u = 1 — Generator ON

Grid / الشبكةg = 0 kW Generator / مولدp = 6 kW Busعقدة Load / حمل6 kW u = 1 (ON)g + p = 6 kWC = 11 CUGlobal optimum / الحل الأمثل
Generator supplies 6 kW; grid import is zero; cost is 11 CU, the global optimum.يغذي المولد الحمل باستطاعة 6 kW، والاستجرار من الشبكة صفر، والكلفة 11 وحدة نقدية، وهو الحل الأمثل العالمي.

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منحنى الكلفة — حالة التشغيل الخطية وحالة الإيقاف المنفصلة

C (CU)p (kW)06111823u=1: C=23−2pOptimal: (6,11)u=0: (0,18)
Blue segment: generator ON (u=1), with 0≤p≤6 and C=23−2p; cost decreases with generator output. Green: the on-case optimum and global MILP minimum (p=6,C=11). Yellow: separate generator-OFF decision (p=0,C=18). The two points at p=0 have different costs because their binary operating states differ.القطعة الزرقاء: المولد يعمل u=1 ضمن المجال 0≤p≤6، وتكون الكلفة C=23−2p؛ لذلك تنخفض مع زيادة إنتاجه. النقطة الخضراء: الحل الأمثل عند p=6 وكلفة C=11. النقطة الصفراء: حالة الإيقاف المستقلة عند p=0 وكلفة C=18. تختلف الكلفتان عند استطاعة صفر لأن حالتي المتغير الثنائي مختلفتان.

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الحل اليدوي بالآلة الحاسبة العادية

  1. Case u=0: p≤0, hence p=0. Balance gives g=6. Cost C=0+3×6+0=18 CU.

    الحالة u=0: القيد يفرض p=0، ومن التوازن g=6. الكلفة 0+3×6+0=18.

  2. Case u=1: g=6−p and 0≤p≤6 because grid import cannot be negative.

    الحالة u=1: g=6−p والمجال 0≤p≤6 لأن الاستجرار من الشبكة غير سالب.

  3. Substitute: C=p+3(6−p)+5=23−2p. It decreases with p, so choose p=6, g=0.

    نعوض C=p+3(6−p)+5=23−2p. التابع متناقص؛ فنختار p=6 وg=0.

  4. On-cost total C=6+0+5=11 CU. Compare 11<18: u=1 is best; saving=7 CU.

    كلفة التشغيل 6+0+5=11. بالمقارنة 11<18 نختار u=1، والتوفير 7.

  5. Check p≤8u: 6≤8; balance 6+0=6; u is binary. This complete enumeration proves optimality for the two cases.

    نفحص 6≤8 والتوازن 6+0=6، وu ثنائية. تعداد الحالتين بالكامل يثبت الأمثلية لهذا المثال.

Python · Kaggle · 06
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شرح الكود

  1. The first matrix row encodes balance; the second encodes p−8u≤0. Lower −infinity leaves this inequality without an artificial lower bound.

    يمثل الصف الأول التوازن، والثاني p−8u≤0. نستخدم −لانهاية لتجنب فرض حد أدنى مصطنع على هذا القيد.

  2. integrality=[0,0,1] leaves p and g continuous and makes u integer; its [0,1] bounds make it binary.

    تبقي integrality=[0,0,1] الاستطاعتين مستمرتين وتجعل u صحيحة؛ وحدود [0,1] تجعلها ثنائية.

  3. 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.

    هذه كلفة ثابتة للفترة، وليست نموذجا زمنيا لانتقال الإقلاع؛ الجدولة الواقعية تضيف قيود الانتقال وأزمنة التشغيل والتوقف الدنيا.

Expected resultالنتيجة المتوقعة
p=6kW; g=0kW; u=1; C=11CU
10

Mixed-Integer Quadratic Programming (MIQP) — on/off plus quadratic costs

البرمجة التربيعية المختلطة — التشغيل والكلفة

Use the previous 6 kW demand and 8 kW generator, but quadratic generator cost 0.5p2 and fixed cost 2 CU. Grid cost remains 3 CU/kWh for one hour.

نستخدم الحمل 6 kW وسعة المولد 8 kW، لكن كلفة المولد 0.5p2 والكلفة الثابتة 2. تبقى كلفة الشبكة 3 وحدات نقدية/kWh لمدة ساعة.

Model and unitsالنموذج والوحدات
minC=0.5p2+3g+2u; p+g=6; 0≤p≤8u; g≥0; u∈{0,1}

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 / المولد متوقف

Grid / الشبكةg = 6 kWGenerator / مولدp = 0 kWBus / عقدةp + g = 6Load / حمل6 kWu = 0 (OFF)C = 18 CUFeasible / مسموح
Grid covers all 6 kW, generator is off, and cost is 18 CU.تغطي الشبكة الحمل كله باستطاعة 6 kW، والمولد متوقف، وتبلغ الكلفة 18 وحدة نقدية.

Case 2 — Generator ON / المولد يعمل

Grid / الشبكةg = 3 kWGenerator / مولدp = 3 kWBus / عقدةp + g = 6Load / حمل6 kWu = 1 (ON)C = 15.5 CUOptimal / أمثل
The optimal sharing is generator 3 kW plus grid 3 kW. Total cost is 15.5 CU.التوزيع الأمثل هو 3 kW من المولد و3 kW من الشبكة، بكلفة كلية 15.5 وحدة نقدية.

Cost curve and optimal operating pointمنحنى الكلفة ونقطة التشغيل المثلى

C (CU)p (kW)0361815.5u=1, p=3, C=15.5u=0, C=18u=1: quadratic cost
Blue: C(p)=0.5(p−3)²+15.5 when u=1 and 0≤p≤6. Green: the minimum at p=3. Yellow: the separate feasible off decision u=0, p=0, C=18. The off decision is one point, not a second continuous cost curve.بالأزرق: منحنى الكلفة C(p)=0.5(p−3)²+15.5 عندما يكون u=1 ضمن المجال 0≤p≤6. بالأخضر: الحل الأدنى عند p=3. بالأصفر: قرار إيقاف المولد u=0 بكلفة 18، وهو نقطة منفصلة وليس منحنى مستمرا ثانيا.

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الحل اليدوي بالآلة الحاسبة العادية

  1. u=0 forces p=0, g=6, C=18.

    عند u=0 يكون p=0 وg=6 والكلفة 18.

  2. u=1: substitute g=6−p to get C=0.5p2−3p+20 on 0≤p≤6.

    عند u=1 نعوض g=6−p فنحصل على C=0.5p2−3p+20 ضمن 0≤p≤6.

  3. Complete the square: C=0.5(p−3)2+15.5. Thus p=3, g=3 is the on-case minimum.

    نكمل المربع C=0.5(p−3)2+15.5؛ إذن p=3 وg=3 يحققان أدنى كلفة في حالة التشغيل.

  4. Check C=0.5×9+3×3+2=4.5+9+2=15.5 CU. Endpoints give C(0)=20 and C(6)=20.

    نتحقق من الكلفة 0.5×9+3×3+2=15.5. ويعطي الطرفان C(0)=20 وC(6)=20.

  5. Compare cases: 15.5<18; choose u=1, p=g=3kW.

    نقارن الحالتين: 15.5<18؛ نختار u=1 وp=g=3kW.

Python · Kaggle · 07
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شرح الكود

  1. 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.

  2. minimize_scalar solves only the one-dimensional continuous subproblem; answers keeps both cases for comparison.

    تحل minimize_scalar المسألة الجزئية المستمرة أحادية المتغير فقط؛ وتحتفظ answers بالحالتين للمقارنة.

Expected resultالنتيجة المتوقعة
C=15.5CU; p=3kW; g=3kW; u=1
11

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 p3 and period fixed cost 2 CU. The cubic coefficient has units CU/kW3 for this period.

في مثال توزيع مبسط لمدة ساعة، الطلب 4 kW وسعر الشبكة 4 وحدات نقدية/kWh وكلفة المولد p3 مع كلفة ثابتة 2. وحدة المعامل التكعيبي هي وحدة نقدية/kW3 لهذه الفترة.

Model and unitsالنموذج والوحدات
minC=p3+4g+2u; p+g=4; 0≤p≤4u; g≥0; u∈{0,1}

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 / المولد متوقف

Grid / الشبكةg = 4 kWGenerator / المولدp = 0 kWBus / عقدةp + g = 4Load / الحمل4 kWu = 0C = 16 CU
Generator off: the grid supplies all demand.المولد متوقف، والشبكة تغذي كامل الحمل.

Case 2 — Optimal dispatch / الحل الأمثل

Grid / الشبكةg = 2.845 kWGenerator / المولدp = 1.155 kWBus / عقدةp + g = 4Load / الحمل4 kWu = 1C = 14.921 CU
Optimal split between generator and grid.الحل الأمثل: تتقاسم الشبكة والمولد تغذية الحمل.

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 / المجال الكامل

C (CU)p (kW)04700min ≈ 14.921OFF = 161.155u = 1

Zoom near optimum / تكبير قرب الحل الأمثل

C (CU)p (kW)022014min ≈ 14.921OFF = 161.155u = 1

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الحل اليدوي بالآلة الحاسبة العادية

  1. u=0: p=0,g=4,C=16CU.

    عند u=0: p=0 وg=4 والكلفة 16.

  2. u=1: g=4−p; C=p3−4p+18,0≤p≤4.

    عند u=1: g=4−p؛ والكلفة C=p3−4p+18 ضمن 0≤p≤4.

  3. Differentiate: C′=3p2−4=0. Thus p2=4÷3 and p=4÷3≈1.154701kW. The negative root is infeasible.

    نشتق C′=3p2−4=0. إذن p2=4÷3، والجذر الممكن p=4÷3≈1.154701kW. الجذر السالب خارج المجال.

  4. g=4−1.154701≈2.845299. Compute C≈1.539601+11.381198+2=14.920799 CU.

    نحسب g≈2.845299. ثم C≈1.539601+11.381198+2=14.920799.

  5. The derivative changes from negative to positive at this root. Endpoints C(0)=18 and C(4)=66 are larger. Compare on-case 14.920799 with off-case 16: operating the generator is cheaper.

    تتغير إشارة المشتقة من سالبة إلى موجبة عند هذا الجذر. كلفتا الطرفين C(0)=18 وC(4)=66 أكبر. نقارن كلفة التشغيل 14.920799 بكلفة الإيقاف 16؛ لذا يكون تشغيل المولد أقل كلفة.

Python · Kaggle · 08
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شرح الكود

  1. Enumeration plus a one-variable solve is complete here because both binary states are covered and the on-case is convex on p≥0.

    التعداد مع حل متغير واحد تام هنا لأننا غطينا الحالتين، ومسألة التشغيل محدبة على p≥0.

  2. A general nonconvex MINLP needs more than this small enumeration. A local NLP result in each branch does not generally prove global optimality.

    تحتاج MINLP العامة غير المحدبة إلى أكثر من هذا التعداد الصغير؛ والحل المحلي لكل فرع لا يثبت العالمية عموما.

Expected resultالنتيجة المتوقعة
C≈14.920799CU; p≈1.154701; g≈2.845299kW; u=1
12

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ثلاثة مكونات أساسية في النموذج

  1. 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:

    الصياغة المضغوطة لنموذج الشبكة غير الخطي: بدلا من كتابة معادلات جريان الاستطاعة الفعالة والردية بالتفصيل، نجمع معادلات النظام وحدود التشغيل في تابعين مع إظهار الاعتماد على الزمن صراحة:

    f(x,u,t)=0   g(x,u,t)≤0

    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 قيود عدم المساواة، ومنها حدود الجهد والتيار والقيم الاسمية للتجهيزات. تعد معاملات الشبكة الثابتة مضمنة ضمن التابعين. ولا تعني هذه الكتابة المختصرة أن المعادلات أصبحت خطية‏.

  2. 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) Δt
  3. Binary 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‏.

Pgrid,ₜ + Pwind,ₜ + Pdch,ₜ = Pload,ₜ + Pch,ₜ
Period / الفترةWind (kW)Load (kW)uCharge (kW)Discharge (kW)Grid (kW)Battery at end (kWh)
12612064
208001.86.22

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 / الشحن

Grid / شبكة6 kWWind / رياح2 kWBus / عقدةBalanceLoad / حمل6 kWBattery / بطارية2 kWu = 1E: 2 → 4 kWh6 + 2 = 6 + 2Power balance (kW)
Grid and wind supply the load and charge the battery.تغذي الشبكة والرياح الحمل مع شحن البطارية.

Period 2 — Discharging / التفريغ

Grid / شبكة6.2 kWWind / رياح0 kWBus / عقدةBalanceLoad / حمل8 kWBattery / بطارية1.8 kWu = 0E: 4 → 2 kWh6.2 + 1.8 = 8Power balance (kW)
Grid and battery discharge supply the load; wind is zero.تغذي الشبكة والبطارية الحمل، ولا يوجد توليد رياح في هذه الفترة.

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 / الكلفة التوضيحية تبعا لاستطاعة الشبكة

Jₜ (CU)gₜ (kW)0910t = 1: 6.36 CUt = 2: 6.5844 CUJₜ = gₜ + 0.01gₜ²
The markers show the selected feasible schedule, not an optimized solution. Period costs are 6.36 and 6.5844 CU; their sum is 12.9444 CU.تمثل النقطتان برنامج التشغيل المسموح المختار، ولا تمثلان حلا أمثل مثبتا. كلفتا الفترتين 6.36 و6.5844 وحدة نقدية، ومجموعهما 12.9444 وحدة نقدية.

Verify each period by handالتحقق اليدوي في كل فترة

  1. Period 1: grid purchase = demand − wind + charge = 6−2+2=6 kW. The battery energy increases from 2 to 4 kWh.

    الفترة الأولى: نحسب الاستطاعة المستجرة من الشبكة بطرح توليد الرياح من الطلب ثم إضافة استطاعة شحن البطارية‏.

    Pgrid,1=6−2+2=6 kW

    تزداد الطاقة المخزنة في البطارية من 2 kWh إلى 4 kWh عند نهاية الفترة‏.

    E₂ = 2 + 1×2×1 = 4 kWh
  2. Period 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، ولذلك نطرح استطاعة التفريغ من الطلب لحساب استطاعة الشبكة‏.

    Pgrid,2=8−1.8=6.2 kW

    نحسب انخفاض الطاقة المخزنة بقسمة استطاعة التفريغ على كفاءة التفريغ وضرب الناتج في مدة الفترة‏.

    ΔE=1.80.9×1=2 kWh
    E₃ = 4 − (1.8/0.9)×1 = 2 kWh
  3. The 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‏.

Python · Kaggle · dynamic MINLP feasibility
# 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))
13

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.

Model and unitsالنموذج والوحدات
gt=1+et−et−1≥0 [kWh]; e0=e2=0; e1∈{0,1}; minΣctgt

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 / شحن

Grid / شبكة2 kWBus / عقدةbalanceLoad / حمل1 kWBattery / بطارية1 kWCost = 2 CU2 = 1 + 1kW balance
Grid covers the load and charges the battery.تغطي الشبكة الحمل وتشحن البطارية خلال الفترة الأولى.

Period 2 — discharge / تفريغ

Grid / شبكة0 kWBus / عقدةbalanceLoad / حمل1 kWBattery / بطارية1 kWCost = 0 CU0 + 1 = 1kW balance
The battery supplies the load; no grid purchase is needed.تغذي البطارية الحمل خلال الفترة الثانية دون شراء طاقة من الشبكة.

Battery-state plot and discrete cost comparisonمنحنى طاقة البطارية ومقارنة الكلفة للقرارات المتقطعة

Optimal battery energy trajectory / مسار طاقة البطارية الأمثل

E (kWh)Time / الزمن01startt = 1t = 2Charge +1Discharge −1
The battery starts empty, stores 1 kWh after the cheap period and returns to zero after the expensive period.تبدأ البطارية فارغة، ثم تخزن 1 kWh بعد الفترة منخفضة السعر، وتعود فارغة بعد الفترة مرتفعة السعر.

Two feasible schedules / مقارَنة برنامجي التشغيل

C (CU)e₁ (kWh)0152No storage: 5 CUOptimal: 2 CUOnly the two points are feasible
The intermediate battery state is discrete: only e₁=0 or e₁=1 is allowed. The dashed connector is a visual guide, not a continuous feasible cost curve. The lower point proves the global minimum among these two schedules.طاقة البطارية بعد الفترة الأولى متقطعة، فلا يسمح إلا بالقيمتين 0 أو 1 kWh. الخط المتقطع بين النقطتين للتوضيح فقط ولا يمثل حلولا مستمرة مسموحة. النقطة الأقل كلفة هي الحل الأمثل بين الخيارين.

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الحل اليدوي بالآلة الحاسبة العادية

  1. The state e is stored energy. In the last period t=2, incoming e1=0 gives g2=1+0−0=1 and remaining cost J2(0)=4.

    الحالة e هي الطاقة المخزنة. في الفترة الأخيرة t=2، إذا كانت e1=0 فاستجرار الشبكة g2=1+0−0=1، والكلفة المتبقية J2(0)=4.

  2. If e1=1, discharge to e2=0: g2=1+0−1=0 and J2(1)=0.

    إذا كانت e1=1 نفرغ إلى e2=0؛ فيكون g2=1+0−1=0 والكلفة J2(1)=0.

  3. Step backward to t=1, e0=0. Choose e1=0: g1=1 and total=1×1+J2(0)=1+4=5.

    نرجع إلى t=1 وe0=0. إذا اخترنا e1=0 فإن g1=1 والكلفة الكلية 1×1+J2(0)=1+4=5.

  4. Choose e1=1: g1=2, total=1×2+J2(1)=2+0=2 CU.

    إذا اخترنا e1=1 فإن g1=2 والكلفة الكلية 1×2+J2(1)=2+0=2.

  5. Select e1=1: buy 2 kWh in period 1, buy zero in period 2; saving=5−2=3 CU. Check g₁+g₂=2 equals total load and final energy is zero.

    نختار e1=1: نشتري 2 kWh أولا، وصفرا ثانيا؛ التوفير 5−2=3. ومجموع الاستجرار 2 يساوي الحمل الكلي والبطارية فارغة في النهاية.

Python · Kaggle · 09
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شرح الكود

  1. J starts with zero terminal cost for empty storage and infinity for a nonempty terminal battery.

    نبدأ J بكلفة نهائية صفر للبطارية الفارغة، ولانهاية للبطارية غير الفارغة.

  2. The backward loop calculates every allowed transition and adds its immediate cost to future cost J[new].

    تحسب الحلقة العكسية كل انتقال مسموح وتضيف كلفته الآنية إلى كلفة المستقبل J[new].

  3. 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.

Expected resultالنتيجة المتوقعة
Minimum cost=2 CU; e1=1kWh; grid energies=[2,0] kWh
14

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 q∈[0,4] 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 لكل منهما. نشتري مسبقا q∈[0,4] بسعر 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 / الوحدة
sScenario index.فهرس السيناريو الذي يحدد حالة التوليد الشمسي.—
L,HLow-PV and high-PV scenarios.الحالة منخفضة التوليد الشمسي والحالة مرتفعة التوليد الشمسي.—
PVsAvailable photovoltaic energy in scenario s: 0 or 4.الطاقة الشمسية المتاحة في السيناريو المعني، وقيمتها في المثال إما 0 أو 4.kWh
DFixed demand; D=4 in this example.طاقة الحمل المطلوبة، وهي ثابتة في المثال وتساوي 4.kWh
qFirst-stage grid energy purchased before PV is known; 0≤q≤4.الطاقة المشتراة مسبقا من الشبكة قبل معرفة التوليد الشمسي، وهي متغير القرار الأول.kWh
rsExtra energy purchased after observing scenario s to cover any shortfall.الطاقة الإضافية المشتراة بعد ظهور السيناريو لتغطية العجز، ولا يمكن أن تكون سالبة.kWh
Cs(q)Total operating cost in scenario s for advance decision q.الكلفة الإجمالية في سيناريو معين عند اتخاذ قرار شراء مسبق محدد.CU
CL,CHCosts of the low-PV and high-PV scenarios, respectively.كلفة حالة غياب الشمس وكلفة حالة وفرة الشمس، على الترتيب.CU
psProbability of scenario s; probabilities sum to 1.احتمال حدوث السيناريو، ومجموع احتمالات السيناريوهات يساوي 1.—
E[C]Expected cost: sum of probability-weighted scenario costs. E denotes expectation, not energy.الكلفة المتوقعة الناتجة عن ضرب كلفة كل سيناريو باحتماله ثم جمع القيم. هنا يرمز E إلى التوقع الرياضي وليس الطاقة.CU
zUpper bound on every scenario cost in the robust linear reformulation.متغير مساعد يمثل حدا أعلى لكلفة جميع السيناريوهات في الصياغة الخطية المتينة.CU
q∗Optimal advance-purchase decision.قيمة الشراء المسبق التي تحقق أقل كلفة وفق معيار التحسين المختار.kWh
min,maxmin selects the smallest value; max selects the largest.ترمز الأولى إلى إيجاد أصغر قيمة، والثانية إلى إيجاد أكبر قيمة أو أسوأ كلفة.—
2,5Advance and later emergency purchase prices, respectively.سعر الطاقة المشتراة مسبقا وسعر الطاقة الإضافية بعد ظهور السيناريو، على الترتيب.CU/kWh
CUCost 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. وهكذا نميز بين قرار الشراء المسبق والشراء الإضافي الذي يتغير حسب السيناريو.

rL=max(4−0−1,0)=3 kWh   CL(1)=2×1+5×3=17 CU
Model and unitsالنموذج والوحدات
rs=max(4−PVs−q,0); Cs(q)=2q+5rs; stochastic: min E[C]; robust: minmaxsCs

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 / غياب الشمس

Advance / مسبقq = 4 kWhPV / شمسي0 kWhRecourse / تعويضr = 0 kWhBus / عقدةBalanceLoad / حمل4 kWhC = 8 CUPV curtailment: 0 kWhOne-hour energy balance
With no PV, the advance purchase serves the 4 kWh load.عند غياب الشمس تغطي الطاقة المشتراة مسبقا كامل الحمل.

Scenario H — High PV / وفرة الشمس

Advance / مسبقq = 4 kWhPV / شمسي4 kWhRecourse / تعويضr = 0 kWhBus / عقدةBalanceLoad / حمل4 kWhC = 8 CUPV curtailment: 4 kWhOne-hour energy balance
With 4 kWh PV available, the 4 kWh advance purchase still meets demand and the surplus PV is curtailed.عند وفرة الشمس تغطي الطاقة المشتراة مسبقا الحمل، ويهدر التوليد الشمسي الفائض وفقا لافتراضات المثال.

Scenario, robust and expected-cost curvesمنحنيات كلفة السيناريوهات والمتانة والكلفة المتوقعة

Scenario costs and robust envelope / كلف السيناريوهات وأسوأ حالة

C (CU)q (kWh)04210Low PV: 20−3qHigh PV: 2qWorst caseq*=4, C=8
The worst-case cost is the upper envelope of scenario costs and reaches its minimum of 8 CU at q=4.تمثل كلفة أسوأ حالة الغلاف العلوي لكلف السيناريوهات، وتصل إلى أصغر قيمة عند شراء الطاقة مسبقا بمقدار الحمل الكامل.

Expected cost and probability / الكلفة المتوقعة والاحتمال

C (CU)q (kWh)04210P(low)=0.5: 10−0.5qP(low)=0.2: 4+qq*=4, C=8q*=0, C=4
When the low-PV probability changes, the slope of expected cost changes sign, and the optimum moves from q=4 to q=0.يتغير ميل منحنى الكلفة المتوقعة عند تغيير احتمال غياب الشمس، فينتقل القرار الأمثل من شراء كامل الطاقة مسبقا إلى عدم الشراء المسبق.

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الحل اليدوي بالآلة الحاسبة العادية

  1. Low PV: rL=4−q and CL=2q+5(4−q)=20−3q.

    عند غياب الشمس rL=4−q والكلفة CL=2q+5(4−q)=20−3q.

  2. High PV: rH=0 and CH=2q.

    عند الشمس العالية rH=0 والكلفة CH=2q.

  3. Expected cost: 0.5(20−3q)+0.5(2q)=10−0.5q. It decreases, so q∗=4 and expected cost=8 CU.

    الكلفة المتوقعة 0.5(20−3q)+0.5(2q)=10−0.5q. وهي متناقصة؛ إذن q∗=4 والكلفة المتوقعة 8.

  4. Worst-case cost is max(20−3q,2q). Lines intersect at 20−3q=2q, so q=20÷5=4; worst cost=8. On [0,4], low-PV cost is the larger line and decreases to 8.

    كلفة أسوأ حالة max(20−3q,2q). يتقاطع الخطان عند 20−3q=2q، أي q=4؛ وأسوأ كلفة 8. على [0,4] خط الشمس المنخفضة هو الأعلى ويتناقص حتى 8.

  5. Change low-PV probability to 0.2: E[C]=0.2(20−3q)+0.8(2q)=4+q. The stochastic optimum becomes q=0, expected cost=4; the robust optimum stays q=4.

    نغير احتمال غياب الشمس إلى 0.2: الكلفة المتوقعة 0.2(20−3q)+0.8(2q)=4+q. يصبح الحل العشوائي q=0 وكلفته المتوقعة 4، بينما يبقى الحل المتين q=4.

Python · Kaggle · 10
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شرح الكود

  1. q is shared across scenarios because it is chosen before observing PV; recourse variables can depend on the observed scenario.

    q مشتركة بين السيناريوهات لأنها تختار قبل معرفة الشمس؛ ويمكن أن تعتمد متغيرات التعويض على السيناريو المشاهد.

  2. Negative coefficients convert the required ≥ inequality to linprog’s≤ convention. z bounds every scenario cost from above.

    تحول الإشارات السالبة قيد ≥ إلى الصيغة ≤ المطلوبة في linprog. والمتغير z حد أعلى لكلفة كل سيناريو.

  3. These examples remain LP after reformulation. Robust does not mean stochastic with an arbitrarily chosen probability.

    يبقى المثالان LP بعد إعادة الصياغة. ولا تعني المتانة مسألة عشوائية باحتمال نختاره اعتباطيا.

Expected resultالنتيجة المتوقعة
prob=.5: q=4,E[C]=8; prob=.2: q=0,E[C]=4; robust: q=4,z=8
15

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 / الوحدة
p1Power from generator 1, cheaper but more polluting.استطاعة المولد الأول، الأقل كلفة والأعلى انبعاثا.kW
p2Power from generator 2, costlier but cleaner.استطاعة المولد الثاني، الأعلى كلفة والأقل انبعاثا.kW
DDemand, fixed at 10 kW for one hour.استطاعة الحمل المطلوبة، وتساوي 10 في المثال.kW
CTotal monetary cost of generation over one hour.الكلفة النقدية الكلية للتوليد خلال ساعة واحدة.CU
ETotal emitted mass in the one-hour period; not the expectation operator of Unit 14.كتلة الانبعاثات الكلية خلال الساعة، ولا تعني هنا مؤثر القيمة المتوقعة في الوحدة 14.kg
ciEnergy price of generator i: 1 or 3.سعر طاقة المولد ذي الفهرس المعني، وتبلغ قيمته 1 أو 3.CU/kWh
aiEmission factor of generator i: 0.8 or 0.2.معامل الانبعاث للمولد المعني، وقيمته 0.8 أو 0.2.kg/kWh
iGenerator index, i=1 or i=2.دليل المولد الذي يميز المولد الأول عن الثاني.—
EmaxUpper limit on total emissions, set to 5 in the example.الحد الأعلى المسموح لكتلة الانبعاثات، ويساوي 5 في المثال.kg
λCarbon price converting an emitted kilogram into monetary cost.سعر الكربون المستخدم لتحويل كتلة الانبعاثات إلى كلفة نقدية مكافئة.CU/kg
C+λEWeighted scalar objective combining cost and monetized emissions.تابع هدف مركب يجمع الكلفة الأصلية وكلفة الانبعاثات المحولة نقديا.CU
εEmissions cap in the epsilon-constraint method, such as ε=5.رمز الحد المفروض على الانبعاثات عند استخدام طريقة قيد إبسيلون.kg
ParetoA feasible solution is Pareto-efficient if no other solution improves one objective without worsening the other.الحل الكفء وفق باريتو هو حل لا يمكن تحسين أحد هدفيه دون زيادة سوء الهدف الآخر.—
CU,kgIllustrative 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. يؤدي ضرب سعر الكربون بوحدة نقدية لكل كيلوغرام في كتلة الانبعاثات إلى كلفة نقدية، فتتوافق الوحدات في تابع الهدف المركب.

Model and unitsالنموذج والوحدات
p1+p2=10; 0≤p1,p2≤10; minC=p1+3p2 [CU]; minE=0.8p1+0.2p2 [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 / الحل مع قيد الانبعاثات

Generator 1 / مولد 15 kWGenerator 2 / مولد 25 kWBus / عقدةp₁ + p₂Load / حمل10 kW

Cost and emissions plots — the Pareto trade-offمنحنيات الكلفة والانبعاثات — المفاضلة وفق باريتو

Cost–emissions Pareto frontier / جبهة باريتو للكلفة والانبعاثات

C (CU)E (kg)283010E=5, C=2030, 210, 8
Moving right reduces monetary cost but increases emitted mass. Every point on the segment is Pareto-efficient; the emissions cap E≤5 kg selects the marked point C=20 CU.كلما اتجهنا يمينا انخفضت الكلفة وازدادت الانبعاثات. جميع نقاط القطعة كفؤة وفق باريتو. يحدد قيد الانبعاثات الأقصى 5 kg النقطة التي تبلغ كلفتها 20 CU.

Objectives versus generator 1 / الهدفان تبعا لاستطاعة المولد الأول

C (CU)E (kg)p₁ (kW)010C = 30 − 2p₁E = 2 + 0.6p₁p₁ = 5
The lines use separate conceptual vertical scales because cost and emission have different units. Read their exact values from the formulas and Pareto plot.يعرض الخطان اتجاه التغير بمقياسين رأسيين مختلفين لأن الكلفة والانبعاثات لهما وحدتان مختلفتان. تؤخذ القيم العددية الدقيقة من المعادلات ورسم باريتو.

Hand calculation — ordinary calculatorالحل اليدوي بالآلة الحاسبة العادية

  1. Set p2=10−p1: C=30−2p1 and E=2+0.6p1. Cost falls while emissions rise.

    نعوض p2=10−p1 فنحصل على C=30−2p1 وE=2+0.6p1. تنخفض الكلفة بينما ترتفع الانبعاثات.

  2. At p1=0: (C,E)=(30,2). At p1=10: (C,E)=(10,8). Neither dominates the other; every point on this segment is Pareto efficient.

    عند p1=0 يكون (C,E)=(30,2)، وعند p1=10 يكون (10,8). لا يتفوق أحدهما في الهدفين معا؛ وكل نقطة على القطعة حل كفء وفق باريتو.

  3. Use an emissions cap E≤5kg: 2+0.6p1≤5 ⇒ p1≤3÷0.6=5kW.

    نفرض حدا للانبعاث E≤5kg: إذن 2+0.6p1≤5 ومنه p1≤3÷0.6=5kW.

  4. Minimize cost under that cap: maximize p1 within [0,5], giving p1=p2=5, C=20CU and E=5kg.

    لتصغير الكلفة ضمن الحد نختار أكبر p1 ضمن [0,5]؛ فنحصل على p1=p2=5 والكلفة 20 والانبعاث 5 kg.

  5. A carbon price λ [CU/kg] gives C+λE, with slope −2+0.6λ. The switch is λ=2÷0.6≈3.3333 CU/kg; at exactly this price all feasible dispatches tie.

    يعطي سعر الكربون λ بوحدة نقدية/kg الهدف C+λE وميله −2+0.6λ. قيمة التحول λ≈3.3333؛ وعندها تتساوى كل التوزيعات الممكنة في الهدف المركب.

Python · Kaggle · 11
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شرح الكود

  1. This is the epsilon-constraint approach: optimize cost while limiting emissions. The resulting subproblem is LP.

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

  2. Never add currency and kilograms without a conversion coefficient or a clearly defined normalization.

    لا نجمع الوحدات النقدية والكيلوغرامات دون معامل تحويل أو تطبيع معرف بوضوح.

Expected resultالنتيجة المتوقعة
p1=p2=5kW; C=20CU; E=5kg
16

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 n2 or n³, 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 / الوحدة
nNumber of variables or a generic problem-size measure in the growth examples.عدد المتغيرات أو مقياس عام لحجم المسألة في أمثلة النمو.count
n2,n3Illustrative polynomial operation counts.مثالان لعدد عمليات ينمو وفق مربع حجم المسألة أو مكعبه.operations
2nIllustrative exponential growth in problem size.مثال على نمو عدد العمليات أسيا مع حجم المسألة.operations
BNumber of binary decision variables.عدد متغيرات القرار الثنائية التي تأخذ 0 أو 1.count
2BNumber of all possible binary assignments before pruning.عدد جميع تركيبات المتغيرات الثنائية قبل أي استبعاد ذكي للحالات.cases
TNumber of time steps; 24 when one day uses hourly periods.عدد الفترات الزمنية، ويساوي 24 عند تقسيم اليوم إلى ساعات.periods
2TTwo continuous dispatch decisions per time period.عدد متغيرات التشغيل المستمرة عند وجود متغيرين في كل فترة.variables
LP,QP,QCQPLinear, quadratic and quadratically constrained quadratic programming.البرمجة الخطية والتربيعية والبرمجة التربيعية ذات القيود التربيعية.—
MILP,MIQP,MINLPMixed-integer linear, quadratic and nonlinear programming.البرمجة الخطية والتربيعية وغير الخطية المختلطة بمتغيرات صحيحة.—
NLP,DPNonlinear Programming and Dynamic Programming.البرمجة غير الخطية والبرمجة الديناميكية.—
NP,NP-hardComplexity classes; NP-hard is a hardness classification, not a runtime formula.تصنيفات نظرية للتعقيد؛ ولا تعني NP-hard معادلة مباشرة لزمن التنفيذ.—
HMatrix of second partial derivatives used to test convexity.مصفوفة المشتقات الجزئية الثانية المستخدمة لفحص التحدب.depends
x,pNumerical solution vector x and power-decision vector p in the QP code.متجه الحل العددي ومتجه استطاعات المولدات في مثال البرمجة التربيعية.MW for p
CQuadratic optimization objective and the reported total generation cost.تابع الهدف التربيعي والكلفة الإجمالية المحسوبة للتوليد.CU/h
tolSmall numerical tolerance used to accept floating-point residuals, such as 10⁻³.هامش خطأ عددي صغير للتحقق من بواقي المعادلات وحدود المتغيرات.as checked quantity
CVXPY,cpPython modeling package and its short import alias.مكتبة بناء نماذج الأمثلية في بايثون واسمها المختصر في الكود.—
OSQP,CLARABELSolver names used for the convex quadratic example.اسما محللين عدديين للمثال التربيعي المحدب.—
OPTIMALSolver status asserting a solution meets its optimality criteria.حالة يعلنها المحلل عندما يستوفي الحل معايير الأمثلية المعتمدة فيه.—
NP≠PThe 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 تعيينا. هذه أعداد حالات البحث المباشر وليست تنبؤا بزمن تشغيل المحلل.

Model and unitsالنموذج والوحدات
ClassالفئةComplexity and guaranteesالتعقيد والضمانات
LPPolynomial-time algorithms exist (input bit length matters). Simplex itself has exponential worst cases.توجد خوارزميات متعددة الحدود؛ ويؤثر طول تمثيل البيانات. للسمبلكس حالات أسوأ أسية.
Convex QP / QCQPBroad standard classes admit efficient approximation algorithms under appropriate assumptions. Precision, conditioning and matrix size matter.تتوفر خوارزميات تقريب فعالة لفئات معيارية واسعة ضمن شروط مناسبة؛ الدقة والحالة العددية وحجم المصفوفات مؤثرة.
MILP / MIQP / MINLPNP-hard in general; small special cases can be easy. A convex continuous relaxation does not remove integer difficulty.صعبة من فئة NP-hard في الحالة العامة؛ وقد تسهل حالات خاصة صغيرة. تحدب الجزء المستمر لا يزيل صعوبة المتغيرات الصحيحة.
Nonconvex NLP / QCQPGlobal optimization is generally hard. Local convergence does not certify global optimality.الأمثلية العالمية صعبة عموما. التقارب المحلي لا يثبت العالمية.
DP / stochastic / robust / multiobjectiveComplexity 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 / تشغيل المولدين

Generator 1 / مولد 168.75 MWGenerator 2 / مولد 231.25 MWBus / عقدةp₁ + p₂Load / حمل100 MW

Quadratic optimum and complexity growthمنحنى الحل التربيعي ونمو التعقيد

Convex QP cost curve / منحنى كلفة البرمجة التربيعية المحدبة

C (CU/h)p₁ (MW)208046503550min: p₁=68.75C=3609.375
Feasible range is 20≤p₁≤80 MW because both generators have an 80 MW upper limit and together produce 100 MW. The unique minimum is p₁=68.75 MW and p₂=31.25 MW.يمتد المجال الممكن لاستطاعة المولد الأول من 20 إلى 80 MW لأن حد كل مولد 80 MW والحمل الكلي 100 MW. تتحقق الكلفة الدنيا عند استطاعتي توليد 68.75 MW و31.25 MW.

Illustrative growth rates / أمثلة على معدلات نمو التعقيد

log₁₀(count)n / B0202ᴮn²logarithmic y-axis
The vertical axis is logarithmic. The curves compare theoretical illustrative counts n² and 2ᴮ, not observed computation time for OSQP or CLARABEL. For 20 binary decisions there are 2²⁰ possible assignments.المحور الرأسي لوغاريتمي. يقارن الرسم بين نمو عدد العمليات التوضيحي n² ونمو عدد التركيبات الثنائية 2ᴮ، ولا يعرض أزمنة فعلية للمحللات. عند وجود 20 متغيرا ثنائيا يبلغ عدد التركيبات الممكنة 1048576.

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الحل اليدوي بالآلة الحاسبة العادية

  1. With B binary choices there are 2B assignments: B=1 gives 2; B=10 gives 1024; B=20 gives 1048576. This counts brute-force cases, not the actual runtime of branch-and-bound.

    مع B متغيرات ثنائية يوجد 2B تعيينا: عند 1 يوجد 2، وعند 10 يوجد 1024، وعند 20 يوجد 1048576. هذا تعداد مباشر للحالات وليس زمنا فعليا لخوارزمية التفرع والتحديد.

  2. For T hours with 2 dispatch variables per hour, there are 2T continuous variables before other states: T=24 gives 48. Adding 2 binary statuses per hour adds 48 binaries, whose naive enumeration has 248 cases.

    مع T ساعة ومتغيري استطاعة لكل ساعة، يوجد 2T متغيرا مستمرا قبل إضافة الحالات؛ عند T=24 يوجد 48. وإضافة حالتي تشغيل ثنائيتين لكل ساعة تعطي 48 متغيرا ثنائيا، وتعدادها المباشر 248 حالة.

  3. 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 rank solvers.

    لكل نتيجة نقارن الهدف وبواقي التوازن ومخالفات الحدود والصحة العددية وحالة الحل. لا نقارن الزمن إلا للنموذج والبيئة نفسيهما؛ ولا تصنف تجربة صغيرة واحدة المحللات.

Python · Kaggle · 12
# 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.

Expected resultالنتيجة المتوقعة
QP (OSQP / CLARABEL): P₁=68.75 MW, P₂=31.25 MW, C≈3609.375 CU/h
ModelالنموذجSuitable free toolsأدوات مجانية مناسبة
LP / MILPSciPy → HiGHS
Convex QPCVXPY → OSQP / CLARABEL
Dynamic MINLP (A-R-OPF)Pyomo / compatible MINLP solvers; verify feasibility and solver guarantees
NLPSciPy → SLSQP (local method)
MIQP / MINLP teaching casesExplicit 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.

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 Smax=5kVA and Q=3kvar. Maximum P?عاكس Smax=5kVA وQ=3kvar؛ ما أكبر P؟
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؟