This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
viluon's profile picture

(R)Evolutionary Algorithms in Lua/CC

Started by viluon, 11 May 2014 - 09:01 AM
viluon #1
Posted 11 May 2014 - 11:01 AM
Hello again, community!

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.")
Basic explanation: we create a random array of individuals, each is a string made of eight symbols, zero or one.
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
Edited on 12 May 2014 - 02:02 PM
Yevano #2
Posted 11 May 2014 - 03:40 PM
Interesting. I look forward to see if this could be applied to turtle swarms. Perhaps some kind of hive mind intelligence? You should also look at how neural networks work. Apparently they're a bit hard to understand, but very easy to implement.
viluon #3
Posted 11 May 2014 - 04:21 PM
(Thanks for feedback!)
I know, I'm currently studying methods of EA and AI. Right now I'm working on a kind of turtle simulator, it allows simple AI scripts to be run inside a sandbox environment and operate with the turtle object. It's pretty hard to code it on mobile :D/>/> so many differences between SigmaScript and CC… I'll post the code when I'm done, it needs a few fixes and it's currently not for simulation of CC turtles, it's just 2D by now. First I need to figure out the basic technique for evolutionary code so the AIs can evolve.

Edit:those turtle swarms… hmm what about turtle battles? :D/>
Edited on 11 May 2014 - 02:22 PM
Dog #4
Posted 12 May 2014 - 02:18 AM
Wow, if you can make this a reality in cc, the possibilities are staggering. I can't wait to see what comes of this :)/>
viluon #5
Posted 12 May 2014 - 03:42 PM
Wow, if you can make this a reality in cc, the possibilities are staggering. I can't wait to see what comes of this :)/>
Thanks Dog :)/>
The turtle simulator is neeaarly done, it has a (very very) very simple UI displaying the "world" that's saved in a table. The world contains fuel resources that can be "mined" by the turtles for refuelling, as each move costs some fuel. I haven't decided what strategy of AI coding and learning should I use, I'm trying to get some inspiration from Polyworld (that brain system… :OOO ), 3DVCE (that movement… :OOO ) and Machine learning.

If you've got some suggestions, post 'em here! :D/>

Sincerely, viluon (Andy)
kornichen #6
Posted 12 May 2014 - 03:47 PM
I really do look forward to this. It would be impossible crazy to have some turtles learning by themselves.
viluon #7
Posted 12 May 2014 - 03:58 PM
I really do look forward to this. It would be impossible crazy to have some turtles learning by themselves.
I'm proud to have your support :)/>
Lyqyd #8
Posted 12 May 2014 - 04:02 PM
Genetic Algorithms (which this appears to be) have a healthy list of useful applications. It is highly unlikely that this will result in a useful self-improving program, unless confined to a very limited problem space.

Also, begging for upvotes is not allowed.
viluon #9
Posted 12 May 2014 - 04:17 PM
Also, begging for upvotes is not allowed.
Sorry for that.

Genetic Algorithms (which this appears to be) have a healthy list of useful applications. It is highly unlikely that this will result in a useful self-improving program, unless confined to a very limited problem space.
Not sure what do you mean by this. I recommend you to check out this video https://www.youtube.com/watch?v=JBgG_VSP7f8
I'd like to simulate the evolution of an AI and later put it inside a turtle (that's why I'm working on the simulator).

Edit: you mean that the AI wouldn't be able to serve more purposes? Don't think so
Download PDF "EA for solving multi-objective Problems" [ 10MB ]

Edit2: updated the download link
Edited on 12 May 2014 - 05:40 PM