Created
June 20, 2016 13:08
-
-
Save avinayak/4e66b379c72b67f66a4466fbf86be83b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | |
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | |
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | |
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title></title> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body> | |
<!--[if lt IE 7]> | |
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> | |
<![endif]--> | |
<tt></tt> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script> | |
<script type="text/javascript"> | |
var domain='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321-=*&^%$#@ _'; | |
var fittest='I Think Therefore I Am'; | |
var mutation_rate=0.01; | |
function gen_rand_name() | |
{ | |
var text = ""; | |
for( var i=0; i < fittest.length; i++ ) | |
text += domain.charAt(Math.floor(Math.random() * domain.length)); | |
return text; | |
} | |
function fitness(name){ | |
var fitness=0; | |
for (var i = 0; i < name.length; i++) { | |
if(name[i]===fittest[i]) | |
fitness+=1; | |
}; | |
return fitness/name.length; | |
} | |
function cross(g1,g2) { | |
var fix=Math.floor(Math.random()*fittest.length) | |
ch_gene=g1.substring(fix)+g2.substring(0,fix); | |
ch_gene=mutate(ch_gene); | |
return {'f':fitness(ch_gene),'n':ch_gene}; | |
} | |
function mutate(gene) { | |
var k=gene.split(""); | |
for (var i = 0; i < gene.length; i++) { | |
for (var j = 0; j < k.length; j++) { | |
if(Math.random()<mutation_rate) | |
k[j]=domain.charAt(Math.floor(Math.random() * domain.length)) | |
}; | |
}; | |
return k.join("") | |
} | |
function best_fit () { | |
var max_fitness=0; | |
var best={}; | |
for (var i = 0; i < DNA.length; i++) { | |
if(DNA[i].f>max_fitness) | |
{ | |
max_fitness=DNA[i].f; | |
best=DNA[i]; | |
} | |
}; | |
return best; | |
} | |
function run () { | |
for (var i = 0; i < DNA.length; i++) { | |
for (var j = 0; j < DNA[i].f*100; j++) { | |
pool.push(DNA[i]); | |
}; | |
}; | |
for (var i = DNA.length-1; i > DNA.length-DNA.length*3/4; i--) { | |
var DNA1=pool[Math.floor(Math.random()*pool.length)] | |
var DNA2=pool[Math.floor(Math.random()*pool.length)] | |
var child=cross(DNA1.n,DNA2.n); | |
DNA[i]=child; | |
}; | |
pool=[]; | |
DNA.sort(function(a,b) { return b.f-a.f; }); | |
gen_count+=1; | |
curr_best_fit=best_fit() | |
$("tt").text(curr_best_fit.n); | |
if(curr_best_fit.n===fittest) | |
{ | |
clearInterval(interval) | |
console.log(gen_count) | |
} | |
} | |
var DNA=[]; | |
var pool=[] | |
var gen_count=0; | |
var curr_best_fit="" | |
var interval; | |
$(document).ready(function () { | |
for (var i = 0; i<100; i++) { | |
DNA.push({'f':0.0,'n':gen_rand_name()}) | |
}; | |
for (var i = 0; i < DNA.length; i++) { | |
DNA[i].f=fitness(DNA[i].n); | |
}; | |
interval=window.setInterval(function(){ | |
run(); | |
}, 1); | |
// | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment