【问题标题】:Cellular Automata returns all 0s [PYTHON]元胞自动机返回全 0 [PYTHON]
【发布时间】:2021-12-04 07:31:50
【问题描述】:

我已经编写了一个程序(见下文)作为 python 中的元胞自动机的实现。当我运行该程序时,我会收到一个随机种子噪声模式和已通过该程序运行的新模式。这 ”。”是零,“#”是一。问题是,当我运行程序时,我收到了随机噪声(看起来不错),但是当我收到平滑版本时,我只得到句点(零)。

app.py

import perlin

line = ""

noise = perlin.Noise(20,20, 420, 1)

print(" ")

for i in range(20):
    line += "="
print(line)

# Parses smoothed map
line = ""

print(" ")
for i in range(len(noise.map)):
    for j in range(len(noise.map[i])):
        if noise.map[i][j] == 0:
            line += "."
        else:
            line += "#"
    print(line)
    line = ""

perlin.py

import numpy as np

class Noise:
    def inbounds(self,x,y):
        if (x >= 0 and x <= self.width) and (y >=0 and y <= self.height):
            return True
        else:
            return False

    def smooth(self,iterations):
        for i in range(iterations):
            temp_map = self.map
            for j in range(self.height):
                for k in range(self.width):
                    neighbor_wall_count = 0
                    for y in range(j-1,j+1):
                        for x in range(k-1, k+1):
                            if self.inbounds(x,y):
                                if y != j or x != k:
                                    if temp_map[y][x] == 1:
                                        neighbor_wall_count += 1
                            else:
                                neighbor_wall_count += 1
                    if neighbor_wall_count > 3:
                        self.map[j][k] = 1
                    else:
                        self.map[j][k] = 0


    def __init__(self, width, height, seed, iter):
        # Sets all of the self variables
        self.width = width
        self.height = height
        self.seed = seed
        np.random.seed(self.seed)
        w, h = self.width, self.height
        # Declares the pattern map list
        map = [[0 for i in range(w)] for j in range(h)]

        #Generator
        for y in range(h):
            for x in range(w):
                map[y][x] = np.random.randint(0,2)
        self.map = map

        # Parser
        line = ""
        for i in range(len(self.map)):
            for j in range(len(self.map[i])):
                if self.map[i][j] == 0:
                    line += "."
                else:
                    line += "#"
            print(line)
            line = ""
        self.smooth(iter)

输出

#..###.#....#...#..#
..#####..##.#.###..#
###.###.###..####...
###....##..###.#.#..
#.#.###..##...##..##
#......#..#..#.##..#
.###.#.#.##..#...##.
##...##....#####..#.
#..##...#....###..#.
#.....#.##.####...##
#..#..#.#.##..##..#.
.#.###.###...#..#..#
...#.##....#..#.##..
#.#..#.#.#.###..####
.#..#....#..##.###..
#..#.#.#........###.
.#####..#.#..####...
##..#.#.##.##..#..##
##.#.#.##.####.#..##
####.........##..##.
 
====================
 
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

【问题讨论】:

  • 哦,天哪...它是如此简单,但我花了这么多时间。 python 范围已排除。 for y in range(j-1,j+2)x 相同)。您还必须更正 inboundsx &lt; self.widthy 相同)
  • 我做了更改,但不是全零,而是全零。
  • 你必须调整neighbor_wall_count
  • 天哪。绝对的传奇。它太小了,我看不见。救生员,非常感谢!
  • 你能把这个作为答案发给我吗?

标签: python random noise perlin-noise cellular-automata


【解决方案1】:

python ranges 中的 stop 参数被排除在范围之外(范围

对于正步,范围 r 的内容由 公式 r[i] = start + step*i 其中 i >= 0 且 r[i]

对于负步,范围的内容仍然由 公式 r[i] = start + step*i,但约束是 i >= 0 并且 r[i] > 停止。

您可以通过更改该测试的值来调整平滑度(例如此处为 3):

if neighbor_wall_count &gt; 3:

【讨论】:

    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多