Chapterبەش 2 · Lectureوانە 2.3
Agent Programs, Pseudocode and Algorithm Analysisبەرنامەکانی ئەیجێنت، نیمچەکۆد و شیکارکردنی ئەلگۆریزم
How an agent program really works: it receives one percept at a time and keeps a memory. Read AIMA-style pseudocode, learn what an algorithm is and how time and space complexity are measured with Big-O, then build a working table-driven vacuum agent in Python and see why its table explodes.
بەڕاستی چۆن کار دەکات: هەر جارە یەک وەردەگرێت و هەڵدەگرێت. شێوازی AIMA بخوێنەوە، فێر بە چییە و و چۆن بە Big-O دەپێورێن. پاشان بۆ گسکی کارەبایی بە دروست بکە کە کار دەکات و ببینە بۆچی خشتەکەی دەتەقێتەوە.
What you will learnچی فێر دەبیت
- Explain the difference between an agent function and an agent program, and why the program needs memory.
- Read and write AIMA-style pseudocode:
function … returns,persistent,←, loops and generators. - Define an algorithm and describe time and space complexity as functions of n, including worst and average case.
- Use Big-O notation and explain why O(bᵈ) makes brute force hopeless.
- Run a table-driven vacuum agent in Python and explain why its table explodes.
- جیاوازیی نێوان و ڕوون بکەیتەوە و بۆچی بەرنامەکە پێویستی بە هەیە.
- شێوازی AIMA بخوێنیتەوە و بنووسیت:
function … returns،persistent،←، و . - پێناسە بکەیت و و وەک لە n وەسف بکەیت، لەگەڵ و .
- بەکار بهێنیت و ڕوونی بکەیتەوە بۆچی O(bᵈ) تاقیکردنەوەی هەموو شتێک بێهیوا دەکات.
- بۆ گسکی کارەبایی بە کار پێبکەیت و ڕوونی بکەیتەوە بۆچی خشتەکەی دەتەقێتەوە.
From agent function to agent programلە کرداری ئەیجێنتەوە بۆ بەرنامەی ئەیجێنت
In the lesson Intelligent agents you met the agent function f: P* → A. It says which action the agent should take for every possible percept sequence. It describes what the agent does. It does not say how the agent is built. The thing that really runs is the agent program: real instructions that run on the agent's physical machine, its architecture, and put the agent function into practice. That is why the course writes agent = architecture + program.
لە وانەی ئەیجێنتەکاندا ناسی: f: P* → A. ئەم دەڵێت بۆ هەر چ کارێک بکات. واتە دەڵێت چی دەکات، بەڵام ناڵێت چۆن دروست کراوە. ئەوەی بەڕاستی کار دەکات : ڕێنمایی ڕاستەقینە کە لەسەر ئامێرە فیزیکییەکەی ، واتە ، کار دەکەن و دەکەنە کردەوە. بۆیە کۆرسەکە دەنووسێت: ئەیجێنت = تەلارسازی + بەرنامە.
The agent program gets one percept at a time from the sensors, not the whole history. If it needs the past, it must remember it itself. So the program keeps an internal state, a memory of the world. With this memory it can decide better, because it does not start fresh every time a new percept arrives.
لە هەر جارێکدا تەنها یەک لە وەردەگرێت، نەک هەموو مێژووەکە. ئەگەر پێویستی بە ڕابردوو بێت، دەبێت خۆی لە بیری بگرێت. بۆیە بەرنامەکە هەڵدەگرێت، واتە بۆ جیهان. بەم باشتر بڕیار دەدات، چونکە هەر جارێک نوێ دێت لە سفرەوە دەست پێناکاتەوە.
A waiter in a busy tea house hears one order at a time: “two teas for table 3”. That is one percept. He does not ask every customer to repeat everything they said today. Instead he keeps a picture in his head: which table ordered what, who has paid and who is waiting. With that memory he decides what to do next.
کارمەندێکی چایخانەیەکی قەرەباڵغ هەر جارە یەک داواکاری دەبیستێت: «دوو چا بۆ مێزی ٣». ئەمە یەک . داوا لە هەموو مشتەرییەک ناکات هەرچی ئەمڕۆ وتوویەتی دووبارەی بکاتەوە. لەبری ئەوە وێنەیەک لە مێشکیدا هەڵدەگرێت: کام مێز چی داوا کردووە، کێ پارەی داوە و کێ چاوەڕێیە. بەو بڕیار دەدات دواتر چی بکات.
Where the analogy stops workingسنووری ئەم نموونەیە
A waiter may forget or mix things up by accident. An agent program remembers exactly what its code stores, nothing more and nothing less.
کارمەند دەکرێت شتێک لە بیر بکات یان تێکەڵی بکات. ڕێک ئەوە لە بیر دەگرێت کە کۆدەکەی هەڵیدەگرێت، نە زیاتر و نە کەمتر.
The program that actually runs on the agent's hardware and carries out the agent function. It takes the current percept as input and returns an action.
ئەو بەرنامەیەیە کە بەڕاستی لەسەر کار دەکات و جێبەجێ دەکات. وەک وەردەگرێت و دەگەڕێنێتەوە.
The skeleton agentئەیجێنتی ئێسکەپەیکەر
A basic outline of a program that shows its parts, such as the names of its functions and their inputs and outputs, but leaves the details to be filled in later.
شێوەیەکی سەرەتایی کە بەشەکانی پیشان دەدات، وەک ناوی کردارەکان و و ، بەڵام وردەکارییەکان بۆ دواتر جێ دەهێڵێت.
Skeleton code is the basic outline of a program. It shows the important parts, such as the names of the functions, their inputs and outputs and how they are organised, but it leaves out the details. Programmers build the full program on top of it later.
شێوەی سەرەتایی . بەشە گرنگەکان پیشان دەدات، وەک ناوی ، و و چۆنیەتیی ڕێکخستنیان، بەڵام وردەکارییەکان بەجێ دەهێڵێت. دواتر لەسەری بنیات دەنرێت.
The name comes from biology. A skeleton gives a body its shape: it shows where each part goes. The flesh is added around it. Skeleton code gives a program its shape; the detailed code is the flesh that comes later.
ناوەکە لە زانستی زیندەوەرزانییەوە هاتووە. ئێسکەپەیکەر شێوە دەدات بە لەش: پیشان دەدات هەر بەشێک دەچێتە کوێ. گۆشت لە دەوری ئێسکەکان زیاد دەکرێت. شێوە دەدات بە ؛ کۆدە وردەکان گۆشتەکەن کە دواتر دێن.
Here is the skeleton of every agent program, written in the pseudocode style of AIMA, the main AI textbook. Read it line by line with the notes:
ئەمە ئێسکەپەیکەری هەموو ، بە شێوازی AIMA نووسراوە، کە کتێبی سەرەکیی . دێڕ بە دێڕ لەگەڵ تێبینییەکاندا بیخوێنەوە:
function SKELETON-AGENT(percept) returns an action
inputs: percept, the input from the sensors // may be left out
persistent: memory, the agent's memory of the world
memory ← UPDATE-MEMORY(memory, percept)
action ← CHOOSE-BEST-ACTION(memory)
memory ← UPDATE-MEMORY(memory, action)
return actionExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The name of the function and what it gives back: an action.
ناوی و ئەوەی دەیگەڕێنێتەوە: کارێک.
- Lineدێڕی 2
The input: one percept from the sensors. Many books leave this line out.
: یەک لە . زۆر کتێب ئەم دێڕە نانووسن.
- Lineدێڕی 3
persistent:memorykeeps its value between calls (explained below).persistent:memoryنرخەکەی لە نێوان بانگکردنەکاندا دەپارێزێت (لە خوارەوە ڕوون دەکرێتەوە).- Lineدێڕی 5
Step 1: add what was just sensed to the memory.
هەنگاوی ١: ئەوەی ئێستا هەستی پێکرا بخە ناو .
- Lineدێڕی 6
Step 2: choose the best action using everything in the memory.
هەنگاوی ٢: بە بەکارهێنانی هەموو ئەوەی لە باشترین کار هەڵبژێرە.
- Lineدێڕی 7
Step 3: also remember the action itself, so the agent knows what it did.
هەنگاوی ٣: کارەکەش لە بیر بگرە، بۆ ئەوەی بزانێت چی کردووە.
- Lineدێڕی 8
Send the action to the actuators.
کارەکە بنێرە بۆ .
The arrow ← means assignment: work out the right-hand side and store the result in the variable on the left. It does the same job as = in Python. For example, UPDATE-MEMORY takes the current memory and the percept, builds a new version of the memory, and this new version replaces the old one.
تیری ← واتە : لای ڕاست حیساب بکە و ئەنجامەکە لە لای چەپدا هەڵبگرە. هەمان کاری = لە دەکات. بۆ نموونە، UPDATE-MEMORY ئێستا و وەردەگرێت، وەشانێکی نوێی دروست دەکات و ئەم وەشانە نوێیە جێگەی کۆنەکە دەگرێتەوە.
The skeleton agentئەیجێنتی ئێسکەپەیکەر
The same algorithm in other formsهەمان ئەلگۆریزم بە شێوەی تر
Books and teachers write pseudocode in slightly different styles. The course notes show the skeleton agent in four forms. They all describe the same algorithm: the algorithm is the idea, and the pseudocode is one way to write it down.
کتێب و مامۆستاکان بە شێوازی کەمێک جیاواز دەنووسن. تێبینییەکانی کۆرسەکە ئێسکەپەیکەر بە چوار شێوە پیشان دەدەن. هەموویان هەمان دەنووسنەوە: بیرۆکەکەیە و یەکێکە لە ڕێگاکانی نووسینەوەی.
| Formشێوە | How it startsچۆن دەست پێدەکات | Where you see itلە کوێ دەیبینیت |
|---|---|---|
| 1١ | def Skeleton-Agent(percept) | The first form in the notes. Avoid it: def belongs to real Python (see the correction above).یەکەم شێوە لە تێبینییەکاندا. خۆتی لێ بپارێزە: def هی ڕاستەقینەیە (بڕوانە ڕاستکردنەوەکەی سەرەوە). |
| 2٢ | function SKELETON-AGENT(percept) returns an action | AIMA, the main AI textbook, and this course.AIMA (کتێبی سەرەکیی ) و ئەم کۆرسە. |
| 3٣ | Input: percept Output: action | Numbered steps 1, 2, 3 under an input and output line; common in lecture notes.هەنگاوی ژمارەدار (١، ٢، ٣) لە ژێر دێڕێکی و ؛ لە تێبینیی وانەکاندا باوە. |
| 4٤ | procedure SKELETON-AGENT(percept) | Research papers, typeset with LaTeX packages such as algorithm2e.توێژینەوە زانستییەکان، کە بە پاکێجەکانی LaTeX وەک algorithm2e ڕێکدەخرێن. |
Input: percept
Output: action
memory: the agent's memory of the world
1. memory ← UPDATE-MEMORY(memory, percept)
2. action ← CHOOSE-BEST-ACTION(memory)
3. memory ← UPDATE-MEMORY(memory, action)procedure SKELETON-AGENT(percept)
memory: the agent's memory of the world
memory ← UPDATE-MEMORY(memory, percept)
action ← CHOOSE-BEST-ACTION(memory)
memory ← UPDATE-MEMORY(memory, action)
return actionWhat an algorithm isئەلگۆریزم چییە
A finite list of clear steps that solves a problem or does a task. Programmers often plan an algorithm with or a before they write the code.
زنجیرەیەک هەنگاوی ڕوون و کۆتاییدارە کە کێشەیەک چارەسەر دەکات یان کارێک ئەنجام دەدات. زۆرجار پێش نووسینی کۆد، هەنگاوەکان بە یان بە دادەڕێژن.
The course gives a short definition: an algorithm is an explicit, clear set of instructions for a computing procedure. Each step is exact, so a computer (or a person) can follow it without guessing.
کۆرسەکە پێناسەیەکی کورت دەدات: کۆمەڵێک ڕێنمایی ڕوون و ئاشکرایە بۆ ڕێکارێکی ژمێریاری. هەموو هەنگاوێک وردە، بۆیە (یان مرۆڤ) بەبێ مەزەندەکردن دەتوانێت پەیڕەوی بکات.
- It solves a whole class of questions. A good algorithm does not answer one question only. A sorting algorithm sorts any list, and a route-finding algorithm finds a route between any two towns on the map.
- It can be defined precisely. Mathematicians define algorithms exactly with models of computation such as the , a simple imaginary machine that reads and writes symbols on a tape.
- It is studied on its own. We analyse an algorithm independently of the programming language, the computer and the particular input. The question is not “how fast is my laptop?” but “how much work does this method need?”.
- کۆمەڵێک پرسیار لە یەک جۆر چارەسەر دەکات. باش تەنها وەڵامی یەک پرسیار نادات. هەر ڕیز دەکات و دۆزینەوەی ڕێگە، ڕێگە لە نێوان هەر دوو شارۆچکەیەکی سەر نەخشەکەدا دەدۆزێتەوە.
- دەتوانرێت بە وردی پێناسە بکرێت. بیرکاریزانەکان بە وردی بە مۆدێلەکانی ژمێریاری پێناسە دەکەن، وەک ، کە ئامێرێکی خەیاڵیی سادەیە و هێما لەسەر شریتێک دەخوێنێتەوە و دەنووسێت.
- بە جیا لێکۆڵینەوەی لێ دەکرێت. بەبێ گوێدانە ، و شی دەکەینەوە. پرسیارەکە ئەوە نییە «لاپتۆپەکەم چەند خێرایە؟» بەڵکو ئەوەیە «ئەم ڕێگایە چەند کاری پێویستە؟».
Learn what an algorithm is with an everyday counting example, how a loop repeats steps, and how to write the steps as pseudocode.
بە نموونەیەکی ڕۆژانەی ژماردن فێردەبیت چییە، چۆن هەنگاوەکان دووبارە دەکاتەوە و چۆن هەنگاوەکان وەک بنووسیت.
Whole video: an algorithm as exact steps, written as pseudocode, and a faster version.
هەموو ڤیدیۆکە: وەک چەند هەنگاوێکی ورد، نووسینی بە و وەشانێکی خێراتری.
Time and space complexityئاڵۆزیی کات و ئاڵۆزیی بیرگە
To compare algorithms we ask two questions. How much time does the algorithm need? How much memory does it need? The answers are called time complexity and space complexity. Both are written as a function of the input size n, for example the number of items in a list.
بۆ بەراوردکردنی دوو پرسیار دەکەین. چەند کاتی پێویستە؟ چەند ی پێویستە؟ وەڵامەکان ناویان و ـە. هەردووکیان وەک لە قەبارەی ، واتە n دەنووسرێن، بۆ نموونە ژمارەی دانەکانی .
| Termزاراوە | Meaningواتا | Searching a list of n namesگەڕان لە n ناودا |
|---|---|---|
| How the number of basic steps grows with n.ژمارەی هەنگاوە بنەڕەتییەکان چۆن لەگەڵ n زیاد دەبێت. | count the comparisonsبەراوردەکان بژمێرە | |
, T_worst(n)، T_worst(n) | The most steps any input of size n can need.زۆرترین هەنگاو کە هەر بە قەبارەی n دەکرێت پێویستی پێی بێت. | the name is last, or missing: n comparisonsناوەکە دوایینە یان نییە: n بەراورد |
, T_avg(n)، T_avg(n) | The average number of steps over all possible inputs.تێکڕای ژمارەی هەنگاوەکان لە هەموو ئەگەرییەکاندا. | about n/2 comparisonsنزیکەی n/2 بەراورد |
| How the memory the algorithm uses grows with n. چەند بەکار دەهێنێت و ئەم بڕە چۆن لەگەڵ n زیاد دەبێت. | no extra memory needed: a constant amount زیادە پێویست نییە: بڕێکی جێگیر |
The program below does not use a stopwatch. It counts steps. One loop over n items needs n steps. Two loops inside each other need n × n steps:
ئەم خوارەوە کاتژمێری وەستان بەکار ناهێنێت. هەنگاوەکان دەژمێرێت. یەک بەسەر n دانەدا n هەنگاوی پێویستە. دوو لەناو یەکتردا n × n هەنگاویان پێویستە:
def count_steps_one_loop(n):
steps = 0
for i in range(n): # look at every item once
steps += 1
return steps
def count_steps_two_loops(n):
steps = 0
for i in range(n): # for every item ...
for j in range(n): # ... look at every item again
steps += 1
return steps
for n in [10, 100, 1000]:
print(n, count_steps_one_loop(n), count_steps_two_loops(n))10 10 100 100 100 10000 1000 1000 1000000
When n grows 10 times, the first count grows 10 times but the second grows 100 times. This is true on every computer.
کاتێک n دە ئەوەندە دەبێت، ژمارەی یەکەم دە ئەوەندە دەبێت، بەڵام دووەم سەد ئەوەندە. ئەمە لەسەر هەموو ڕاستە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
One step for every item: n steps in total.
یەک هەنگاو بۆ هەر دانەیەک: سەرجەم n هەنگاو.
- Lineدێڕی 10-12
For every item, look at every item again: n × n steps.
بۆ هەر دانەیەک، دووبارە سەیری هەموو دانەکان بکە: n × n هەنگاو.
Time and space complexityئاڵۆزیی کات و ئاڵۆزیی بیرگە
Analysing a problem or an algorithm?شیکردنەوەی کێشەیەک یان ئەلگۆریزمێک؟
The course notes say two things that sound different. The main line says that complexity analyses problems rather than algorithms. The footnote says that complexity analysis helps us compare algorithms and choose the most efficient one. Both are true. They are two questions:
تێبینییەکانی کۆرسەکە دوو شت دەڵێن کە جیاواز دەردەکەون. دێڕە سەرەکییەکە دەڵێت ئاڵۆزی کێشەکان شی دەکاتەوە نەک . پەراوێزەکە دەڵێت شیکردنەوەی ئاڵۆزی یارمەتیمان دەدات بەراورد بکەین و کاراترینیان هەڵبژێرین. هەردووکیان ڕاستن. دوو پرسیارن:
| Questionپرسیار | Exampleنموونە |
|---|---|
| Algorithm analysis: how much time and memory does this algorithm need?: ئەم چەند کات و پێویستە؟ | Comparing every pair of names in a list needs about n² steps.بەراوردکردنی هەموو جووتێک ناو لە نزیکەی n² هەنگاوی پێویستە. |
| Problem complexity: how much work does the best possible algorithm for the problem need?ئاڵۆزیی کێشە: باشترین کە بۆ کێشەکە هەبێت چەند کاری پێویستە؟ | To find the largest number in an unsorted list, any algorithm must look at all n numbers, so at least n steps.بۆ دۆزینەوەی گەورەترین ژمارە لە ڕیزنەکراودا، هەر دەبێت سەیری هەموو n ژمارەکە بکات، کەواتە لانیکەم n هەنگاو. |
In this course we mostly use the first view: we measure search algorithms and choose the better one for a job.
لەم کۆرسەدا زیاتر بۆچوونی یەکەم بەکار دەهێنین: دەپێوین و باشترەکەیان بۆ کارێک هەڵدەبژێرین.
Big-O notation in one screenنیشانەی O-ی گەورە لە یەک لاپەڕەدا
A short way to write how fast the cost of an algorithm grows, keeping only the biggest part: 3n² + 5n + 2 steps is written O(n²).
شێوازێکی کورتە بۆ نووسینی ئەوەی تێچووی چەند خێرا گەورە دەبێت، تەنیا گەورەترین بەشی دەهێڵێتەوە: 3n² + 5n + 2 هەنگاو وەک O(n²) دەنووسرێت.
Exact step counts are long, for example 3n² + 5n + 2. When n is large, the biggest part (3n²) is almost all of it, and the constant 3 does not change how fast the count grows. So we keep only the fastest-growing part and drop the constant: 3n² + 5n + 2 steps is written O(n²), read “order n squared”. Unless we say otherwise, the O describes the worst case. How fast is fast? in Fundamentals of Programming shows constant, linear and quadratic time in sorting programs.
ژمارەی وردی هەنگاوەکان درێژە، بۆ نموونە 3n² + 5n + 2. کاتێک n گەورەیە، گەورەترین بەش (3n²) نزیکەی هەمووی پێکدەهێنێت و ژمارە جێگیرەکەی 3 ناگۆڕێت کە ژمارەکە چەند خێرا گەورە دەبێت. بۆیە تەنها خێراترین بەشی گەشەکردوو دەهێڵینەوە و ژمارە جێگیرەکە لادەبەین: 3n² + 5n + 2 هەنگاو بە O(n²) دەنووسرێت (بە ئینگلیزی دەخوێنرێتەوە: «order n squared»). ئەگەر شتێکی تر نەڵێین، O وەسف دەکات. خێرا چەندە خێرایە؟ لە بنەماکانی ، و لە پیشان دەدات.
| Orderپلە | Nameناو | Exampleنموونە |
|---|---|---|
O(1) | constantجێگیر | read my_list[0]خوێندنەوەی my_list[0] |
O(log n) | logarithmicلۆگاریتمی | take the smallest item out of a heap (next chapter)دەرهێنانی بچووکترین دانە لە (بەشی داهاتوو) |
O(n) | linearهێڵی | look at every item of a list onceیەک جار سەیرکردنی هەموو دانەکانی |
O(n²) | quadraticدووجایی | compare every item with every itemبەراوردکردنی هەموو دانەیەک لەگەڵ هەموو دانەیەک |
O(bᵈ) | exponentialتەوانی | try every sequence of d actions, with b choices each timeتاقیکردنەوەی هەموو زنجیرەیەکی d کار، کە هەر جارە b هەڵبژاردە هەیە |
The last row is the one that matters most in AI. Imagine a problem where the agent has b possible actions at every step, and the goal is d steps away. The number of action sequences is b × b × … × b = bᵈ. Watch it explode when b = 10:
دوایین ڕیز لە لە هەمووی گرنگترە. کێشەیەک بهێنە بەرچاوت کە تێیدا لە هەر هەنگاوێکدا b کاری ئەگەریی هەیە و ئامانجەکە d هەنگاو دوورە. ژمارەی زنجیرە کارەکان دەبێتە b × b × … × b = bᵈ. سەیر بکە کاتێک b = 10 چۆن دەتەقێتەوە:
b = 10 # 10 possible actions at every step
for d in [2, 4, 6, 8, 10]: # the answer is d steps away
print(f"d = {d:2}: {b ** d:,} possible action sequences")d = 2: 100 possible action sequences d = 4: 10,000 possible action sequences d = 6: 1,000,000 possible action sequences d = 8: 100,000,000 possible action sequences d = 10: 10,000,000,000 possible action sequences
Ten more steps multiply the work by ten billion. Even at a billion sequences per second, d = 10 needs about 10 seconds, d = 15 needs about 11 days and d = 20 needs over 3,000 years.
دە هەنگاوی زیاتر کارەکە دە ملیار ئەوەندە دەکات. تەنانەت بە ملیارێک زنجیرە لە چرکەیەکدا، d = 10 نزیکەی ١٠ چرکەی پێویستە، d = 15 نزیکەی ١١ ڕۆژ و d = 20 زیاتر لە ٣٠٠٠ ساڵ.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
b: how many choices the agent has at each step.
b: لە هەر هەنگاوێکدا چەند هەڵبژاردەی هەیە.
- Lineدێڕی 3
b ** dis b to the power d;:,prints thousands separators.b ** dواتە b بە توانی d؛:,فاریزە دەخاتە نێوان هەزارەکان.
Big-O in short: drop constants, keep the fastest-growing term, and use the worst case, with constant, linear and quadratic examples.
بە کورتی: ژمارە جێگیرەکان لابدە، تەنها ئەو بەشەی خێراتر گەورە دەبێت بهێڵەرەوە و حساب بۆ بکە، لەگەڵ نموونەی کاتی جێگیر، هێڵی و دووجایی.
Whole video: constant, linear and quadratic time, and why Big-O drops constants.
هەموو ڤیدیۆکە: کاتی جێگیر و هێڵی و دووجایی. هەروەها بۆچی ژمارە جێگیرەکان لادەبات.
Reading AI pseudocodeخوێندنەوەی نیمچەکۆدی ژیریی دەستکرد
A plan of a program written in short, simple lines that look like English, not in a real . It shows the steps of an , but a computer cannot run it.
پلانی کە بە دێڕی کورت و سادە دەنووسرێت کە لە زمانی ئاسایی دەچن، نەک بە کۆدی ڕاستەقینە. هەنگاوەکانی پیشان دەدات، بەڵام کۆمپیوتەر ناتوانێت جێبەجێی بکات.
Pseudocode is a simple language for describing algorithms. It looks like programming languages such as C, Java, Lisp or Python, but it is informal: a step may be written as a mathematical formula or in ordinary English. Nobody compiles pseudocode; people read it. AI books follow a small set of conventions, so once you know them you can read any algorithm in this course.
زمانێکی سادەیە بۆ وەسفکردنی . لە دەچێت وەک C، Java، Lisp یان ، بەڵام فەرمی نییە: هەنگاوێک دەکرێت بە هاوکێشەیەکی بیرکاری یان بە زمانی ئاسایی بنووسرێت. کەس وەرناگێڕێت بۆ زمانی ئامێر؛ خەڵک دەیخوێنێتەوە. کتێبەکانی چەند ڕێسایەکی کەم پەیڕەو دەکەن، بۆیە کاتێک فێریان بوویت دەتوانیت هەموو لەم کۆرسەدا بخوێنیتەوە.
Reading AI pseudocodeخوێندنەوەی نیمچەکۆدی ژیریی دەستکرد
Variables, functions and valuesگۆڕاوەکان، کردارەکان و نرخەکان
| Conventionڕێسا | Pseudocode | Meaningواتا |
|---|---|---|
| 1. Persistent variable١. | persistent: memory | Gets a starting value the first time the function is called and keeps its value in later calls. Agent programs use it for memory. Variable names are lowercase italic.یەکەم جار کە بانگ دەکرێت نرخێکی سەرەتایی وەردەگرێت و لە بانگکردنەکانی دواتردا نرخەکەی دەپارێزێت. بۆ بەکاری دەهێنن. ناوی بە پیتی بچووک و لار دەنووسرێن. |
| 2. Functions as values٢. وەک نرخ | f ← SQUARE | A variable may hold a function. Functions and procedures have capitalised names. دەتوانێت هەڵبگرێت. ناوی و ڕێکارەکان بە پیتی گەورە دەنووسرێن. |
| 3. Default values٣. | function F(x, y = 0) returns a number | y is optional; its default value is 0, so F(3, 0) and F(3) are the same call.y ئارەزوومەندانەیە؛ 0ـە، بۆیە F(3, 0) و F(3) هەمان بانگکردنن. |
| 4. Indentation٤. | if … then
do this | Indentation shows which lines belong to a loop, a condition or a block, exactly as in Python. پیشان دەدات کام دێڕانە سەر بە ، یان بلۆکێکن، ڕێک وەک . |
| 5. Assignment٥. | x ← value | Work out the right-hand side, then store it in the variable on the left.لای ڕاست حیساب بکە، پاشان لە لای چەپدا هەڵیبگرە. |
| 5a. Destructuring assignment٥ئا. | x, y ← pair | The right-hand side is a pair (a two-item tuple): the first item goes to x and the second to y. Also in loops: for each x, y in pairs do.لای ڕاست جووتێکە ( دوو دانەیی): دانەی یەکەم دەچێتە x و دووەم دەچێتە y. لە : for each x, y in pairs do. |
Here are the same ideas in real Python, so you can see that pseudocode is close to code:
ئەمانە هەمان بیرۆکەکانن بە ڕاستەقینە، بۆ ئەوەی ببینیت چەند نزیکە لە کۆدەوە:
# 1. A persistent variable: initialised once, keeps its value between calls
def make_counter():
calls = 0
def count():
nonlocal calls
calls += 1
return calls
return count
count = make_counter()
print(count(), count(), count())
# 2. Functions as values
def square(n):
return n * n
f = square # f now holds the function itself
print(f(5))
# 3. Default values for parameters
def F(x, y=0):
return x + y
print(F(3, 0), F(3))
# 5a. Destructuring assignment
x, y = (4, 7)
print(x, y)
for x, y in [(1, 2), (3, 4)]:
print(x + y)1 2 3 25 3 3 4 7 3 7
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-10
Python has no
persistentkeyword. An inner function that remembers a variable of the outer function (herecalls) gives the same effect.nonlocallets the inner function change it.وشەی
persistentی نییە. ناوەکی کە لە لە بیر دەگرێت (لێرەداcalls) هەمان کاریگەری دەدات.nonlocalڕێگە دەدات بیگۆڕێت.- Lineدێڕی 22
fnow holds the function itself (no brackets, so it is not called yet).ئێستا
fخودی هەڵدەگرێت (بەبێ کەوانە، بۆیە هێشتا بانگ ناکرێت).- Lineدێڕی 27
y=0is the default value.y=0.- Lineدێڕی 34-37
Destructuring assignment: one tuple fills two variables, also inside a
forloop.: یەک دوو پڕ دەکاتەوە، لە ناو .
Reading AI pseudocodeخوێندنەوەی نیمچەکۆدی ژیریی دەستکرد
Conditions, loops, generators and listsمەرجەکان، خولەکان، بەرهەمهێنەرەکان و لیستەکان
| Conventionڕێسا | Pseudocode | Meaningواتا |
|---|---|---|
| 6. Conditional٦. | if c then … else … | If condition c holds, do the first part; otherwise do the else part.ئەگەر c ڕاست بێت بەشی یەکەم بکە؛ ئەگەر نا بەشی else بکە. |
| 7.1 for each٧.١ for each | for each x in c do | Run the loop once for every element of the collection c, with x bound to that element. بۆ هەر لە کۆکراوەی c جارێک جێبەجێ بکە، x دەبێتە ئەو دانەیە. |
| 7.2 counting٧.٢ ژماردن | for i = 1 to n do | i takes the whole numbers 1, 2, …, n (n included).i ژمارە تەواوەکانی 1، 2، …، n وەردەگرێت (n خۆشی لەگەڵدایە). |
| 7.3 while٧.٣ while | while c do | Test c before every round; stop when c is false. Some books write loop for this.c پێش هەموو خولێک تاقی بکەرەوە؛ کاتێک c هەڵەیە بوەستە. هەندێک کتێب loop دەنووسن. |
| 7.4 repeat … until٧.٤ repeat … until | repeat … until c | The body always runs once; c is tested after each round, and the loop stops when c is true.ناوەڕۆکەکە هەمیشە جارێک جێبەجێ دەبێت؛ c دوای هەر خولێک تاقی دەکرێتەوە و کاتێک c ڕاستە دەوەستێت. |
| 8. Generator٨. | generator G(x) yields numbers | A function containing yield hands out a sequence of values, one each time it reaches yield, and then continues from the next statement. کە yieldی تێدایە زنجیرەیەک نرخ دەدات، هەر جارێک دەگاتە yield یەک نرخ، پاشان لە دواترەوە بەردەوام دەبێت. |
| 9. Data structures٩. | [x, y, z] [first|rest] {x, y, z} | Lists in square brackets; [first|rest] is a list split into its first item and the rest; sets in curly braces. Arrays start at index 1, as in mathematics, not 0 as in Python. لە ناو [ ]دا؛ [first|rest] کە کراوە بە یەکەم دانە و ئەوانی تر؛ لە ناو { }دا. لە 1ەوە دەست پێدەکەن، وەک بیرکاری، نەک 0 وەک . |
| 10. Comments١٠. | // a remark | Text after // is an explanation for the reader, not an instruction.دەقی دوای // ڕوونکردنەوەیە بۆ خوێنەر، نەک ڕێنمایی. |
# 7.2 for i = 1 to n do
n = 3
for i in range(1, n + 1):
print("i =", i)
# 7.4 repeat ... until c (the body runs at least once)
k = 10
while True:
k = k + 5
if k > 12:
break
print("k =", k)
# 8. A generator: yields one value each time
def G(x):
for i in range(x):
yield i * i
print(list(G(4)))
# 9. Arrays: pseudocode starts at 1, Python at 0
letters = ['a', 'b', 'c']
print(letters[0], letters[len(letters) - 1])i = 1 i = 2 i = 3 k = 15 [0, 1, 4, 9] a c
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
range(1, n + 1)gives 1, 2, …, n: the end number is not included, so we add 1.range(1, n + 1)ژمارەکانی 1، 2، …، n دەدات: ژمارەی کۆتایی ناگرێتەوە، بۆیە 1 زیاد دەکەین.- Lineدێڕی 8-11
Python has no
repeat … until.while Truewith a test andbreakat the end of the body does the same: the body runs at least once.repeat … untilی نییە.while Trueلەگەڵ تاقیکردنەوەیەک وbreakلە کۆتایی ناوەڕۆکەکەدا هەمان کار دەکات: ناوەڕۆکەکە لانیکەم جارێک جێبەجێ دەبێت.- Lineدێڕی 16-18
A generator: each
yieldhands out one value.: هەر
yieldێک یەک نرخ دەدات.- Lineدێڕی 24
Python counts from 0: the first letter is
letters[0]; pseudocode would call itletters[1].لە 0ەوە دەژمێرێت: یەکەم پیت
letters[0]ـە؛ پێی دەڵێتletters[1].
How AI code is organisedکۆدی ژیریی دەستکرد چۆن ڕێکدەخرێت
Pseudocode is written once and can be turned into code in many languages: Python, Java, C/C++, Lisp, Prolog and others. In AI code collections (such as the code that goes with AIMA), the code for each topic is usually split into four folders:
یەک جار دەنووسرێت و دەکرێت بکرێتە کۆد بە زۆر زمان: ، Java، C/C++، Lisp، Prolog و هی تر. لە کۆمەڵە کۆدەکانی (وەک ئەو کۆدەی لەگەڵ AIMA دێت)، کۆدی هەر بابەتێک زۆرجار دابەش دەکرێت بەسەر چوار بوخچەدا:
- Agents: code that defines agent types and agent programs.
- Algorithms: the methods that the agent programs use, for example search algorithms.
- Environments: code that defines environment types and simulates them.
- Domains: problem types and concrete problems that are given to the algorithms as input, for example a map of towns or a puzzle.
- Agents: ئەو کۆدەی جۆرەکانی و پێناسە دەکات.
- Algorithms: ئەو ڕێگایانەی بەکاریان دەهێنن، بۆ نموونە .
- Environments: ئەو کۆدەی جۆرەکانی پێناسە دەکات و لاساییان دەکاتەوە.
- Domains: جۆرەکانی کێشە و کێشە دیارەکان کە وەک دەدرێن بە ، بۆ نموونە نەخشەی شارۆچکەکان یان مەتەڵێک.
The course notes also sketch the skeleton agent in Python, using those folders as modules:
تێبینییەکانی کۆرسەکە ئێسکەپەیکەریش بە دەنووسنەوە و ئەو بوخچانە وەک بەکار دەهێنن:
import environment as en
import domain as do
import action as ac
def skeleton_agent(percept):
m = []
m = do.push(percept)
a = ac.choose() # chosen from the action class
m = ac.update()
return aThe table-driven agentئەیجێنتی خشتەیی
An agent program that looks up its action in a table listing an action for every possible percept sequence. It works only for tiny problems, because the table becomes impossibly large.
کە کارەکەی لە خشتەیەکدا دەدۆزێتەوە کە بۆ هەر کارێکی تێدا نووسراوە. تەنیا بۆ کێشەی زۆر بچووک دەشێت، چونکە خشتەکە بە شێوەیەکی مەحاڵ گەورە دەبێت.
The simplest possible agent program does not think at all. It keeps every percept it has ever received and looks up the whole sequence in a big table that lists the right action for every possible sequence. This is the TABLE-DRIVEN-AGENT from AIMA:
سادەترین هەر بیر ناکاتەوە. هەموو ئەو تا ئێستا وەریگرتوون هەڵدەگرێت و بە دوای هەموو زنجیرەکەدا لە خشتەیەکی گەورەدا دەگەڕێت کە کاری دروستی بۆ هەر زنجیرەیەکی ئەگەری تێدایە. ئەمە TABLE-DRIVEN-AGENTـی AIMAیە:
function TABLE-DRIVEN-AGENT(percept) returns an action
persistent: percepts, a sequence, initially empty
table, a table of actions, indexed by percept sequences,
initially fully specified
append percept to the end of percepts
action ← LOOKUP(percepts, table)
return actionExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-4
Two persistent variables: the sequence of all percepts so far, and the table, which must be filled in completely before the agent starts.
دوو : زنجیرەی هەموو تا ئێستا و خشتەکە، کە دەبێت پێش دەستپێکردنی بە تەواوی پڕ کرابێتەوە.
- Lineدێڕی 6
Add the new percept to the end of the sequence.
بخە کۆتایی زنجیرەکە.
- Lineدێڕی 7
Find the row for the whole sequence and read its action.
ڕیزی هەموو زنجیرەکە بدۆزەوە و کارەکەی بخوێنەوە.
In the vacuum world from the lesson on intelligent agents, the table starts like this: [A, Clean] → Right, [A, Dirty] → Suck, [B, Clean] → Left, [B, Dirty] → Suck, then rows for sequences of two percepts, three percepts, and so on.
لە جیهانی گسکی کارەبایی لە وانەی ، خشتەکە بەم شێوەیە دەست پێدەکات: [A, Clean] ← Right، [A, Dirty] → Suck، [B, Clean] → Left، [B, Dirty] → Suck، پاشان ڕیز بۆ زنجیرەی دوو ، سێ و هتد.
The table-driven agentئەیجێنتی خشتەیی
The table-driven vacuum agent in Pythonئەیجێنتی خشتەیی بۆ گسکی کارەبایی بە Python
Now a real program. The two squares are loc_A = (0, 0) and loc_B = (1, 0). The table is a Python dictionary: each key is a percept sequence written as a tuple of percepts, and each value is an action. (Dictionaries and tuples are explained in The Python you need for the AI labs.)
ئێستا ڕاستەقینە. دوو چوارگۆشەکە loc_A = (0, 0) و loc_B = (1, 0)ن. خشتەکە : هەر کە وەک لە نووسراوە و هەر کارێکە. ( و لە ئەو Pythonـەی بۆ تاقیگەکانی ژیریی دەستکرد پێویستە ڕوون کراونەتەوە.)
loc_A, loc_B = (0, 0), (1, 0) # the two squares of the vacuum world
def table_driven_agent_program(table):
"""A table is a dictionary of {percept_sequence: action} pairs."""
percepts = [] # persistent: the percept sequence so far
def program(percept):
percepts.append(percept)
action = table.get(tuple(percepts))
return action
return program
table = {
((loc_A, 'Clean'),): 'Right',
((loc_A, 'Dirty'),): 'Suck',
((loc_B, 'Clean'),): 'Left',
((loc_B, 'Dirty'),): 'Suck',
((loc_A, 'Dirty'), (loc_A, 'Clean')): 'Right',
((loc_A, 'Clean'), (loc_B, 'Dirty')): 'Suck',
((loc_B, 'Clean'), (loc_A, 'Dirty')): 'Suck',
((loc_B, 'Dirty'), (loc_B, 'Clean')): 'Left',
((loc_A, 'Dirty'), (loc_A, 'Clean'), (loc_B, 'Dirty')): 'Suck',
((loc_B, 'Dirty'), (loc_B, 'Clean'), (loc_A, 'Dirty')): 'Suck',
}
agent = table_driven_agent_program(table)
for percept in [(loc_A, 'Dirty'), (loc_A, 'Clean'), (loc_B, 'Dirty')]:
print(percept, '->', agent(percept))((0, 0), 'Dirty') -> Suck ((0, 0), 'Clean') -> Right ((1, 0), 'Dirty') -> Suck
Each call gets one percept, but the decision uses the whole sequence so far.
هەر بانگکردنێک یەک وەردەگرێت، بەڵام بڕیارەکە هەموو زنجیرەکە تا ئێستا بەکار دەهێنێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The two locations of the vacuum world.
دوو شوێنەکەی جیهانی گسکی کارەبایی.
- Lineدێڕی 4-14
The outer function receives the table once and returns the agent program
program.یەک جار خشتەکە وەردەگرێت و ، واتە
program، دەگەڕێنێتەوە.- Lineدێڕی 6
perceptsis the persistent variable:programremembers it between calls.percepts:programلە نێوان بانگکردنەکاندا لە بیری دەگرێت.- Lineدێڕی 9-11
Append the new percept, then look up the whole sequence.
tuple()is needed because a list cannot be a dictionary key..get()returnsNonewhen there is no such row.زیاد بکە، پاشان بە دوای هەموو زنجیرەکەدا بگەڕێ.
tuple()پێویستە چونکە ناتوانێت ببێتە ..get()کاتێک ڕیزێکی وا نییەNoneدەگەڕێنێتەوە.- Lineدێڕی 16-27
The table from the course notes: one row for every percept sequence of length 1, 2 and 3 that it covers.
خشتەکەی تێبینییەکانی کۆرسەکە: یەک ڕیز بۆ هەر بە درێژیی 1، 2 و 3 کە دەیگرێتەوە.
The table-driven agentئەیجێنتی خشتەیی
Running the agent in the vacuum worldکارپێکردنی ئەیجێنتەکە لە جیهانی گسکی کارەباییدا
The course notes also test the agent in a small environment: both squares start dirty, the agent runs, and at the end both squares must be clean. Here is a tiny environment of our own that does the same. Replace the last three lines of table_agent.py (the for loop) with this code:
تێبینییەکانی کۆرسەکە لە بچووکیشدا تاقی دەکەنەوە: هەردوو چوارگۆشەکە بە پیسی دەست پێدەکەن، کار دەکات و لە کۆتاییدا دەبێت هەردووکیان پاک بن. ئەمە بچووکی خۆمانە کە هەمان کار دەکات. سێ دێڕی کۆتایی table_agent.py ( for) بەم کۆدە بگۆڕە:
def run_vacuum_world(program, status, location, steps):
"""Run an agent program in the two-square world; return the final status."""
for _ in range(steps):
percept = (location, status[location])
action = program(percept)
print(percept, '->', action)
if action == 'Suck':
status[location] = 'Clean'
elif action == 'Right':
location = loc_B
elif action == 'Left':
location = loc_A
else: # None: the table has no row for this sequence
break
return status
status = {loc_A: 'Dirty', loc_B: 'Dirty'}
agent = table_driven_agent_program(table)
final = run_vacuum_world(agent, status, loc_A, 4)
print(final)
print(final == {(1, 0): 'Clean', (0, 0): 'Clean'})((0, 0), 'Dirty') -> Suck
((0, 0), 'Clean') -> Right
((1, 0), 'Dirty') -> Suck
((1, 0), 'Clean') -> None
{(0, 0): 'Clean', (1, 0): 'Clean'}
TrueThe squares are clean after 3 steps. On the 4th step the agent returns None: the table has no row for a sequence of 4 percepts.
دوای 3 هەنگاو چوارگۆشەکان پاکن. لە هەنگاوی چوارەمدا None دەگەڕێنێتەوە: خشتەکە هیچ ڕیزێکی بۆ زنجیرەیەکی 4 نییە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-15
The environment: give the agent a percept, carry out its action, repeat.
: بدە بە ، کارەکەی جێبەجێ بکە، دووبارەی بکەرەوە.
- Lineدێڕی 13-14
Nonemeans the table has no row for this percept sequence: stop.Noneواتە خشتەکە هیچ ڕیزێکی بۆ ئەم نییە: بوەستە.- Lineدێڕی 20
Run 4 steps, starting in square A.
4 هەنگاو کار بکە، لە چوارگۆشەی A دەست پێبکە.
- Lineدێڕی 22
The same test as in the notes: are both squares clean?
هەمان تاقیکردنەوەی ناو تێبینییەکان: ئایا هەردوو چوارگۆشەکە پاکن؟
The table-driven agentئەیجێنتی خشتەیی
Why the table explodesبۆچی خشتەکە دەتەقێتەوە
The 4th step showed the real problem, which the lesson Intelligent agents already hinted at. The table needs a row for every possible percept sequence, and every extra step multiplies the number of sequences. In the vacuum world there are only 4 possible percepts: (A, Clean), (A, Dirty), (B, Clean), (B, Dirty). Let us count the rows for an agent that lives T steps:
هەنگاوی چوارەم کێشە ڕاستەقینەکەی پیشان دا، کە وانەی ئەیجێنتەکان پێشتر ئاماژەی پێ دابوو. خشتەکە پێویستی بە ڕیزێکە بۆ هەر کە دەکرێت ڕوو بدات و هەر هەنگاوێکی زیادە ژمارەی زنجیرەکان چەند ئەوەندە دەکات. لە جیهانی گسکی کارەباییدا تەنها 4 ئەگەری هەیە: (A، Clean)، (A، Dirty)، (B، Clean) و (B، Dirty). با ڕیزەکان بژمێرین بۆ کە T هەنگاو دەژی:
possible_percepts = 4 # (A, Clean), (A, Dirty), (B, Clean), (B, Dirty)
rows = 0
for T in range(1, 11): # T = how many steps the agent lives
rows += possible_percepts ** T
if T in (1, 2, 3, 5, 10):
print(f"lifetime {T:2} steps: {rows:,} rows")lifetime 1 steps: 4 rows lifetime 2 steps: 20 rows lifetime 3 steps: 84 rows lifetime 5 steps: 1,364 rows lifetime 10 steps: 1,398,100 rows
A cleaner that lives only 10 steps already needs 1,398,100 rows. A taxi whose camera sees millions of different images every second would need more rows than there are atoms in the universe.
گسکێکی کارەبایی کە تەنها 10 هەنگاو بژی پێویستی بە 1,398,100 ڕیزە. تەکسییەک کە کامێراکەی هەموو چرکەیەک ملیۆنان وێنەی جیاواز دەبینێت، پێویستی بە ڕیزی زیاترە لە ژمارەی گەردیلەکانی گەردوون.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
After T steps there are 4ᵀ new sequences of length T; we add them to the total.
دوای T هەنگاو 4ᵀ زنجیرەی نوێ بە درێژیی T هەیە؛ دەیانخەینە سەر کۆی گشتی.
So the table-driven agent is correct in theory but useless in practice: no memory could hold the table, nobody could write it, and the agent could never learn all of it. The challenge of AI is to write small programs that behave as well as a huge table would. The next lesson shows four kinds of such programs, starting with a reflex agent that does the vacuum job with four short rules.
کەواتە لە ڕووی تیۆرییەوە دروستە، بەڵام لە کرداردا بێسوودە: هیچ ناتوانێت خشتەکە هەڵبگرێت، کەس ناتوانێت بینووسێت و هەرگیز ناتوانێت هەمووی فێر بێت. ئاڵنگاریی ئەوەیە بچووک بنووسین کە هێندەی خشتەیەکی زەبەلاح باش ڕەفتار بکەن. وانەی داهاتوو (جۆرەکانی ئەیجێنت) چوار جۆر لەو بەرنامانە پیشان دەدات، بە دەست پێدەکات کە کاری گسکەکە بە چوار یاسای کورت دەکات.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | Betterباشتر |
|---|---|
Starting pseudocode with def and a hyphenated name.دەستپێکردنی بە def و ناوێکی بە - بەستراو. | function NAME(…) returns … in pseudocode; def name_with_underscores(…): in Python.function NAME(…) returns … لە ؛ def name_with_underscores(…): لە . |
| Measuring time complexity in seconds.پێوانی بە چرکە. | Count basic steps as a function of n and write the result in Big-O form.هەنگاوە بنەڕەتییەکان وەک لە n بژمێرە و ئەنجامەکە بە شێوەی Big-O بنووسە. |
| Thinking a persistent variable is a global variable.وابزانیت هەمان . | It belongs to one function and keeps its value between that function's calls.سەر بە یەک و نرخەکەی لە نێوان بانگکردنەکانی ئەو دەپارێزێت. |
Copying A[1] from pseudocode into Python for the first element.گواستنەوەی A[1] لە بۆ بۆ یەکەم دانە. | Pseudocode counts from 1, Python from 0: use A[0]. لە 1ەوە دەژمێرێت، لە 0ەوە: A[0] بەکار بهێنە. |
| Using a list as a dictionary key.بەکارهێنانی وەک . | Convert it first: tuple(percepts).سەرەتا بیگۆڕە: tuple(percepts). |
| Expecting a table-driven agent to scale.چاوەڕوانی ئەوەی بۆ کێشەی گەورە بگونجێت. | Its table grows exponentially; real agents use rules, models, goals or utilities.خشتەکەی بە شێوەی تەوانی گەورە دەبێت؛ یاسا، مۆدێل، ئامانج یان بەکار دەهێنن. |
Exercisesڕاهێنانەکان
Write the agent program of a room thermostat in AIMA-style pseudocode, following the skeleton agent. The percept is the room temperature. The agent remembers the last temperature it read, and returns HEAT-ON, HEAT-OFF or NOTHING.
ترمۆستاتی ژوورێک بە شێوازی AIMA بنووسە، بە پەیڕەوکردنی ئێسکەپەیکەر. ئەو ژمارەیەیە کە گەرمیی ژوورەکە پیشان دەدات. دوایین ژمارەی گەرمی کە خوێندوویەتییەوە لە بیر دەگرێت و HEAT-ON، HEAT-OFF یان NOTHING دەگەڕێنێتەوە.
Hintئاماژە
Start from the skeleton agent: header, persistent memory, a few assignments, return.
لە ئێسکەپەیکەرەوە دەست پێبکە: سەردێڕ، persistent، چەند ، return.
Show answerوەڵامەکە پیشان بدە
function THERMOSTAT-AGENT(temperature) returns an action
persistent: last, the last temperature read, initially 20
if temperature < 19 and temperature ≤ last then action ← HEAT-ON
else if temperature > 23 then action ← HEAT-OFF
else action ← NOTHING
last ← temperature // remember for next time
return actionAny answer is fine if it has the function … returns line, a persistent variable for the memory, ← for assignment, and returns one action. Here the memory lets the agent see whether the room is still getting colder.
هەر وەڵامێک باشە ئەگەر دێڕی function … returns، بۆ ، ← بۆ تێدا بێت و یەک کار بگەڕێنێتەوە. لێرەدا ڕێگە بە دەدات بزانێت ئایا ژوورەکە هێشتا ساردتر دەبێت.
In the vacuum-world run, the 4th percept (loc_B, 'Clean') got None. Add one row to the table so that, after both squares are clean, the agent does NoOp (nothing). Then run 5 steps. What happens on step 5, and what does that show?
لە کارپێکردنی جیهانی گسکی کارەباییدا، چوارەم (loc_B, 'Clean') Noneی وەرگرت. یەک ڕیز بخە سەر خشتەکە بۆ ئەوەی دوای پاکبوونەوەی هەردوو چوارگۆشەکە، NoOp بکات (هیچ نەکات). پاشان 5 هەنگاو کار پێبکە. لە هەنگاوی 5دا چی ڕوو دەدات و ئەمە چی پیشان دەدات؟
Show answerوەڵامەکە پیشان بدە
loc_A, loc_B = (0, 0), (1, 0) # the two squares of the vacuum world
def table_driven_agent_program(table):
"""A table is a dictionary of {percept_sequence: action} pairs."""
percepts = [] # persistent: the percept sequence so far
def program(percept):
percepts.append(percept)
action = table.get(tuple(percepts))
return action
return program
table = {
((loc_A, 'Clean'),): 'Right',
((loc_A, 'Dirty'),): 'Suck',
((loc_B, 'Clean'),): 'Left',
((loc_B, 'Dirty'),): 'Suck',
((loc_A, 'Dirty'), (loc_A, 'Clean')): 'Right',
((loc_A, 'Clean'), (loc_B, 'Dirty')): 'Suck',
((loc_B, 'Clean'), (loc_A, 'Dirty')): 'Suck',
((loc_B, 'Dirty'), (loc_B, 'Clean')): 'Left',
((loc_A, 'Dirty'), (loc_A, 'Clean'), (loc_B, 'Dirty')): 'Suck',
((loc_B, 'Dirty'), (loc_B, 'Clean'), (loc_A, 'Dirty')): 'Suck',
((loc_A, 'Dirty'), (loc_A, 'Clean'), (loc_B, 'Dirty'), (loc_B, 'Clean')): 'NoOp',
}
def run_vacuum_world(program, status, location, steps):
"""Run an agent program in the two-square world; return the final status."""
for _ in range(steps):
percept = (location, status[location])
action = program(percept)
print(percept, '->', action)
if action == 'Suck':
status[location] = 'Clean'
elif action == 'Right':
location = loc_B
elif action == 'Left':
location = loc_A
elif action == 'NoOp':
pass # do nothing
else: # None: the table has no row for this sequence
break
return status
status = {loc_A: 'Dirty', loc_B: 'Dirty'}
agent = table_driven_agent_program(table)
final = run_vacuum_world(agent, status, loc_A, 5)
print(final)
print(final == {(1, 0): 'Clean', (0, 0): 'Clean'})((0, 0), 'Dirty') -> Suck
((0, 0), 'Clean') -> Right
((1, 0), 'Dirty') -> Suck
((1, 0), 'Clean') -> NoOp
((1, 0), 'Clean') -> None
{(0, 0): 'Clean', (1, 0): 'Clean'}
TrueExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 27
The new row: a sequence of 4 percepts → NoOp.
ڕیزە نوێیەکە: زنجیرەیەکی 4 ← NoOp.
- Lineدێڕی 43-44
The environment must also accept
NoOp.دەبێت
NoOpیش قبووڵ بکات.
Step 4 now works, but step 5 gives None again: a sequence of 5 percepts is a new key. Every extra step needs a whole new layer of rows. That is the table explosion in action.
ئێستا هەنگاوی 4 کار دەکات، بەڵام هەنگاوی 5 دووبارە None دەدات: زنجیرەیەکی 5 نوێیە. هەر هەنگاوێکی زیادە چینێکی تەواوی نوێی ڕیزی پێویستە. ئەمە تەقینەوەی خشتەکەیە لە کرداردا.
This function compares every pair of items once. Count its steps for n = 10, 100 and 1000 without running it, then give its Big-O.
ئەم هەموو جووتێک لە دانەکان یەک جار بەراورد دەکات. بەبێ کارپێکردن، هەنگاوەکانی بۆ n = 10، 100 و 1000 بژمێرە، پاشان Big-Oـەکەی بڵێ.
def count_pairs(n):
steps = 0
for i in range(n):
for j in range(i + 1, n):
steps += 1
return stepsShow answerوەڵامەکە پیشان بدە
def count_pairs(n):
steps = 0
for i in range(n):
for j in range(i + 1, n): # every pair (i, j) with i < j, once
steps += 1
return steps
for n in [10, 100, 1000]:
print(n, count_pairs(n))10 45 100 4950 1000 499500
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
j starts after i, so each pair is counted once.
j لە دوای i دەست پێدەکات، بۆیە هەر جووتێک یەک جار دەژمێردرێت.
The count is n(n − 1)/2 = ½n² − ½n. Keep the biggest part and drop the constant ½: O(n²). Half as many steps as two full loops, but the same order of growth.
ژمارەکە n(n − 1)/2 = ½n² − ½nـە. گەورەترین بەش بهێڵەوە و ژمارە جێگیرەکەی ½ لاببە: O(n²). نیوەی هەنگاوەکانی دوو تەواو، بەڵام هەمان پلەی گەشەکردن.
Translate this pseudocode into Python and say what it prints: x ← 3, then repeat x ← x × 2 until x > 100, then print x.
ئەم وەربگێڕە بۆ و بڵێ چی چاپ دەکات: x ← 3، پاشان repeat x ← x × 2 until x > 100، پاشان x چاپ بکە.
Show answerوەڵامەکە پیشان بدە
x = 3
while True: # repeat ...
x = x * 2
if x > 100: # ... until x > 100
break
print(x)192
3 → 6 → 12 → 24 → 48 → 96 → 192. The test comes after the doubling, so the loop stops at the first value above 100: 192.
3 ← 6 ← 12 ← 24 ← 48 ← 96 ← 192. تاقیکردنەوەکە دوای دووجاکردنەکە دێت، بۆیە لە یەکەم نرخی سەرووی 100 دەوەستێت: 192.
How many rows does the vacuum table need for an agent that lives exactly 4 steps? Use the rule from the last screen.
خشتەی گسکەکە چەند ڕیزی پێویستە بۆ کە ڕێک 4 هەنگاو دەژی؟ یاساکەی دوایین لاپەڕە بەکار بهێنە.
Show answerوەڵامەکە پیشان بدە
4 + 4² + 4³ + 4⁴ = 4 + 16 + 64 + 256 = 340 rows. (The program printed 84 for 3 steps; 84 + 256 = 340.)
4 + 4² + 4³ + 4⁴ = 4 + 16 + 64 + 256 = 340 ڕیز. ( بۆ 3 هەنگاو 84ی چاپ کرد؛ 84 + 256 = 340.)
Summary and reviewپوختە و پێداچوونەوە
- The agent function says what to do for every percept sequence; the agent program is the code that does it, one percept at a time, keeping an internal state (memory).
- The skeleton agent: update the memory with the percept, choose the best action, remember the action, return it.
- AIMA pseudocode:
function … returns …,persistentvariables,←for assignment, indentation for blocks, arrays from 1,//for comments. - An algorithm is a clear set of steps for a whole class of problems. Its time and space complexity are functions of the input size n, not seconds or bytes, written in Big-O form.
- O(bᵈ) grows so fast that trying every action sequence is hopeless.
- A table-driven agent works in principle, but its table needs a row for every percept sequence and explodes.
- دەڵێت بۆ هەر چی بکرێت؛ ئەو کۆدەیە کە دەیکات، هەر جارە یەک ، لەگەڵ هەڵگرتنی ().
- ئێسکەپەیکەر: بە نوێ بکەرەوە، باشترین کار هەڵبژێرە، کارەکە لە بیر بگرە، بیگەڕێنەوە.
- AIMA:
function … returns …،persistent،←بۆ ، بۆ بلۆکەکان، لە 1ەوە،//بۆ . - کۆمەڵێک هەنگاوی ڕوونە بۆ هەموو کێشەکانی یەک جۆر. و لە قەبارەی (n)، نەک چرکە یان بایت. بە شێوەی Big-O دەنووسرێن.
- O(bᵈ) هێندە خێرا گەورە دەبێت کە تاقیکردنەوەی هەموو زنجیرە کارەکان بێهیوایە.
- لە بنەڕەتدا کار دەکات، بەڵام خشتەکەی بۆ هەر ڕیزێکی پێویستە و دەتەقێتەوە.
What is the difference between the agent function and the agent program?جیاوازیی نێوان کرداری ئەیجێنت و بەرنامەی ئەیجێنت چییە؟
The agent function is a description: percept sequence → action, for all sequences. The agent program is real code that runs on the architecture, receives one percept at a time and keeps memory to produce that behaviour.
وەسفێکە: ← کار، بۆ هەموو زنجیرەکان. کۆدی ڕاستەقینەیە کە لەسەر تەلارسازییەکە کار دەکات، هەر جارە یەک وەردەگرێت و هەڵدەگرێت بۆ ئەوەی ئەو ڕەفتارە بەرهەم بهێنێت.
What does persistent mean in pseudocode?persistent لە نیمچەکۆددا واتای چییە؟
The variable gets an initial value on the first call and keeps its value in all later calls of the same function.
لە یەکەم بانگکردندا نرخێکی سەرەتایی وەردەگرێت و لە هەموو بانگکردنەکانی دواتری هەمان نرخەکەی دەپارێزێت.
Why is time complexity not measured in seconds?بۆچی ئاڵۆزیی کات بە چرکە ناپێورێت؟
Seconds depend on the machine and the language. Counting steps as a function of n describes the algorithm itself.
چرکە پەیوەستە بە ئامێر و زمانەکەوە. ژماردنی هەنگاوەکان وەک لە n خودی وەسف دەکات.
What is the difference between worst-case and average-case time?جیاوازیی نێوان کاتی خراپترین بار و باری مامناوەند چییە؟
Worst case: the most steps any input of size n can need. Average case: the average number of steps over all inputs of size n.
: زۆرترین هەنگاو کە هەر بە قەبارەی n دەکرێت پێویستی پێی بێت. : تێکڕای هەنگاوەکان لە هەموو بە قەبارەی n.
Why does a table-driven agent fail for real problems?بۆچی ئەیجێنتی خشتەیی بۆ کێشە ڕاستەقینەکان سەرکەوتوو نابێت؟
The table needs one row per possible percept sequence. Their number grows exponentially with the agent's lifetime, so the table cannot be written, stored or learned.
خشتەکە بۆ هەر کە دەکرێت ڕوو بدات ڕیزێکی پێویستە. ژمارەیان بە شێوەی تەوانی لەگەڵ تەمەنی زیاد دەبێت، بۆیە ناتوانرێت خشتەکە بنووسرێت، هەڵبگیرێت یان فێری بکرێت.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Types of Agents and Multi-Agent Systemsجۆرەکانی ئەیجێنت و سیستەمە فرەئەیجێنتەکان
Related lecturesوانە پەیوەندیدارەکان
- Types of Agents and Multi-Agent Systemsجۆرەکانی ئەیجێنت و سیستەمە فرەئەیجێنتەکان
- The Python You Need for the AI Labsئەو Pythonـەی بۆ تاقیگەکانی ژیریی دەستکرد پێویستە
- Task Environments: PEAS and the Eight Propertiesژینگەکانی ئەرک: PEAS و هەشت تایبەتمەندییەکە
- Programming Languages and Algorithmsزمانەکانی بەرنامەسازی و ئەلگۆریزمەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Russell & Norvig, Artificial Intelligence: A Modern Approach, 4th ed. — contents (ch. 2.4 Agent programs; Appendix A Big-O; Appendix B pseudocode)
- AIMA pseudocode: TABLE-DRIVEN-AGENT
- aima-python: agents.py (TableDrivenAgentProgram, TrivialVacuumEnvironment)
- UC Berkeley CS188 notes: Uninformed search (complexity as O(b^m), O(bm))
- Python documentation: The nonlocal statement
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.