Data update

This commit is contained in:
Ingy döt Net 2023-10-02 18:11:16 -07:00
parent 796d366b97
commit 35bcdeebf8
504 changed files with 7045 additions and 610 deletions

View file

@ -27,39 +27,39 @@ extension evoHelper
class EvoAlgorithm : Enumerator
{
object theTarget;
object theCurrent;
object theVariantCount;
object _target;
object _current;
object _variantCount;
constructor new(s,count)
{
theTarget := s;
theVariantCount := count.toInt();
_target := s;
_variantCount := count.toInt();
}
get() = theCurrent;
get Value() = _current;
bool next()
{
if (nil == theCurrent)
{ theCurrent := theTarget.Length.randomString(); ^ true };
if (nil == _current)
{ _current := _target.Length.randomString(); ^ true };
if (theTarget == theCurrent)
if (_target == _current)
{ ^ false };
auto variants := Array.allocate(theVariantCount).populate:(x => theCurrent.mutate:P );
auto variants := Array.allocate(_variantCount).populate:(x => _current.mutate:P );
theCurrent := variants.sort:(a,b => a.fitnessOf:Target > b.fitnessOf:Target ).at:0;
_current := variants.sort:(a,b => a.fitnessOf:Target > b.fitnessOf:Target ).at:0;
^ true
}
reset()
{
theCurrent := nil
_current := nil
}
enumerable() => theTarget;
enumerable() => _target;
}
public program()

View file

@ -0,0 +1,7 @@
(var c 100) ;number of children in each generation
(var p 0.05) ;mutation probability
(var target "METHINKS IT IS LIKE A WEASEL")
(var tsize (len target))
(var alphabet (to-vec " ABCDEFGHIJLKLMNOPQRSTUVWXYZ"))

View file

@ -0,0 +1,5 @@
(var fitness (comp @(map = target) (count val)))
(var perfect-fit? (comp fitness (= tsize)))
(var rand-char #(rand-pick alphabet))
(var mutate (map #(if (< (rand) p) (rand-char) %)))

View file

@ -0,0 +1,12 @@
(function evolve generation parent
(print (pad-left " " 3 generation) " " (... str parent) " " (fitness parent))
(return-when (perfect-fit? parent))
(let children (times c #(mutate parent))
fittest (max-by fitness (... vec parent children)))
(recur (inc generation) fittest))
(evolve 1 (times tsize rand-char))