简体   繁体   English

元胞自动机检查邻居

[英]Cellular Automata Checking Neighbors

I have a pretty simple problem but I cant seem to figure it out. 我有一个非常简单的问题,但似乎无法解决。 I believe it is a logic error having to do with the checking of neighbors in cellular automata. 我相信这是逻辑错误,与检查细胞自动机中的邻居有关。 Here is my code that runs once a second for growing and checking neighbors: 这是我的代码,每秒运行一次,用于增长和检查邻居:

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;
}

Im using a simple 12345/3(Survival/Birth) rule in my Cellular Automata. 我在“细胞自动机”中使用了简单的12345/3(生存/出生)规则。

The problem currently is I have a 100x100 grid with a 10x10 space of ALIVE/ON cells in the center. 当前的问题是我有一个100x100的网格,中间有10x10的ALIVE / ON单元格。 After my code runs once, all the cells die. 我的代码运行一次后,所有单元格都死亡。

If anyone needs more information, feel free to ask. 如果有人需要更多信息,请随时询问。 Thanks in advance! 提前致谢!

There are some problems but I'm not sure how everything is dead as result. 有一些问题,但是我不确定所有结果如何。

First problem: 第一个问题:

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);

Let's assume the cell have 1 live neighbour. 假设该小区有1个居住邻居。 Then first clause turns it off and then second one turns it back on. 然后第一个子句将其关闭,然后第二个子句将其重新打开。 So "death" rule doesn't work. 因此,“死亡”规则不起作用。 Solution: use else if . 解决方案: else if使用else if

Second problem: 第二个问题:

You check everything in the same place. 您在同一位置检查所有内容。 For example the field is: 例如,该字段是:

**.
*.*

We check cell (0,0) and then field is: .*. 我们检查单元格(0,0) ,然后字段为:。*。 .

Then after all first row is checked the field is: ... . 然后,所有第一行被选中后场是:...。 Then everyone dies. 然后每个人都死了。 :) Solution: first check neighbour number for each cell and store it in each cell. :)解决方案:首先检查每个单元格的邻居编号并将其存储在每个单元格中。 Only after that switch them on and off by the rules. 只有在那之后,才根据规则打开和关闭它们。

And third problem: on field edges some neighbours are checked twice. 第三个问题:在田野边缘,对某些邻居进行了两次检查。 For example cell (0,0) is on and we check neighbours for cell (0,1) . 例如,像元(0,0)处于打开状态,我们检查像元(0,1)邻居。 First we try (-1, 0) that is changed to (0,0) and added to amount. 首先,我们尝试将(-1, 0)更改为(0,0)并添加到数量中。 Later (0,0) is checked again as left neihbour and added to amount again. 稍后(0,0)再次检查为左neihbour,并再次添加到数量中。

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

Here you will only consider neighbors that aren't on the same row AND column than your center cell. 在这里,您将只考虑与中心单元格不在同一行AND列中的邻居。 As a result, youŕe considering 4 cells instead of 8. You probably meant this: 结果,您考虑的是4个单元格而不是8个单元格。您可能的意思是:

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

So I implemented some changed which solved the problems you talked about. 因此,我进行了一些更改,从而解决了您所讨论的问题。 Another problem has popped up though, which is now it makes a massive pyramid to the right of the starting blocks and slowly grows out with seemingly no rhyme or reason. 但是,又出现了另一个问题,这是它在起跑线的右边构成了一个巨大的金字塔,并且似乎没有任何韵律或理由而逐渐长大。 Iv updated my code IV更新了我的代码

Is Cell a class? 细胞是一堂课吗? Because you assign nCell directly from the grid. 因为您直接从网格分配nCell。 If you do this by reference, you also change the value of the cell in the old gridview. 如果您通过引用执行此操作,则还可以更改旧的gridview中的单元格的值。 Doing this will create patterns which have a tendency to spread to the bottom-right corner of the grid. 这样做会创建易于扩散到网格右下角的图案。

EDIT: Just realized this is Java, and the above may not be true there. 编辑:刚刚意识到这是Java,并且上面可能不正确。 Disregard this if it is the case. 如果是这种情况,请忽略。

EDIT2: Also: 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);

This doesn't mesh with 1-5 survival 3 birth rule. 这与1-5生存3的出生规则不符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM