Last active
August 29, 2015 14:05
-
-
Save yazgazan/d35399551596a8767fee 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
genetics = require '../lib/genetics' | |
Genetic = genetics.Genetic | |
Solution = genetics.Solution | |
gen = null | |
mod = 1000 | |
ratio = 100 | |
modb = Math.round(mod / ratio) | |
moda = mod - 1 | |
best = mod * mod | |
class Sol extends Solution | |
random: -> | |
@a = Math.round(Math.random() * 1000) % mod | |
@b = Math.round(Math.random() * 1000) % mod | |
@c = Math.round(Math.random() * 1000) % mod | |
return | |
crossOver: (sola, solb) -> | |
@a = Math.round((sola.a + solb.a) / 2) | |
@b = Math.round((sola.b + solb.b) / 2) | |
@c = Math.round((sola.c + solb.c) / 2) | |
return | |
mutate: -> | |
newSol = new Sol | |
newSol.a = @a | |
newSol.b = @b | |
newSol.c = @c | |
if Math.random() < 0.3 | |
newSol.a = Math.round(Math.random() * 1000) % (mod + 1) | |
if Math.random() < 0.3 | |
newSol.b = Math.round(Math.random() * 1000) % (mod + 1) | |
if Math.random() < 0.3 | |
newSol.c = Math.round(Math.random() * 1000) % (mod + 1) | |
return newSol | |
eval: -> | |
# calculate total iterating through gen.pop (declared on line 6) | |
@fit = (@a * @b - @c) / best | |
return | |
class Gene extends Genetic | |
end: -> | |
console.log @gen + ', ' + @bestfit().fit | |
return @gen == 100 or @bestfit().fit >= 1 | |
select: -> | |
@selectSUS() | |
gen = new Gene Sol, 10000 | |
gen.init() | |
gen.eval() | |
console.log gen.bestfit() | |
gen.run() | |
console.log gen.bestfit() | |
console.log 'last gen : ' + gen.gen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment