CSCS530 Winter 2016

Complex Systems 530 - Computer Modeling of Complex Systems (Winter 2016)


In [2]:
%matplotlib inline
# Imports
import numpy
import matplotlib.pyplot as plt

import seaborn; seaborn.set()


:0: FutureWarning: IPython widgets are experimental and may change in the future.

In [3]:
# Set the grid and cell parameters
grid_size = 25
num_cells = 10

# Create the space and activate random cells
space = numpy.zeros((grid_size, grid_size))

# Now sample the agents.
for cell_id in range(num_cells):
    # Sample random position
    row = numpy.random.randint(0, grid_size)
    col = numpy.random.randint(0, grid_size)
    
    # "Endow" the cell with the resource by setting its value to 1.
    space[row, col] = 1
    
    # Output some info about the agent.
    print("Endowing cell ({0}, {1}).".format(row, col))

# Now show the space
plt.figure()
plt.pcolor(space, snap=True)
plt.colorbar()


Endowing cell (9, 8).
Endowing cell (14, 15).
Endowing cell (20, 24).
Endowing cell (1, 3).
Endowing cell (24, 6).
Endowing cell (17, 4).
Endowing cell (22, 24).
Endowing cell (4, 20).
Endowing cell (18, 3).
Endowing cell (23, 12).
Out[3]:
<matplotlib.colorbar.Colorbar at 0x7f64c4604ed0>

In [11]:
# Now show the space
plt.figure()
plt.pcolormesh(space, snap=True, shading='flat', edgecolor='#eeeeee')
plt.colorbar()

# Now highlight a few cells
from matplotlib.patches import Rectangle

# Get current axes
ax = plt.gca()

# Iterate over all cells in the grid
for i in range(grid_size):
    for j in range(grid_size):
        # In reality, we'd want this to represent something real; let's sample a random number for now.
        neighbor_similarity = numpy.random.random()
        
        # Draw the rectangle
        ax.add_patch(Rectangle((i, j), 1, 1, fill=False, edgecolor='blue', lw=neighbor_similarity))