Machine Learning with Starbursts

This article was featured on HackerNoon magazine, YoungCoder, and was featured in Kath Ceceri’s book: Bots! Robotics Engineering with Hands-On Makerspace Activities. Her book is also a fantastic resource for many other activities.

This past week I had the opportunity to partner with a local school for the Hour of Code. With the trust and support of the educators, I gave myself a new goal: spend the Hour of Code teaching and coding the basic concepts of Artificial Intelligence and Machine Learning with 5th and 6th graders — and without the help of any technology, I was left to use Starbursts instead. If I could successfully distill the concepts of A.I. and Machine Learning to a group of 5th graders without relying on any technology, it could create a very compelling case for how unnecessary technology is when it comes to teaching children to code.

. . .

Years ago in college, I’d taken a course on the Philosophy of A.I. and Machine Learning. It was a fascinating course that perfectly complimented the computer science program. I’d been using computers since I was 5, started coding at an early age, taken the science path throughout school — but it took a philosophy course for me fundamentally appreciate how computers work. Computers became less of an abstract magical black box, and I suddenly saw them for their entirely mechanical selves.

Now that computers and tablets generally work and don’t require (or even allow) people to look inside them, my fear is that computers have become even more enigmatic to children. In wanting to convey to the classroom just how simple computers truly are, and to hopefully make them less intimidating, I kicked-off my presentation with the following slide:



Just like water flowing through different paths down a mountainside, electricity flows through different switches in a computer. If you could easily control the flow of the river at many different points, and if the river flowed fast enough, you’d have yourself a rudimentary computer. Any fork in the river represents a 0/1, true/false switch. The mountain river is a not-so-instant messenger: 26 different paths could represent the alphabet, and if I’m at the top controlling the direction, I could write messages to someone at the bottom.

And with many, many, many more paths — we could have an artificially intelligent mountain river that can leverage machine learning techniques to easily beat the world’s best chess master.

. . .

The basic approach to learning is the same with machines as it is with animals, businesses, and people: Reinforce good choices, discourage bad ones (alternatively ignore them, depending on your parenting style). This simple idea lends itself beautifully to a demonstration that requires no technology at all — just lots of Starbursts.

I was inspired by a tech-less Machine Learning system called MENACE (see video at the bottom of this article). MENACE is a “computer” made up of more than 150 matchboxes and “plays” Tic-Tac-Toe against a human opponent.

At first, moves are random choices. But, over time as the better moves are reinforced, the machine will eventually only win or draw. It’s a fascinating approach that dates back to the 1960s, but unfortunately because of the number of matchboxes and the length of time, it doesn’t translate easily to a 1 hour session with a younger class.

Starbursts, on the other hand, have a few great properties that make them an ideal substitute to both technology and matchboxes:

  1. They come in many different colors (more in this in a moment).
  2. They are large and easy to manipulate but are also covered in wax paper so they don’t get grubby.
  3. Because they don’t get grubby, you can make eating them part of the algorithm.

The only remaining challenge was determining a game for the “computer” to play that had far fewer permutations.

. . .

Nim is a mathematical game in which players take turns taking 1, 2, or 3 marbles from a row of marbles. The player which takes the last marble in the row loses. With the right setup, you can always win Nim, regardless of your opponent’s moves— making for a great parlor trick:

  1. Starting with 13 marbles, have your opponent go first.
  2. Whatever number your opponent takes (1, 2 or 3), you should take an amount that would have a total of 4 marbles removed in that round. Your opponent takes 3, you take 1. Next round, they take 2, you take 2, etc.

This strategy works for 5, 9, 13 or larger numbers (4x+1).


Because Nim may lead to losses when one doesn’t follow these rules, but will guarantee a win otherwise, it’s perfect for demonstrating a reinforced Machine Learning process.

. . .

We played with a 5 marble Nim setup. There are fewer iterations, and it’s easier for the 5th graders to look ahead and determine winning/losing moves.

I had the students divide into groups of four. One player was designated the human player, whereas the other 3 players would represent the computer. While the human would play strategically, the Computer players could only play based on random selection of a Starburst color. A pink Starburst represented removing 3 marbles, orange represented removing 2, and yellow meant to remove one.

There are 5 initial marbles, and the human will go first, selecting 1, 2, or 3 marbles. So, on the computer’s turn there will either be 4 marbles, 3 marbles, or 2 to choose from. We only need to cover these 3 cases — there’s one scenario which could leave the computer faced with having to choose from only 1 marble (First round: human picks 2 marbles, and computer picks 1 marble. Second round: human picks 1 marble, leaving the computer to pick the last marble). But, since the computer has lost at this point, we don’t need to represent it. So the 3 cases (4, 3, or 2 marbles remaining) were represented by a different clear plastic cup.

Because there were three students acting as the computer, each of them received a cup numbered 4, 3, or 2. After the human’s turn, whatever number of marbles were left on the table, the computer player with that cup number would determine how many marbles the computer takes. Remember, the winning strategy: Number of marbles to take = 4 - number taken by human.

Initially, each of the cups had 1 of each color Starburst in it, so the computer’s choice would be evenly distributed and entirely random (the student would close their eyes and pick one from the cup of Starbursts).

If the computer picked the wrong color Starburst and immediately caused a loss, the human player got to eat that Starburst, discouraging the bad choice from being made on a future game. If, however, the computer made a winning choice, that color Starburst remained in the cup and another of the same color would be added to reinforce that color selection.

After playing a few rounds the computer cups all had the winning color in the designated cup: the 4 cup had only pink Starbursts (pink representing taking 3), the 3 cup only had oranges, and the 2 cup only had yellows. The computer players got to eat their respective Starbursts at that point.

In this setup we’re both discouraging and reinforcing — but you can also only reinforce or only discourage. You can also have 9, 13, or 17 cups — but these would only prolong the exercise.

A typical run through went like this:

  • 5 marbles, human takes 3, leaving 2 on the table.
  • Computer player with 2 cup randomly picks a pink Starburst from their cup containing a pink (3), orange (2), and yellow (1).
  • Computer loses (there were only 2 and computer removes 3.) The game resets, and the human gets to eat the pink Starbust from the 2 cup.
  • 5 marbles, human takes 3, leaving 2.
  • Computer player with 2 cup pulls randomly pulls out yellow. Computer removes 1 marble, leaving 1 on the table.
  • Human loses. 2 cup gets reinforced with an additional yellow.

. . .
Condensing this entire lesson to an hour is a challenge and was daunting for both the class and me. Having the educators there to assist me was definitely a huge plus. I believe most of the challenge arose in first having to explain the game of Nim before then getting into Machine Learning.

I’d suggest to any educators to split the lesson into two parts. 1. Cover games like Nim that may guarantee a player a win provided they follow the right algorithm. 2. Once Nim and the winning strategy is established, the Machine Learning component becomes more straightforward and can be taught then.

Alternatively, another entirely different approach worth exploring is a task-driven process: Have a grid-like map, and place a vehicle somewhere in the map. The goal is to navigate the map and reach a goal. The direction of each step is chosen randomly and reinforced when correct (perhaps the vehicle gets more “fuel”).

. . .

All in all, while the class may have initially been overwhelmed with all the information, I was reassured by seeing how many of them understood the concept towards the end. As I asked the class questions, I could see the students connecting the dots and having “Aha!” moments. From that point it was easy to expand to real-world implementations of Machine Learning they’d undoubtedly encountered (Apple’s Siri, for example).

Whether you’re an educator, or a Software Engineer that works with children, I’d encourage giving it a shot. Coding without technology can be a far less distracting to children, and in the end a more engaging experience.

Popular

Let's Clear Up The Ambiguity!

FAQs for a Software Engineering Hiring Manager

7 Steps to Writing an Amazing Resume

7 Steps to Building your Portfolio MVP

Work Experience vs Professional Experience