Skip to content

Commit 235d012

Browse files
antmarakisnorvig
authored andcommitted
Update games.ipynb (aimacode#539)
1 parent 342d6a3 commit 235d012

File tree

1 file changed

+109
-14
lines changed

1 file changed

+109
-14
lines changed

games.ipynb

Lines changed: 109 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
"This notebook serves as supporting material for topics covered in **Chapter 5 - Adversarial Search** in the book *Artificial Intelligence: A Modern Approach.* This notebook uses implementations from [games.py](https://github.com/aimacode/aima-python/blob/master/games.py) module. Let's import required classes, methods, global variables etc., from games module."
1010
]
1111
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# Contents\n",
17+
"\n",
18+
"* Game Representation\n",
19+
"* Game Examples\n",
20+
" * Tic-Tac-Toe\n",
21+
" * Figure 5.2 Game\n",
22+
"* Min-Max\n",
23+
"* Players\n",
24+
"* Let's Play Some Games!"
25+
]
26+
},
1227
{
1328
"cell_type": "code",
1429
"execution_count": 1,
@@ -200,7 +215,7 @@
200215
},
201216
{
202217
"cell_type": "code",
203-
"execution_count": 6,
218+
"execution_count": 4,
204219
"metadata": {
205220
"collapsed": true
206221
},
@@ -225,7 +240,7 @@
225240
},
226241
{
227242
"cell_type": "code",
228-
"execution_count": 7,
243+
"execution_count": 5,
229244
"metadata": {},
230245
"outputs": [
231246
{
@@ -249,7 +264,7 @@
249264
},
250265
{
251266
"cell_type": "code",
252-
"execution_count": 9,
267+
"execution_count": 6,
253268
"metadata": {
254269
"collapsed": true
255270
},
@@ -267,7 +282,7 @@
267282
},
268283
{
269284
"cell_type": "code",
270-
"execution_count": 10,
285+
"execution_count": 7,
271286
"metadata": {
272287
"collapsed": true
273288
},
@@ -278,7 +293,7 @@
278293
},
279294
{
280295
"cell_type": "code",
281-
"execution_count": 11,
296+
"execution_count": 8,
282297
"metadata": {},
283298
"outputs": [
284299
{
@@ -302,7 +317,7 @@
302317
},
303318
{
304319
"cell_type": "code",
305-
"execution_count": 12,
320+
"execution_count": 9,
306321
"metadata": {
307322
"collapsed": true
308323
},
@@ -313,7 +328,7 @@
313328
},
314329
{
315330
"cell_type": "code",
316-
"execution_count": 13,
331+
"execution_count": 10,
317332
"metadata": {},
318333
"outputs": [
319334
{
@@ -337,7 +352,7 @@
337352
},
338353
{
339354
"cell_type": "code",
340-
"execution_count": 14,
355+
"execution_count": 11,
341356
"metadata": {
342357
"collapsed": true
343358
},
@@ -348,7 +363,7 @@
348363
},
349364
{
350365
"cell_type": "code",
351-
"execution_count": 15,
366+
"execution_count": 12,
352367
"metadata": {},
353368
"outputs": [
354369
{
@@ -372,7 +387,7 @@
372387
},
373388
{
374389
"cell_type": "code",
375-
"execution_count": 16,
390+
"execution_count": 13,
376391
"metadata": {
377392
"collapsed": true
378393
},
@@ -383,7 +398,7 @@
383398
},
384399
{
385400
"cell_type": "code",
386-
"execution_count": 17,
401+
"execution_count": 14,
387402
"metadata": {},
388403
"outputs": [
389404
{
@@ -407,7 +422,7 @@
407422
},
408423
{
409424
"cell_type": "code",
410-
"execution_count": 18,
425+
"execution_count": 15,
411426
"metadata": {
412427
"collapsed": true
413428
},
@@ -418,7 +433,7 @@
418433
},
419434
{
420435
"cell_type": "code",
421-
"execution_count": 19,
436+
"execution_count": 16,
422437
"metadata": {},
423438
"outputs": [
424439
{
@@ -442,7 +457,7 @@
442457
},
443458
{
444459
"cell_type": "code",
445-
"execution_count": 20,
460+
"execution_count": 17,
446461
"metadata": {
447462
"collapsed": true
448463
},
@@ -451,6 +466,86 @@
451466
"%psource Fig52Game"
452467
]
453468
},
469+
{
470+
"cell_type": "markdown",
471+
"metadata": {},
472+
"source": [
473+
"# MIN-MAX\n",
474+
"\n",
475+
"This algorithm (often called *Minimax*) computes the next move for a player (MIN or MAX) at their current state. It recursively computes the minimax value of successor states, until it reaches terminals (the leaves of the tree). Using the `utility` value of the terminal states, it computes the values of parent states until it reaches the initial node (the root of the tree). The algorithm returns the move that returns the optimal value of the initial node's successor states.\n",
476+
"\n",
477+
"Below is the code for the algorithm:"
478+
]
479+
},
480+
{
481+
"cell_type": "code",
482+
"execution_count": 18,
483+
"metadata": {
484+
"collapsed": true
485+
},
486+
"outputs": [],
487+
"source": [
488+
"%psource minimax_decision"
489+
]
490+
},
491+
{
492+
"cell_type": "markdown",
493+
"metadata": {},
494+
"source": [
495+
"We will now play the Fig52 game using this algorithm. Take a look at the Fig52Game from above to follow along.\n",
496+
"\n",
497+
"It is the turn of MAX to move, and he is at state A. He can move to B, C or D, using moves a1, a2 and a3 respectively. MAX's goal is to maximize the end value. So, to make a decision, MAX needs to know the values at the aforementioned nodes and pick the greatest one. After MAX, it is MIN's turn to play. So MAX wants to know what will the values of B, C and D be after MIN plays.\n",
498+
"\n",
499+
"The problem then becomes what move will MIN make at B, C and D. The successor states of all these nodes are terminal states, so MIN will pick the smallest value for each node. So, for B he will pick 3 (from move b1), for C he will pick 2 (from move c1) and for D he will again pick 2 (from move d3).\n",
500+
"\n",
501+
"Let's see this in code:"
502+
]
503+
},
504+
{
505+
"cell_type": "code",
506+
"execution_count": 19,
507+
"metadata": {},
508+
"outputs": [
509+
{
510+
"name": "stdout",
511+
"output_type": "stream",
512+
"text": [
513+
"b1\n",
514+
"c1\n",
515+
"d3\n"
516+
]
517+
}
518+
],
519+
"source": [
520+
"print(minimax_decision('B', fig52))\n",
521+
"print(minimax_decision('C', fig52))\n",
522+
"print(minimax_decision('D', fig52))"
523+
]
524+
},
525+
{
526+
"cell_type": "markdown",
527+
"metadata": {},
528+
"source": [
529+
"Now MAX knows that the values for B, C and D are 3, 2 and 2 (produced by the above moves of MIN). The greatest is 3, which he will get with move a1. This is then the move MAX will make. Let's see the algorithm in full action:"
530+
]
531+
},
532+
{
533+
"cell_type": "code",
534+
"execution_count": 20,
535+
"metadata": {},
536+
"outputs": [
537+
{
538+
"name": "stdout",
539+
"output_type": "stream",
540+
"text": [
541+
"a1\n"
542+
]
543+
}
544+
],
545+
"source": [
546+
"print(minimax_decision('A', fig52))"
547+
]
548+
},
454549
{
455550
"cell_type": "markdown",
456551
"metadata": {},

0 commit comments

Comments
 (0)