Evolutionary Computing

Who Am I?

Michael Bosworth

Director of Engineering for

Bunchball a division of BIWorldwide

 

@bozzltron

Why Evolutionary Computing?

Because this...

This book introduced me to the idea that computers can mimic biological systems to product meaningful results.  How cool is that?

The First Experiment

Could a computer produce a mathematical equation without knowing anything about mathematics?

Evolving Equation

What I Got Right

The most successful agents passed on their genetic information to the next generation.

What I Got Wrong

Pretty much everything else.

High-Level Biology Comparison

Biology EC
Environment Problem
Population of individuals Population of candidate solutions
Fitness Quality
Sexual selection Selecting fit parents
New generation of species New generation of agents

Low-Level Biology Comparison

Biology EC
Fertilization between two gametes to produce a genetically unique zygote
(Meiosis, Recombination)
Crossover of two agents to produce a genetically unique agent
Mutations occur during DNA replication
(Mitosis)
Agents are mutated according to a ratio and in a variety of methods.  

In Summary

We are trading animals for agents and DNA for data-structures.

Evolutionary Algorithm

Initialize Population

Evaluate Candidate Fitness

Terminate?

Select Parents for crossover

Crossover / Recombine Parents

Mutate Offspring

Select Next Generation

Evaluate new candidates

Initialization

let agents = [];
let population = 10;
let chars = "012345678+-/*";
let initialLength = 10;

function sample(ary, howMuch){
    howMuch = howMuch || 1;
    let sample = '';
    for(let i=0; i<howMuch; i++) {
      sample += ary[Math.floor(Math.random() * ary.length)];
    }
    return sample;
}

for(let i=0; i<population; i++) {
    let agent = {
        code: sample(chars, 10)
    }
    agents.push(agent);
}

Evaluate Fitness

function evaluate(agent){

    agent.fitness = 0;

    try {
        agent.result = eval(agent.code);

        agent.fitness += agent.result != undefined ? 1 : 0;

        agent.fitness += typeof(agent.result) == 'number' ? 1 : 0;

        agent.fitness += Math.round( (1 /  Math.abs(42 - agent.result ) * 100)) 
    } catch(e) {

    }
}

agents.forEach(evaluate);

Terminate?

function notTerminating(){
    return agents.filter((agent)=>{  
        return agent.result == 42 
    }).length == 0
}

while(notTerminating()){

 // Do things

}

Select Parents

agents = agents.sort((a, b)=>{ return b.fitness - a.fitness });
let numberOfParents = 5;
let parents = agents.slice(0,5)

Crossover

let offspring = [];
let newAgents = 10;

function crossover(mom, dad) {
    let length = mom.code.length > dad.code.length ? dad.code.length : mom.code.length;
    let breakpoint = getRandomNumberBetween(1, length), 
      dad = dad.code.split('').slice(0, breakpoint)
    mom = mom.code.split('').slice(breakpoint, mom.code.length);    
    return dad.concat(mom).join('')
}

for(let i=0; i<newAgents; i++){
    let mom = getRandomNumberBetween(0, parents.length)
    let dad = getRandomNumberBetween(0, parents.length)
    while(dad == mom){
        dad = getRandomNumberBetween(0, parents.length)
    }
    offspring.push(crossover(mom, dad))
}

String Crossover

Mom

Dad

45/1*46+174
89*60/32313

Randomly pick an index along the string

5

Split strings on the index

let mom = ["45/1*", "46+174"] 
let dad = ["89*60", "/32313"]

Combine to for a unique child

45/1*/32313
 01235678910
 01235678910

Mutation

function mutate (agent, charsToMutate, characters) {
    let type = getRandomNumberBetween(0,3)
    for(let i=0; i<charsToMutate; i++){
        let index = getRandomNumberBetween(0,agent.code.length-1)
        let sample = sample(characters, 1)
      switch(type){
        case 0:
          // add 
          agent.code = agent.code.concat(sample)
        break;
        case 1:
          // swap
          agent.code = agent.code.substr(0, index) + sample + 
            agent.code.substr(index + sample.length);
        break;
        case 2:
          // remove
        agent.code = agent.code.slice(0,index) + agent.code.slice(index+1, agent.code.length)
        break
      }
   }
}

let mutationRate = 0.1;
let mutations = getRandomNumberBetween(0, Math.round(offspring.length 
    * mutationRate));
for(let i=0; i<mutations; i++) {
    let index = getRandomNumberBetween(0, offspring.length-1);
    mutate(offspring[index], 1, chars);
}


String Mutation

Agent

 45/1*46+174

Randomly pick an index along the string

5

Do one of three different things

let mutated = "45/1*746+174" 

1. Add new character(s)

2. Swap characters

3. Delete a character

let mutated = "45/1/46+174"
let mutated = "45/146+174" 
 01235678910

Survivor Selection

let agents = agents;
agents.forEach(evaluate);
agents = agents.sort((a, b)=>{ return b.rank - a.rank });
agents = agents.slice(0, 10);

The Second Experiment

Could a computer write JavaScript without "knowing" how to code?

Example Data Structure : Tree

Mom

Dad

Crossover

Randomly prune branches from both trees

3

Swap branches to create a unique new tree

=

i

let

1

;

=

x

let

3

;

=

i

let

3

;

Example Data Structure : Tree

Agent

Mutation

Add

=

i

let

1

;

=

i

let

3

=

i

let

3

;

=

i

let

4

;

Swap

Delete

-

Problem Types

Optimization

Modeling

Simulation

?

Known

Specified

Known

Known

?

Known

Known

?

Real Applications

AI!

Example code is here

The
End
 

Made with Slides.com