public static void InitializeNeighbourhood2D(TwoDimCell cell, ICell[][] cells, int cellBoardSize) { int neighbourhoodWidth = cell.neighbourhoodSize.Neighbourhood1DRadius(); bool upperCell = cell.x < 0 + neighbourhoodWidth; bool rightCell = cell.y > cellBoardSize - 1 - neighbourhoodWidth; bool lowerCell = cell.x > cellBoardSize - 1 - neighbourhoodWidth; bool leftCell = cell.y < 0 + neighbourhoodWidth; bool VertMidCell = !rightCell && !leftCell; bool HoriMidCell = !upperCell && !lowerCell; bool safeCell = VertMidCell && HoriMidCell; if (safeCell) { Safe2DInitialize(cell, cells, neighbourhoodWidth); } else if (VertMidCell) { InitializeVerticalMiddle(cell, cells, neighbourhoodWidth, cellBoardSize, upperCell, lowerCell); } else if (HoriMidCell) { InitializeHorizontalMiddle(cell, cells, neighbourhoodWidth, cellBoardSize, rightCell, leftCell); } else { InitializeCornerCell(cell, cells, neighbourhoodWidth, cellBoardSize, upperCell, rightCell, lowerCell, leftCell); } }
public Test2DimCA() : base() { parameters = new CAParameters(NeighbourHoodSize: 5, CellStateCount: 2, CellWorldWidth: 10, MaxGeneration: 4); MakeStates(parameters.CellStateCount); seed = new float[10][]; seed[0] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[1] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[2] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[3] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[4] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[5] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[6] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[7] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[8] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; seed[9] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal = new float[10][]; goal[0] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[1] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[2] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[3] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[4] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[5] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[6] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[7] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[8] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; goal[9] = new float[] { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 }; if (seed.Length != parameters.CellWorldWidth || goal.Length != parameters.CellWorldWidth) { throw new FormatException("seed not compatible with experiment parameters" + seed.Length + " " + goal.Length); } cells = new TwoDimCell[parameters.CellWorldWidth][]; for (int i = 0; i < parameters.CellWorldWidth; i++) { cells[i] = new TwoDimCell[parameters.CellWorldHeight]; for (int j = 0; j < parameters.CellWorldWidth; j++) { cells[i][j] = new TwoDimCell(i, j, cells, parameters.NeighbourHoodSize); } } foreach (TwoDimCell[] cellRow in cells) { foreach (TwoDimCell cell in cellRow) { cell.InitializeNeighbourhood(); } } }
private static void InitializeVerticalMiddle(TwoDimCell cell, ICell[][] cells, int neighbourhoodWidth, int cellBoardSize, bool upperCell, bool lowerCell) { if (upperCell) { Upper2DInitialize(cell, cells, neighbourhoodWidth, cellBoardSize); } else if (lowerCell) { Lower2DInitialize(cell, cells, neighbourhoodWidth, cellBoardSize); } }
private static void InitializeHorizontalMiddle(TwoDimCell cell, ICell[][] cells, int neighbourhoodWidth, int cellBoardSize, bool rightCell, bool leftCell) { if (leftCell) { Left2DInitialize(cell, cells, neighbourhoodWidth, cellBoardSize); } else if (rightCell) { Right2DInitialize(cell, cells, neighbourhoodWidth, cellBoardSize); } }
private static void UpperLeft2DInitialize(TwoDimCell cell, ICell[][] cells, int neighbourhoodWidth, int cellBoardSize) { List <ICell> neighbourhood = new List <ICell>(); //width == 1 neighbourhood.Add(cells[cellBoardSize - 1][0]); neighbourhood.Add(cells[0][1]); neighbourhood.Add(cells[1][0]); neighbourhood.Add(cells[0][cellBoardSize - 1]); neighbourhood.Add(cells[0][0]); //width == 2 if (neighbourhoodWidth >= 2) { neighbourhood.Add(cells[cellBoardSize - 1][cellBoardSize - 1]); neighbourhood.Add(cells[cellBoardSize - 1][1]); neighbourhood.Add(cells[1][1]); neighbourhood.Add(cells[1][cellBoardSize - 1]); } cell.Neighbourhood = neighbourhood; }
private static void Right2DInitialize(TwoDimCell cell, ICell[][] cells, int neighbourhoodWidth, int cellBoardSize) { List <ICell> neighbourhood = new List <ICell>(); int i = cell.x; //width == 1 neighbourhood.Add(cells[i - 1][cellBoardSize - 1]); neighbourhood.Add(cells[i][0]); neighbourhood.Add(cells[i + 1][cellBoardSize - 1]); neighbourhood.Add(cells[i][cellBoardSize - 2]); neighbourhood.Add(cells[i][cellBoardSize - 1]); //width == 2 if (cellBoardSize >= 2) { neighbourhood.Add(cells[i - 1][cellBoardSize - 2]); neighbourhood.Add(cells[i - 1][0]); neighbourhood.Add(cells[i + 1][0]); neighbourhood.Add(cells[i + 1][cellBoardSize - 2]); } cell.Neighbourhood = neighbourhood; }
private static void Safe2DInitialize(TwoDimCell cell, ICell[][] cells, int neighbourhoodWidth) { List <ICell> neighbourhood = new List <ICell>(); int i = cell.x; int j = cell.y; //width == 1 neighbourhood.Add(cells[i - 1][j]); neighbourhood.Add(cells[i][j + 1]); neighbourhood.Add(cells[i + 1][j]); neighbourhood.Add(cells[i][j - 1]); neighbourhood.Add(cells[i][j]); //width == 2 if (neighbourhoodWidth == 2) { neighbourhood.Add(cells[i - 1][j - 1]); neighbourhood.Add(cells[i - 1][j + 1]); neighbourhood.Add(cells[i + 1][j + 1]); neighbourhood.Add(cells[i + 1][j - 1]); } cell.Neighbourhood = neighbourhood; }
private static void InitializeCornerCell(TwoDimCell cell, ICell[][] cells, int neighbourhoodWidth, int cellBoardSize, bool upperCell, bool rightCell, bool lowerCell, bool leftCell) { bool URCell = upperCell && rightCell; bool LRCell = rightCell && lowerCell; bool LLCell = lowerCell && leftCell; bool ULCell = leftCell && upperCell; if (URCell) { UpperRight2DInitialize(cell, cells, neighbourhoodWidth, cellBoardSize); } if (LRCell) { LowerRight2DInitialize(cell, cells, neighbourhoodWidth, cellBoardSize); } if (LLCell) { LowerLeft2DInitialize(cell, cells, neighbourhoodWidth, cellBoardSize); } if (ULCell) { UpperLeft2DInitialize(cell, cells, neighbourhoodWidth, cellBoardSize); } }
public static void InitializeNeighbourhood2D(TwoDimCell cell, ICell[][] cells) { NeighbourhoodInitializor2D.InitializeNeighbourhood2D(cell, cells, cells.GetLength(0)); }