summaryrefslogtreecommitdiff
path: root/js/ui/evolver.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2016-05-23 01:07:46 -0400
committerJules Laplace <jules@okfoc.us>2016-05-23 01:07:46 -0400
commitcbe5a189dca1c228186fb1e433071578614c4e91 (patch)
tree8e878e1febe2a35dbe97fa71f4ae8e78009fbedc /js/ui/evolver.js
parent7ac0cf096e9b9f0ff0c15968e7f04caeeb220f36 (diff)
genetic algorhehrithm
Diffstat (limited to 'js/ui/evolver.js')
-rw-r--r--js/ui/evolver.js199
1 files changed, 199 insertions, 0 deletions
diff --git a/js/ui/evolver.js b/js/ui/evolver.js
new file mode 100644
index 0000000..e65df20
--- /dev/null
+++ b/js/ui/evolver.js
@@ -0,0 +1,199 @@
+var evolver = (function(){
+
+ setTimeout(init)
+
+ var hash = window.location.hash
+ var opt = {
+ src: "img/sistene-chapel.jpg",
+ w: 40,
+ h: 16,
+ }
+ if (hash.length) {
+ hash.split("#").forEach(function(s){
+ var kv = s.split("=")
+ if (opt.hasOwnProperty(kv[0])) {
+ opt[kv[0]] = kv[1]
+ }
+ })
+ }
+
+ var target, last_score = 0
+ var clones = [], clone_count = 10, strokes_per_iteration = 1
+ var clone_index = 0
+ var main_canvas
+
+ var compare_w = 64, compare_h = 64
+
+ var target = document.createElement('canvas')
+ target.width = compare_w
+ target.height = compare_h
+ var t_ctx = target.getContext('2d')
+
+ var compare = document.createElement('canvas')
+ compare.width = compare_w
+ compare.height = compare_h
+ var c_ctx = compare.getContext('2d')
+
+
+ brush.rebuild = function(){
+ this.initialize()
+ }
+
+ function init () {
+ nopaint.debug = false
+
+ main_canvas = canvas
+ canvas.resize(opt.w, opt.h)
+// canvas.forEach(function(lex,x,y){
+// lex.bg = randint(16)
+// })
+ for (var i = 0; i < clone_count; i++) {
+ clones[i] = {
+ el: document.createElement("div"),
+ canvas: canvas.clone(),
+ score: 0,
+ hash: -1,
+ }
+// clones[i].canvas.append(clones[i].el)
+// clones[i].el.className = "rapper"
+// document.getElementById("clones").appendChild( clones[i].el )
+ clones[i].canvas.forEach(function(lex){
+ lex.build = noop
+ })
+ }
+ load(opt.src, go)
+ }
+ function load(src, fn){
+ var img = new Image ()
+ img.onload = function(){
+ target = drawImage(t_ctx, img)
+ last_score = 0
+ go()
+ }
+ if (opt.src.match(/^http/)) {
+ opt.src = "http://asdf.us/cgi-bin/proxy?" + opt.src
+ }
+ img.src = src
+ }
+ function drawImage (ctx, img) {
+ ctx.drawImage(img, 0, 0, compare_w, compare_h)
+ return { width: compare_w, height: compare_h, data: ctx.getImageData(0,0,compare_w,compare_h).data, channels: 4 }
+ }
+ function go () {
+ if (evolver.paused) return
+ clone_index = 0
+ paint_next()
+ }
+
+ function paint_next () {
+ canvas = clones[clone_index].canvas
+ for (var i = 0; i < strokes_per_iteration; i++) {
+ nopaint.paint()
+ }
+ render(canvas, function(c){
+ compare = drawImage(c_ctx, c)
+
+ clones[clone_index].score = ImageSSIM.compare(target, compare).ssim
+ if (++clone_index == clone_count) {
+ fitness()
+ changed = false
+ requestAnimationFrame(go)
+ }
+ else {
+ paint_next()
+ }
+ })
+ }
+ function fitness () {
+ clones.sort(function(a,b){ return b.score - a.score })
+
+ var best_clone, next_best_clone, third_best_clone
+ var clones_to_keep
+
+ var max_score = clones[0].score
+
+// console.log(clones.filter(function(c,i){ return i < 10 }).map(function(c){ return c.score.toFixed(2) }).join(" "))
+
+ if (max_score < last_score) {
+ console.log("no improvement [%s] [%s]", max_score.toFixed(3), last_score.toFixed(3))
+ clones_to_keep = 2
+ best_clone = main_canvas
+ next_best_clone = clones[0].canvas
+ third_best_clone = clones[1].canvas
+ }
+ else {
+ last_score = max_score
+
+ clones_to_keep = 3
+ best_clone = clones[0].canvas
+ next_best_clone = clones[1].canvas
+ third_best_clone = clones[2].canvas
+
+ console.log("top clone [%s]", max_score.toFixed(5))
+ main_canvas.forEach(function(lex,x,y){
+ lex.assign( best_clone.getCell(x,y) )
+ })
+ }
+
+ var best_clone = clones[0].canvas
+ clones.forEach(function(clone, i){
+ if (i < clones_to_keep) return
+ var clone_to_copy
+ if (i < clones.length / 4) {
+ clone_to_copy = third_best_clone
+ }
+ else if (i < clones.length / 2) {
+ clone_to_copy = next_best_clone
+ }
+ else {
+ clone_to_copy = best_clone
+ }
+ clone.canvas.forEach(function(lex,x,y) {
+ lex.assign( clone_to_copy.getCell(x,y) )
+ })
+ })
+
+ }
+
+/*
+ function check () {
+ clipboard.export_canvas(function(canvas){
+ var hash = simi.hash(canvas)
+ var score = simi.compare(hash, target)
+ if (score > last_score) {
+ last_score = score
+ console.log(score.toFixed(3), "PAINT")
+ nopaint.paint()
+ }
+ else {
+ console.log(score.toFixed(3), "NO")
+ nopaint.no()
+ }
+ requestAnimationFrame(check)
+ })
+ }
+*/
+
+ var buffer_canvas = document.createElement('canvas')
+
+ function render (canvas, done_fn) {
+ var opts = {
+ palette: 'mirc',
+ font: 'fixedsys_8x15',
+ fg: 0,
+ bg: 1,
+ canvas: buffer_canvas
+ }
+ opts.done = function(c){
+ done_fn(c)
+ }
+ colorcode.to_canvas(canvas.mirc(), opts)
+ }
+
+ return {
+ paused: false,
+ pause: function(){
+ evolver.paused = ! evolver.paused
+ }
+ }
+})() \ No newline at end of file