我有一个二维细胞自动机。在某些单元格中可能存在演员(代理人)。每个演员都是一个主题。我需要根据演员的细胞周围的9个单元移动actor。我想同时这样做,所以单元格中的actor(4,5)可以使用邻居单元格(3,4),(4,4),(5,4),(3,5),(5,5) ,(3,6),(4,6),(5,6),没有其他演员可以使用这个细胞。如果某个演员在他的邻居中有这些细胞,他必须等到第一个演员移动。但我想允许同时移动没有共同社区的演员。所以(4,5)中的演员可以与(10,5)中的演员同时移动,因为他们没有共同的邻居。
最佳解决方案是什么?你能提出一些建议吗?
答案 0 :(得分:1)
粗略的想法如下。
请注意,Actor开始移动的单元格在下面的代码中不受保护。 此外,如果每个单元格都已填充,您会期望有什么?
import java.util.ArrayList;
import java.util.List;
public class CellularAutomata {
public static void main(String ... args) throws InterruptedException {
final int rows = 5;
final int cols = 5;
Cell[][] matrix = new Cell[rows][cols];
List<Actor> actors = new ArrayList<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = new Cell();
//populate actors
if ((i + j) % 2 == 0) {
Actor actor = new Actor(matrix, i, j);
actor.setName(String.format("Actor %d %d", i, j));
actors.add(actor);
}
}
}
for (Actor actor : actors) {
actor.start();
}
for (Actor actor : actors) {
actor.join();
}
}
public static class Cell {}
public static class Actor extends Thread {
private final static int[][] circleMoves = {
{-1, -1}, {-1, 0}, {-1, 1}
, {0, 1}, {1, 1}, {1, 0}
, {1, -1}, {0, -1}, {0, 0}
};
private final Cell[][] matrix;
private final int row;
private final int col;
public Actor(Cell[][] matrix, int row, int col) {
this.matrix = matrix;
this.row = row;
this.col = col;
}
@Override
public void run() {
for (int i = 0; i < circleMoves.length; i++) {
int nextRow = row + circleMoves[i][0];
int nextCol = col + circleMoves[i][1];
if (nextRow >= 0 && nextRow < matrix.length
&& nextCol >= 0 && nextCol < matrix[nextRow].length) {
Cell targetCell = matrix[nextRow][nextCol];
System.out.println(Thread.currentThread().getName() + " waiting for cell (" + nextRow + ";" + nextCol + ")");
synchronized (targetCell) {
try {
System.out.println(Thread.currentThread().getName() + " moved to cell (" + nextRow + ";" + nextCol + ")");
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
}
}
}
}