This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding. Usually shorter than 500 words. Often with pictures. Livecoding happens almost every Sunday at 2pm PDT on multiple channels. You should follow My Youtube channel to catch me live.

Sound on YouTube was borked yesterday. I sounded like Mr. Robot. It all worked well on LiveEdu, but they didn’t save the video it looks like. ¯\_(ツ)_/¯

Anyway, we made some progress from last time. Our evolutionary approach still gets stuck in a local maximum after just a few iterations, but it no longer gets stuck on a single solution.

So even though the fitness score is, say, 0.4545 and never changes, the result itself keeps changing. We achieved that by improving the pairing function.

Our algorithm can now choose between two different pairing functions. Either the top 50% of the population breeds pairwise, or we take the top 30 specimens and breed each specimen with all others in the top 30.

const PAIRING_STRATEGIES = {
    'top_half_pairs': (population) => _.chunk(_.take(population,
                                                     population.length/2),
                                              2),
    'top_30_superset': (population) => {
        const top30 = _.take(population, 30);
 
        return _.flatten(top30.map(A => top30.map(B => [A, B])))
    }
}

This creates a lot of thrashing at the top, and it also makes our code faster to run. Thrashing is what we wanted to achieve, but unfortunately, it didn’t have the effect on fitness scores that we hoped for.

Observe:

16
{ fitness: 0.45454545454545453,
  code: '*15+',
  fitnessLast: 0.45454545454545453,
  codeLast: '4584',
  size: 200 }
17
{ fitness: 0.45454545454545453,
  code: '06*0',
  fitnessLast: 0.45454545454545453,
  codeLast: '4584',
  size: 200 }
18
{ fitness: 0.45454545454545453,
  code: '686.+9',
  fitnessLast: 0.45454545454545453,
  codeLast: '4584',
  size: 200 }
19
{ fitness: 0.45454545454545453,
  code: '86.+',
  fitnessLast: 0.45454545454545453,
  codeLast: '4584',
  size: 200 }
20
{ fitness: 0.45454545454545453,
  code: '28+',
  fitnessLast: 0.45454545454545453,
  codeLast: '0+*-/',
  size: 200 }
21
{ fitness: 0.45454545454545453,
  code: '800406',
  fitnessLast: 0.45454545454545453,
  codeLast: '092-092-',
  size: 200 }
22
{ fitness: 0.45454545454545453,
  code: '00406*',
  fitnessLast: 0.45454545454545453,
  codeLast: '092-092-',
  size: 200 }

We achieved total randomness with no convergence. This is kind of pointless, but at least the simplified character set produces more syntactically correct solutions.

A glimmer of hope

Towards the end of my livecoding session, we found a glimmer of hope. Using Prettier.

Prettier can take a JavaScript AST and convert it into plain JavaScript code. This means we can go 1-up on abstraction levels and still produce readable code in the end.

Prettier also pointed us towards Babylon, which is a JavaScript parser that takes code and turns it into an AST. This means we can take any syntactically valid code and turn it into an AST.

ASTs, by the way, are abstract syntax trees. They’re how computers read code.

What’s left now is to figure out:

  1. How to generate an initial population of ASTs
  2. How to breed and mutate ASTs

🤔

You can see the full current code on Github. Not too useful yet unless you’re reading months from now. Github keeps links looking the same 🙂

Learned something new? 💌

Join 8,400+ people becoming better Frontend Engineers!

Here's the deal: leave your email and I'll send you an Interactive ES6 Cheatsheet 📖 right away.  After that you'll get an email once a week with my writings about React, JavaScript,  and life as an engineer.


You should follow me on twitter, here.