【问题标题】:Cellular Automata Checking Neighbors元胞自动机检查邻居
【发布时间】:2016-11-03 14:03:52
【问题描述】:

我有一个非常简单的问题,但我似乎无法弄清楚。我认为这是与元胞自动机中的邻居检查有关的逻辑错误。这是我的代码,每秒运行一次,用于增长和检查邻居:

public void grow(){
    Cell[][] next = new Cell[100][100];
    for(int row = 0; row < (SIZE_X/SIZE); row++){
        for(int col = 0; col < (SIZE_Y/SIZE); col++){
            Cell cell = grid[row][col]; 
            Cell nCell = grid[row][col]; // gets 

            if(cell != null){
                int amount = neighbors(row, col); // find out how many neighbors are ALIVE/ON

                if(cell.isOn() == true && amount != 3) // if the current cell is on but doesnt have 3 alive neighbors, it gets turned off
                    nCell.onOff(false);
                else if(cell.isOn() == false && (amount >= 1 && amount <= 4)) // if it is off and has 1-5 alive neighbors it gets turned on
                    nCell.onOff(true);

                next[row][col] = nCell;
            }
        }
    }
    grid = next;
}

public int neighbors(int row, int col){ // checks the amount of neighbors that are ALIVE/ON
    int amount = 0;

    for(int r = row-1; r <= row+1; r++){ // stepping through a 3x3 area of the grid, which surrounds the selected block
        for(int c = col-1; c <= col+1; c++){

            // clamp
            if((r > 0 && r < 99) && (c > 0 && c < 99)){
                if(grid[r][c].isOn() == true && (r != row && c != col)) // checks if the current neighbor is ALIVE/ON 
                    amount++; // if it is then add one to the count
            }
        }
    }
    return amount;
}

我在我的元胞自动机中使用了一个简单的 12345/3(生存/出生)规则。

目前的问题是我有一个 100x100 的网格,中心有一个 10x10 的 ALIVE/ON 单元格空间。我的代码运行一次后,所有的单元格都死掉了。

如果有人需要更多信息,请随时提出。提前致谢!

【问题讨论】:

    标签: java logic cellular-automata


    【解决方案1】:

    有一些问题,但我不确定结果如何一切都死了。

    第一个问题:

    if(cell.isOn() == true && amount != 3) // if the current cell is on but doesnt have 3 alive neighbors, it gets turned off
      cell.onOff(false);
    if(cell.isOn() == false && (amount >=1 && amount <= 5)) // if it is off and has 1-5 alive neighbors it gets turned on
      cell.onOff(true);
    

    假设该单元格有 1 个实时邻居。然后第一个子句将其关闭,然后第二个子句将其重新打开。所以“死亡”规则不起作用。解决方法:使用else if

    第二个问题:

    您在同一个地方检查所有内容。例如该字段是:

    **.
    *.*
    

    我们检查单元格(0,0),然后字段是: .*. .

    然后在检查所有第一行之后,该字段是: ... . 然后每个人都死了。 :) 解决方案:首先检查每个单元格的邻居编号并将其存储在每个单元格中。只有在那之后才能按规则打开和关闭它们。

    第三个问题:在场边缘,一些邻居被检查了两次。例如单元格(0,0) 处于打开状态,我们检查邻居单元格(0,1)。首先我们尝试将(-1, 0) 更改为(0,0) 并添加到数量。稍后(0,0) 再次被检查为left neihbour 并再次添加到数量。

    【讨论】:

    • 抱歉回复晚了。所以我实施了一些改变,解决了你谈到的问题。然而,另一个问题出现了,现在它在起始块的右侧形成了一个巨大的金字塔,并在看似没有韵律或理由的情况下慢慢长大。 Iv 更新了我的代码
    【解决方案2】:
    if(grid[r][c].isOn() == true && (r != row && c != col))
    

    在这里,您将只考虑与您的中心单元格不在同一行和列上的邻居。因此,您考虑的是 4 个单元而不是 8 个。 你可能是这个意思:

    if(grid[r][c].isOn() == true && (r != row || c != col)
    

    【讨论】:

      【解决方案3】:

      所以我实施了一些改变,解决了你所说的问题 关于。然而,另一个问题出现了,现在它产生了一个 起始块右侧的巨大金字塔并缓慢增长 似乎没有韵律或理由。 iv 更新了我的代码

      Cell 是一个类吗?因为您直接从网格分配 nCell。如果您通过引用来执行此操作,您还会更改旧网格视图中单元格的值。这样做会创建倾向于扩散到网格右下角的模式。

      编辑:刚刚意识到这是Java,上面的可能不是真的。如果是这种情况,请忽略它。

      EDIT2:另外:

                       if(cell.isOn() == true && amount != 3) // if the current cell is on but doesnt have 3 alive neighbors, it gets turned
       off
                           nCell.onOff(false);
                       else if(cell.isOn() == false && (amount >= 1 && amount <= 4)) // if it is off and has 1-5 alive neighbors it gets turned on
                          nCell.onOff(true);
      

      这不符合 1-5 生存 3 出生规则。

      【讨论】:

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