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.

            
              var pop = [];
var dim = 100;
var rows = 5;
var gene_size = 32;

var binops = ["+", "*"];
var unfuns = ["Math.sin", "Math.cos"];
var binfuns = ["wrap"];
var ops = [].concat(binops).concat(unfuns).concat(binfuns);

function is_in_list(list) {
  var set = {};
  for (var k of list) {
    set[k] = true;
  }
  return function(s) {
    return set[s];
  }
};

function pick(arr) {
  return arr[random(arr.length)];
}

is_binop = is_in_list(binops);
is_unfun = is_in_list(unfuns);
is_binfun = is_in_list(binfuns);

function random_instruction() {
  return [pick(ops), random(100)];
}

function geno_make(len) {
  var g = [];
  for (var i = 0; i < len; i++) {
    g.push(random_instruction());
  }
  return g;
}

function geno_clone(a) {
  var copy = [];
  for (var i=0; i<a.length; i++) {
    copy.push([
      a[i][0],
      a[i][1],
    ]);
  }
  return copy;
}

function geno_develop(individual) {
  var stats = [];
  var stack = ["y", "x"];
  var g = individual.geno;
  for (var i = 0; i < g.length; i++) {
    var node = g[i];
    var op = node[0];
    var name = "r" + i;
    var val = "0";
    var a = stack[stack.length - 1];
    var b = stack[node[1] % stack.length];

    if (is_binop(op)) {
      val = a + op + b;
    } else if (is_unfun(op)) {
      val = op + "(" + a + ")";
    } else if (is_binfun(op)) {
      val = op + "(" + a + "," + b + ")";
    }
    stack.push(name);
    stats.push("var " + name + " = " + val + ";");
  }
  stats.push("var r = wrap(" + stack[stack.length - 1] + ",1);");
  stats.push("var g = wrap(" + stack[stack.length - 2] + ",1);");
  stats.push("var b = wrap(" + stack[stack.length - 3] + ",1);");
  stats.push("return [r,g,b];");
  individual.code = stats.join("\n");
  var f = new Function("x", "y", individual.code);
  return function(x, y) {
    x = x / dim;
    y = y / dim;
    return f(x, y);
  };
}

function xover(geno, mate) {
  var i1 = random(geno.length);
  var i2 = random(geno.length);
  var lo = Math.min(i1, i2);
  var hi = Math.max(i1, i2);
  var a = geno.slice(0, lo);
  var b = geno.slice(lo, hi);
  var c = geno.slice(hi);
  return a.concat(b).concat(c);
}

function mutate(geno) {
  var index = random(geno.length);
  geno[index] = random_instruction();
  
  var index = random(geno.length);
  var item = geno[index];
  geno.splice(index, 0);
  geno.unshift(item);
                      
  var index = random(geno.length);
  var item = geno[index]; 
  var index = random(geno.length);
  geno[index] = item;
  
  return geno;
}

function make_img() {
  for (var i = 0; i < pop.length; i++) {
    var f = geno_develop(pop[i]);
    pop[i].img.set(f).normalize();
    
  }
}

function reset() {
  for (var i = 0; i < rows*rows; i++) {
    pop[i] = {
      geno: geno_make(gene_size),
      img: new field2D(dim),
    };
  }
  make_img();
}

reset();

function mouse(e, p) {
  if (e == "down") {
    var row = Math.floor(p[1]*rows);
    var col = Math.floor(p[0]*rows);
    var src = row*rows+col;
    
    var dst = random(pop.length);
    pop[dst].geno = xover(pop[src].geno, pop[dst].geno);   
    var f = geno_develop(pop[dst]);
    pop[dst].img.set(f).normalize();
    
    var dst = random(pop.length);
    pop[dst].geno = mutate(geno_clone(pop[src].geno)); 
    var f = geno_develop(pop[dst]);
    pop[dst].img.set(f).normalize();
  }
}

function draw() {
  draw2D.scale(1 / rows);

  for (var i = 0; i < pop.length; i++) {
    draw2D.push().translate(i % rows, Math.floor(i/rows));
    pop[i].img.draw();
    draw2D.pop();
  }
}
            
          
!
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