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

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console