AI-Based Optimal Power Flow
تكلفة التوليد وفواقد الشبكة وقيود الجهد والتحسين الذكي
🎯 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.
1. من التوزيع الاقتصادي إلى OPF
يركز التوزيع الاقتصادي أساسًا على استطاعات التوليد الفعالة، بينما يضيف OPF متغيرات وقيود الشبكة الكهربائية.
2. Simplified Two-Bus Network
Consider a simple educational two-bus system.
Generator bus
Load bus
The generator supplies:
The load consumes:
2. شبكة مبسطة من عقدتين
لدينا عقدة توليد وعقدة حمل، والهدف هو اختيار نقطة تشغيل اقتصادية مع مراعاة الفواقد والجهد.
3. Simplified Network-Loss Model
For educational purposes, approximate active-power losses as:
where:
This is not a full AC power-flow model, but it introduces the idea that larger power transfers produce larger losses.
3. نموذج مبسط لفواقد الشبكة
هذا ليس نموذج AC كاملًا، لكنه يوضح للطالب أن الفواقد تزداد عادةً مع ارتفاع القدرة المنقولة.
4. Power-Balance Constraint
Generation must supply both the load and losses:
Therefore:
4. قيد توازن القدرة
يجب أن تغطي قدرة المولد الحمل والفواقد معًا.
5. Generator Cost
Assume:
The first component of the objective is operating cost.
5. تكلفة التوليد
تمثل هذه الدالة تكلفة تشغيل المولد عند قدرة معينة.
6. Simplified Voltage Model
To introduce voltage constraints, use the educational approximation:
with:
Qc represents a controllable reactive-power support variable.
6. نموذج مبسط للجهد
نستخدم هذا النموذج فقط لأغراض تعليمية حتى نربط القدرة الفعالة والدعم الردي مع قيمة الجهد.
7. Voltage Constraint
Operating points outside this range are penalized.
7. قيد الجهد
يجب أن تبقى قيمة الجهد ضمن المجال المسموح.
8. Decision Variables
We optimize:
with limits:
8. متغيرات القرار
أي أننا نبحث عن قدرة التوليد والدعم الردي المناسبين.
9. Multi-Objective Cost
Define:
where:
The weight wL determines how strongly losses are penalized relative to cost.
9. دالة هدف متعددة العناصر
تجمع دالة الهدف بين الاقتصاد وتقليل الفواقد واحترام القيود.
10. Power-Balance Penalty
Use:
10. عقوبة توازن القدرة
إذا لم يتحقق توازن القدرة، نضيف عقوبة كبيرة إلى دالة الهدف.
11. Voltage Penalty
If:
then:
and if:
then:
11. عقوبة الجهد
تضاف عقوبة عندما يصبح الجهد أقل من الحد الأدنى أو أعلى من الحد الأعلى.
12. Complete Objective Function
12. دالة الهدف الكاملة
13. Population Representation
Each chromosome contains:
Example:
13. تمثيل المجتمع
يمثل كل كروموسوم نقطة تشغيل كاملة:
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:
calculate:
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.
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 التي تحقق تكلفة جيدة وفواقد منخفضة وقيود جهد وتوازن قدرة مقبولة.
20. Interpreting the Final Solution
After optimization, check:
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;
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:
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:
GA, PSO, and related methods can search complex OPF spaces.
Machine Learning can approximate expensive power-flow calculations.
AI can estimate a good initial operating point.
Trained models can provide very fast approximations online.
26. الذكاء الاصطناعي وOPF
يمكن استخدام الذكاء الاصطناعي للتحسين المباشر أو لبناء نماذج بديلة أو لتقدير حلول ابتدائية جيدة أو لدعم التشغيل في الزمن الحقيقي.
27. Lab Tasks
- Implement the simplified loss model.
- Implement the simplified voltage model.
- Construct the objective function.
- Add power-balance penalties.
- Add voltage-limit penalties.
- Run the evolutionary optimizer.
- Record PG, Qc, Ploss, and V2.
- Verify power balance.
- Change the load and repeat.
- Change wLoss and compare results.
- Remove reactive support and compare voltage.
- Explain how the simplified model differs from AC-OPF.
27. مهام المختبر
- نفذ نموذج الفواقد المبسط.
- نفذ نموذج الجهد المبسط.
- أنشئ دالة الهدف.
- أضف عقوبة توازن القدرة.
- أضف عقوبة حدود الجهد.
- شغّل خوارزمية التحسين.
- سجل PG وQc وPloss وV2.
- تحقق من توازن القدرة.
- غيّر الحمل وأعد التجربة.
- غيّر وزن الفواقد وقارن النتائج.
- ألغِ الدعم الردي وقارن الجهد.
- اشرح الفرق بين النموذج الحالي وAC-OPF الحقيقي.
📝 Review Questions
- What is the difference between Economic Dispatch and OPF?
- Why are network losses included in OPF?
- What does Qc represent in this laboratory?
- Why do we need a voltage penalty?
- Why do we need a power-balance penalty?
- What is the effect of increasing wLoss?
- Why must an optimal solution also be physically feasible?
- What additional equations appear in AC-OPF?
- How can AI accelerate OPF calculations?
📝 أسئلة المراجعة
- ما الفرق بين التوزيع الاقتصادي وOPF؟
- لماذا نضيف فواقد الشبكة إلى OPF؟
- ماذا يمثل Qc في هذا المختبر؟
- لماذا نحتاج إلى عقوبة الجهد؟
- لماذا نحتاج إلى عقوبة توازن القدرة؟
- ما تأثير زيادة wLoss؟
- لماذا يجب أن يكون الحل الأمثل قابلًا للتنفيذ فيزيائيًا؟
- ما المعادلات الإضافية الموجودة في AC-OPF؟
- كيف يمكن للذكاء الاصطناعي تسريع حسابات OPF؟