我试图在Python中创建基于代理的模型。对于环境,我一直在使用MxN大小的numpy数组。每个像素代表一片土地。我想为每个土地分配一个所有者,以便创建的较大的块是连续的。理想情况下,我希望能够指定更大的块数。
我在尝试生成随机地图时非常困难。我已经能够将一个非常粗糙的解决方案合并在一起,但它仍然存在一些重大缺陷。我认为在接受这种命运之前我会引出其他人的想法。
答案 0 :(得分:1)
无法拒绝尝试,所以这是尝试使用scipy.ndimage.grey_dilation
似乎相当快。 grey_dilation
使用"结构元素扩展区域" - "增长内核"在下面的代码中。我没有任何关于他们对这个过程给予多少控制的经验,但他们是你可以玩的东西:
import numpy as np
from scipy import ndimage
growth_kernels = """
010 000 010 111
111 111 010 111
010 000 010 111
"""
growth_kernels = """
555555555 543212345
444444444 543212345
333333333 543212345
222222222 543212345
111101111 543202345
222222222 543212345
333333333 543212345
444444444 543212345
555555555 543212345
"""
def patches(shape, N, maxiter=100):
# load kernels
kernels = np.array([[[int(d) for d in s] for s in l.strip().split()]
for l in growth_kernels.split('\n')
if l.strip()], np.int)
nlev = np.max(kernels) + 1
# special case for binary kernels
if nlev == 2:
kernels = 2 - kernels
nlev = 3
kernels = -kernels.swapaxes(0, 1) * N
key, kex = kernels.shape[1:]
kernels[:, key//2, kex//2] = 0
# seed patches leave a gap between 0 and the first patch
out = np.zeros(shape, int)
out.ravel()[np.random.choice(out.size, N)] = np.arange((nlev-1)*N+1, nlev*N+1)
# shuffle labels after each iteration, so larger numbers do not get
# a systematic advantage
shuffle = np.arange((nlev+1)*N+1)
# also map negative labels to zero
shuffle[nlev*N+1:] = 0
shuffle_helper = shuffle[1:nlev*N+1].reshape(nlev, -1)
for j in range(maxiter):
# pick one of the kernels
k = np.random.randint(0, kernels.shape[0])
# grow patches
out = ndimage.grey_dilation(
out, kernels.shape[1:], structure=kernels[k], mode='constant')
# shuffle
shuffle_helper[...] = np.random.permutation(
shuffle[(nlev-1)*N+1:nlev*N+1])
out = shuffle[out]
if np.all(out):
break
return out % N
res = patches((30, 80), 26)
print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