Man vs Machine (Learning)

The Idea

Despite a decades-long history of aiding in technical progress (and determining which cat picture appears on the top of your Instagram feed), artificial intelligence, for many, is still something to be feared. For proof, look no further than popular film, where Hal 9000, Blade Runner’s renegade replicants, and Ultron, the android antagonist of 2015’s Avengers: Age of Ultron, all serve as sentient, artificially intelligent villains, bent on destruction of humankind. And if not frightening, I fear AI is at least totally mystifying to most. This is something we at Moonshot would like to change, so as creative technologist, I set out to teach people about artificial intelligence and machine learning in a way that would be fun and memorable for anyone of any age.

In order to teach about a subject with as much depth and nuance as machine learning, I wanted to build a compelling interactive experience that would be eminently understandable. Rather than asking a user to learn how to interact with the tool, I wanted to rely on the user’s prior knowledge. I started to think about common games that could be co-opted to teach about artificial learning. Suddenly, it hit me: Rock Paper Scissors! Universal and simple enough to easily teach a computer how to play, the game would be a perfect medium for teaching about how an AI makes decisions, and how it is trained to make better decisions in the future. With this idea in mind, I got to work.

What’s In a Name?

There’s an old joke among developers that there are only two hard things in Computer Science: naming things, cache invalidation, and off-by-one errors. In order to get my nascent creation under version control, I had to come up with a name. While I don’t always practice the dogma of ‘first thought, best thought’, one name came to me almost immediately: RoShamBot. Growing up in the midwest, I had never known RPS as roshambo (as it is known in some parts of the west coast), but I remembered having come across the name on a crossword puzzle years ago. I decided to go with my gut and the name ended up sticking. Just like that, RoShamBot was born.

Teaching a Bot to Learn

In the game of RPS, a truly random player will be unbeatable in the long run; making an ‘educated guess’ will do no better than guessing randomly because the opponent does not follow any logical or repeating patterns. But humans are quite bad at being truly random, so it becomes possible to train a model to find and exploit their tendencies.

To do this, I first designed a data structure that would record every throw played against the bot. We decided that each game would be best of 5 (long enough to glean player insights, short enough to not be boring), so I established a tree with nodes representing each possible play combination within a memory of 5 plays. Each time a throw is played, the weight associated with the corresponding node is incremented. Then, in order to make a prediction, I traverse the tree, summing up the probability of the human player throwing any given play. I add in a bit of randomness which prevents the bot from seeming predictable even if the human is acting entirely predictably. At the count of three, the bot plays the throw to beat what it predicted the human would play, it reads the human’s play, then takes note of the result of the hand. It continues in this fashion until either the bot or the player have won 3 games.

Training the Model

Most neural networks will be trained thousands, if not hundreds of thousands of times before they become useful. If RoShamBot was to begin to discover and exploit tendencies in the way humans play Rock Paper Scissors, it would be necessary for the bot to play lots of games against a lot of different players. Over the course of development, RoShamBot rather quickly learned how I played the game, but others would still beat it quite handily. What RoShamBot needed next was a large pool of competitors with a variety of different approaches to playing the game.

A few days after I began development of RoShamBot, I went for a walk around Barkley’s headquarters and noticed a billboard right across the street that I hadn’t seen before. As luck would have it, the billboard advertised a Rock Paper Scissors competition hosted by a local bank, scheduled for only two weeks away. Voilà! Who better to train my wimpy, naive AI than the best of the best? The timeline would be tight, but I returned to work newly emboldened and with the soundtrack of an 80’s training montage ringing in my ears.

Iteration

Throughout its lifetime, RoShamBot has taken many different forms. The first iteration was simply comprised of a Leap Motion controller hooked up to my laptop and a plaintext interface printing out what the bot had decided to play. Shortly after that, we added an Arduino microcontroller and a few servo motors hooked up to a toy grabber hand we found on Amazon, so that the bot could actually throw moves. We still had plaintext interface to indicate the score as well as when the human should throw their play, but we were one step closer to getting away from the screen altogether. Next we added a strand of NeoPixel LEDs, an alphanumeric display to keep score, and a 3d-printed hand, all mounted in a cabinet made out of foam-core. We had finally found a form factor that allowed for the interaction we desired: no screens (save for the score counter), and intuitive feedback regarding timing and the results of each individual throw.

Having access to a 3D printer was indispensable for rapid prototyping. By my count, we printed at least 5 different hands for the bot, as well as several different housings for the servos and internal mounts for various peripherals. Because the feedback loop between idea, implementation, and testing was so tight, we were able to quickly see what works and what doesn’t, which allowed us to quickly find the right form factor and user experience.

Technical Difficulties

Throughout the process of building RoShamBot, we burned through 4 different servo motors, 1 strand of NeoPixel LEDs, and 1 Arduino board. For a reason we could not determine, the servo motors would subtly but constantly tremor while powered, which caused them to overheat and eventually fry the encoder. For weeks, we frustratedly tested the circuit, slightly tweaking every single variable we could think would cause the servos to oscillate, but to no avail. Finally, with no small amount of resignation, we went to Google for a solution and thankfully, we found the solution. It turns out that there’s an inherent issue with combining NeoPixel LEDs with servo motors in the same Arduino sketch; a strand of NeoPixels demands an unbroken stream of data from the microcontroller, so it disables all interrupts, which the servo library relies upon for steady operation! I found a wonderfully detailed article on Adafruit, which not only describes the issue, but also provides a software replacement for the servo library which eliminates the problem altogether. After the library switch, the motors were quiet and cool, which after hearing them twitch and burn for weeks on end was music to our ears.

The People vs RoShamBot

Despite the technical difficulties, RoShamBot was in good working order for the Rock Paper Scissors competition. We set up the bot as a training dojo for competitors to play for warm-up. The event fell on a First Friday, which meant lots of foot traffic in the Crossroads Arts District in Kansas City. Over the course of the evening, RoShamBot faced players of all ages and backgrounds. The bot even bested last year’s RPS champion 3 times in a row!

It was particularly interesting to see how players interacted with the AI. Some, in an attempt to confuse the bot, would play entirely predictably and others would play erratically. Others seemed to be totally oblivious of the AI, and played exactly as if RoShamBot was a human opponent. I suspect that as machine learning becomes more and more ubiquitous, we’ll all grow more comfortable with the ways in which it can improve our lives even without us noticing.

Going Forward

We learned a lot from this project, both about the way people understand and interact with AI, and about some of the hardware / software difficulties and limitations in bringing a project like this into fruition. I’m looking forward to diving more deeply into machine learning offerings and exploring tools like sentiment analysis, natural language processing, and generative style transfer. For now, RoShamBot will live on in the Moonshot lab, awaiting new challengers and slowly, yet surely, getting smarter and smarter.

If you’re interested in checking out the code, the repo’s here: https://github.com/MoonshotLab/roshambot.

-Preston


Thanks for reading! If you liked this story please recommend it (by clicking the ❤ button) to help others find it. And check out more like it here.