Esempio n. 1
0
    def __init__(self, height=50, width=50, server = True, num_steps = 1000):
        '''
        Create a new playing area of (height, width) cells.
        '''

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)
        self.num_steps = num_steps
        # Use a hexagonal grid, where edges wrap around.
        self.grid = HexGrid(height, width, torus=True)
        self.server = server
        # Place a dead cell at each location.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)

        # activate the center(ish) cell.
        centerishCell = self.grid[width // 2][height // 2]

        centerishCell.state = 1
        for a in centerishCell.neighbors:
            a.isConsidered = True

        self.running = True
        
        #Datacollector -- default for this model is no data collection, but one can use OABM to assign one. 
        #so this is an empty DataCollector instance from MESA

        self.datacollector = DataCollector()
Esempio n. 2
0
    def __init__(self, height=50, width=50):
        '''
        Create a new playing area of (height, width) cells.
        '''

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)

        # Use a hexagonal grid, where edges wrap around.
        self.grid = HexGrid(height, width, torus=True)

        # Place a dead cell at each location.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)

        # activate the center(ish) cell.
        centerishCell = self.grid[width // 2][height // 2]

        centerishCell.state = 1
        for a in centerishCell.neighbors:
            a.isConsidered = True

        self.running = True
Esempio n. 3
0
class HexSnowflake(Model):
    '''
    Represents the hex grid of cells. The grid is represented by a 2-dimensional array of cells with adjacency rules specific to hexagons.
    '''

    def __init__(self, height=50, width=50, server = True, num_steps = 1000):
        '''
        Create a new playing area of (height, width) cells.
        '''

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)
        self.num_steps = num_steps
        # Use a hexagonal grid, where edges wrap around.
        self.grid = HexGrid(height, width, torus=True)
        self.server = server
        # Place a dead cell at each location.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)

        # activate the center(ish) cell.
        centerishCell = self.grid[width // 2][height // 2]

        centerishCell.state = 1
        for a in centerishCell.neighbors:
            a.isConsidered = True

        self.running = True
        
        #Datacollector -- default for this model is no data collection, but one can use OABM to assign one. 
        #so this is an empty DataCollector instance from MESA

        self.datacollector = DataCollector()

    def step(self):
        '''
        Have the scheduler advance each cell by one step
        '''
        self.schedule.step()
    def run_model(self, n = None):

        if n:
            self.num_steps = n
        if self.server == False:
            for _ in range(self.num_steps):
                self.step()
            return self
        else:
            from .server import server

            server.launch()
Esempio n. 4
0
    def __init__(self, height=50, width=50):
        '''
        Create a new playing area of (height, width) cells.
        '''

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)

        # Use a hexagonal grid, where edges wrap around.
        self.grid = HexGrid(height, width, torus=True)

        # Place a dead cell at each location.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)

        # activate the center(ish) cell.
        centerishCell = self.grid[width // 2][height // 2]

        centerishCell.state = 1
        for a in centerishCell.neighbors:
            a.isConsidered = True

        self.running = True
Esempio n. 5
0
 def setUp(self):
     '''
     Create a test non-toroidal grid and populate it with Mock Agents
     '''
     width = 3
     height = 5
     self.grid = HexGrid(width, height, torus=True)
     self.agents = []
     counter = 0
     for x in range(width):
         for y in range(height):
             if TEST_GRID[x][y] == 0:
                 continue
             counter += 1
             # Create and place the mock agent
             a = MockAgent(counter, None)
             self.agents.append(a)
             self.grid.place_agent(a, (x, y))
Esempio n. 6
0
class TestHexGrid(unittest.TestCase):
    """
    Testing a hexagonal grid.
    """

    def setUp(self):
        """
        Create a test non-toroidal grid and populate it with Mock Agents
        """
        width = 3
        height = 5
        self.grid = HexGrid(width, height, torus=False)
        self.agents = []
        counter = 0
        for x in range(width):
            for y in range(height):
                if TEST_GRID[x][y] == 0:
                    continue
                counter += 1
                # Create and place the mock agent
                a = MockAgent(counter, None)
                self.agents.append(a)
                self.grid.place_agent(a, (x, y))

    def test_neighbors(self):
        """
        Test the hexagonal neighborhood methods on the non-toroid.
        """

        neighborhood = self.grid.get_neighborhood((1, 1))
        assert len(neighborhood) == 6

        neighborhood = self.grid.get_neighborhood((0, 2))
        assert len(neighborhood) == 4

        neighborhood = self.grid.get_neighborhood((1, 0))
        assert len(neighborhood) == 3

        neighborhood = self.grid.get_neighborhood((1, 4))
        assert len(neighborhood) == 5

        neighborhood = self.grid.get_neighborhood((0, 4))
        assert len(neighborhood) == 2

        neighborhood = self.grid.get_neighborhood((0, 0))
        assert len(neighborhood) == 3

        neighborhood = self.grid.get_neighborhood((1, 1), include_center=True)
        assert len(neighborhood) == 7
Esempio n. 7
0
class TestHexGrid(unittest.TestCase):
    '''
    Testing a hexagonal grid.
    '''

    def setUp(self):
        '''
        Create a test non-toroidal grid and populate it with Mock Agents
        '''
        width = 3
        height = 5
        self.grid = HexGrid(width, height, torus=False)
        self.agents = []
        counter = 0
        for x in range(width):
            for y in range(height):
                if TEST_GRID[x][y] == 0:
                    continue
                counter += 1
                # Create and place the mock agent
                a = MockAgent(counter, None)
                self.agents.append(a)
                self.grid.place_agent(a, (x, y))

    def test_neighbors(self):
        '''
        Test the hexagonal neighborhood methods on the non-toroid.
        '''

        neighborhood = self.grid.get_neighborhood((1, 1))
        assert len(neighborhood) == 6

        neighborhood = self.grid.get_neighborhood((0, 2))
        assert len(neighborhood) == 4

        neighborhood = self.grid.get_neighborhood((1, 0))
        assert len(neighborhood) == 3

        neighborhood = self.grid.get_neighborhood((1, 4))
        assert len(neighborhood) == 5

        neighborhood = self.grid.get_neighborhood((0, 4))
        assert len(neighborhood) == 2

        neighborhood = self.grid.get_neighborhood((0, 0))
        assert len(neighborhood) == 3

        neighborhood = self.grid.get_neighborhood((1, 1), include_center=True)
        assert len(neighborhood) == 7
Esempio n. 8
0
class HexSnowflake(Model):
    '''
    Represents the hex grid of cells. The grid is represented by a 2-dimensional array of cells with adjacency rules specific to hexagons.
    '''

    def __init__(self, height=50, width=50):
        '''
        Create a new playing area of (height, width) cells.
        '''

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)

        # Use a hexagonal grid, where edges wrap around.
        self.grid = HexGrid(height, width, torus=True)

        # Place a dead cell at each location.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)

        # activate the center(ish) cell.
        centerishCell = self.grid[width // 2][height // 2]

        centerishCell.state = 1
        for a in centerishCell.neighbors:
            a.isConsidered = True

        self.running = True

    def step(self):
        '''
        Have the scheduler advance each cell by one step
        '''
        self.schedule.step()
Esempio n. 9
0
    def __init__(self, height=150, width=150):
        """
        Create a new playing area of (height, width) cells.
        """

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)

        # Use a hexagonal grid, where edges wrap around.
        self.grid = HexGrid(height, width, torus=True)

        # Place a dead cell at each location.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)
            
        data = self.get_data("../data/input24.txt")

        # activate the center(ish) cell.
        centerishCell = self.grid[width // 2][height // 2]
        centerishPos = (width // 2, height // 2)
        
        for d in data:
            pos = self.find_cell(centerishPos, d)
            c = self.grid[pos[0]][pos[1]]
            c.toggle()
            for a in c.neighbors:
                a.isConsidered = True
        #centerishCell.state = Cell.ALIVE
        #for a in centerishCell.neighbors:
        #    a.isConsidered = True

        print(self.count_type(self, Cell.ALIVE))
        
        self.running = True
Esempio n. 10
0
class HexSnowflake(Model):
    '''
    Represents the hex grid of cells. The grid is represented by a 2-dimensional array of cells with adjacency rules specific to hexagons.
    '''
    def __init__(self, height=50, width=50):
        '''
        Create a new playing area of (height, width) cells.
        '''

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)

        # Use a hexagonal grid, where edges wrap around.
        self.grid = HexGrid(height, width, torus=True)

        # Place a dead cell at each location.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)

        # activate the center(ish) cell.
        centerishCell = self.grid[width // 2][height // 2]

        centerishCell.state = 1
        for a in centerishCell.neighbors:
            a.isConsidered = True

        self.running = True

    def step(self):
        '''
        Have the scheduler advance each cell by one step
        '''
        self.schedule.step()
Esempio n. 11
0
 def setUp(self):
     '''
     Create a test non-toroidal grid and populate it with Mock Agents
     '''
     width = 3
     height = 5
     self.grid = HexGrid(width, height, torus=True)
     self.agents = []
     counter = 0
     for x in range(width):
         for y in range(height):
             if TEST_GRID[x][y] == 0:
                 continue
             counter += 1
             # Create and place the mock agent
             a = MockAgent(counter, None)
             self.agents.append(a)
             self.grid.place_agent(a, (x, y))
Esempio n. 12
0
class TestHexGridTorus(TestBaseGrid):
    '''
    Testing a hexagonal toroidal grid.
    '''

    torus = True

    def setUp(self):
        '''
        Create a test non-toroidal grid and populate it with Mock Agents
        '''
        width = 3
        height = 5
        self.grid = HexGrid(width, height, torus=True)
        self.agents = []
        counter = 0
        for x in range(width):
            for y in range(height):
                if TEST_GRID[x][y] == 0:
                    continue
                counter += 1
                # Create and place the mock agent
                a = MockAgent(counter, None)
                self.agents.append(a)
                self.grid.place_agent(a, (x, y))

    def test_neighbors(self):
        '''
        Test the hexagonal neighborhood methods on the toroid.
        '''

        neighborhood = self.grid.get_neighborhood((1, 1))
        assert len(neighborhood) == 6

        neighborhood = self.grid.get_neighborhood((1, 1), include_center=True)
        assert len(neighborhood) == 7

        neighborhood = self.grid.get_neighborhood((0, 0))
        assert len(neighborhood) == 6

        neighborhood = self.grid.get_neighborhood((2, 4))
        assert len(neighborhood) == 6
Esempio n. 13
0
    def __init__(self, N=20, height=21, width=21, push_ratio = 0.5):
        super().__init__()
        self.height = height
        self.width = width
        self.num_agents = N

        self.exit_x = self.width - 1
        self.exit_y = round(self.height/2)

        self.push_probs = np.array([[0.,0.],[1.,0.5]])

        self.grid = HexGrid(self.width, self.height, torus=False)
        self.schedule = RandomActivation(self)

        # decide for ID whether it is a pusher
        is_pusher = np.zeros(N, dtype = int)
        idx = self.random.sample([i for i in range(N)], int(push_ratio * N))
        is_pusher[idx] = 1
        
        # Add N pedestrians
        taken_pos = []
        for i in range(self.num_agents):
            # Add the agent to a random grid cell
            while True:
                x = self.random.randrange(1, self.grid.width-1)
                y = self.random.randrange(1, self.grid.height-1)
                pos = (x,y)
                if not pos in taken_pos:
                    break
            a = Pedestrian(i, self, pos, self.exit_x, self.exit_y, is_pusher[i])
            self.schedule.add(a)

            self.grid.place_agent(a, pos)
            taken_pos.append(pos)
        print(len(taken_pos))
        # Place vertical walls
        for i in range(self.height):

             # Left
            x=0
            y=i

            if x == self.exit_x and y == self.exit_y:
                e = Exit(self, (x, y))
                #self.schedule.add(e)
                self.grid.place_agent(e, (x, y))
            else:
                w = Wall(self, (x, y))
                #self.schedule.add(w)
                self.grid.place_agent(w, (x, y))

            # Right
            x=self.width-1
            y=i

            # One exit
            if x == self.exit_x and y == self.exit_y:
                e = Exit(self, (x, y))
                #self.schedule.add(e)
                self.grid.place_agent(e, (x, y))
            else:
                w = Wall(self, (x, y))
                #self.schedule.add(w)
                self.grid.place_agent(w, (x, y))


        # Place horizontal walls
        for i in range(self.width):

            # Up
            x=i
            y=0

            if x == self.exit_x and y == self.exit_y:
                e = Exit(self, (x, y))
                #self.schedule.add(e)
                self.grid.place_agent(e, (x, y))
            else:
                w = Wall(self, (x, y))
                #self.schedule.add(w)
                self.grid.place_agent(w, (x, y))
            # Down
            x=i
            y=self.height-1

            # One exit
            if x == self.exit_x and y == self.exit_y:
                #e = Exit(self, (x, y))
                #self.schedule.add(e)
                #self.grid.place_agent(e, (x, y))
                continue
            else:
                w = Wall(self, (x, y))
                #self.schedule.add(w)
                self.grid.place_agent(w, (x, y))


        self.data_collector = DataCollector({
            "Evacuees": lambda m: self.count_evacuees(),
            "Evacuated": lambda m: self.count_evacuated()
        })

         # this is required for the data_collector to work
        self.running = True
        self.data_collector.collect(self)
Esempio n. 14
0
class HexSnowflake(Model):
    """
    Represents the hex grid of cells. The grid is represented by a 2-dimensional array of cells with adjacency rules specific to hexagons.
    """

    def __init__(self, height=150, width=150):
        """
        Create a new playing area of (height, width) cells.
        """

        # Set up the grid and schedule.

        # Use SimultaneousActivation which simulates all the cells
        # computing their next state simultaneously.  This needs to
        # be done because each cell's next state depends on the current
        # state of all its neighbors -- before they've changed.
        self.schedule = SimultaneousActivation(self)

        # Use a hexagonal grid, where edges wrap around.
        self.grid = HexGrid(height, width, torus=True)

        # Place a dead cell at each location.
        for (contents, x, y) in self.grid.coord_iter():
            cell = Cell((x, y), self)
            self.grid.place_agent(cell, (x, y))
            self.schedule.add(cell)
            
        data = self.get_data("../data/input24.txt")

        # activate the center(ish) cell.
        centerishCell = self.grid[width // 2][height // 2]
        centerishPos = (width // 2, height // 2)
        
        for d in data:
            pos = self.find_cell(centerishPos, d)
            c = self.grid[pos[0]][pos[1]]
            c.toggle()
            for a in c.neighbors:
                a.isConsidered = True
        #centerishCell.state = Cell.ALIVE
        #for a in centerishCell.neighbors:
        #    a.isConsidered = True

        print(self.count_type(self, Cell.ALIVE))
        
        self.running = True

    def step(self):
        """
        Have the scheduler advance each cell by one step
        """
        self.schedule.step()
        print(self.schedule.time, self.count_type(self, Cell.ALIVE))
        if self.schedule.time == 100:
            self.running = False
        
    @staticmethod
    def find_cell(pos, d):
        for nd in d:
            pos = HexSnowflake.adj_pos(pos, nd)
        return pos
    
    @staticmethod
    def adj_pos(pos, direction):
        x, y = pos
        if direction == "e":
            return (x, y + 1)
        elif direction == "w":
            return (x, y - 1)
        if x % 2 == 0:
            if direction == "ne":
                return (x + 1, y + 1)
            elif direction == "nw":
                return (x + 1, y)
            elif direction == "se":
                return (x - 1, y + 1)
            elif direction == "sw":
                return (x - 1, y)
        else:
            if direction == "ne":
                return (x + 1, y)
            elif direction == "nw":
                return (x + 1, y - 1)
            elif direction == "se":
                return (x - 1, y)
            elif direction == "sw":
                return (x - 1, y - 1)
    
    @staticmethod
    def get_data(filename):
        fin = open(filename)
        data = fin.readlines()
        fin.close()
        fixed = []
        for d in data:
            d = d.strip()
            fixed.append([])
            i = 0
            while i < len(d):
                if d[i] == "e" or d[i] == "w":
                    fixed[-1].append(d[i])
                    i += 1
                else:
                    fixed[-1].append(d[i:i+2])
                    i += 2
        return fixed
    
    @staticmethod
    def count_type(model, condition):
        """
        Helper method to count trees in a given condition in a given model.
        """
        count = 0
        for spot in model.schedule.agents:
            if spot.state == condition:
                count += 1
        return count