简体   繁体   中英

Create irregular shaped cellular automata space in Python Mesa

The examples within Mesa's readthedocs and in their Github all have fixed mxn grids. I'm trying to create an irregularly shaped agent space, for example:

3 x 3, connected to another 3 x 3 via a 1 x 1 "tunnel" (almost like a dumbbell shape). I'm happy to explicitly feed a series of tuples with grid coordinates but can't tell if that's possible. As a work around, I presently have agents in the system which don't interact, serving as a wall of sorts. In Mesa's Schelling example here's where the grid is formed:

class Schelling(Model):
    """
    Model class for the Schelling segregation model.
    """

    def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=3):
        """ """

        self.width = width
        self.height = height
        self.density = density
        self.minority_pc = minority_pc
        self.homophily = homophily

        self.schedule = RandomActivation(self)
        self.grid = SingleGrid(width, height, torus=True)

Any insight is appreciated. At a loss but I'd have to think this is possible.

Figured it out and answering my own question for the knowledge base.

It would appear that explicitly turning off cells is not built into Mesa's functionality. However, I produced a workaround for shaping, but it will only work with Mesa's SingleGrid which only allows one agent per grid location.

First, create an agent that doesn't interact or move:

class WallAgent(Agent):
    """
    Agent that acts as a wall. No interaction with other agents
    """

    def __init__(self, pos, model, agent_type):
        super().__init__(pos, model)
        self.pos = pos
        self.type = agent_type

Then, when placing agents within Mesa's model class you dynamically or explicitly state where to assign the wall agents:

       wall = [(0,0),(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),(8,0),(11,0),
            (12,0),(13,0),(14,0),(15,0),(16,0),(17,0),(18,0),(19,0),(0,1),(1,1),
            (2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1),(11,1),(12,1),(13,1),(14,1),
            (15,1),(16,1),(17,1),(18,1),(19,1)]
            
        for pos in wall:
            agent_type = 'wall'
            agent = WallAgent(pos, self, agent_type)
            self.grid.position_agent(agent, pos[0], pos[1])
            self.schedule.add(agent)

If using Mesa's visualization suite you'll get a result such as this where the black dots represent the wall/shaping. The blue and orange agents are ones that move and interact (for the problem I'm currently researching). 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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