LAB 06 | المختبر 06

AI-Based Optimal Power Flow

تدفق القدرة الأمثل باستخدام الذكاء الاصطناعي
Generation Cost, Network Losses, Voltage Constraints, and Intelligent Optimization
تكلفة التوليد وفواقد الشبكة وقيود الجهد والتحسين الذكي

🎯 Lab Objectives

  • Understand the difference between Economic Dispatch and OPF.
  • Formulate a simplified Optimal Power Flow problem.
  • Include generation cost and network losses in the objective.
  • Handle voltage and generation constraints.
  • Use penalty functions for infeasible operating points.
  • Implement a simplified evolutionary optimizer in MATLAB.
  • Interpret the optimal solution physically.

🎯 أهداف المختبر

  • فهم الفرق بين التوزيع الاقتصادي وOPF.
  • صياغة مسألة تدفق قدرة أمثل مبسطة.
  • إضافة تكلفة التوليد وفواقد الشبكة إلى دالة الهدف.
  • التعامل مع قيود الجهد والتوليد.
  • استخدام دوال العقوبة للحالات غير الممكنة.
  • تنفيذ خوارزمية تحسين تطورية مبسطة في MATLAB.
  • تفسير الحل الأمثل فيزيائيًا.

1. From Economic Dispatch to OPF

Economic Dispatch mainly determines generator active powers. Optimal Power Flow also considers the electrical network.

Economic Dispatch: min C(PG)
OPF: min J(PG,QG,V,θ,...)
OPF is an optimization problem constrained by the physics of power flow.

1. من التوزيع الاقتصادي إلى OPF

يركز التوزيع الاقتصادي أساسًا على استطاعات التوليد الفعالة، بينما يضيف OPF متغيرات وقيود الشبكة الكهربائية.

يمكن النظر إلى OPF على أنه تحسين مقيد بقوانين تدفق القدرة.

2. Simplified Two-Bus Network

Consider a simple educational two-bus system.

Bus 1

Generator bus

Bus 2

Load bus

The generator supplies:

PG

The load consumes:

PL = 100 MW

2. شبكة مبسطة من عقدتين

لدينا عقدة توليد وعقدة حمل، والهدف هو اختيار نقطة تشغيل اقتصادية مع مراعاة الفواقد والجهد.

PL = 100 MW

3. Simplified Network-Loss Model

For educational purposes, approximate active-power losses as:

Ploss = k PG²

where:

k = 0.0005

This is not a full AC power-flow model, but it introduces the idea that larger power transfers produce larger losses.

3. نموذج مبسط لفواقد الشبكة

Ploss = k PG²

هذا ليس نموذج AC كاملًا، لكنه يوضح للطالب أن الفواقد تزداد عادةً مع ارتفاع القدرة المنقولة.

4. Power-Balance Constraint

Generation must supply both the load and losses:

PG = PL + Ploss

Therefore:

PG - PL - kPG² = 0

4. قيد توازن القدرة

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

PG = PL + Ploss

5. Generator Cost

Assume:

C(PG)=0.005PG²+5PG+300

The first component of the objective is operating cost.

5. تكلفة التوليد

C(PG)=0.005PG²+5PG+300

تمثل هذه الدالة تكلفة تشغيل المولد عند قدرة معينة.

6. Simplified Voltage Model

To introduce voltage constraints, use the educational approximation:

V2 = 1 - r PG / 1000 + q Qc

with:

r = 0.08
q = 0.04

Qc represents a controllable reactive-power support variable.

6. نموذج مبسط للجهد

V2 = 1 - r PG / 1000 + q Qc

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

7. Voltage Constraint

0.95 ≤ V2 ≤ 1.05 pu

Operating points outside this range are penalized.

7. قيد الجهد

0.95 ≤ V2 ≤ 1.05 pu

يجب أن تبقى قيمة الجهد ضمن المجال المسموح.

8. Decision Variables

We optimize:

x = [PG,Qc]

with limits:

80 ≤ PG ≤ 140 MW
-0.5 ≤ Qc ≤ 0.5 pu

8. متغيرات القرار

x=[PG,Qc]

أي أننا نبحث عن قدرة التوليد والدعم الردي المناسبين.

9. Multi-Objective Cost

Define:

J = C(PG) + wL Ploss + Penalties

where:

wL = 100

The weight wL determines how strongly losses are penalized relative to cost.

9. دالة هدف متعددة العناصر

J = C(PG)+wL Ploss+العقوبات

تجمع دالة الهدف بين الاقتصاد وتقليل الفواقد واحترام القيود.

10. Power-Balance Penalty

ΔP = PG - PL - Ploss
PenaltyP = λP (ΔP)²

Use:

λP = 10000

10. عقوبة توازن القدرة

إذا لم يتحقق توازن القدرة، نضيف عقوبة كبيرة إلى دالة الهدف.

PenaltyP=λP(ΔP)²

11. Voltage Penalty

If:

V2 < Vmin

then:

PenaltyV = λV(Vmin-V2)²

and if:

V2 > Vmax

then:

PenaltyV = λV(V2-Vmax)²

11. عقوبة الجهد

تضاف عقوبة عندما يصبح الجهد أقل من الحد الأدنى أو أعلى من الحد الأعلى.

12. Complete Objective Function

Jpen = C(PG) + wLPloss + PenaltyP + PenaltyV
Feasible operating points with low cost and low losses become attractive to the optimizer.

12. دالة الهدف الكاملة

Jpen = تكلفة التوليد + تكلفة الفواقد + عقوبة التوازن + عقوبة الجهد

13. Population Representation

Each chromosome contains:

[PG,Qc]

Example:

[110,0.20]

13. تمثيل المجتمع

يمثل كل كروموسوم نقطة تشغيل كاملة:

[PG,Qc]

14. Optimization Parameters

popSize = 50;
maxGen  = 120;

Pc = 0.8;
Pm = 0.12;

sigmaPG = 5;
sigmaQ  = 0.05;

14. معاملات التحسين

نحدد حجم المجتمع وعدد الأجيال واحتمالات التزاوج والطفرة.

15. Initial Population

PGmin = 80;
PGmax = 140;

Qmin = -0.5;
Qmax = 0.5;

population = zeros(popSize,2);

population(:,1) = ...
    PGmin + rand(popSize,1)*(PGmax-PGmin);

population(:,2) = ...
    Qmin + rand(popSize,1)*(Qmax-Qmin);

15. المجتمع الابتدائي

نولد نقاط تشغيل عشوائية ضمن حدود PG وQc.

16. Evaluate One Operating Point

For one chromosome:

x=[PG,Qc]

calculate:

Ploss=kPG²
V2=1-rPG/1000+qQc
C=0.005PG²+5PG+300
ΔP=PG-PL-Ploss

16. تقييم نقطة تشغيل واحدة

لكل كروموسوم نحسب الفواقد والجهد وتكلفة التوليد وعدم توازن القدرة.

17. MATLAB Objective Evaluation

PG = population(i,1);
Qc = population(i,2);

Ploss = k*PG^2;

V2 = 1 - r*PG/1000 + q*Qc;

Cgen = 0.005*PG^2 + 5*PG + 300;

mismatch = PG - PL - Ploss;

penP = lambdaP*mismatch^2;

penV = 0;

if V2 < Vmin
    penV = lambdaV*(Vmin-V2)^2;
elseif V2 > Vmax
    penV = lambdaV*(V2-Vmax)^2;
end

J = Cgen + wLoss*Ploss + penP + penV;

17. حساب دالة الهدف في MATLAB

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

18. Selection, Crossover, Mutation

We use the same evolutionary logic introduced in Lab 05.

Selection
Crossover
Mutation
Constraint Bounds

18. الاختيار والتزاوج والطفرة

نستخدم الآلية نفسها التي تعلمناها في المختبر السابق، لكن على متغيري PG وQc.

19. Complete MATLAB Program

clear; clc; close all;

% ------------------------------------------------
% AI-Based Optimal Power Flow
% Simplified educational example
% ------------------------------------------------

PL = 100;

% Simplified loss coefficient
k = 0.0005;

% Simplified voltage model coefficients
r = 0.08;
q = 0.04;

% Voltage limits
Vmin = 0.95;
Vmax = 1.05;

% Decision-variable limits
PGmin = 80;
PGmax = 140;

Qmin = -0.5;
Qmax = 0.5;

% Objective weights
wLoss = 100;

lambdaP = 10000;
lambdaV = 10000;

% GA parameters
popSize = 50;
maxGen = 120;

Pc = 0.8;
Pm = 0.12;

sigmaPG = 5;
sigmaQ  = 0.05;

% ------------------------------------------------
% Initial population
% ------------------------------------------------

population = zeros(popSize,2);

population(:,1) = ...
    PGmin + rand(popSize,1)*(PGmax-PGmin);

population(:,2) = ...
    Qmin + rand(popSize,1)*(Qmax-Qmin);

bestHist = zeros(maxGen,1);

% ------------------------------------------------
% Evolutionary optimization
% ------------------------------------------------

for gen = 1:maxGen

    Jvec = zeros(popSize,1);

    for i = 1:popSize

        PG = population(i,1);
        Qc = population(i,2);

        % Network losses
        Ploss = k*PG^2;

        % Simplified voltage
        V2 = 1 - r*PG/1000 + q*Qc;

        % Generation cost
        Cgen = 0.005*PG^2 + 5*PG + 300;

        % Power mismatch
        mismatch = PG - PL - Ploss;

        % Power-balance penalty
        penP = lambdaP*mismatch^2;

        % Voltage penalty
        penV = 0;

        if V2 < Vmin

            penV = ...
                lambdaV*(Vmin-V2)^2;

        elseif V2 > Vmax

            penV = ...
                lambdaV*(V2-Vmax)^2;

        end

        % Penalized objective
        Jvec(i) = ...
            Cgen + ...
            wLoss*Ploss + ...
            penP + ...
            penV;

    end

    % ------------------------------------------------
    % Elitism
    % ------------------------------------------------

    [bestJ,bestIdx] = min(Jvec);

    elite = population(bestIdx,:);

    bestHist(gen) = bestJ;

    % ------------------------------------------------
    % New population
    % ------------------------------------------------

    newPopulation = zeros(size(population));

    newPopulation(1,:) = elite;

    kpop = 2;

    while kpop <= popSize

        % --------------------------------------------
        % Tournament Parent 1
        % --------------------------------------------

        a = randi(popSize);
        b = randi(popSize);

        if Jvec(a) < Jvec(b)
            parent1 = population(a,:);
        else
            parent1 = population(b,:);
        end

        % --------------------------------------------
        % Tournament Parent 2
        % --------------------------------------------

        a = randi(popSize);
        b = randi(popSize);

        if Jvec(a) < Jvec(b)
            parent2 = population(a,:);
        else
            parent2 = population(b,:);
        end

        % --------------------------------------------
        % Crossover
        % --------------------------------------------

        if rand < Pc

            alpha = rand;

            child1 = ...
                alpha*parent1 + ...
                (1-alpha)*parent2;

            child2 = ...
                (1-alpha)*parent1 + ...
                alpha*parent2;

        else

            child1 = parent1;
            child2 = parent2;

        end

        % --------------------------------------------
        % Mutation
        % --------------------------------------------

        if rand < Pm
            child1(1) = child1(1) + sigmaPG*randn;
        end

        if rand < Pm
            child1(2) = child1(2) + sigmaQ*randn;
        end

        if rand < Pm
            child2(1) = child2(1) + sigmaPG*randn;
        end

        if rand < Pm
            child2(2) = child2(2) + sigmaQ*randn;
        end

        % --------------------------------------------
        % Variable limits
        % --------------------------------------------

        child1(1) = ...
            min(max(child1(1),PGmin),PGmax);

        child1(2) = ...
            min(max(child1(2),Qmin),Qmax);

        child2(1) = ...
            min(max(child2(1),PGmin),PGmax);

        child2(2) = ...
            min(max(child2(2),Qmin),Qmax);

        % --------------------------------------------
        % Store offspring
        % --------------------------------------------

        newPopulation(kpop,:) = child1;

        if kpop+1 <= popSize
            newPopulation(kpop+1,:) = child2;
        end

        kpop = kpop + 2;

    end

    population = newPopulation;

end

% ------------------------------------------------
% Final evaluation
% ------------------------------------------------

Jvec = zeros(popSize,1);

for i = 1:popSize

    PG = population(i,1);
    Qc = population(i,2);

    Ploss = k*PG^2;

    V2 = 1 - r*PG/1000 + q*Qc;

    Cgen = 0.005*PG^2 + 5*PG + 300;

    mismatch = PG - PL - Ploss;

    penP = lambdaP*mismatch^2;

    penV = 0;

    if V2 < Vmin

        penV = ...
            lambdaV*(Vmin-V2)^2;

    elseif V2 > Vmax

        penV = ...
            lambdaV*(V2-Vmax)^2;

    end

    Jvec(i) = ...
        Cgen + ...
        wLoss*Ploss + ...
        penP + ...
        penV;

end

[bestJ,bestIdx] = min(Jvec);

best = population(bestIdx,:);

PGbest = best(1);
Qbest  = best(2);

PlossBest = k*PGbest^2;

V2best = ...
    1 - r*PGbest/1000 + q*Qbest;

Cbest = ...
    0.005*PGbest^2 + ...
    5*PGbest + 300;

mismatchBest = ...
    PGbest - PL - PlossBest;

% ------------------------------------------------
% Display results
% ------------------------------------------------

fprintf('AI-Based OPF Result\n\n')

fprintf('PG = %.4f MW\n',PGbest)

fprintf('Qc = %.4f pu\n',Qbest)

fprintf('Load = %.4f MW\n',PL)

fprintf('Losses = %.4f MW\n',PlossBest)

fprintf('Voltage V2 = %.4f pu\n',V2best)

fprintf('Power mismatch = %.6f MW\n', ...
        mismatchBest)

fprintf('Generation cost = %.4f\n', ...
        Cbest)

fprintf('Best objective J = %.4f\n', ...
        bestJ)

% ------------------------------------------------
% Convergence curve
% ------------------------------------------------

figure

plot(1:maxGen,bestHist,'LineWidth',1.6)

grid on

xlabel('Generation')
ylabel('Best Objective Function')

title('AI-Based OPF Convergence')

19. البرنامج الكامل في MATLAB

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

هذا نموذج تعليمي مبسط لفهم منطق OPF، وليس بديلًا عن حل AC-OPF كامل.

20. Interpreting the Final Solution

After optimization, check:

PG ≈ PL + Ploss
0.95 ≤ V2 ≤ 1.05

If both conditions are satisfied and the objective is low, the candidate represents a good operating point.

20. تفسير الحل النهائي

يجب التأكد من أن التوليد يغطي الحمل والفواقد، وأن الجهد ضمن الحدود.

الحل الأمثل ليس مجرد رقم صغير لدالة الهدف، بل يجب أن يكون قابلًا للتنفيذ فيزيائيًا.

21. Experiment 1 — Change Load

PL = 80;
PL = 100;
PL = 120;

Observe PG, losses, voltage, and total objective value.

21. التجربة 1 — تغيير الحمل

غيّر الحمل ولاحظ كيف تتغير القدرة المولدة والفواقد والجهد والحل الأمثل.

22. Experiment 2 — Increase Loss Weight

wLoss = 10;
wLoss = 100;
wLoss = 500;
A larger loss weight makes the optimizer care more strongly about reducing network losses.

22. التجربة 2 — زيادة وزن الفواقد

كلما زادت قيمة wLoss أصبح تقليل الفواقد أكثر أهمية ضمن دالة الهدف.

23. Experiment 3 — Tighten Voltage Limits

Vmin = 0.98;
Vmax = 1.02;

Observe how the optimal Qc changes.

23. التجربة 3 — تشديد حدود الجهد

عند تضييق المجال المسموح للجهد قد يضطر المحسن إلى استخدام دعم ردي مختلف.

24. Experiment 4 — Remove Reactive Support

Force:

Qmin = 0;
Qmax = 0;

Compare voltage and objective value with the controllable-Q case.

24. التجربة 4 — إزالة الدعم الردي

ثبت Qc عند الصفر ثم قارن جودة الحل والجهد بالحالة التي يكون فيها Qc قابلًا للتحكم.

25. Toward Real AC-OPF

A real AC Optimal Power Flow problem uses:

Pᵢ = Σ VᵢVⱼ [Gᵢⱼcos(θᵢ-θⱼ) + Bᵢⱼsin(θᵢ-θⱼ)]
Qᵢ = Σ VᵢVⱼ [Gᵢⱼsin(θᵢ-θⱼ) - Bᵢⱼcos(θᵢ-θⱼ)]

with constraints on:

  • Generator active power
  • Generator reactive power
  • Bus voltages
  • Line currents
  • Transformer taps
  • Battery operation

25. نحو AC-OPF الحقيقي

في OPF الحقيقي نستخدم معادلات تدفق القدرة غير الخطية ونضيف قيود الجهد والتيار والقدرة الفعالة والردية وغيرها.

المختبر الحالي يعلّم منطق صياغة المسألة قبل الانتقال إلى النموذج الكامل.

26. AI and OPF

AI can support OPF in several ways:

Metaheuristic Optimization

GA, PSO, and related methods can search complex OPF spaces.

Surrogate Models

Machine Learning can approximate expensive power-flow calculations.

Warm Start

AI can estimate a good initial operating point.

Real-Time Decision Support

Trained models can provide very fast approximations online.

26. الذكاء الاصطناعي وOPF

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

27. Lab Tasks

  1. Implement the simplified loss model.
  2. Implement the simplified voltage model.
  3. Construct the objective function.
  4. Add power-balance penalties.
  5. Add voltage-limit penalties.
  6. Run the evolutionary optimizer.
  7. Record PG, Qc, Ploss, and V2.
  8. Verify power balance.
  9. Change the load and repeat.
  10. Change wLoss and compare results.
  11. Remove reactive support and compare voltage.
  12. Explain how the simplified model differs from AC-OPF.

27. مهام المختبر

  1. نفذ نموذج الفواقد المبسط.
  2. نفذ نموذج الجهد المبسط.
  3. أنشئ دالة الهدف.
  4. أضف عقوبة توازن القدرة.
  5. أضف عقوبة حدود الجهد.
  6. شغّل خوارزمية التحسين.
  7. سجل PG وQc وPloss وV2.
  8. تحقق من توازن القدرة.
  9. غيّر الحمل وأعد التجربة.
  10. غيّر وزن الفواقد وقارن النتائج.
  11. ألغِ الدعم الردي وقارن الجهد.
  12. اشرح الفرق بين النموذج الحالي وAC-OPF الحقيقي.

📝 Review Questions

  1. What is the difference between Economic Dispatch and OPF?
  2. Why are network losses included in OPF?
  3. What does Qc represent in this laboratory?
  4. Why do we need a voltage penalty?
  5. Why do we need a power-balance penalty?
  6. What is the effect of increasing wLoss?
  7. Why must an optimal solution also be physically feasible?
  8. What additional equations appear in AC-OPF?
  9. How can AI accelerate OPF calculations?

📝 أسئلة المراجعة

  1. ما الفرق بين التوزيع الاقتصادي وOPF؟
  2. لماذا نضيف فواقد الشبكة إلى OPF؟
  3. ماذا يمثل Qc في هذا المختبر؟
  4. لماذا نحتاج إلى عقوبة الجهد؟
  5. لماذا نحتاج إلى عقوبة توازن القدرة؟
  6. ما تأثير زيادة wLoss؟
  7. لماذا يجب أن يكون الحل الأمثل قابلًا للتنفيذ فيزيائيًا؟
  8. ما المعادلات الإضافية الموجودة في AC-OPF؟
  9. كيف يمكن للذكاء الاصطناعي تسريع حسابات OPF؟