Submitted by Robert Welbourn
http://www.welbourn.com/weasel/
Includes the source code, an installer and a screenshot.
Monday, March 30, 2009
Sunday, March 29, 2009
Tcl the weasel
Submitted by Eric Amundsen
#!/usr/bin/env tclsh
proc pick {{range 27}} {
return [expr {int(rand() * $range)}]
}
set alphabet " abcdefghijklmnopqrstuvwxyz"
for {set j 0} {$j < [string length $alphabet]} {incr j} {
set ::[string index $alphabet $j] $j
}
proc calcOffspringScore {offspring} {
set score 0
for {set j 0} {$j < [string length $::target]} {incr j} {
set score [expr {$score + abs([set ::[string index $::target $j]] - [set ::[string index $offspring $j]])}]
}
return $score
}
proc createOffspring {parent} {
set splitPoint [pick [string length $parent]]
set offspring [string range $parent 0 [expr {$splitPoint - 1}]]
append offspring [string index $::alphabet [pick]]
append offspring [string range $parent [expr {$splitPoint + 1}] end]
return $offspring
}
proc breed {parent numOffspring} {
set bestKid -1
set bestScore 10000
for {set j 0} {$j < $numOffspring} {incr j} {
set kid [createOffspring $parent]
set kids($j) $kid
set score [calcOffspringScore $kid]
if {$bestScore > $score} {
set bestKid $j
set bestScore $score
}
}
return $kids($bestKid)
}
if {[llength $argv] == 0} {
puts "methink.tcl usage :
methink.tcl <# offspring / gen> <(optional) target string>
default target => \"methinks it is like a weasle\"
target string can only include a-z lowercase and space"
return
} elseif {[llength $argv] == 1} {
set ::target "methinks it is like a weasle"
} else {
set ::target [lindex $argv 1]
}
set numOffspring [lindex $argv 0]
set initString ""
for {set j 0} {$j < [string length $target]} {incr j} {
append initString [string index $alphabet [pick]]
}
puts [set genString $initString]
for {set j 0} {$j < 10000000} {incr j} {
puts [set genString [breed $genString $numOffspring]]
if {$genString == $target} {
puts "[incr j] generations"
return
}
}
puts "sorry, gave up after [incr j] generations"
Saturday, March 28, 2009
VBA Excel weasel
Submitted by Ian Rhile
Sub weasel()
'An Excel macro for Dawkin's weasel algorithm
gentarget = 0
mutationrate = 0.05
Sheets("Sheet1").Cells.ClearContents
target = "methinks it is like a weasel"
alphabet = "abcdefghijklmnopqrstuvwxyz "
'generate initial random string
For x = 1 To Len(target)
y = Int(27 * Rnd + 1)
seq = seq + Mid(alphabet, y, 1)
Next x
Sheets("Sheet1").Range("A1").Value = "original sequence:"
Sheets("Sheet1").Range("B1").Value = seq
Sheets("Sheet1").Range("A2").Value = "generation"
Sheets("Sheet1").Range("B2").Value = "original generational sequence"
Sheets("Sheet1").Range("C2").Value = "matches"
50 gen = gen + 1
Sheets("Sheet1").Cells(gen + 2, 1) = gen
Sheets("Sheet1").Cells(gen + 2, 2) = seq
maxfit = 0
maxfitseq = seq
For x = 1 To 100
newseq = seq
For u = 1 To Len(seq)
If Rnd < mutationrate Then
w = Int(27 * Rnd + 1)
Mid(newseq, u, 1) = Mid(alphabet, w, 1)
End If
Next u
Sheets("Sheet1").Cells(gen + 2, x + 4) = newseq
fit = 0
For t = 1 To Len(newseq)
If Mid(target, t, 1) = Mid(newseq, t, 1) Then fit = fit + 1
Next t
If fit > maxfit Then
maxfit = fit
maxfitseq = x
End If
Next x
Sheets("Sheet1").Cells(gen + 2, 3) = maxfit
seq = Sheets("Sheet1").Cells(gen + 2, maxfitseq + 4).Value
If gentarget > 0 And gen > gentarget Then GoTo 100
If seq = target Then
If gentarget > 0 Then GoTo 50
gentarget = gen + 100
End If
GoTo 50
100 End Sub
JavaScript weasel
Wesley R. Elsberry: Interactive JavaScript weasel
http://www.antievolution.org/features/ec/weasel102.html
http://www.antievolution.org/features/ec/weasel102.html
Pythonic weasels
Anders Gorm Pedersen: Non-locking and locking weasels in Python
http://www.cbs.dtu.dk/courses/27615.mol/weasel.php
http://www.cbs.dtu.dk/courses/27615.mol/weasel.php
Subscribe to:
Posts (Atom)