September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
14
Task/Evolutionary-algorithm/Zkl/evolutionary-algorithm.zkl
Normal file
14
Task/Evolutionary-algorithm/Zkl/evolutionary-algorithm.zkl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
const target = "METHINKS IT IS LIKE A WEASEL";
|
||||
const C = 100; // Number of children in each generation.
|
||||
const P = 0.05; // Mutation probability.
|
||||
const A2ZS = ["A".."Z"].walk().append(" ").concat();
|
||||
fcn fitness(s){ Utils.zipWith('!=,target,s).sum(0) } // bigger is worser
|
||||
fcn rnd{ A2ZS[(0).random(27)] }
|
||||
fcn mutate(s){ s.apply(fcn(c){ if((0.0).random(1) < P) rnd() else c }) }
|
||||
|
||||
parent := target.len().pump(String,rnd); // random string of "A..Z "
|
||||
gen:=0; do{ // mutate C copies of parent and pick the fittest
|
||||
parent = (0).pump(C,List,T(Void,parent),mutate)
|
||||
.reduce(fcn(a,b){ if(fitness(a)<fitness(b)) a else b });
|
||||
println("Gen %2d, dist=%2d: %s".fmt(gen+=1, fitness(parent), parent));
|
||||
}while(parent != target);
|
||||
Loading…
Add table
Add a link
Reference in a new issue