Yesterday I started with Evolutionary Computing/Programming in Lua. I did not find any tutorials for it, so I used an old (2003) article designed for Perl (no, I don't know how to perl but I used the theory) and coded a simple implementation via Lua in SigmaScript all on my smartphone. I haven't tested the code on CC but it should work as it is pure Lua.
Here's the code:
Spoiler
local individuals={}
for i=1,10 do
local result=tostring(math.random(0,1))
for _=1,7 do result=result..tostring(math.random(0,1)) end
table.insert(individuals,result)
end
local done=false
local TARGET="1"
local count=0
local maxFit=0
local function fitness(x)
local _,fit=tostring(x):gsub(TARGET,TARGET)
return fit
end
local function updateMaxFit()
for k,v in pairs(individuals) do
if fitness(v)>maxFit then maxFit=fitness(v) end
end
return maxFit
end
local function selectParent()
local possibleParents={}
updateMaxFit()
for k,v in pairs(individuals) do
if fitness(v)>=maxFit/2 then table.insert(possibleParents,v) end
end
return possibleParents[math.random(#possibleParents)]
end
local function mut(y)
local shuffle=math.random(1,3)
if shuffle==1 then
y=y:sub(1,math.random(1,3))..y:sub(5,math.random(5,8))
elseif shuffle==2 then
y=y:sub(5,math.random(5,8))..y:sub(1,math.random(1,3))
else
y=y:sub(4,math.random(4,8))..y:sub(1,math.random(1,1))
end
if #y<8 then for i=#y,8 do y=y..tostring(math.random(0,1)) end end
return y:sub(1,8)
end
repeat
count=count+1
print("getting parents")
par1,par2=tostring(selectParent()),tostring(selectParent())
print("reproducing from "..par1.." and "..par2)
local offspring=par1:sub(1,4)..par2:sub(5,8)
print("Congrats! A new part\n of the family is "..offspring)
offspring=mut(offspring)
print("Oh no, it's a mutant! "..offspring)
local lastV=0
for k,v in pairs(individuals) do
if fitness(v)<fitness(offspring) then print(offspring.." replaced "..v) individuals[k]=offspring end
lastV=v
end
until updateMaxFit()==8
print("Perfection reached\n after "..count.." generations.")
Every time the main loop is run, we pick up two parents from the better half of the individuals to form an offspring.
We perform a mutation of the offspring to keep the diversity of the population highest possible.
We then select an individual to be replaced by the new offspring if it's less "fit" than the offspring, this way we improve the overall fitness of the population.
I know I'm not really good at explaining, hope you understood.
This is a part of my secret project I am working on now.
The next step will be implementing this evolution inside Lua script that will improve its own effectiveness.
My final goal is creating an evolutionary AI that will be hopefully able to learn new things and add new features to itself. It will be a long journey, but I believe I can do it.
Note: the code is weirdly written because of SigmaScript limitations. I'll fix both the code and this post up as soon as I get to my PC.
Cheers, viluon