Genetic Algorithms - Simple EcoSystem

I am interested in creating something similar to this:
http://www.shiffman.net/itp/classes/nature/week09_s09/evolution/

which is a “simple ecosystem”. It has “creatures” which find food,
can breed etc.
read more here:
http://www.shiffman.net/teaching/nature/ga/

I am wondering if anyone can supply any sample source code or help
for corona/lua as an example of how to do something similar?

much appreciated,
-winter [import]uid: 58922 topic_id: 16673 reply_id: 316673[/import]

Have you seen the example provided earlier by BrandonTreb? He has made a sample of boids that keep wandering around the screen, you can extend that to seek for food, etc.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16673 reply_id: 62305[/import]

@JayantV, I had no idea that Brandon Treb released this. This is purely awesome.

http://brandontreb.com/autonomous-steering-behaviors-corona-sdk/ [import]uid: 14218 topic_id: 16673 reply_id: 62371[/import]

Do you understand what is genetic algorithm? Game of life is strange method to use it but it’s great way to find solution without exact result :slight_smile: Here you go:

[lua]problemSize = 64
mutationRate = 0.005
crossoverRate = 0.98
populationSize = 64
maxGenerations = 50
selectionTournamentSize = 3
seed = os.time()

function crossover(a, b)
if math.random() > crossoverRate then
return “”…a
end
local cut = math.random(a:len()-1)
local s = “”
for i=1, cut do
s = s…a:sub(i,i)
end
for i=cut+1, b:len() do
s = s…b:sub(i,i)
end
return s
end

function mutation(bitstring)
local s = “”
for i=1, bitstring:len() do
local c = bitstring:sub(i,i)
if math.random() < mutationRate then
if c == “0”
then s = s…“1”
else s = s…“0” end
else s = s…c end
end
return s
end

function selection(population, fitnesses)
local pop = {}
repeat
local bestString = nil
local bestFitness = 0
for i=1, selectionTournamentSize do
local selection = math.random(#fitnesses)
if fitnesses[selection] > bestFitness then
bestFitness = fitnesses[selection]
bestString = population[selection]
end
end
table.insert(pop, bestString)
until #pop == #population
return pop
end

function reproduce(selected)
local pop = {}
for i, p1 in ipairs(selected) do
local p2 = nil
if (i%2)==0 then p2=selected[i-1] else p2=selected[i+1] end
child = crossover(p1, p2)
mutantChild = mutation(child)
table.insert(pop, mutantChild);
end
return pop
end

function fitness(bitstring)
local cost = 0
for i=1, bitstring:len() do
local c = bitstring:sub(i,i)
if(c == “1”) then cost = cost + 1 end
end
return cost
end

function random_bitstring(length)
local s = “”
while s:len() < length do
if math.random() < 0.5
then s = s…“0”
else s = s…“1” end
end
return s
end

function getBest(currentBest, population, fitnesses)
local bestScore = currentBest==nil and 0 or fitness(currentBest)
local best = currentBest
for i,f in ipairs(fitnesses) do
if(f > bestScore) then
bestScore = f
best = population[i]
end
end
return best
end

function evolve()
local population = {}
local bestString = nil
– initialize the popuation random pool
for i=1, populationSize do
table.insert(population, random_bitstring(problemSize))
end
– optimize the population (fixed duration)
for i=1, maxGenerations do
– evaluate
fitnesses = {}
for i,v in ipairs(population) do
table.insert(fitnesses, fitness(v))
end
– update best
bestString = getBest(bestString, population, fitnesses)
– select
tmpPop = selection(population, fitnesses)
– reproduce
population = reproduce(tmpPop)
display.newText(">gen %d, best cost=%d [%s]\n", i, fitness(bestString), bestString)
end
return bestString
end

– run
display.newText(“Genetic Algorithm on OneMax, with %s\n”,_VERSION);
math.randomseed(seed)
best = evolve()
display.newText(“Finished!\nBest solution found had the fitness of %d [%s].\n”, fitness(best), best)[/lua] [import]uid: 12704 topic_id: 16673 reply_id: 62466[/import]

@gtatarkin:
Runtime error: bad argument #2 to ‘_newText’ (number expected, got string)

is what i get when running this?
[import]uid: 58922 topic_id: 16673 reply_id: 62477[/import]

Try it with IntelliJ IDEA + Kahlua :slight_smile: Or just change to work with Corona Simulator. What it does? Just solving problem - this is what genetic algorithm do best :slight_smile: [import]uid: 12704 topic_id: 16673 reply_id: 62487[/import]