LECTURE 06 | المحاضرة 06

Genetic Algorithms

الخوارزميات الجينية
Population, Fitness, Selection, Crossover, Mutation, and Optimization
المجتمع ودالة الملاءمة والاختيار والتزاوج والطفرة والتحسين

🎯 Learning Objectives

  • Understand the basic idea of evolutionary optimization.
  • Explain population, chromosome, gene, and fitness.
  • Understand selection, crossover, mutation, and elitism.
  • Formulate an optimization problem mathematically.
  • Understand how fitness is connected to the objective function.
  • Follow one complete GA generation step by step.
  • Apply Genetic Algorithms to power-system optimization.

🎯 أهداف التعلم

  • فهم الفكرة الأساسية للتحسين التطوري.
  • فهم المجتمع والكروموسوم والجين ودالة الملاءمة.
  • فهم الاختيار والتزاوج والطفرة والنخبوية.
  • صياغة مسألة تحسين بصورة رياضية.
  • فهم العلاقة بين دالة الهدف ودالة الملاءمة.
  • متابعة جيل كامل من الخوارزمية الجينية خطوة بخطوة.
  • تطبيق الخوارزميات الجينية على مسائل نظم الطاقة الكهربائية.

1. What is a Genetic Algorithm?

A Genetic Algorithm (GA) is an optimization method inspired by natural evolution.

Better candidate solutions are more likely to survive and produce new solutions.
Population
Fitness
Selection
Crossover
Mutation
New Population

1. ما هي الخوارزمية الجينية؟

الخوارزمية الجينية هي طريقة تحسين مستوحاة من مبادئ التطور الطبيعي.

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

2. Optimization Problem

A general optimization problem can be written as:

x* = arg min J(x)

subject to:

gᵢ(x) ≤ 0
hⱼ(x) = 0

where:

  • x = decision variables
  • J(x) = objective function
  • gᵢ(x) = inequality constraints
  • hⱼ(x) = equality constraints

2. صياغة مسألة التحسين

يمكن كتابة مسألة التحسين العامة بالشكل:

x* = arg min J(x)

مع وجود قيود:

gᵢ(x) ≤ 0
hⱼ(x) = 0

حيث x تمثل متغيرات القرار، وJ(x) تمثل دالة الهدف.

3. Population, Chromosome, and Gene

Population

A collection of candidate solutions.

Chromosome

One complete candidate solution.

Gene

One decision variable inside a chromosome.

Generation

One iteration of the evolutionary process.

3. المجتمع والكروموسوم والجين

المجتمع

مجموعة من الحلول المرشحة.

الكروموسوم

حل كامل واحد.

الجين

متغير قرار واحد داخل الحل.

الجيل

دورة واحدة من العملية التطورية.

4. Example of a Chromosome

Suppose we want to optimize three variables:

x = [x₁, x₂, x₃]

One chromosome may be:

x = [2.1, 5.4, 0.8]
Each number is a gene. The full vector is one candidate solution.

4. مثال على كروموسوم

إذا كان لدينا ثلاثة متغيرات قرار:

x = [x₁, x₂, x₃]

فقد يكون أحد الحلول:

x = [2.1, 5.4, 0.8]
كل قيمة تمثل جينًا، أما المتجه الكامل فيمثل حلًا مرشحًا.

5. Fitness Function

The fitness function measures how good a candidate solution is.

If we are minimizing a positive cost J(x), one simple fitness transformation is:

F(x) = 1 / (1 + J(x))

Therefore:

Smaller J(x) → Larger F(x)

5. دالة الملاءمة

تقيس دالة الملاءمة جودة الحل المرشح.

إذا كنا نريد تصغير دالة كلفة موجبة J(x)، يمكن تعريف:

F(x) = 1 / (1 + J(x))

وبالتالي كلما انخفضت الكلفة ارتفعت الملاءمة.

6. Why Do We Need a Population?

Classical local optimization often follows one solution trajectory. A Genetic Algorithm works with many solutions simultaneously.

Population-based search gives the algorithm the ability to explore several regions of the search space at the same time.

6. لماذا نحتاج إلى مجتمع من الحلول؟

تعتمد بعض طرائق التحسين التقليدية على مسار واحد للحل، بينما تستخدم الخوارزمية الجينية عدة حلول في الوقت نفسه.

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

7. Initial Population

The first population is usually generated randomly within the allowed bounds:

xᵢ ∈ [xᵢ,min , xᵢ,max]

A random value can be generated as:

xᵢ = xᵢ,min + r(xᵢ,max - xᵢ,min)

where:

r ∈ [0,1]

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

يتم عادةً توليد المجتمع الأول عشوائيًا ضمن حدود المتغيرات:

xᵢ ∈ [xᵢ,min , xᵢ,max]

ويمكن توليد قيمة عشوائية وفق العلاقة:

xᵢ = xᵢ,min + r(xᵢ,max - xᵢ,min)

8. Selection

Selection determines which chromosomes become parents.

Better fitness → Higher probability of being selected.

Common selection methods include:

  • Roulette-wheel selection
  • Tournament selection
  • Rank-based selection

8. الاختيار

تحدد عملية الاختيار الحلول التي ستصبح آباءً للجيل الجديد.

كلما كانت ملاءمة الحل أعلى، زادت فرصته في الاختيار.

من أشهر الطرائق:

  • عجلة الروليت
  • اختيار البطولة
  • الاختيار المعتمد على الترتيب

9. Roulette-Wheel Selection

Suppose the fitness values are:

F₁, F₂, ..., Fₙ

Then the selection probability of chromosome i can be:

pᵢ = Fᵢ / ΣFⱼ
A chromosome with twice the fitness receives approximately twice the selection probability.

9. الاختيار باستخدام عجلة الروليت

إذا كانت قيم الملاءمة:

F₁, F₂, ..., Fₙ

فإن احتمال اختيار الحل i يمكن حسابه من:

pᵢ = Fᵢ / ΣFⱼ

10. Crossover

Crossover combines information from two parents to produce offspring.

For arithmetic crossover:

Child₁ = α Parent₁ + (1-α) Parent₂
Child₂ = (1-α) Parent₁ + α Parent₂

where:

0 ≤ α ≤ 1

10. التزاوج

تجمع عملية التزاوج معلومات من حلين أبويين لإنتاج حلول جديدة.

في التزاوج الحسابي:

Child₁ = α Parent₁ + (1-α) Parent₂
Child₂ = (1-α) Parent₁ + α Parent₂

11. Simple Crossover Example

Assume:

Parent₁ = [2,4]
Parent₂ = [6,8]

and:

α = 0.25

Then:

Child₁ = 0.25[2,4] + 0.75[6,8]
Child₁ = [5,7]

11. مثال مبسط على التزاوج

إذا كان:

Parent₁ = [2,4]
Parent₂ = [6,8]
α = 0.25

فإن:

Child₁ = [5,7]

12. Mutation

Mutation introduces small random changes into offspring.

xᵢ,new = xᵢ + Δ

where Δ is a small random perturbation.

Mutation helps maintain diversity and prevents the population from becoming too similar too early.

12. الطفرة

تضيف الطفرة تغيرًا عشوائيًا صغيرًا إلى بعض الجينات.

xᵢ,new = xᵢ + Δ
تساعد الطفرة على المحافظة على التنوع داخل المجتمع وتمنع جميع الحلول من التشابه بسرعة كبيرة.

13. Elitism

Elitism means copying one or more of the best chromosomes directly into the next generation.

The best solution found so far should not be lost by random crossover or mutation.

13. النخبوية

تعني النخبوية نقل أفضل حل أو عدد من أفضل الحلول مباشرة إلى الجيل التالي.

بهذه الطريقة نضمن ألا نفقد أفضل حل تم العثور عليه بسبب العمليات العشوائية.

14. Complete GA Cycle

Population Fitness Selection Crossover Mutation New Gen. Best?
Repeat the cycle until a stopping condition is satisfied.

14. الدورة الكاملة للخوارزمية الجينية

المجتمع ← تقييم الملاءمة ← الاختيار ← التزاوج ← الطفرة ← الجيل الجديد ← التحقق من شرط الإيقاف ← التكرار

15. Stopping Criteria

The algorithm may stop when:

  • A maximum number of generations is reached.
  • The best objective value is sufficiently small.
  • The solution improvement becomes negligible.
|Jbest(k) - Jbest(k-1)| < ε

15. شروط الإيقاف

يمكن إيقاف الخوارزمية عند تحقق أحد الشروط التالية:

  • الوصول إلى عدد أعظمي من الأجيال.
  • وصول دالة الهدف إلى قيمة جيدة بما يكفي.
  • أصبح التحسن بين جيلين متتاليين صغيرًا جدًا.
|Jbest(k) - Jbest(k-1)| < ε

16. Handling Constraints

One common approach is to add a penalty when a solution violates constraints:

Jpen(x) = J(x) + λP(x)

where P(x) measures the constraint violation.

Feasible solutions receive little or no penalty. Infeasible solutions become less attractive.

16. التعامل مع القيود

إحدى الطرق الشائعة هي إضافة عقوبة للحلول التي تنتهك القيود:

Jpen(x) = J(x) + λP(x)

حيث P(x) تقيس مقدار انتهاك القيود.

17. Example Optimization Problem

Consider:

J(x) = (x-3)²

The minimum is clearly:

x* = 3

Assume the initial population is:

{0, 2, 5, 8}
x J(x) F(x)=1/(1+J)
0 9 0.10
2 1 0.50
5 4 0.20
8 25 0.038
x=2 has the highest fitness, so it has a high probability of producing offspring.

17. مثال عددي على التحسين

لنفترض أن دالة الهدف هي:

J(x) = (x-3)²

أفضل حل رياضيًا هو:

x* = 3

في المجتمع الابتدائي تكون قيمة x=2 هي الأفضل بين الحلول الموجودة، ولذلك تحصل على قيمة ملاءمة أكبر وفرصة اختيار أكبر.

18. Exploration vs Exploitation

Exploration

Search new areas of the solution space.

Mutation supports exploration.

Exploitation

Improve promising solutions already found.

Selection and elitism support exploitation.

18. الاستكشاف مقابل الاستغلال

الاستكشاف

البحث في مناطق جديدة من فضاء الحلول.

الاستغلال

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

الخوارزمية الجيدة تحتاج إلى توازن بين استكشاف حلول جديدة واستغلال الحلول الجيدة الحالية.

19. Important GA Parameters

Parameter Meaning
Population Size Number of candidate solutions
Crossover Rate Probability of applying crossover
Mutation Rate Probability of random mutation
Elite Count Number of best solutions preserved
Max Generations Maximum number of iterations

19. أهم معاملات الخوارزمية الجينية

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

20. Power-System Application: Economic Dispatch

Economic Dispatch determines generator outputs that minimize generation cost.

Minimize: J = Σ Cᵢ(Pᵢ)

subject to power balance:

ΣPᵢ = Pload + Ploss

and generator limits:

Pᵢ,min ≤ Pᵢ ≤ Pᵢ,max
Each chromosome can represent a vector of generator powers.

20. تطبيق في نظم الطاقة: التوزيع الاقتصادي

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

Minimize: J = Σ Cᵢ(Pᵢ)

مع شرط توازن القدرة:

ΣPᵢ = Pload + Ploss

وحدود كل مولد:

Pᵢ,min ≤ Pᵢ ≤ Pᵢ,max

21. Power-System Application: PV/BESS Sizing

A chromosome may represent:

x = [PPV , EBESS]

where:

  • PPV = installed photovoltaic capacity
  • EBESS = battery energy capacity

An objective could combine investment cost and operating cost:

J = Cinvestment + Coperation
GA searches many PV and battery-size combinations and keeps improving the most promising ones.

21. تطبيق في نظم الطاقة: تحديد حجم PV وBESS

يمكن أن يمثل الكروموسوم:

x = [PPV , EBESS]

أي قدرة النظام الكهروضوئي وسعة البطارية.

ويمكن أن تجمع دالة الهدف بين تكلفة الاستثمار وتكلفة التشغيل.

22. Power-System Application: Optimal Power Flow

In an OPF problem, GA can search for variables such as:

x = [PG, QG, V, Tap, Battery Power, ...]

while minimizing an objective such as:

J = Cost + Losses + Penalties
GA is attractive when the optimization problem contains nonlinear, discrete, nonconvex, or mixed-variable decisions.

22. تطبيق في نظم الطاقة: تدفق القدرة الأمثل

يمكن للخوارزمية الجينية البحث عن متغيرات مثل:

x = [PG, QG, V, Tap, Battery Power, ...]

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

23. MATLAB Concept

A basic GA program follows this logic:

1. Generate population
2. Evaluate objective function
3. Calculate fitness
4. Select parents
5. Apply crossover
6. Apply mutation
7. Preserve elite solutions
8. Repeat

A simplified objective evaluation may look like:

J(i) = objective(population(i,:))

23. الفكرة البرمجية في MATLAB

تتكون الخوارزمية الأساسية من:

توليد المجتمع ← حساب دالة الهدف ← حساب الملاءمة ← الاختيار ← التزاوج ← الطفرة ← النخبوية ← التكرار

24. Advantages of Genetic Algorithms

  • Do not require derivatives of the objective function.
  • Can handle nonlinear and nonconvex problems.
  • Can work with continuous and discrete variables.
  • Search several candidate solutions simultaneously.
  • Can be adapted to many engineering optimization problems.

24. مزايا الخوارزميات الجينية

  • لا تحتاج إلى مشتقات دالة الهدف.
  • يمكنها التعامل مع مسائل غير خطية وغير محدبة.
  • يمكنها التعامل مع متغيرات مستمرة ومتقطعة.
  • تبحث باستخدام عدة حلول في الوقت نفسه.
  • يمكن تكييفها مع الكثير من مسائل التحسين الهندسي.

25. Limitations

  • Can require many objective-function evaluations.
  • Performance depends on parameter tuning.
  • No guarantee of finding the exact global optimum in finite time.
  • Poor constraint handling can produce infeasible solutions.
  • Results may vary between independent runs.

25. القيود والتحديات

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

26. Summary

  • GA is a population-based evolutionary optimization method.
  • A chromosome represents one candidate solution.
  • Fitness evaluates the quality of a solution.
  • Selection favors better solutions.
  • Crossover combines parent information.
  • Mutation introduces diversity.
  • Elitism preserves the best solutions.
  • Penalty functions can be used to handle constraints.
  • GA is useful for Economic Dispatch, OPF, and PV/BESS sizing.

26. الخلاصة

  • الخوارزمية الجينية طريقة تحسين تطورية تعتمد على مجتمع من الحلول.
  • يمثل الكروموسوم حلًا مرشحًا واحدًا.
  • تقيس دالة الملاءمة جودة الحل.
  • يفضل الاختيار الحلول الأفضل.
  • يجمع التزاوج معلومات من الآباء.
  • تحافظ الطفرة على التنوع.
  • تحافظ النخبوية على أفضل الحلول.
  • يمكن استخدام دوال العقوبة لمعالجة القيود.
  • يمكن تطبيق GA على التوزيع الاقتصادي وOPF وتحديد حجم PV/BESS.

📝 Review Questions

  1. What is a Genetic Algorithm?
  2. What is the difference between a chromosome and a gene?
  3. What is the purpose of the fitness function?
  4. How does roulette-wheel selection work?
  5. Explain arithmetic crossover.
  6. Why is mutation necessary?
  7. What is elitism?
  8. What is the difference between exploration and exploitation?
  9. How can penalties be used for constraint handling?
  10. How can GA be applied to Economic Dispatch?
  11. How can GA be used for PV/BESS sizing?

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

  1. ما هي الخوارزمية الجينية؟
  2. ما الفرق بين الكروموسوم والجين؟
  3. ما وظيفة دالة الملاءمة؟
  4. كيف يعمل اختيار عجلة الروليت؟
  5. اشرح التزاوج الحسابي.
  6. لماذا نحتاج إلى الطفرة؟
  7. ما المقصود بالنخبوية؟
  8. ما الفرق بين الاستكشاف والاستغلال؟
  9. كيف تستخدم العقوبات لمعالجة القيود؟
  10. كيف يمكن تطبيق GA على التوزيع الاقتصادي؟
  11. كيف يمكن استخدام GA لتحديد أحجام PV وBESS؟