The Sunkist Principle

It was 3:30 in the morning. I wasn’t the least bit tired. Tossing and turning in bed is miserable — you just want to be asleep. I had even gotten up early and should have been exhausted. I groped for some reasonable explanation. Then, I recalled a similar situation: I once bought regular coffee thinking it was decaffeinated (I’m a mental klutz sometimes) and had a cup before bed. I’m sensitive to caffeine.

An unlikely hypothesis formed in my mind. I’d bought some Diet Sunkist earlier and had consumed about a liter and a half before I went to sleep (yes…not the healthiest choice, I’m aware). But…surely, it wasn’t caffeinated? When the search results for “diet sunkist caffeine” done on my bedside cellphone came up with “WTF? Diet Sunkist has caffeine in it?” and other modulations of surprise and/or rage, you can imagine my frustration. Until I fell asleep at 4:15, I was seething at Sunkist’s makers for stealing time from me.

Sunkist Principle: Be Explicit When You Violate Common Assumptions

From wikipedia:

Unlike many other competing orange sodas, Sunkist contains caffeine (41.0 mg).

Now, maybe most of you already knew that Sunkist had caffeine in it. But why are there people like me who were surprised? Because Sunkist is an orange soda, and oranges aren’t caffeinated.

At first glance you might think I’m an idiot; of course Diet Sunkist is made entirely out of a chemical soup that is the opposite of earthly oranges. But we humans rely on certain patterns to manage the chaos of the world. This is why I fell prey to a naturalistic assumption: When something is created artificially in the spirit of something natural, most of the natural thing’s functional attributes will be mimicked in the artificial.

In other words, an orange soda, even if artificially flavored, brings to mind the concept of an orange. So one’s basic assumption (without other information) is that orange soda will not be caffeinated. And in line with this assumption, nearly all fruit sodas are not caffeinated. An interesting related fact: Nearly all Root Beers have no caffeine, except for Barq’s.

Please — Don’t be like Experts Exchange

Anyone doing design work should keep this in mind: User anger often results from violating intuitive assumptions. For example, take fab.com, “the marketplace for everyday design.” A facebook ad came up for an interesting sale of what looked like a homemade guitar kit. So I click. Here’s what I see:

What is awful about this is that this meets my expectations for a page in which I can get more information about the guitar. This is what page seems to do. In fact, I can see the guitar that I am interested in, in the background. So my assumption is that like all other such pages I will be able to view the information and perhaps buy the kit if I am so inclined. But I can’t. I can’t bypass this overlay unless I give my email address.

Why? It doesn’t explain why I must join before I can peruse the information. There is clearly no added function for this overlay — and I can’t click around it! This non-functional block just screams sleeze, because it is clear that although they could let me just see the guitar — they won’t, unless I give them personal information. Perhaps there is a legitimate reason for this kind of deception, but it is not explained, so I must assume it is just a tacky gimmick to get me on their mailing list.

This immediately reminded me of experts exchange, the sleazy technical Q/A website that would show you part of an answer to a question you had, then ask you to pay money to join. Perhaps this association alone made me never want to return to the fab website.

When you break my expectations, at least give me a reason! At least acknowledge the deviation from my assumptions. Sunkist: Put a label on your product that says it is caffeinated. Fab: Give me a reason why I should have to give you my information — even if it is just an admission of what I expect: “You can’t view the site until you sign up, because we really want your e-mail address to bug you whenever we feel like it.”

However, in the end the Sunkist principle is just a corollary of a broader design principle: The overall design should be unified in an overarching purpose. For satisfying humans, this kind of purpose should nearly always include rarely having things work in unintuitive ways, and explicitly acknowledging and explaining exceptions when they are really necessary.

Licking trees: Why do we drink coffee instead of taking caffeine pills?

I have a mild coffee addiction. The aroma of good coffee is intoxicating, and the balanced bitterness of an Americano just hits the spot in the morning.

The funny thing is, I used to hate the taste of coffee. Yet now on most days I will gladly spend a few dollars at Einstein’s or Starbucks.

I also appreciate a good beer. A brown ale with a hint of vanilla, a pale ale like Magic Hat #9, or a Tucher hefewizen. There is a remarkable variety and depth among beers, and I love trying out new brews.

However, my first Natural Light in a friend’s basement in high school was awful. I just couldn’t understand why anyone would drink something that tasted like urine. But now, I can spend $5 on a bottle of craft beer without second thought because it tastes delicious to me.

Coffee as a drug-vehicle

It’s funny how over time coffee and beer became habits of mine. I had to acquire both tastes. Why is it that so many people do acquire these particular tastes that initially offend our tongues? The answer is simple and unsurprising: Both coffee and beer are vehicles for psychoactive drugs (caffeine and alcohol) with pleasant effects.

You might initially drink a coffee as a pick-me-up in the morning (not initially for its flavor), and perhaps have a beer to relax after a long day of work (though at first not because you like beer). But here’s an interesting question: Why not just take a caffeine pill instead of coffee when we need some energy — or a quick shot of alcohol instead of a beer when we want to take the edge off?

While this might seem like a dumb question at first, it is interesting because a caffeine pill more purely meets the need that coffee initially fills, i.e. a quick burst of energy.

It’s cheaper and more direct — a caffeine pill costs less than 10 cents and is equivalent (in terms of active ingredient) to a cup and a half of fresh coffee. Further, it doesn’t have the “learning curve” of coffee — it’s flavorless. Yet how many people do you know that pop caffeine pills instead of drinking coffee? No one does that. Even though on some level it is more logical — it delivers the effect without the unnecessary ceremony. You should take caffeine pills for energy, and drink a beverage for its flavor.

Drinking Honestly

A friend of mine relayed an interesting story about a drinking habit of someone they knew. When this person wanted to drink, he would measure out a few shots of alcohol and drink it quickly with a funnel (kind of like some people funnel beers in college). In about an hour, the alcohol would take effect suddenly.

While most people might think this is weird, I respect this odd habit: It seems more intellectually honest. Many people drink to get drunk sometimes on the weekend, and get caught up in elaborate ceremonies: Drinking games, beer after beer, mixed drinks and shots taken over time. Why not just embrace that ultimately drinking is all about alcohol: Purify the effect and admit that you are just consuming a drug to temporarily affect your psychology.

Licking Trees

The point of all this is just to bring to awareness that the habits you acquire are mostly a function of what they are a vehicle for; running and sex provide endorphins, wine and beer provide alcohol, coffee and soda provide caffeine. Many things have the potential for the same depth and variety that beers or coffees demonstrate.

But beers and coffees are drugs at heart, socially-acceptable ones at that, with years of accumulated ceremony and culture. You can probably acquire a taste for anything given enough persistence, but you are more likely to acquire those that bait your reward circuits and/or are already socially established.

Hidden depth and variety is all around us. As a parting thought, imagine that every day for two years you go out each morning and you lick a different tree. I think that over time if you kept the habit up you would actually develop a taste for them. You would begin to perhaps prefer deciduous over conifers, and find new dimensions of variation among different barks. You might begin to recognize the effects of different soils on flavor, or how being struck by lightning leaves a certain bitterness. Perhaps in some alternate world, such a hobby emerged instead of stamp collecting or bird-watching (both of which also have surprising depth).

Also, my life will be complete if someone actually undertakes and documents this experiment for me.

Innovation, Abstraction, and Raptor Hoodies

Reddit is a river of content. Sometimes different posts collide in my mind in interesting ways. Because I like to think about innovation, these collisions sometimes relate to how interesting things are created.

Comic crosswords and Raptor Hoodies

Take for instance a post on comic crosswords, where the format of newspaper crosswords is merged with that of comic strips; that is, instead of words that overlap, the crossword consists of funny overlapping mini-comics, where intersecting comics must share a common frame.

It’s an innovative take on comic strips, an unexploited comic-niche. I would read more of them if they were well done (which this one is). It reminds me of the artist that only draws comic strips on the back of business cards. The format, whether of shared frames or the inherent size limitations on a business card, imposes interesting constraints. When these constraints are met in clever ways they can impress and entertain us.

A separate example is of a raptor hoodie that has awesome integrated functionality: If a kid crosses his arms in front of himself, they act as the movable jaws of the raptor that can be chomped. When I first saw this, I was impressed by the cleverness of the design; the form of the shirt acts on two levels, the sleeves are reinterpreted as jaws that interact with the shirtfront. If I were a 10-year old boy I would have loved something like this.

Innovation and Abstraction

Now, you might wonder why I believe these examples relate to innovation in general — why should someone trying to create care about raptor shirts or strange comics? We can learn from these examples — in an abstract way — to help us become more creative.

Take the crossword comic example. The nature of the innovation is a clever recombination of comic strips and crossword puzzles. Can we analyze this particular example to create other clever combinations? What is the general spirit of a crossword puzzle that allows it to be melded with other concepts? This is all about searching for a good abstraction.

The main abstract concept underpinning a crossword is one-dimensional sequences of atoms [letters] that form larger holistic units [words] that are embedded and overlap in a two-dimensional space. In a traditional crossword, the sequences are of letters and the larger unit is a word. In the crossword comic, the sequences are of comic frames atoms, and the holistic unit is a cohesive joke formed from the frames. In this case, the innovation comes from varying the units.

Variations on a Theme

Here’s the interesting part: We can think of other things with the same abstract properties and recombine them as well to make something cool and new. The trick is to think of examples where we build from some smaller vocabulary a larger coherent construction. Each individual part of the vocabulary should be ambiguous enough to serve a different role in multiple constructions, e.g. a letter serves different purposes when at the intersection of two words.

A poem is a sequence of words, a book is a sequence of pages, a song is a sequence of notes or phrases, a dialogue is a sequence of alternating sentences, and a computer program can be seen as a sequence of statements. Any of these also could be substituted for crossword units in a similar way as the comic strip idea, a creative way to engage with your favorite medium.

A trivial straightforward example would be cross-poems, where lines (or even stanzas) of a poem form the lower-level units that compose an entire poem. Because lines of poetry are sensitive to context (like individual letters or frames of a comic), they can be utilized creatively in the intersection point for different ends in both of the resulting poems. Two poets could take turns laying down short poems as a fun collaborative exercise.

But the point is that working on the abstract level lets you consider other possibilities for crosswords easily;  you clearly state the rules of the game you are dealing with so you can vary them. The dimensionality can be varied (you could have 1d sequences embedded in 3d space for a 3d crossword, or 2d patterns embedded in a 3d space), additional constraints can be added (the sequences must make sense when interpreted both forwards and backwards), the 1d sequences can curve in space rather than be strictly linear (imagine crosswords not based on a strict grid, but with spaces of varying sizes and connectivities, e.g. hexagon-based crosswords). There are many creative possibilities, many never-before-explored, ripe for discovery.

Form and Multiple Function

Taking a closer look at the other example, the raptor hoodie is a really cool example of exaptation, where things created for one purpose (sleeves of a shirt) are adapted to an unforseen context (acting as the moving jaw of a raptor). The abstract idea here is that a functional item [t-shirt] can have its aesthetic properties [design] hijacked to serve a secondary function [raptor jaws]. Now this is an interesting abstraction as well to play with as well, but I’ll leave it as an exercise to the reader to think of fun generalizations or to examine if there are other ways to learn from the raptor hoodie.

The point of this post is just to illustrate that by deeply examining something that we find interesting, we can learn the meaningful degrees of variation that may lead to many more interesting things in the same spirit. In other words, we reverse-engineer to isolate the meaningful conceptual knobs we can twist to further create.

While Not Climax: Insert, Remove

The way current illicit drugs work are crude. You take them, they short-circuit your pleasure circuits, they create addiction and ruin lives. But it seems likely that science will at some point allow us to create more effective mind-altering drugs. For example, what if there was a drug or a surgery that allowed you to change the structure of your internal reward system. First let’s just consider if you could remap what foods you found delicious.

Rethinking Delicious

We find fatty and sugary foods so hard to resist because they are energy-dense. Our bodies evolved in a time when energy was scarce. So our pleasure circuits reflect this. If you were drawn to high-fat food in cave man days, you were more likely to survive the winter. But those evolutionary circumstances no longer hold, as our waistlines show. People literally eat themselves to death because of our genes’ obsolescence (evolution is slow relative to how fast our culture has progressed).

What if you could make carrots taste like twinkees? What if you could remap your tastes so that what was healthy and what was scrumptious were found right in the middle of that Venn diagram. How disruptive would that be? Dessert shops would sell spinach-celery brownies glazed with salmon frosting, and all bars would offer up wheatgrass shots. We’d be healthier and happier, and the obesity crisis would be averted painlessly.

Rethinking Orgasm

Now consider sex. Imagine how much time is wasted, how much of the world’s intrigues are driven by a process that at heart is: Insert part A into part B — repeat — repeat. Before you get all huffy about how nice sex feels, remember that technology is granting us the ability to remap our desires. What if you got the sensation of super-orgasm from solving a particularly difficult equation — or from knocking the next item off your to-do list?

Don’t forget that sex feels so good only because our genes can control us through our desire for it. People cheat on those they love because it requires so much discipline to conquer the id. Our genes want to control us, to take us from our high-minded aspirations and make sure that we reproduce them. We’re their vehicles — they exert their dominance through our sex drive.

The Point

The point is this. At some point in the future, technology may force us to resolve the philosophical question that humans have always struggled with: What does it mean to be human? If being human is not about sex, or about delicious food (which we can see as transparent reflections of our genes’ wishes for us) what is it about? When we have the ability to align our motivations with our greatest aspirations (which I think is of vital importance to the human race, lest we destruct ourselves), then is what remains humanity?

Nature has imposed some weird constraints upon us. We eat ourselves to death, we hurt those we love, we fail to do the things we want, all because of our outdated genes. When the technology comes to help us overcome the need for discipline, it will be an interesting day.

Disfigurement Paradox; Hacking Attraction

One characteristic of hackers is that they are generally sensitive to logical inconsistencies; for example, the WAT talk making its rounds points out comical seeming inconsistencies in javascript and ruby: Why should empty array plus empty array = empty string? A more stereotypical example is noticing plot holes in movies that undermine plausibility, e.g. the entire Indiana Jones and the Crystal Skull movie.

Recently I’ve been thinking about the logic of relationships and dating. What follows is a paradox that reveals an inconsistency in our values.

Disfigurement Paradox

Imagine you are dating someone; she is attractive and funny and a perfect match for you. Over time, you fall deeply in love with her and eventually you get married. It is clear to you that she is the love of your life. Then, tragically, she is in a horrible auto accident by no fault of her own. She survives, but is horribly disfigured.

What do you do? You would hopefully stick by this person, this love of your life, even though you may no longer find her physically attractive. By this point in the relationship, what you most value about her are deeper qualities such as personality, sense of humor, and values. We would deem it shallow and perhaps even cowardly to leave only because of such a superficial trait.

Yet in the beginning of relationships such superficiality is the norm. That is, given the option of more attractive mates people tend not to date ugly people. At the limit case, I imagine it is a sad truth that those who are extremely disfigured and single find dating a significant challenge.

So what is inconsistent is that a key factor in beginning a relationship becomes increasingly unimportant as that relationship progresses; had your future spouse gotten disfigured before you met they would have likely been disqualified from your dating process. This is weird. It would be like if a key factor for hiring you for a job as a computer programmer was an orthogonal factor like how green your eyes were.

Biology imposes some weird things on us that we are tempted to accept as normal because it is ubiquitous as part of the human condition. We are attracted to weird things like hip ratios and facial symmetry because at one point these indicators were relevant to our survival and were encoded into our genes. Yet they don’t make sense any more. For example, obesity is a problem caused by our bodies being overly conservative, greedily storing energy just in case of a bad winter, because this made sense when we had to hunt and pizzas could not be delivered on demand.

Hacking Attraction

Imagine you were free of biological constraint, what would a replacement attraction system for dating look like? That is, if you could program what you were attracted to, what would it be? I would entirely discount physical appearance and focus on the merits of our intellectual matching, the quality of our conversation, our shared values.

The important thing would be that in this replacement system, I would not be turned off by ugliness. I might be aware of it, but I wouldn’t care. My ideals of attractiveness would simply be at odds with what biology intended.

As of right now, my heuristics for dating eliminate unfairly the ugly despite how unimportant that trait should be. It is a glaring biological hindrance that I am not strong enough to dispel. How would the world be better if we could reprogram attraction? Imagine if medical technology made such a thing possible? Would you allow yourself to reprogram your own attraction, or perhaps even your own pleasure circuits?

Bill Buxton: An Unsung Hero of the iPad

C is quirky, flawed, and an enormous success.
-Dennis Ritchie

For whatever reason, the lead singer of a band nearly always draws all the attention. But perhaps the drummer is the one writing all the songs, and surely she and the bassist are the ones laying the foundation of the song upon which the singer and guitarists will build.

The unfortunate closely-timed deaths of technological superstars Dennis Ritchie and Steve Jobs echo the lead singer phenomenon. Both men were geniuses, yet Dennis Ritchie’s pervasive influence through co-developing both C and Unix surely is of greater historical significance — in fact, Apple’s OS is a descendant of Unix, and the Objective-C language prevalent in iOS descends from C’s legacy. As a result, there was a bit of unrest in the computer science world when memorial to Jobs vastly out-shined that given to Ritchie.

There is nothing particularly surprising in this, it is simply that Jobs was the lead singer and Ritchie played the drums. But the stories of the drummers are as interesting of those of the lead singers even if they aren’t as often told.

From Drums to iPads

Another drummer that I learned about from NPR a week ago, is Bill Buxton, a musician turned computer scientist that was a pioneer of the multi-touch technology underlying the iPad. In 1975, he was trying to invent a drum synthesizer, but ended up facilitating computer interfaces:

I wasn’t trying to make a computer interface, I was just trying to make a drum. It turns out that if you want to make a hand drum, that you want to be able to tap it and at the same time have your palm drag against the ‘virtual skin’ of the drum so to speak to change the tone, that was all we were trying to do.

Buxton admits that at the time he had no expectation of changing the world; the only reason he was able to accomplish a multi-touch interface before anyone else was that he had no idea how difficult it was:

In many cases, people who come from outside the normal discipline [have an advantage]. I was trained in music and not in technology, and no one told us it was hard. It [multi-touch] seemed like a pretty obvious thing to do at the time.

But what wasn’t clear is [because of our outsider status] that we had different insights and just the right people around to make it happen.

And I love that part of things, when people just completely open up their imagination trying to do creative things, and have no business doing that kind of technological innovation, and they actually have insights that turn out many years after the fact to have had huge impact.

I like it in that sense that its just your imagination that’s driving it, and you’re not trying to be so deliberate, about trying to something really important. That usually just makes you uptight and strained.

It’s far better to find something you love doing and chase it down, and the rest will just fall out.

While it may be sexier to be a lead singer, the world will always depend on drummers to lay the beat.

Van Gogh Didn’t Like The Starry Night

“It is absolutely certain that I shall never do important things.”
-Vincent Van Gogh, two months before his suicide.

Though the tormented life of Vincent van Gogh can be interpreted in many ways, I’d like to paint it today as a parable: Accurately evaluating what you’ve created is a difficulty all its own.

The starry night is widely regarded as Van Gogh’s magnum opus, yet Van Gogh himself was not impressed with it. A short while after painting it he sent it and a few other paintings to his brother Theo along with a letter.

The paintings were separated into two categories, four studies (something done for practice) and four works Van Gogh considered more complete. Starry night, was first called by van Gogh, “Study of the night.”  He said of these paintings:

In all this batch I think nothing at all good save the field of wheat, the mountain, the orchard, the olives with the blue hills and the portrait and the entrance to the Quarry, and the rest says nothing to me, because it lacks individual intention and feeling in the lines

In other words, he did not consider his magnum opus at all good. One problem in judging one’s own work is the intimacy you have with it, that intimacy that no one else will have on first viewing.

We become blinded by our singular perspective resulting from our immersion in our own work. In other words, other people do not share the long history of your creative efforts — they are examining your creation in a more objective context.

The moral is not that we must rudder our ship based on popular opinion but that we must admit that our evaluations of our own work can become colored and disengaged from the perception of others.

What is also interesting is that just from reading a single letter from van Gogh to his brother you get a sense of his tireless work ethic and his passion for his craft:

I have now seven copies out of the ten of Millet’s “Travaux des Champs.” I can assure you that making copies interests me enormously, and it means that I shall not lose sight of the figure, even though I have no models at the moment.

[...]

I should also like to copy the sower and the diggers.

[...]

I am going to copy the Good Samaritan by Delacroix too.

[...]

Many people do not copy, many others do – I started on it accidentally, and I find that it teaches me, and above all it sometimes consoles me.

[...]

When you say in your letter that I have done nothing but work, no – that is not right. I am myself very, very dissatisfied with my work, and the only thing that comforts me is that people with experience say you must paint for ten years for nothing. But what I have done is only those ten years of miserable and unwelcomed studies. Now a better period may come, but I shall have to get the figure stronger and I must refresh my memory by a very close study of Delacroix and Millet. Then I shall try to get my drawing clearer. Yes, misfortune is good for something, you gain time for study. I am adding to the roll of canvases a study of flowers – nothing much, but after all I do not want to tear it up.

The other moral we already know; producing great things is lubricated by mastery achieved through consistent deliberate practice.

Change the World: That Fleeting Holiday Idealism

And making an exception only for his parents, he gradually became
contemptuous of all grownups who had made this mess and were unable to clear
it up. He was sure that when he was big he would straighten it all out.

–Boris Pasternak, from “Dr. Zhivago”

Spending the holidays with family consistently invokes in me a pensive mood. The usual unquestioning inertia of work done for its own sake is replaced by a deeper context. Seeing the effects of aging, the universal struggle for happiness, and the deep bonds of family, I’m forced to reevaluate the importance of what I’ve been striving towards.

For a brief period of lucidity, I’ll understand that people and meaningful relationships between people are what is most important, and that money and success are passing vanities. Inevitably, as I return to my routine this wisdom fades away yet again.

So, while that flickering idealism remains at the forefront, I remember that years back I dreamed of changing the world for the better. I think we all have had this naive dream at some time. But this youthful dream becomes trampled; there are no easy solutions to the most difficult problems, and humanity consistently falls far short of its potential.

Still, we live in a time of increasingly powerful technologies, and many of us are riding the crest of its wave. So we have some say in how it will crash and shape the future.

Let’s leverage technology to make this a better place somehow.

Using Celery+Python to make a Distributed Genetic Algorithm

Note: If you like to follow along with complete source, feel free to grab this project on github.

Celery is a distributed task queue that integrates smoothly with python. I stumbled upon it and immediately saw it as a cool technology looking for an application. You can define tasks in python by just adding a decorator (@task) that can be performed in the background asynchronously by hungry worker compute nodes. For example, here is a sample (simple) task gratuitously lifted from the basic celery tutorial:

@task
def add(x, y):
    return x + y

You can then call the task with its “delay” method and it will be serialized, whisked away to Celery’s backend queue, unserialized by a worker, computed, and then the result will be ferried back to the python caller:

>>> from tasks import add
>>> add.delay(4, 4)
>>> result.get()
8

What is especially nice about this is how seamless it is. If your prior exposure to parallelization is something like MPI in C++, the simplicity and elegance of celery is refreshing. It just works, and for expensive parallel tasks it is a nice way of circumventing the GIL in python.

My PhD research often involves genetic algorithms. The idea is that you have an array of artificial genomes (which are themselves often lists of numbers) and a way of judging these genomes (an error function of some kind). Iteratively you apply the error function to weed out the worst of these artificial genomes, and replace those worst genomes with slight variations of the better ones. The hope is that over time you can find a genome that minimizes the error function (usually called the fitness function in GA parlance).

Usually the performance bottleneck in GAs is evaluating each genome through the fitness function. For example, sometimes this evaluation can consist of expensive physics simulations (e.g. simulations of virtual creatures).

What is nice from a parallelization standpoint, is that each simulation is done on a per-genome basis and is (usually) independent of every other genome. So if you had a population of 1000 genomes that you need to evaluate, you can create a celery task for each of them that can be offloaded to either spare cores on the same computer or onto worker instances on other computers.

Of course, celery will be handy not only for GAs but wherever you have many expensive tasks that are independent of each other. For example, rendering individual frames of a ray-tracing animation or multiplying many large matrices. If you can decompose your computational bottleneck into significant independent chunks, Celery may be a good fit for easily reaping the benefits of parallelization.

For the genetic algorithm application, I just had to wrap the fitness function as a celery task:

@task
def evaltask(domain,genome):
 fit=domain.evaluate(genome)
 return fit

And then I augmented the serial evaluation of the genomes (shown here):

#evaluate sequential
 if(not eval_concurrent):
  for k in population:
   k.fitness = dom.evaluate(k)

With the parallel evaluation that uses the celery server:

#else evaluate concurrently
else:
  tasks=[]
  for ind in population:
   tasks.append(evaltask.subtask((dom,ind)))

  job = TaskSet(tasks=tasks)

  result=job.apply_async()
  fits=result.join()

What this code does is make a list of celery subtasks, one for each genome to be evaluated. It composes this into a larger “taskset,” which is run asychronously, and the results are gathered (in order) by the join call once they all complete. If each task is non-trivial (i.e. it takes significantly longer to run than the overhead associated with serializing it and throwing it to celery), then the speed-up gained from paralellization can be very nice.

Feel free to check out the complete source on github (including the celeryconfig.py file that).

Using concurrency requires a working celery install (see http://www.celeryproject.org/tutorials/first-steps-with-celery/ for how to run celery, it is fairly simple to install):

You can install Celery either via the Python Package Index (PyPI) or from source.

To install using pip:

1
$ pip installCelery

To install using easy_install,:

1
$ easy_install Celery

And a backend for celery is necessary for it to run. The default is rabbitMQ, available on ubuntu as package: rabbitmq-server

Frankenstein Meets Darwin

Teaser video:



“Neuroevolution,” which literally means “Evolving brains,” sounds like a tired sci-fi plot: Scientists create a super-smart computer that gets out of control and goes on a wild rampage, stopped only seconds from starting a nuclear holocaust (or not, in the case of the Terminator movies).

Yet, far from fiction, neuroevolution is what I do on a daily basis: Evolving artificial brains in a computer is the foundation of my Ph.D. research. It’s not something I came up with, and is not a brand new idea — neuroevolution is an approach to artificial intelligence that has been around for 15 years. It’s inspired by how biological brains evolved through natural evolution.

“Oh, um. You evolve…brains? In a computer?”

Whenever you meet someone new, the question, “What do you do?” inevitably percolates into conversation. To avoid the glazed-over eyes that tend to result from my explanations,  my vague canned response is that I am a graduate student in computer science. Although what I do is not insanely complicated, it is far-enough removed from typical day-to-day experience that explaining it in a sound-bite is damn near impossible.

The more abstract a concept is, the craftier one must be to describe it both intelligably and succinctly. For example, it’s fairly easy to explain what Sushi is to someone who doesn’t know because Sushi is “concrete” — it’s touchable, edible and related to “fish,” a universal concept.

You could say, “carefully-prepared raw fish,” and not be too far off the mark. But it is harder to explain to the uninitiated what electric current is, because it is not something “physical” nor directly visible. But although electric current is abstract, we can quickly begin to understand it by relating it to something familiar and “real” like a river current. Electricity “flows” in a similar way to water.

So, while someone’s eyes might glaze over when you talk about electrons, relating electricity to a river is more intuitive. So it turns out that only I am to blame for the glazed-over eyes of my acquaintances because I have not been clever enough to explain clearly and simply what I do. And so I hope to atone for past blank stares of nonrecognition with this post: Neuroevolution is simple and interesting, and I want to tell you why in an easy way.

Dancing Bees

Have you ever been to a zoo? Or an aquarium? The seemingly limitless variety of living creatures, and the amazing things they can do, is fascinating. An archer fish shoots a powerful, carefully-aimed stream of water from its mouth to knock insects into the water, then quickly swallows the stunned critters. A bumblebee dances in two separate ways, depending on how far away it is from food, to show its comrades where to forage. Crows fashion tools and even learn to bait-fish with bread crumbs. The diversity and complexity of nature is awe-inspring.

The source of this diversity and complexity is natural evolution. Although it is hard at first to accept that an unguided process could produce such magnificent artifacts (like your face,  you handsome reader), evolution is nearly universally accepted by scientists because of an overwhelming amount of evidence, such as fossils and vestigial organs.

“Nothing in Biology makes sense except in the light of evolution,” said a famous evolutionary Biologist, Theodosious Dobzhansky. If you believe in evolution, then you also must believe that evolution shaped our brains; our impressive intelligence is somehow the result of billions of years of trial and error experiments.

The point is that, without intelligence, evolution created a ton of really cool things, including our own intelligence. This mind-blowing possibility hints at the potential of neuroevolution: Perhaps we can harness the general process of evolution within a computer to evolve intelligent computer-brains! Trippy, right? But, before we are ready for a more complete description, I want to briefly digress and talk about artificial intelligence in general, the field of research that seeks to make smarter machines.

And what country is Toronto in, Watson?

Perhaps most of us are most familiar with Artificial Intelligence from movies such as 2001: A Space Oddessey, AI, Stealth, the Terminator, etc. In these movies scientists create an intelligent computer, and then it goes crazy and kills everybody. One part of this is based in reality: There are researchers that try to create more intelligent computers. In fact, I am one of those researchers.

To put your minds at rest, current research in AI is far from creating true human level intelligence. I’d guess that it will at least be a few decades yet before we reach that ambitious goal. A question people often ask is “Why?”: Why would we want intelligent computer programs? There are many different answers. One might side-step the “why” and appeal to curiosity; I for one, am curious about what intelligence is, and how we can better understand it. I think we might truly understand intelligence only when we can recreate it.

But there are also practical reasons we might want intelligent programs, too. For example, it wouldn’t be practical for Amazon to hire a massive team to create a personalized list of products one might like to buy for everyone who ever purchased things on Amazon. However, with AI techniques, a computer can find correlations like: people who bought product X also tended to buy product Y, and from these correlations automatically make personalized lists. More and more tasks that once had to be done painfully by hand, like arithmetic or checking spelling, can now be automated by computer programs with a small spark of what might be called intelligence.

There have been many interesting successes of AI. I’ll highlight two of the most famous here.

The first is when the best chess player in the world, Gary Kasparov, lost to Deep Blue, a supercomputer made by IBM. We once thought that chess was something that required true intelligence to master, because the game is deep, has many complicated strategies, and is difficult for humans to master. However, it is clear that chess does not require true intelligence to master, because deep blue, although it is very good at chess, can not do anything else at all. It won only by considering many many moves, using brute force calculation impossible for humans.

A second very famous success of AI is Watson, the Jeopardy-playing AI. It beat Ken Jennings, arguably the best jeopardy player of all time. This at first seems incredibly impressive, and it is! Who would think that a computer could be competitive with humans in a task like general question and answer? It is especially impressive because the questions in jeopardy tend to be playful and tricky.

However, Watson, like Deep Blue, is far from any “true AI” in that it is not “creative.” It won by clever human-designed algorithms coupled with four terabytes of human-curated data (Wikipedia and more). Also informative was its embarassing failure to recognize that “Toronto” is not a US city. Watson, like Deep Blue, does not have ‘general intelligence’ — it can only answer questions, and does not ‘reason’ in the traditional sense but again relies on cleverly designed tricks, raw brute force, and an enormous database of curated facts to perform well. Wouldn’t you be pretty good at jeopardy too if you could browse all of Wikipedia in a fraction of a second?

These Watson and Deep Blue approaches, although impressive and practical, seem to have almost no relation to general intelligence, which is what I am curious about. Why is that a toddler can still do things that a supercomputer can’t?

We only have one working example of general intelligence: Humans. And humans are the result of natural evolution; so, in order to create artificial intelligence, isn’t the most obvious approach to follow that one working example and examine evolving brains through artificial evolution? Of course, it’s not the only approach, but it has precedent on its side.

Evolution: Natural and Artificial

Before explaining the strange concept of artificial evolution within a computer, it’d be good to first understand how natural evolution works. How does it create organisms that are well-adapted to their environments like archer fishes or bumblebees? While we might all remember some vague ideas from science class involving “survival of the fittest” and “natural selection,”here are the three simple, concrete principles that underlie evolution:

1) Selection
In nature, animals typically have more children than can survive in the world. There’s not enough food for all the rabbits, and in addition wolves will eat those that are too slow. Natural selection is just that a lot of animals die before they reproduce. It is like a filter that generally only allows better-adapted animals to pass through.

Animals better at surviving and reproducing will tend to have more offspring, which spreads their genes and physical traits onwards. Animals worse at surviving and reproducing have less chance of spreading their genes. So, a gene that makes a rabbit faster might spread, while a gene that makes a rabbit slower would probably die out, because wolves might catch most slow rabbits before they had a chance to make rabbit babies.

While in nature animals are ‘selected’ naturally if they survive long enough to reproduce, selection can also be artificial. That is, humans can do the selecting instead of nature: Plant breeders might choose to mate the prettiest flowers together, or dog breeders might choose dogs that are smaller or friendlier or cuter, hoping that the children of that dog will also share those traits. We breed race horses for speed, breed cows to give more milk, and so on.

Selection, whether artificial or natural, tends to give evolution some direction, whether towards cuter dogs or faster rabbits better able to evade wolves.

2) Variation
The first principle was selection, that some animals would spread their genes, and others wouldn’t, and that evolution is given some sort of direction by what we select for. Nature selects for the ability to survive and reproduce, but human breeders may select dogs for cuteness instead.

The second principle is variation. That is, every dog is not exactly the same; dogs vary. Some racehorses might run faster or slower, some flowers might be prettier or uglier, and some archer fish might be more or less accurate at drilling insects with its built-in squirt gun. Variation enables selection to push evolution in some particular direction.

If all dogs were exactly the same (no variation), selection would have no distinctions to make among those dogs; you would find no cuter dogs because they would all be of the same cuteness.

Variation in itself doesn’t provide a direction, because it may create uglier dogs more often than it creates cuter dogs. But, selection among a variation on animals can push evolution in a direction, because it might filter out those uncute dogs; a human breeder would breed together the cute ones and not the uncute ones. What variation does is give that breeder choices.

3) Heritability
The third principle is heritability. Evolution requires that variation is generally passed on to its children.
People might say you resemble your mother, or that you have your father’s eyes: You resemble your parents. The reason for this is that your DNA, all of your genes, is a combination of your parents’ genes.

Heritability is the property that a child resembles their parent. If this wasn’t the case then there would be no continuity — if a bumblebee gave birth to a chipmunk, then all would be chaos. In that crazy world, you would never be able to find a really cool bumblebee if that is what you were were looking for, because a bumblebee’s children could be anything.

Another way of looking at this is by thinking about the game “telephone.” There is a line of kids, and the first kid thinks of a clever phrase. He then whispers it to the second kid, who whispers it to the third, and so on. By the end, the message is horribly garbled. In this situation there is little heritability, because the message has changed so much.

On the other hand, if one simply forwards an email without making any changes, the message will stay the same despite how many times it is forwarded. In this situation, there is high heritability. Of course, if heritability is perfect then there will be no variation; but if heritability is too flawed, then selection will have trouble producing anything cool because things will get too garbled too quickly.

It seems like the best idea is to have almost perfect copying, but allow for small mistakes to sometimes seep in, to provide for modest variation. This is what natural evolution does; each of us has tiny mutations in our DNA that make us unique.

Evolution, in nature, in a nutshell:
1) Selection – is natural selection, that creatures better at surviving will pass on their genes, meaning that in general the ability to survive tends to improve over time.
2) Variation – happens through animals having unique DNA, which occurs through infrequent errors when copying DNA and also through mating, which makes a mix of two existing DNAs from different parents.
3) Heritability – happens because copying errors are infrequent in DNA, so the game of telephone between parent’s DNA and child’s DNA is accurate. There may be some small changes, but if these changes are too severe — for example, a frog born with an additional leg, that frog is unlikely to survive to pass on its genes.

Evolution of Hip Hop

What is cool about evolution is that it is *general*, it isn’t just confined togenes and DNA. For example, ideas also evolve. Leeches are no longer best practice for treating disease and fanny-packs and pogs are no longer cool. Right now, paranormal romance (read twilight, etc.) is particularly popular, but twenty years from now, some other genre will be in vogue.

Music evolves too! A branch of blues music evolved into funk, and a branch of funk led to hip-hop. We tend to support those artists with addictive, cool-sounding new songs, and variations of those songs fill the air-waves. Musical ideas are ‘selected’ by listeners who buy albums or call into radio stations to request songs. These musical ideas vary over time: Artists inspired by a cool song will make new songs with similar sounds or traits, and other artists might be inspired by this new song. The evolution of cultural ideas like fads or music is called memetics, which is an intriguing topic in its own right.

However, right now, I want to discuss artificial evolution within a computer, which is but a hop, skip, and a jump away from finally understanding neuroevolution!

Evolving bits

When you were in middle school, your teacher gave you annoying algebra problems to solve. She’d say, “Hey, Joel, find the value of x for this equation: 2x-10=3.” Listen, lady, I’d sure love to but there’s a Buffy marathon on TV. Wouldn’t it be cool if we could let evolution solve this math problem for us while we eat ham sandwiches instead?

The answer, is yes, of course! We all love ham sandwiches and despise intellectual labor. The question, though, is how we can set up those three factors that cause evolution (selection, variation, and heredity) to do our homework for us?
Let’s start with heredity.

1) Computerized Heredity
In nature, DNA is what is changed by evolution. For evolution in a computer, we can make our own DNA up. Whatever we want. Muahahaha. It can be as simple as a list of numbers. For simplicity’s sake, it can even be just a single number.
Okay. So now we have our artificial DNA that is of just one number. Let’s call this one number x, because that is what we are trying to find to solve this lame algebra problem. So, our ideal piece of artificial DNA, which is just a number, will contain the correct value of x, the one that makes the equation true.

We can make copies of this artificial DNA, this one number, by simply writing down this number in a different place. These copies of that number are little chidldren DNAs of the original artififical DNA. Because these copies of the DNA resemble each other (well, they are the same), we’ve achieved heredity! Boom.

2) Computerized Variation
Okay, well, how can we make variations of this artificial DNA, this one number? How about when we copy a parent DNA number to a child DNA number, we make a slight random change to it. We just add a little bit or subtract a little bit. This way, the child number will resemble the parent number, but it won’t be exactly the same. So, the parent number might be 2.31, and the child might be 2.35 or 2.25. In this way, the children will vary. Boom.

3) Computerized Selection
The last element of this computerized evolution game is selection. We need to come up with a way of selecting those pieces of artificial DNA that are closer to solving our algebra problem than others.

One way of doing this is just plugging the artificial DNA x into the equation, and seeing how close it is to making the equation true. For example, if we set x to be 1 in our equation of 2x-10=3, we get 2-10=3 or -8=3. The left side is 11 different from the right side. We can say that we want to select those x’s that make the difference between the left side and right side of the equation smaller. If we set x to be 2, we get 4-10=3 or -6=3. In this case, the left side is 9 different from the right side, which is a smaller error than when x was 1. So, given a choice between artificial DNA with x=1 and x=2, we’d select x=2.
Our automatic selection mechanism plugs in the artificial DNA x into the equation and calculates how big the error is. We choose the DNAs that make this error the smallest.

It’s magic

We can now make a recipe for solving our little math problem. This is called an evolutionary algorithm, because it employs evolution in a methodical way that can be computerized.
Step 1: Put ten Artifical DNAs in a list (aka pick ten random numbers)
Step 2: Assign a score to each artificial DNA based on its error when we plug it into the equation 2x-10=3 (the higher the error, the worse the artificial DNA is). If the error is really small quit, otherwise go to step 3.
Step 3: Make a new list by taking the five best artificial DNAs from the last list and writing them down twice; when writing down a number, add or subtract a small number from it first to generate some variation.
Step 4: Go to step 2

Magic in practice

So I went ahead and translated our little recipe into Python, an actual computer language. You can look at the code here if you are curious. Each time through the loop described in steps 2 and 3, the program prints out the error of the best artificial dna and its x value. Next we’ll look at a print-out from a run, and then a graph. You can see that it quickly finds the solution to our equation 2x-10=3, which turns out is 6.5.

Loop # 1  Lowest error: 2.48  Best DNA: 5.26
Loop # 2  Lowest error: 2.35  Best DNA: 5.32
Loop # 3  Lowest error: 2.24  Best DNA: 5.38
Loop # 4  Lowest error: 2.15  Best DNA: 5.42
Loop # 5  Lowest error: 2.07  Best DNA: 5.46
Loop # 6  Lowest error: 2.03  Best DNA: 5.49
Loop # 7  Lowest error: 1.88  Best DNA: 5.56
Loop # 8  Lowest error: 1.79  Best DNA: 5.60
Loop # 9  Lowest error: 1.72  Best DNA: 5.64
Loop # 10  Lowest error: 1.68  Best DNA: 5.66
Loop # 11  Lowest error: 1.57  Best DNA: 5.72
Loop # 12  Lowest error: 1.43  Best DNA: 5.79
Loop # 13  Lowest error: 1.47  Best DNA: 5.77
Loop # 14  Lowest error: 1.30  Best DNA: 5.85
Loop # 15  Lowest error: 1.14  Best DNA: 5.93
Loop # 16  Lowest error: 1.04  Best DNA: 5.98
Loop # 17  Lowest error: 0.95  Best DNA: 6.03
Loop # 18  Lowest error: 0.76  Best DNA: 6.12
Loop # 19  Lowest error: 0.77  Best DNA: 6.11
Loop # 20  Lowest error: 0.72  Best DNA: 6.14
Loop # 21  Lowest error: 0.61  Best DNA: 6.20
Loop # 22  Lowest error: 0.49  Best DNA: 6.25
Loop # 23  Lowest error: 0.30  Best DNA: 6.35
Loop # 24  Lowest error: 0.44  Best DNA: 6.28
Loop # 25  Lowest error: 0.28  Best DNA: 6.36
Loop # 26  Lowest error: 0.25  Best DNA: 6.37
Loop # 27  Lowest error: 0.13  Best DNA: 6.43
Loop # 28  Lowest error: 0.01  Best DNA: 6.50

And here is a little graph showing how error decreases to around zero over time:

So what we’ve shown here is that it is possible to harness evolution to solve problems within a computer. We could have it solve other equations by changing the error function. One cool aspect of evolving solutions to problems is that we just specify what we are looking for, and evolution will try and find it.

While our artificial DNA here was just a single number, the artificial can be more complicated. For example, we could evolve x and y for a more complicated equation. But more interesting than this trivial addition is neuroevolution, where the artificial DNA does not represent a sequence of numbers but an artificial brain that can control robots. Through neuroevolution we can get robots to do cool things while we eat ham sandwiches.

Darwin meets Frankenstein

What’s in a brain, anyways? Basically our brain is a collection of neurons that are wired together in various ways, like an incredibly complicated circuit. Neurons are cells that do basic computation. They can get signals from the real world (like from our eyes or ears) or control our muscles. Neurons are what enable us to sense the world and do things. A bunch of neurons centralized in our brain is what give us instincts and thoughts.

Now, if we want to make an artificial brain for a robot, we can think of a brain as a collection of neurons and wires that connect them together. In the context of a robot, these wires may connect neurons to the robot’s motors or to its sensors (like a video camera), so that the robot can move in response to how it sees the world. The wires may also just connect neurons to each other.

Intermediate neurons that sit between the sensors and the motors can compute functions of sensory inputs to enable more sophisticated robot behaviors. For example, running away when you smell a predator is a simple behavior that can be accomplished by directly connecting the nose sensor to the motors. However, predicting the trajectory of a moving baseball might require doing some basic calculations that require lots of neurons connected in complicated ways.

But beyond understanding how a robot could have an artificial brain and what such a brain might be built from, what we would really like to do is to have evolution design these artificial brains for us, which is the essence of neuroevolution. A simple way to do this is to construct an artificial DNA that acts as a wiring diagram for an artificial brain. You can imagine that such diagrams could be twiddled slightly, by changing the strength of a connection between two neurons, or adding in new neurons, or by adding or removing connections entirely. This is how evolution can mutate an artificial robot brain.

And so you can implant these robot brains in a robot, turn the robot on, and see what its brain induces it to do. Different brains can cause different behaviors because they will enact differing policies. One brain’s policy might be to purposefully ram into walls, another might spin aimlessly in circles. The question remaining is how can we use evolution now to get a brain that does something we want?

Like we did in the case of evolving the solution to the equation, we need a measure of goodness. This measure can aide us to select good robot brains and to throw away the bad ones. Perhaps you want to evolve a robot that can navigate a corridor. One measure of goodness for evolving such a policy is to look at where the robot ends up, and see how far it is from where we would like it to be.  That is, imagine we always start a robot at the beginning of a corridor, and let it roam for awhile. After a fair amount of time has passed, we look at its position. If it is near the end of the corridor, we deem it as good.

With the ability to represent a brain as a wiring diagram, to slightly mutate these diagrams, and a way of evaluating how good these diagrams are, we are ready to craft our first neuroevolution experiment!

Magic part two: Evolving Robot Brains

We can now make a recipe for solving our corridor navigation problem with another evolutionary algorithm.
Step 1: Put one hundred Aritifical Brain DNAs in a list (aka make one hundred random wiring diagrams)
Step 2: Assign a score to each artificial DNA based on how far a robot gets through the corridor when we plug in the brain the artificial brain describes. The farther the robot is from the end of the corridor than the worse the artificial DNA is. If the robot gets very close to the end then we are successful and we can stop, otherwise go to step 3.
Step 3: Make a new list by taking the fifty best artificial DNAs from the last list and writing them down twice. Then slightly change the wiring diagrams of all of these DNAs to induce some new variation.
Step 4: Go to step 2

Frankenstein in the Corridor

I went ahead and codified this experiment as well, the source code is written in Python and is available through github. Here is a picture of the corridor:

This is a screenshot from the corridor simulator, viewing the corridor from a top-down perspective.  The blue X’s are the walls of the corridor, the grey + is the robot’s intended destination, and the red line is the robot. The green stars projecting from the robot are a visualization of its wall sensors, which are also visualized at the bottom of the screen.

After 200 generations, usually the evolutionary algorithm has evolved a brain that can navigate the corridor. Now, for the fun part, a few videos showing a few examples of evolved artificial brains. The first video is from an intermediate solution, one that does not completely make it through the corridor.



The next video is from a solution:



What is really cool here is that evolution has discovered a (albeit simple) brain that can drive a simulated robot through a corridor. We didn’t have to program it to accomplish the task, we trained it. Our measure of how close a robot got to the end of the corridor was used to guide evolution towards a good brain.

It isn’t always easy to train artificial brains (and in fact these difficulties are what my research is centered on), but I can remember how excited I was when I discovered that evolution could be used within a computer to automatically learn things. Hopefully I’ve propagated some of that excitement to you.