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

View file

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

View file

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