需要帮助:本地化了一个错误,但仍然无法消除它



下面的程序是对一个有效程序的实质性简化,但尽管付出了数小时的努力,我还是无法确定为什么会出现以下错误:一切都如预期的那样工作;如果行为[2,0]>t然后goright(大脑,刺激(结束";执行时;estimulus.each{|n|刺激[n,0]=0}";不会像在其他情况下那样将刺激向量的所有元素重置为0。取而代之的是;1〃;通过";对于c in(0..$选择(";从而在只期望一个时产生双重行为(左转弯和右转弯(。我已经评论了所有我认为无关紧要的事情,但它仍然会发生。

class Matrix
def []=(i, j, x)
@rows[i][j] = x
end
end #code to allow putting individual elements in matrix at i,j
brain=  Matrix[ [90,0,0,0,0,0,0,0,0,0,0],
[0,90,0,10,10,10,10,0,0,0,0],
[0,0,90,10,10,10,10,0,0,0,0] ]
longmem=Matrix[ [90,0,0,0,0,0,0,0,0,0,0],
[0,90,0,10,10,10,10,0,0,0,0],
[0,0,90,10,10,10,10,0,0,0,0] ]
stimulus=Matrix.column_vector([0,0,0,0,0,0,0,0,0,0,0])
behavior=Matrix.column_vector([0,0,0])
t=89 # t=threshold
# decay_rate=2
$Stimmax=10
$Behavmax=2
$Choices=2
# begin defining behavioral methods
def learn(ix, brain, stimulus)
psp=20
for j in (8..$Stimmax)
if brain[ix,j] > 0 then brain[ix,j]+= 20 end #modified hunting for bug
end # for j
end # learn
#def positive_fixer(brain,stimulus,longmem,energy)
#   reinf=0.9
#   for i in (0..$Behavmax)
#   for j in (7..$Stimmax)
#      if longmem[i,j]>0 then longmem[i,j]+=(reinf*(brain[i,j]-longmem[i,j])).round end
#    end 
#    end 
#    learn(0, brain, stimulus)
#end #positive fixer comment out in bug hunt
def goleft(brain,stimulus)
puts "              Left Turn"
learn(1,brain,stimulus)
end # def left
def goright(brain,stimulus)
puts "              Right Turn"
learn(2,brain,stimulus)
end # def right
# end defining behavioral methods
# begin MAIN PROGRAM
for c in (0..$Choices)
stimulus.each {|n| stimulus[n,0]=0 }
# SET ALL STIMS TO O
puts "Should by all 0s"
stimulus.to_a.each {|r| puts r.inspect} # aid in bug hunt
stimulus[rand(1..2),0]= 1
#add print stimulus vector for debugging
puts "Should by just a single 1"
stimulus.to_a.each {|r| puts r.inspect} # aid in bug hunt
# memory decay
#for i in (0..$Behavmax)
#for j in (7..$Stimmax)
#  if brain[i,j]>longmem[i,j] then brain[i,j]+=-(brain[i,j]-longmem[i,j])/decay_rate end
#  if brain[i,j]<longmem[i,j] then brain[i,j]+=-1*((brain[i,j]+longmem[i,j])/decay_rate) end
#end #for j
#end #for i
# memory decay commented out in search for bug
behavior=brain*stimulus
if behavior[0,0] > t then positive_fixer(brain, stimulus, longmem, energy) end
if behavior[1,0] > t then goleft(brain,stimulus) end
if behavior[2,0] > t then goright(brain,stimulus) end
end #for c
puts
brain.to_a.each {|r| puts r.inspect}
# end main program```

行"estimulus.each{|n|刺激[n,0]=0}";不会像那样将刺激向量的所有元素重置为0

如何重置矢量的方法之一:

require 'matrix'
vector = Matrix.column_vector([1, 2, 3])
# => Matrix[[1], [2], [3]]
vector.each_with_index { |_, i| vector[i, 0] = 0 }
# => Matrix[[0], [0], [0]]

所以你需要使用each_with_index而不是each

相关内容

最新更新