cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              // the desired result:
var target = "the quick brown fox jumped over the lazy dog";

// the set of components:
var dictionary = ["I", "a", "an", "bleep", "blown", "bog", "box", "broad", "brown", "bumped", "cat", "cog", "coloured", "colourful", "colourless", "crazy", "creep", "crown", "dig", "do", "dog", "door", "dot", "ever", "fit", "fix", "flick", "for", "fox", "frown", "glean", "great", "greed", "green", "greet", "hazy", "he", "hog", "ideas", "idols", "in", "it", "jumble", "jumped", "jumper", "jumps", "last", "lazy", "leaped", "lumped", "maze", "of", "offer", "ogre", "older", "on", "or", "over", "quick", "quite", "rapid", "she", "sheen", "sheep", "sick", "sleep", "slept", "slow", "spelt", "spilt", "the", "town", "under"];

// how many candidates in a generation:
var pop_size = 30;
// the base probability of mutations:
var gene_mutation_rate = 0.3;

// initialize:
var generation = 0;
// generate a random population:
var pop = [];
for (var i = 0; i < pop_size; i++) {
  var geno = [];
  for (var j = 0; j < 9; j++) {
    geno[j] = random(dictionary.length);
  }
  pop.push(geno);
}

function develop(geno) {
  var pheno = [];
  for (var i = 0; i < geno.length; i++) {
    pheno[i] = dictionary[geno[i]];
  }
  return pheno.join(" ");
}

function geno_eval(geno) {
  var pheno = develop(geno);

  geno.str = pheno;

  var err = 0;
  for (var i = 0; i < pheno.length; i++) {
    if (pheno.substr(i, 1) != target.substr(i, 1)) {
      err++;
    }
    // and backwards too?
    if (pheno.substr(pheno.length - 1 - i, 1) != target.substr(target.length - 1 - i, 1)) {
      //err++;
    }
  }

  geno.fitness = 1 / (1 + err / pheno.length);
}

function update() {
  // evaluate each member of the gene pool:
  for (var i = 0; i < pop_size; i++) {
    geno_eval(pop[i]);
  }

  // sort by fitness:		
  pop.sort(function(a, b) {
    return b.fitness - a.fitness;
  });

  // show them:
  for (var i = 0; i < pop_size; i++) {
    write(generation, i, pop[i].fitness, pop[i].str);
  }

  // make a new generation:
  var newpop = [];
  for (var i = 0; i < pop_size; i++) {
    // fitter candidates are less volatile:
    var mutability = i / pop_size * gene_mutation_rate;

    var child = [];

    // pick a parent (by using random(i) we bias to fitter parents)
    var parent = pop[random(i)];

    // an alternative using proximity
    //var idx = i + random(9)-5;
    //idx = Math.min(Math.max(idx, 0), pop_size-1);
    //var parent = pop[ idx ];

    // copy or mutate genes:
    for (var j = 0; j < parent.length; j++) {
      // mutate?
      if (random() < mutability) {
        child[j] = random(dictionary.length);
      } else {
        // copy:
        child[j] = parent[j]
      }
      
      // jumping gene:
      if (random() < mutability) {
          // remove a random gene (returns an array of length 1)
          var removed = child.splice(random(child.length), 1);
          // concatenate this at the end to create a new child:
          child = child.concat(removed);
        }
    }

    // add to population:
    newpop[i] = child;
  }

  // replace:
  pop = newpop;
  generation++;
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console