在ruby中创建任意深度的嵌套循环
我想写一个ruby程序,它可以在任意多个维度上跨越空间在ruby中创建任意深度的嵌套循环,ruby,Ruby,我想写一个ruby程序,它可以在任意多个维度上跨越空间 search_space = search_spaces_1Ds.shift.product(*search_space_1Ds) search_space.map do |vec| # calculate something with vec end 在三维空间中,我正在做的事情如下所示: x_range = (-1..1) y_range = (-1..1) z_range = (-1..1) step_size = 0.01
search_space = search_spaces_1Ds.shift.product(*search_space_1Ds)
search_space.map do |vec|
# calculate something with vec
end
在三维空间中,我正在做的事情如下所示:
x_range = (-1..1)
y_range = (-1..1)
z_range = (-1..1)
step_size = 0.01
x_range.step(step_size) do |x|
y_range.step(step_size) do |y|
z_range.step(step_size) do |z|
# do something with the point x,y,z
end
end
end
n = 5 # 5 dimentions
x = (-1..1).to_a
x.product(*[x]*(n-1)).each {|i| p i}
我想对
n
维度执行相同的操作如果范围不太大,可以执行以下操作:
x_range = (-1..1)
y_range = (-1..1)
z_range = (-1..1)
step_size = 0.01
x_range.step(step_size) do |x|
y_range.step(step_size) do |y|
z_range.step(step_size) do |z|
# do something with the point x,y,z
end
end
end
n = 5 # 5 dimentions
x = (-1..1).to_a
x.product(*[x]*(n-1)).each {|i| p i}
结果:
[-1, -1, -1, -1, -1]
[-1, -1, -1, -1, 0]
[-1, -1, -1, -1, 1]
[-1, -1, -1, 0, -1]
[-1, -1, -1, 0, 0]
[-1, -1, -1, 0, 1]
[-1, -1, -1, 1, -1]
[-1, -1, -1, 1, 0]
[-1, -1, -1, 1, 1]
[-1, -1, 0, -1, -1]
[-1, -1, 0, -1, 0]
# skipped
这是我想到的第一件事:
def enumerate(nDimens, bottom, top, step_size)
bottom = (bottom / step_size).to_i
top = (top / step_size).to_i
range = (bottom..top).to_a.map{ |x| x * step_size }
return range.repeated_permutation(nDimens)
end
stepper = enumerate(4, -1, 1, 0.1)
loop do
puts "#{stepper.next()}"
end
这将产生:
[-1.0, -1.0, -1.0, -1.0]
[-1.0, -1.0, -1.0, -0.9]
[-1.0, -1.0, -1.0, -0.8]
# Lots more...
[1.0, 1.0, 1.0, 0.8]
[1.0, 1.0, 1.0, 0.9]
[1.0, 1.0, 1.0, 1.0]
这假设所有维度都具有相同的范围,但如果由于某些原因无法保持,则很容易进行调整。这就是您可以做的。。。下面是一个迭代器示例
#next(l[dim] array of lower ranges ,h[dim] = upper ranges, step[dim], dim = dimensions -1, curr[dim] = current state in dim dimensions )
def nextx(l ,h, step, dim, curr)
x = dim
update= false
while (update==false)
if curr[x] == h[x]
if x > 0
x = x-1
else
exit
end
else
curr[x]= curr[x]+step[x]
while (x < dim)
x = x+1
curr[x] = l[x]
end
update = true
end
end
return curr
end
l = [0,0,0]
h = [3,3,3]
step = [1,1,1]
currx = [0,0,2]
i = 0
while i < 70
currx = nextx(l, h, step, 2, currx)
puts currx.inspect
i=i+1
end
#下一步(l[dim]下限数组,h[dim]=上限,步长[dim],dim=维度-1,curr[dim]=维度中的当前状态)
def nextx(长、高、阶跃、变暗、当前)
x=dim
更新=错误
while(update==false)
如果curr[x]==h[x]
如果x>0
x=x-1
其他的
出口
结束
其他的
当前[x]=当前[x]+步进[x]
while(x
递归可以轻松完美地解决此类问题。下面的代码适用于任何数量的尺寸,也适用于各种长度的范围
def traversal(ranges, step_size, conjunction = [], &blk)
ranges[0].step(step_size) do |x|
conjunction.push(x)
if ranges.size > 1
traversal(ranges[1..-1], step_size, conjunction, &blk)
else
blk.call(conjunction) if block_given?
conjunction.pop
end
end
conjunction.pop
end
运行:(尺寸=4,长度=3,3,4,2)
输出:(共72分,为3*3*4*2)
这通常在探索搜索空间的算法中遇到。
do
循环从一维范围创建产品空间
首先将需要的范围打包到一个阵列中,例如
search_space_1Ds = [x_range.step(step_size).to_a, y_range.step(step_size).to_a, z_range.step(step_size).to_a]
然后,以下内容将适用于任意数量的维度
search_space = search_spaces_1Ds.shift.product(*search_space_1Ds)
search_space.map do |vec|
# calculate something with vec
end
这个实现不仅简洁,而且非常清楚你的算法在做什么;枚举作为一维搜索空间的乘积空间创建的搜索空间。
product
是范围的未定义方法