## [BOB] Artificial Stupidity

• From: "Gerben van den Broeke" <gebbun@xxxxxxxxx>
• To: "BOB mailing list" <projectbob@xxxxxxxxxxxxx>
• Date: Sat, 7 Apr 2007 16:30:39 +0200

```Yesterday Luuk and I created Artificial Stupidity, probably the
simplest AI ever. It has absolutely no abstraction at all, but it can
learn how to play tic tac toe, and probably other simple games as
well.

How it works:
The AS gets input, then returns output, then gets input, etc. At the
end of the game it hears if it has won, lost or the game ended as a
draw. Then it stores every step of the game in a hashtable, together
with it has lost or won the game. During the game, when it needs to
decide which action to do, it looks if it has had excactly the same
input before. If it has it looks which action in that situation
resulted in winning the game. Then it does that action. If it has
never been in the exact situation before it does a random action.

Example:
Tic tac toe game starts, AS sees an empty field. Because it's not been
in this situation before it randomly chooses one of the nine possible
actions, say the upper left cell. Then it sees the field with in the
upper left cell a 1 (player 1) and the middle field a 2 (opponent's
action), again chooses randomly. After a few turns it loses the game.
The next game it plays it sees an empty field. From the 9 actions
possible it knows that the upper left  caused it to lose once, and it
never tried the other actions. So now it tries one of the others.
After a few turns it wins. Next game it sees an empty field it chooses
the same action that made it win. If it wins again it adds one to how
many times it's won.
If actions have caused it to win and lose games, it calculates which
action has the highest win/lose ratio.

The results:
Testing in my python shell I was amazed by how quickly it learns to
win, even without being able to understand that it has to have 3 cells
in a row to win.
There are 3 types of players: human, randomdebiel (randomizer) and as
(the artificial stupidity).
The function play(player1, player2, times) in tictactoe returns a
string with a character for every game played (set by times):  '_' if
player1 won, 'X' if player2 won, and '-' if the game ended in a draw.

The attachment is a test in a shell showing how that it really learns
how to win.

The next step is designing a system with abstraction that can
understand a situation, simulate what would happen and see
resemblences in different situations.

Gerben
```
```Python 2.4.3 (#1, Mar 12 2007, 10:41:03)
[GCC 4.1.1 (Gentoo 4.1.1-r3)] on linux2

****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface.  This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 1.1.3
>>> import tictactoe
>>> import as
>>> luuk = tictactoe.randomdebiel(); # randomizer
>>> bob = as.as(); # Artificial Stupidity
>>> tictactoe.play(bob,luuk,10000);
'X_-_X___XX-__--X____X_-____X__XXX-X_X___X__-_XXX-X____X_____X______XX-X____-_X-XX____-_____X_X_-_XX______________X__X-_XX___X_-X___X-__-___X_-XX___-_--__X_____-_X_____-___________-_-______X___X-_____-__X_-X____X___-__--_-__-____-____-_________--_-X__-____-__-_-____-______-_X____-X_____-____X___-________________-_X-_X_________X___________-_X__-_-____--_____X-X_____-_-__-X_-___X-__XX_____X__---___-____X-___-____-_____-____-X_______________X__-_X--__-X_XX_X_X__X--X___-XX_____-__-_X--_X_________X___X_____X-________X_____X____________-_____-__________-_-______X___X____X-________-_X______________-__-_X______-_-__________-_______-___-____X__-______-______X________--___-____-__-________-_________-___-____X________________________________-______-________________-___--___________________-____-X__________-________________-___________X________X___-_-___________-_______-______--___--______X___________-_____________________-______-__-____-___-X_________________-_-__________-___-____________________________-_-__________________--_--________X-______-__--________-_--_______-_____________-___________________________-_--_____________-______-___-___-_______-________________---__-____________-___-__X__X-______-_____________-__________-_____-__X_-_____-_______________-____________________-____________________--____--_-_____--____________X_-_____________-___-______-____________-___-____-___-______-______-_-___-_____-_______-_--____________-_-___________-______-_-____-____-_X___________-_-_____-________-__________-_________________--_________-____-____-_____---___-__-____-___--______-________-_____________-_-______-__________________-__-____________________-_____________________-___-__________-_____-______-___________X_---_____________-________--__-_-__________-_______________-__-_________-_________-_-___________-_________________---X_-___-________-____-__________-_______-___-____________________-__-_-_-__-__-___-_____-________________-_-_-________-____-___-_______-___________-____-X_________-______--______-________-_________-_________________-_-______-__X________-______-______________X_-___-____________________-__---_-__-____-_-_______-_______-___-___-____-_-X-_____________-__-_________-_____________-_--__-___-____________-_-___--__________________-__X_X_-___-__________________________-__--__________-___-________________-___________-____________-__________________________--__-_--______________________-__________X-___-____-____-_______-_-____________-___-_______-_______-______-__--____-______-_____-_-_--________________________________-_______-_________-___________-______________--_______-_________-_______-_______-__-______-____-_-___X________--______________-____-_______-_______X_-__-_-_________________-_______________________-__-_-_________--___________-X____________-____-_____________-____-__-_____________-_____--___________________-______-______-____-_-__--______-_________________________________________________-___________X___-____________-_-________-________-____-_-___-_______________-__________--__________-_____-_______________________________________-_____________-__________________X_____-___________________________________-________________-__-_________--____-________-____-____________-_________--_________X-__--_____-________________________-___-__-____-_______--__________-__--_-___________-__________-____-___________________________________-__-___________________-_______-__-_-__-___-__-____-____-_______-__-____________________________-__________-________-_________-________-________--_-_____-___________--___-____-____-______-______-_-_X___-____-___________________--____-___________-____-____-________________________-__-_______-__-__________-___________-__-_______________________________--_____________________________________-___-________-_____-________-__________-___-_-___________-__-____-___-_______-____________---__-_________-___________-____--______-_-______________-__-____-________________________--_______________-_-_-__-_____-_______-_________---___________________-___-_-_________________________________-___________-_______--_____________-__-____-__--_____-________-_____________-__________--_____-_______________-____-________--________--_____-__________--_________________-_____________-_-_____________-__-____-________--______-____---__-_____-____-_______-________________-______________________-_______-_________________-__-______________________________________________-_________-___--__________-_____-_____--___-_______--________-____-_______-_______-___________________________________-_--______________-____-_________-______________-________________________-____-__-___-_______-__-___-______-_-___-__-__-_--____-_____________-___________________-____-___-_-_-__--___________-__-______________________________________-___________________-__-________________-__-__-__________-___-________-____-___-_-_____-_-___-_-_-____-__-_____________-_______---___________________-________________-__-_____-___-_____-____-__-_-___-__-_______-_________-________-_______-___________-____________-___________-_-_-_________________________-_______-__--_--__-____-__-________-__--__-_________________-_______________-_____________________________-_____-____-____-_-______-____-__--_-_____-___________________-____-__________-______________________-___________-___-___________-__________________-_______-_-______-_-________________-__________________-_-________--__-________-____-_________-_-____________________-_______-__-_-________-___________________--__-________________-_______________________-____-__________-___-_--___-___________-____-_-__________-___---__-____________________________-_-__-___-_---_-__-___-_____-____________-__-____-__________________________-___________--___-________--_______-______-____-__-_______-____-___-______-____________-_____-_-_-_____-_--___________-_______________________--____-__-__________________________-_______________--______--___-______________________-_______-____-___-___________-___________________---____-_____________________________________________--____________________-__-___________-_____-_______________-_______________________-_-________________________-_-_______________________-_________-__-____-_____-______-__-_-__________________-____-_________-______-_______________--_-______________--_-__-_-_______-______________________________________-_____-____________________-_-______--_________-__-________-__-_____________-__-_-__-___-_____-_______________-________-____-______________-_______-_-__--___________-_________________-____-_-_______-_____-_____-___________________________________________-__________-_-_______-_-____-_____________-____________-______-___-_______-______-________--_________-____-______________-_____--______-__-____----_____-___-_______________________-______-____-_______________-______-____-___--_______-_____-___-_____--__________-_______________-_-________________-____________-________________________-__-________-________-______-______-___________________________-____________-_________-__________________-_-_____________-___________-___-_______-__________________-______________-______--__________________-_-_____________________-_______________-__-____-__--______________-_________________-__-_______-___-_____-_________-_____-____-_-___-________-__--____-_________________________-_______--_______________--_____--___________________-___-_____-____________-_-_______-_________-____________-________-_____-________-_-__-__-______________-____________________-_--__________-_-_________________-__-____________-_____-___-_____________-___-________________--_____--__________-_______________-________________________________________-___________-_--_-___-_____-_____-__________________-___-__--_______________-___________________---____--_-_-_______-__________________________________________-________________-____-________-_______-__________-____-_____________-______-___________-___-__-______________---___-_-_____-_____--______--_________________-_-__-_________-___-_____-______--_____________________________________--______-_____-_________________-_____-___-_________-____-______________________________-_____-_-_______-___-_________--__-_______-__-________-_____-______-____________________________-_-___________-__________-________________________________-__-_____--__-____________--__________-_________________-_____-____-______-__________-__________________-______-_-__-_______-_________-___-__________-__________-____________--_____-_-______-_-___-___-___________________________________-__________-___-___--______________________-____________-_-_-___-__--_________-____--_-_____--_____--____--___-___________-_--__-___-______-______________________-___--___-_________-____________________-_________-___-_____--_--_-________________________________--____________-_________________________________-__________-_____-__-_____________________-_________-______________-__--________-____-______________________________________-_________________________-_______-_-___-_-__-_-___________________________-_-_______-________________-______________________-______-_______________________-_-____--_____-__-___-_____________-_________________________________________________________-____-___--________________--_-__-___-___-__-_______--____________-_-__-_--_______-_--_______-________-_______-________________-______________-_________-_______-___________-______-__-__________________--____-_____-_______-___-______-____________-_-___-_______-____-__________-___________________________-_______-_________________-_-__-_-_____-__________-____________-_-______-____-__________-__-_-_______-_-____________-__-_________________-_-__________________-___-__-_____-_________-_______________-__________-________-___-____________-_-__-________-__-___-___-____-____--__-_-_________________-______________________________-_____________-_______-__________-__-____-________-____-______-___________________-___-__--________________________________-_______________-____________'
>>> # After some time bob doesn't lose anymore :)
>>> # Now I'm going to create a new AS instance, which will fight against the
>>> henk = as.as();
>>> tictactoe.play(bob,henk,10000);
'________--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
>>> # They now only end in draw :D (they're probably playing the same game over
>>> and over again)
>>> # Now human intelligence versus artificial stupidity:
>>> me = tictactoe.human()
>>> tictactoe.play(bob,me)
[0, 0, 0]
[0, 0, 0]
[1, 0, 0]
You's turn: enter a digit between 1 and 9 (on numpad)
>>> 5
[1, 0, 0]
[0, 2, 0]
[1, 0, 0]
You's turn: enter a digit between 1 and 9 (on numpad)
>>> 4
[1, 0, 0]
[2, 2, 1]
[1, 0, 0]
You's turn: enter a digit between 1 and 9 (on numpad)
>>> 2
[1, 1, 0]
[2, 2, 1]
[1, 2, 0]
You's turn: enter a digit between 1 and 9 (on numpad)
>>> 3
You lose! :(
'_'
>>> # The stupidity beat my intelligence! (yes I'm not good at this, didn't see
>>> the upper row was all his)
>>> # It's quite nice to see how good it blocks me every time I almost have 3
>>> in a row.
>>> ```

### Other related posts:

• » [BOB] Artificial Stupidity