DEV Community

Timothee Simon
Timothee Simon

Posted on • Updated on

You can't beat this algorithm

So in my company (Hackages.io) we have what we call a HackFriday. So every other Friday we have one full day to experiment with tech we want to know more about. For me it was deep learing, but as a frontend dev I discovered it is quite hard to implement in the frontend, this is how IRMA was born.

IRMA

IRMA (Invicible Rock-paper-sissor Medium Algorithm) is a fortune teller that can predict with an accuracy of ~70% your next move at rock-paper-sissor.
You can try the game here, but be careful of the spell that could be cast on you if you beat her...
You can also use the j-k-l buttons on your keyboard to play faster.

Technical part

So IRMA is just a statistical computing algorithm that learn from the player. She remember every moves and try to predict the next one based on the previous successions. If you think you can be smarter than her and generate random move, try to stay bellow 50% accuracy after 100 moves.
Human beeings are not able to deliver true randomness, and IRMA know it. Let's take for exemple the 20 first moves an user (me) made:

["📝", "✂️", "✂️", "🗿", "✂️", "📝", "🗿", "📝", "🗿", "🗿", "✂️", "🗿", "📝", "✂️", "📝", "🗿", "✂️", "📝", "🗿", "✂️"]

It looks pretty random and it seems like it would be really difficult to predict my next move.
Now let's try to divide this list in 3-grams, the 3-grams is the list of all successions of 3 choices that were made by the user.
This is not the same as dividing the list by groups of 3, for exemple the list [a, b, c, d] would produce 2 grams: [a, b, c] and [b, c, d].

Here we have :

["📝", "✂️", "✂️"]
["✂️", "✂️", "🗿"]
["✂️", "🗿", "✂️"]
["🗿", "✂️", "📝"]
["✂️", "📝", "🗿"]
["📝", "🗿", "📝"]
["🗿", "📝", "🗿"]
["📝", "🗿", "🗿"]
["🗿", "🗿", "✂️"]
["🗿", "✂️", "🗿"]
["✂️", "🗿", "📝"]
["🗿", "📝", "✂️"]
["📝", "✂️", "📝"]
["✂️", "📝", "🗿"]
["📝", "🗿", "✂️"]
["🗿", "✂️", "📝"]
["✂️", "📝", "🗿"]
["📝", "🗿", "✂️"]

Still looks pretty random, but maybe you can already see some pattern. That is because, even for only 20 values I struggled to create random data, fearing to repeat myself, and yet I still did without realising it.

Now let's take the last 2 values : ["🗿", "✂️"] and filter out all the grams that do not start with those values:

["🗿", "✂️", "📝"]
["🗿", "✂️", "🗿"]
["🗿", "✂️", "📝"]

As we can see I have the tendency to put a "📝" after writing ["🗿", "✂️"], so IRMA will play "✂️". Now do this but with a lot of data and several grams sizes and you have a fully fonctionning IRMA.

Conclusion

IRMA is not deep learning, but it can still be considered machine learning. Unlike deep learning it does not require a lot of computing power, to prove this, IRMA run solely in the frontend with Javascript.
It can be a good alternative for developpers who want to implement a bit of "magical" intelligence to their frontend easily without extreme statistical knowledge and without deep learning disadvantages.

If you have idea to improve IRMA or idea of real world adaptation don't hesitate to leave a comment ;)

Top comments (0)