Fix loading pools. Move input modification outside main loop so we can

use the timer for Promise synchronization.
This commit is contained in:
Empathic Qubit 2021-05-01 23:17:59 -04:00
parent 91cf9ec489
commit d90c03c3af
3 changed files with 30 additions and 37 deletions

View file

@ -14,18 +14,11 @@ local hasThreads =
config.NeatConfig.Threads > 1
-- Only the parent should manage ticks!
-- I'm not terribly thrilled with this logic
if hasThreads then
callback.register('timer', function()
callback.register('timer', function()
Promise.update()
set_timer_timeout(1)
end)
set_timer_timeout(1)
else
callback.register('input', function()
Promise.update()
end)
end
end)
set_timer_timeout(1)
local Runner = nil
if hasThreads then
@ -453,7 +446,8 @@ local function loadFile(filename)
return
end
local contents = file:read("*all")
local ok, obj = serpent.load(libDeflate:DecompressDeflate(contents:sub(11, #contents - 8)))
local decomp, _ = libDeflate:DecompressDeflate(contents:sub(11, #contents - 8))
local ok, obj = serpent.load(decomp)
if not ok then
message("Error parsing pool file", 0x00990000)
return
@ -465,8 +459,9 @@ end
local function savePool()
local filename = _M.saveLoadFile
writeFile(filename)
return writeFile(filename):next(function()
message(string.format("Saved \"%s\"!", filename:sub(#filename - 50)), 0x00009900)
end)
end
local function loadPool()
@ -716,7 +711,7 @@ local function mainLoop(currentSpecies, topGenome)
if saveRequested then
saveRequested = false
savePool()
return savePool()
end
if topRequested then

View file

@ -4,9 +4,12 @@ local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1")
local Promise = dofile(base.."/promise.lua")
-- Only the parent should manage ticks!
callback.register('input', function()
callback.register('timer', function()
Promise.update()
set_timer_timeout(1)
end)
set_timer_timeout(1)
local Runner = dofile(base.."/runner.lua")
local serpent = dofile(base.."/serpent.lua")

View file

@ -379,7 +379,20 @@ local function evaluateNetwork(_M, network, inputs, inputDeltas)
return outputs
end
local controller = nil
local controller = {}
local function updateController()
for b=0,#config.ButtonNames - 1,1 do
if controller[b] then
input.set(0, b, 1)
else
input.set(0, b, 0)
end
end
end
local frame = 0
local lastFrame = 0
local function evaluateCurrent(_M)
local genome = _M.currentSpecies.genomes[_M.currentGenomeIndex]
@ -396,14 +409,6 @@ local function evaluateCurrent(_M)
controller[4] = false
controller[5] = false
end
for b=0,#config.ButtonNames - 1,1 do
if controller[b] then
input.set(0, b, 1)
else
input.set(0, b, 0)
end
end
end
local function fitnessAlreadyMeasured(_M)
@ -421,9 +426,6 @@ local function rewind()
return promise
end
local frame = 0
local lastFrame = 0
local function rewound()
frame = 0
lastFrame = 0
@ -547,14 +549,6 @@ local function mainLoop(_M, genome)
evaluateCurrent(_M)
end
for b=0,#config.ButtonNames - 1,1 do
if controller[b] then
input.set(0, b, 1)
else
input.set(0, b, 0)
end
end
game.getPositions()
local timeoutConst = 0
if game.vertical then
@ -861,6 +855,7 @@ local function run(_M, species, generationIdx, genomeCallback)
end)
register(_M, 'input', function()
frame = frame + 1
updateController()
processFrameAdvanced(_M)
saveLoadInput(_M)
end)