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

Intelligent Agents & Search

الوكلاء الأذكياء وخوارزميات البحث
From problem formulation to intelligent decision-making
من صياغة المسألة إلى اتخاذ القرار الذكي

🎯 Learning Objectives

By the end of this lecture, students should be able to:

  • Explain the concept of an intelligent agent.
  • Describe sensors, actuators, environment, and actions.
  • Formulate a problem using states, actions, goals, and costs.
  • Understand search trees and state spaces.
  • Compare BFS, DFS, and A* search.
  • Interpret the functions g(n), h(n), and f(n).
  • Apply search concepts to a simple power-system problem.

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

بعد الانتهاء من هذه المحاضرة يجب أن يكون الطالب قادرًا على:

  • شرح مفهوم الوكيل الذكي.
  • توضيح مفهوم الحساسات والمشغلات والبيئة والأفعال.
  • صياغة المسألة باستخدام الحالات والأفعال والهدف والكلفة.
  • فهم فضاء الحالات وشجرة البحث.
  • المقارنة بين BFS وDFS وA*.
  • تفسير الدوال g(n) وh(n) وf(n).
  • تطبيق مفهوم البحث على مثال مبسط من نظم الطاقة.

1. What is an Intelligent Agent?

An intelligent agent is a system that observes its environment, processes the available information, and chooses an action that helps achieve a desired objective.

Environment
Sensors
Agent
Actuators
Environment
The key idea is simple: perceive → decide → act.

1. ما هو الوكيل الذكي؟

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

الفكرة الأساسية بسيطة: استشعار ← اتخاذ قرار ← تنفيذ فعل.

2. Agent Components

Environment

The world in which the agent operates.

Sensors

Devices or data channels used to observe the environment.

Agent

The decision-making system.

Actuators

Mechanisms used to execute actions.

2. مكونات الوكيل الذكي

البيئة

العالم أو النظام الذي يعمل ضمنه الوكيل.

الحساسات

الوسائل المستخدمة للحصول على معلومات عن البيئة.

الوكيل

النظام المسؤول عن اتخاذ القرار.

المشغلات

الوسائل المستخدمة لتنفيذ القرارات أو الأفعال.

3. PEAS Representation

A common way to describe an intelligent agent is the PEAS framework:

PEAS = Performance + Environment + Actuators + Sensors
Element Power-System Example
Performance Low cost, secure voltage, low losses
Environment Electrical distribution network
Actuators Battery inverter, transformer tap, controllable load
Sensors Voltage, current, power, SOC, electricity price

3. تمثيل PEAS

يمكن توصيف الوكيل الذكي باستخدام إطار PEAS:

PEAS = Performance + Environment + Actuators + Sensors

أي: معيار الأداء + البيئة + المشغلات + الحساسات.

4. Problem Formulation

Before a computer can search for a solution, the engineering problem must first be represented mathematically.

A search problem can be defined by:

  • Initial state
  • Set of possible actions
  • State-transition model
  • Goal condition
  • Path cost
Problem = { S, A, T, G, C }

where:

  • S = set of states
  • A = set of actions
  • T = transition model
  • G = goal condition
  • C = cost function

4. صياغة المسألة

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

يمكن توصيف مسألة البحث من خلال:

  • الحالة الابتدائية
  • مجموعة الأفعال الممكنة
  • نموذج الانتقال بين الحالات
  • شرط الوصول إلى الهدف
  • كلفة المسار

5. State Space

The state space is the set of all possible configurations that may occur during problem solving.

S = { s₀, s₁, s₂, ..., sₙ }

Each action moves the system from one state to another:

s(k+1) = T(s(k), a(k))

This equation simply says that the next state depends on the current state and the selected action.

5. فضاء الحالات

فضاء الحالات هو مجموعة جميع الحالات أو التشكيلات الممكنة التي قد يمر بها النظام أثناء عملية البحث.

S = { s₀, s₁, s₂, ..., sₙ }

ويؤدي تنفيذ فعل معين إلى نقل النظام من حالة إلى أخرى:

s(k+1) = T(s(k), a(k))

أي أن الحالة التالية تعتمد على الحالة الحالية والفعل المختار.

6. Search Tree

A search tree is a graphical representation of possible decisions.

S A B C D G E 2 5 4 2 1 3

The green node represents a goal state. The numbers along the edges represent transition costs.

6. شجرة البحث

شجرة البحث هي تمثيل رسومي للقرارات الممكنة والانتقالات بين الحالات.

العقدة الخضراء تمثل حالة الهدف، بينما تمثل الأرقام المكتوبة على الأفرع كلفة الانتقال من حالة إلى أخرى.

7. Breadth-First Search — BFS

BFS explores the tree level by level.

First visit all nodes at depth 1, then depth 2, then depth 3, and so on.

BFS is complete when the branching factor and solution depth are finite, but it may require a large amount of memory.

7. البحث بالعرض — BFS

تقوم خوارزمية BFS باستكشاف شجرة البحث مستوى بعد مستوى.

يتم أولًا فحص جميع العقد في المستوى الأول، ثم المستوى الثاني، ثم الثالث، وهكذا.

تعد BFS مناسبة عندما نرغب في إيجاد حل قريب من الجذر، لكنها قد تحتاج إلى ذاكرة كبيرة.

8. Depth-First Search — DFS

DFS follows one branch as deeply as possible before returning and exploring another branch.

DFS requires less memory than BFS, but it may explore a very long unproductive path.

8. البحث بالعمق — DFS

تتبع خوارزمية DFS أحد الأفرع إلى أكبر عمق ممكن، ثم تعود للخلف لاستكشاف فرع آخر.

تحتاج DFS عادةً إلى ذاكرة أقل من BFS، لكنها قد تضيع وقتًا في استكشاف مسار طويل لا يؤدي إلى الحل.

9. Search Cost

If every movement has a cost, then the total path cost is the sum of all transition costs.

g(n) = Σ cᵢ

Here, g(n) represents the actual cost from the initial state to node n.

9. كلفة البحث

إذا كان لكل انتقال كلفة، فإن الكلفة الكلية للمسار تساوي مجموع تكاليف جميع الانتقالات.

g(n) = Σ cᵢ

وتمثل g(n) الكلفة الفعلية التي دُفعت للوصول من الحالة الابتدائية إلى العقدة n.

10. Heuristic Function

A heuristic function estimates how far a current state is from the goal.

h(n) ≈ estimated remaining cost

Unlike g(n), the heuristic is not the cost already paid. It is an estimate of the cost that may still be required.

g(n) = past cost
h(n) = estimated future cost

10. الدالة الإرشادية

تستخدم الدالة الإرشادية لتقدير مدى بُعد الحالة الحالية عن الهدف.

h(n) ≈ الكلفة المتبقية المقدرة
g(n) = الكلفة التي دُفعت حتى الآن
h(n) = الكلفة المستقبلية المقدرة

11. A* Search

A* combines the known cost and the heuristic estimate:

f(n) = g(n) + h(n)

The algorithm normally expands the node with the smallest value of f(n).

A* asks:

“How much have I already paid?” + “How much do I expect to pay from here to the goal?”

11. خوارزمية A*

تجمع خوارزمية A* بين الكلفة الفعلية والتقدير المستقبلي:

f(n) = g(n) + h(n)

وتقوم الخوارزمية عادةً باختيار العقدة التي تمتلك أصغر قيمة للدالة f(n).

يمكن فهم A* بالسؤال التالي:

كم دفعت حتى الآن؟ + كم أتوقع أن أدفع حتى أصل إلى الهدف؟

12. Worked Numerical Example

Assume three candidate nodes:

Node g(n) h(n) f(n)=g+h
A 3 7 10
B 5 2 7
C 2 9 11

A* selects node B because:

f(B) = 5 + 2 = 7

and 7 is smaller than 10 and 11.

12. مثال عددي محلول

لدينا ثلاث عقد مرشحة A وB وC.

تحسب A* لكل عقدة:

f(n) = g(n) + h(n)

بالنسبة إلى B:

f(B) = 5 + 2 = 7

ولذلك يتم اختيار B لأن القيمة 7 هي الأصغر.

13. Power-System Example: Battery Scheduling

Consider a battery connected to a distribution network.

The current state is:

SOC = 60%

The possible actions are:

Charge

Increase stored energy.

Idle

Keep the battery unchanged.

Discharge

Supply stored energy to the network.

Goal

Minimize energy cost while respecting SOC limits.

A simplified state transition may be written as:

SOC(k+1) = SOC(k) + ΔSOC(a(k))

The search algorithm evaluates different sequences of actions and chooses the path with the best total cost.

13. مثال من نظم الطاقة: جدولة البطارية

لنفترض وجود بطارية متصلة بشبكة توزيع كهربائية.

الحالة الحالية:

SOC = 60%

الأفعال الممكنة:

  • الشحن
  • عدم اتخاذ أي إجراء
  • التفريغ

والهدف هو تقليل تكلفة الطاقة مع المحافظة على حدود حالة الشحن.

SOC(k+1) = SOC(k) + ΔSOC(a(k))

تقوم خوارزمية البحث بمقارنة مسارات متعددة من الأفعال، ثم تختار المسار الأفضل وفقًا لدالة الكلفة.

14. From Search to Optimization

Search and optimization are closely related.

In search, we explore alternatives. In optimization, we aim to identify the best alternative according to an objective function.

x* = arg min J(x)

where J(x) is the objective or cost function.

14. من البحث إلى التحسين

يوجد ارتباط وثيق بين البحث والتحسين.

في البحث نستكشف البدائل الممكنة، بينما في التحسين نحاول إيجاد أفضل بديل وفقًا لدالة هدف أو كلفة.

x* = arg min J(x)

حيث تمثل J(x) دالة الهدف أو دالة الكلفة.

15. MATLAB Concept

A simple search algorithm can be understood computationally as:

1. Generate candidate states
2. Evaluate their cost
3. Select one candidate
4. Expand it
5. Repeat until the goal is reached

Later, the MATLAB lab will implement a small search problem step by step.

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

يمكن فهم خوارزمية البحث برمجيًا وفق الخطوات التالية:

1. توليد الحالات المرشحة
2. حساب كلفة كل حالة
3. اختيار إحدى الحالات
4. توسيعها وتوليد حالات جديدة
5. تكرار العملية حتى الوصول إلى الهدف

سنقوم لاحقًا في مختبر MATLAB بتنفيذ مسألة بحث بسيطة خطوة بخطوة.

16. Summary

  • An intelligent agent perceives, decides, and acts.
  • Search problems can be described using states, actions, transitions, goals, and costs.
  • BFS explores level by level.
  • DFS explores one branch deeply.
  • A* combines actual cost g(n) and heuristic cost h(n).
  • Search methods can support decision-making in power-system applications.

16. الخلاصة

  • يقوم الوكيل الذكي بالاستشعار واتخاذ القرار وتنفيذ الأفعال.
  • يمكن تمثيل مسألة البحث باستخدام الحالات والأفعال والانتقالات والهدف والكلفة.
  • تبحث BFS مستوى بعد مستوى.
  • تبحث DFS بعمق ضمن أحد الأفرع.
  • تجمع A* بين الكلفة الفعلية g(n) والكلفة المقدرة h(n).
  • يمكن تطبيق خوارزميات البحث في مسائل اتخاذ القرار في نظم الطاقة.

📝 Review Questions

  1. What is an intelligent agent?
  2. What do sensors and actuators do?
  3. What is a state space?
  4. What is the difference between BFS and DFS?
  5. What does g(n) represent?
  6. What does h(n) represent?
  7. Why does A* use f(n)=g(n)+h(n)?
  8. How can search be used for battery scheduling?

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

  1. ما هو الوكيل الذكي؟
  2. ما وظيفة الحساسات والمشغلات؟
  3. ما المقصود بفضاء الحالات؟
  4. ما الفرق بين BFS وDFS؟
  5. ماذا تمثل g(n)؟
  6. ماذا تمثل h(n)؟
  7. لماذا تستخدم A* العلاقة f(n)=g(n)+h(n)؟
  8. كيف يمكن استخدام البحث في جدولة بطارية تخزين الطاقة؟