Something went wrong. Try again.
[Archived] evolutionary genetic algorithm project and mob plugin for Minetest as part of a bachelor degree final thesis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
dofile("./trainer.lua")dofile("../goblin/goblin.lua")
local trainer = adaptive_ai.trainerlocal helper = adaptive_ai.helperlocal calc = adaptive_ai.calclocal goblins = adaptive_ai.goblinslocal file_tag = goblins.trained_tag
local random = math.random--local mod = math.fmod or math.modlocal move_creature = trainer.move_creaturelocal truncate = calc.truncatelocal distance = calc.dist_euclideanlocal pop = helper.poplocal valid = trainer.valid
local adjacent = helper.adjacentlocal turn_max = 300local popul_size = 100
local function fill_cell_goblin(cell, y) if random() <= 0.01 then -- Putting impassable obstacle cell[1] = nil else if random() <= 0.02 then -- Planting mushroom cell[2] = true else cell[2] = false end endend
local function fill_goblin(goblin, mother, father) goblin.fear_height = goblins.stats.fear_height goblin.pos = {x=random(1,40), z=random(1,40)}
goblins.setup(goblin, mother, father)
goblin.fitness = 0 trainer.console_log("Created new goblin: "..goblin.name)end
local function reset_goblin(goblin, map) goblin.food = {} goblin.fitness = 0 goblin.hp = goblins.stats.hp_max goblin.hunger = goblins.stats.hunger_max
goblin.pos.y = map[goblin.pos.x][goblin.pos.z][1] while not goblin.pos.y do goblin.pos = {x=random(1,40), z=random(1,40)} goblin.pos.y = map[goblin.pos.x][goblin.pos.z][1] endend
local goblin_decisions = { function(self, dtime) -- idle end, function(self, dtime) -- wander local n = random(1,#adjacent) move_creature(self, n) self.hunger = self.hunger - 1 end, function(self, dtime) -- eat if self.food and #self.food > 0 then local satiate = pop(self.food) self.hunger = self.hunger + satiate self.hp = self.hp + 1 end end, function(self, dtime) -- breed self.fitness = self.fitness + 2 self.hunger = self.hunger - 10 end, function(self, dtime) -- move North move_creature(self, 5) end, function(self, dtime) -- move South move_creature(self, 4) end, function(self, dtime) -- move East move_creature(self, 7) end, function(self, dtime) -- move West move_creature(self, 2) end, function(self, dtime) -- move NE move_creature(self, 8) end, function(self, dtime) -- move SE move_creature(self, 6) end, function(self, dtime) -- move NW move_creature(self, 3) end, function(self, dtime) -- move SW move_creature(self, 1) end,}
local function manage_goblin(player, map, dtime) if player.hp > 0 then local neighbors = {} local p, pos = {}, player.pos for i=1,#adjacent do p.x = pos.x + adjacent[i].x p.z = pos.z + adjacent[i].z if valid(p) and map[p.x][p.z][1] then p.y = map[p.x][p.z][1] else p.y = pos.y + player.climb + 1 end neighbors[i] = {x=p.x, y=p.y, z=p.z} --print("Neighbor "..i.." = "..p.y) end
player.hunger = player.hunger - dtime local decision, gene = player.decide(player, neighbors) if decision then player.action = decision player.last_gene = gene goblin_decisions[decision](player, dtime) else if not player.action then player.action = 1 end end
pos = player.pos
if map[pos.x][pos.z][2] then table.insert(player.food, random(2,5)) map[pos.x][pos.z][2] = false if player.closest_food and player.closest_food.x == pos.x and player.closest_food.z == pos.z then player.closest_food = nil end end
if player.hunger < 0 then player.hunger = 0 player.hp = player.hp - 1 end player.fitness = player.fitness + player:score() return false else return true endend
local function print_terrain(map, player) local s = string.rep("#",42).."\n" local line, cell local p = player.pos
for i=1,40 do line = "#" for j=1,40 do if p.x == i and p.z == j then cell = "i" elseif map[i][j][1] == nil then cell = "O" elseif map[i][j][1] == p.y and map[i][j][2] == true then cell = "T" elseif map[i][j][1] == p.y then cell = "▒" elseif map[i][j][1] - p.y == -1 then cell = "▓" elseif map[i][j][1] - p.y == 1 then cell = "░" elseif map[i][j][1] - p.y < -1 then cell = "█" elseif map[i][j][1] - p.y > 1 then cell = " " else cell = "?" end line = line..cell end s = s..line.."#\n" end s = s..string.rep("#",42) return send
local modifier = function(turn, max_turn) return (1 + turn/max_turn) end
local function turn_log_goblin(player, map, gen_stats, turn) local s = "\n\nBest creature: "..gen_stats.champ.."\tBest creature fitness: "..truncate(gen_stats.max_peak).."\n" s = s.."Best creature lifespan: "..(gen_stats.lifespan or "None").."\tBest creature gen: "..(gen_stats.gen or "None").."\n" s = s.."Last gen average: "..truncate(gen_stats.avg).."\t\tLast gen peak: "..truncate(gen_stats.peak).."\n\n" s = s.."Current gen: "..gen_stats.num.."\t\tCurrent creature: "..player.id.."\n" s = s.."Turn: "..(turn < 10 and "0" or "")..turn.."\t\tCreature name: "..player.name.."\n" s = s.."Action: "..goblins.actions[player.action].."\t\tFocus: "..player.focus.."\n" s = s.."Gene: "..(player.last_gene or "None").."\t\tFitness: "..truncate(player.fitness * modifier(turn, turn_max)).."\n\n"
s = s..print_terrain(map, player) return s, 0.008end
local function gen_log_goblin(num, gen_stats) local s = "\n\nBest creature: "..gen_stats.champ.."\tBest creature fitness: "..truncate(gen_stats.max_peak).."\n" s = s.."Best creature lifespan: "..(gen_stats.lifespan or "None").."\tBest creature gen: "..(gen_stats.gen or "None").."\n" s = s.."Last gen average: "..truncate(gen_stats.avg).."\t\tLast gen peak: "..truncate(gen_stats.peak).."\n\n"end
local function map_checks_goblin(player, map) if random() <= 0.05 then map[random(1,40)][random(1,40)][2] = true end
for i=1,40 do for j=1,40 do if map[i][j][1] and map[i][j][2] == true then local pos = {x=i, y=map[i][j][1], z=j} if not pos.y then error("No pos.y in "..i..","..j) end if not player.pos.y then error("No player.pos.y") end if player.closest_food and not player.closest_food.y then error("No player.closest_food.y") end if not player.closest_food or distance(player.pos, pos) < distance(player.pos, player.closest_food) then player.closest_food = pos--{x=pos.x, y=pos.y, z=pos.z} end end end endend
local function gen_stats_update(champ, gen_stats) gen_stats.champ = champ.name gen_stats.peak = champ.fitness gen_stats.lifespan = champ.lifespanend
local function global_log(gen_stats) local s = string.rep("-", 42).."\n"..string.rep("-", 42).."\n\n" s = s.."Last champion: "..gen_stats.champ.."\n" s = s.."From generation "..gen_stats.gen.."\tTotal generations: "..gen_stats.num.."\n" s = s.."Fitnes score: "..gen_stats.max_peak.."\tLifespan: "..(gen_stats.lifespan or "None").."\n" s = s.."\n"..string.rep("-", 42).."\n"..string.rep("-", 42).."\n"
return send
local function train() local input = false local option = input and 0 or 10000 local offset = 0 local population = {} repeat local gens_option = tonumber(option) if gens_option and gens_option > 0 then local elapsed = os.clock() local stats = { champ = "None", lifespan = 0, gen = 0, }
population, offset = trainer.train({ dims = {x=40, y=10, z=40}, output = input, gens = gens_option, fill_cell = fill_cell_goblin, fill_creature = fill_goblin, reset_creature = reset_goblin, decision_managers = goblin_decisions, manage_creature = manage_goblin, clone = goblins.clone, map_checks = map_checks_goblin, turn_log = turn_log_goblin, gen_log = gen_log_goblin, training_log = global_log, fitness_threshold = 40, fitness_modifier = modifier, max_turn = turn_max, popul = population, custom_stats = gen_stats_update, gen_stats = stats, to_file = file_tag, top_size = 4, }, offset)
if not minetest then elapsed = os.clock() - elapsed
print(global_log(stats)) print() print("Elapsed time: ", elapsed) end end
if minetest or not input then option = "q" else print("Please enter number of gens to simulate:") option = io.read() end until option == "q" or option == "Q"end
train()