【发布时间】:2017-01-20 03:54:50
【问题描述】:
由于某种原因,Lua 告诉我这一行出错了:
for i=1, #set do
其中 set 是我制作的函数的二维表参数。 它给了我一个错误:
尝试获取 nil 值的长度(本地集)
奇怪的是,我运行这个函数超过 1000 次,但错误只出现一次。我不太确定为什么。 任何帮助,将不胜感激。而且我还可以打印 set 中的值,它们会显示出来,我认为这是一个可能的错误。
编辑:这是功能:(或它的重要部分)
function GeneticTrainer:evaluate(id,correct,set)
local returny=0
local net=self.Networks[id]
for i=1, #set do
-- print(i..k[i])
net:Update(set[i])
local error= math.abs(correct[i]-net.output[1])
returny = returny + (1-(error/10))
end
这就是我所说的:
function GeneticTrainer:Evolve(Problem,Set)
local A=math.random(1,self.Population)
local B=math.random(1,self.Population)
if A==B then
B=math.random(1,self.Population)
end
local AFitness = self:evaluate(A,Problem,Set)
local BFitness = self:evaluate(B,Problem,Set)
这就是我所说的:
BinarySet={{0,0},{1,0},{0,1},{1,1}}
for i=1, iterations do
GATrainer:Evolve(Target,BinarySet) end
【问题讨论】:
-
显然,
set是nil。如果您不发布相关代码(最好是minimal reproducible example),就不可能知道为什么会发生这种情况。 -
您的
net:Update是否修改了set或在它仍在循环中迭代时更改项目计数?