我正在使用setTimeout
来查看强化学习算法,并对其行为感到好奇。
以下代码显示了while aop中使用的setTimeout。循环的每次迭代都应使用state
的版本,该版本已由前一次迭代修改。是否有可能第二次调用setTimeout将在第一次调用之前发生,并且state的值将无法正确更新?我问的部分是因为我认为我实际上已经注意到了一些奇怪的行为,当延迟时间太小时,调用doLearningInnerLoop的控制台日志会出现故障(即时间将= 1 4 3 2 5 6 ..等等)。
var doLearningInnerLoop = function(time, observedData, stateHolder, domain, sampleAction, selectModel, numSamples, depth, discount, stateQueries) {
console.log("new Round, time = " + time);
var state = stateHolder.state;
var currentModel = selectModel(observedData, 10, stateQueries);
var bestAction = sparseSampleMcmc(depth, numSamples, discount, currentModel, state, sampleAction, stateQueries);
var newStateReward = domain.executeAction(bestAction, stateQueries);
observedData.push(bestAction, newStateReward[1], newStateReward[0]);
stateHolder.state = newStateReward[0];
}
var doLearningLoops = function(time, observedData, state, domain, sampleAction, selectModel, numSamples, depth, discount, stateQueries) {
stateHolder = {};
stateHolder.state = state;
while(stateQueries['canContinue']['codeAsFunction'](time, stateHolder.state) === true) {
setTimeout(doLearningInnerLoop, 1000, time, observedData, stateHolder, domain, sampleAction, selectModel, numSamples, depth, discount, stateQueries);
time += 1;
}
return state;
}
答案 0 :(得分:0)
我相信我发现类似的setTimeout调用问题没有按预期顺序发生。
请参阅:Are equal timeouts executed in order in Javascript?
上面的答案是Firefox似乎正确地做到了(基于来源)但是其他人也有一些人反对这个问题(也许他们在不同的浏览器上)。