3 ms·
Love it! I wrote a small script that evolves them based on a scoring function, paste in your console and run evolve(1000, score) function evolve(numGenera
by rwinn 12y ago
Love it!
I wrote a small script that evolves them based on a scoring function, paste in your console and run evolve(1000, score)
function evolve(numGenerations, scoringFn, stepInterval) {
var children = document.querySelectorAll('[id*=child]'), generation = 0
if (stepInterval == null) stepInterval = 10
function run() {
var i, child, ctx, data, score, winner, winningScore = -Infinity
for (i = children.length - 1; i >= 0; i--) {
child = children[i]
ctx = child.getContext('2d')
data = ctx.getImageData(0, 0, 318, 148)
score = scoringFn(data)
if (score > winningScore) {
winningScore = score
winner = i
}
}
children[winner].click()
if (++generation < numGenerations) setTimeout(run, stepInterval)
// console.log(winner, winningScore)
}
run()
}
function score(imageData) {
var i, r, g, b, rv = 0,
data = imageData.data, l = data.length
for (i = 0; i < l; i += 4) {
r = 255 - data[i]
g = 255 - data[i+1]
b = 255 - data[i+2]
a = data[i+3] / 255
rv += (r + g + b) * a
}
return rv
}