Skip to content

Instantly share code, notes, and snippets.

@akyoto
Created June 29, 2012 15:37
Show Gist options
  • Select an option

  • Save akyoto/3018664 to your computer and use it in GitHub Desktop.

Select an option

Save akyoto/3018664 to your computer and use it in GitHub Desktop.
import playground.Everything
TARGETSTRING = "This is the string that will be evolved. Let's make it longer and longer and longer still.."
const
POPSIZE = 10
MUTATIONRATE = 3
population = Array<Vector<Byte>>(POPSIZE)
bestScore : Int = 1
bestInd : Int
#---------------------------------------------------------------------------------------------------
createPOP
for i = 0 until POPSIZE
#population[i] = Vector<Byte>()
population[i] = Vector<Byte>(TARGETSTRING.lengthInBytes)
#= 0 until TARGETSTRING.length
for j in TARGETSTRING
population[i].add(rand(32, 125))
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
findBest
best : Int = 0
bscore : Int = 999999
score : Int = 0
for x in population counting pidx
score = getScore(x)
if score < bscore
bscore = score
best = pidx
bestInd = best
return best
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
getScore a : Vector<Byte>
score : Int = 0
for x in TARGETSTRING.bytes counting xpos
score += abs(x - a[xpos])
bestScore = score
return score
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
mutatePOP best : Int
#Vector<Byte>()
b = Vector<Byte>(TARGETSTRING.lengthInBytes)
#b= Vector<Byte>()
#clone population[best],b
for x in population[best]
b.add(x)
#elitism
clone b, population[0]
for i = 1 until POPSIZE
clone b, population[i]
mutate population[i]
#---------------------------------------------------------------------------------------------------
clone source : Vector<Byte>, dest : Vector<Byte>
for x in source counting xpos
dest[xpos] = x
#print dest[xpos]
#print x
#print " "
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
mutate indiv : Vector<Byte>
for x in indiv counting xpos
if rand(1, 100) <= MUTATIONRATE
if rand(1, 3) == 1
indiv[xpos] = rand(32, 125)
else
# this can drift outside range but whatever..
indiv[xpos] = indiv[xpos] + rand(-2, 2)
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
printIndiv indiv : Vector<Byte>
# clone the vec, add null and convert to string
nul : Byte = 0
#vst = Vector<Byte>(TARGETSTRING.lengthInBytes+1) #Vector<Byte>()
vst = Vector<Byte>()
#clone indiv, vst
for x in indiv counting idx
vst.add(x)
#vst[idx] = x
vst.add(0)
print String(vst.data)
#---------------------------------------------------------------------------------------------------
createPOP()
generation : Int = 0
setRandSeed systemTime()
gaMain
while bestScore > 0
mutatePOP findBest()
generation += 1
if generation > 5000
exit()
#printIndiv population[bestInd]
#print " Score is $bestScore"
in Benchmark("test")
gaMain()
print "\n\n"
printIndiv population[0]
print "done in $generation generations. \nYay Darwin!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment