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 lcp1(s, t) {
  // go through s1 first, char by char
  for (var i = 0; i < s.length; i++) {
    var si = s[i];

    // now go through t
  }
}

// an algorithm to return the longest common substring of two strings
// transcribed from https://en.wikipedia.org/wiki/Longest_common_substring_problem
function longest_common_substring(s, t) {
  var L = new Array(s.length);
  var z = 0; // length of the longest common fragment so far
  var ret = "";
  for (var i = 0; i < s.length; i++) {
    L[i] = new Array(t.length);
    for (var j = 0; j < t.length; j++) {
      if (s[i] == t[j]) {
        var v = 1;
        if (i > 0 && j > 0) {
          v = L[i - 1][j - 1] + 1;
        }
        L[i][j] = v;
        // is this the longest we found yet?
        if (v > z) {
          ret = s.substring(i - v + 1, i + 1);
          z = v;
        }
      } else {
        L[i][j] = 0;
      }
    }
  }
  return ret;
}
lcs("jmp", "the quick brown fox");

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

  geno.str = pheno;
  // evaluate fitness according the length of common string
  var longest = longest_common_substring(pheno, target);
  geno.fitness = longest.length / target.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);
        // re-insert at a random location:
        child.splice(random(child.length), 0, removed[0]);
      }
    }

    // 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