简体   繁体   English

并发细胞自动机演员移动

[英]Concurrent Cellular Automata actors moving

I have a two-dimensional cellular automata. 我有一个二维细胞自动机。 In some cells there may be an actor (agent). 在某些单元格中可能存在一个参与者(代理)。 Each actor is a thread. 每个演员都是一个线程。 I need to move actor based on the 9 cells around the actor's cell. 我需要根据actor单元周围的9个单元来移动actor。 I want to do this concurrently so the actor in a cell (4,5) can use neighbors cells (3,4), (4,4), (5,4), (3,5), (5,5), (3,6), (4,6), (5,6) and no other actors can use this cells. 我想同时执行此操作,以便单元格(4,5)中的actor可以使用邻居单元格(3,4),(4,4),(5,4),(3,5),(5,5) ,(3,6),(4,6),(5,6),其他任何参与者都不能使用此单元格。 If some actor has these cells in his neighborhood, he has to wait until the first actor moved. 如果某个演员在附近有这些小室,他必须等到第一个演员搬走。 But I want to allow moving the actor, who has no common neighborhood, concurrently. 但我想允许同时移动没有共同邻居的演员。 So the actor in (4,5) can move at the same time as an actor in (10,5) because they have no common neighborhood. 因此(4,5)中的演员可以与(10,5)中的演员同时移动,因为他们没有共同的邻居。

What is the best solution of that? 最好的解决方案是什么? Can you propose something? 你能提出一些建议吗?

The rough idea is below. 大致的想法如下。

  1. Create matrix of Cell objects, which will be used for synchronization 创建Cell对象矩阵,该矩阵将用于同步
  2. Assign Actors to the cells 将Actor分配给单元
  3. Whenever Actor moves to another cell it must get a monitor on the cell 每当Actor移动到另一个单元时,都必须在该单元上安装一个监视器

Note that the cell, from which Actor starts to move, is not protected in the code below. 请注意,Actor开始从其移动的单元格在下面的代码中不受保护。 Also, what would you expect if every cell is populated has an Actor? 另外,如果每个单元格都有一个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);
                        }
                    }
                }
            }
        }

    }

}

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

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