projectmesa / mesa-viz-tornado

Apache License 2.0
2 stars 8 forks source link

CanvasGrid does not work for very high grid_width or grid_height #3

Open nivedkrishnans opened 4 years ago

nivedkrishnans commented 4 years ago

When using CanvasGrid(portrayal_method, grid_width, grid_height, canvas_width=500, canvas_height=500), beyond a certain value of grid_width or grid_height, the visualisation does not show any agents. (Image attached) I noticed it from the 'Advanced Tutorial'. grid = CanvasGrid(agent_portrayal, 134, 134, 400, 400) Here for a canvas size of 400x400, grid sizes from 134x134 does not work. If you increase the canvas size, it will support a higher grid size, until it stops working again. I increase the agent indicator size to see if the agents were just too small; that is not the case.

Screenshot_20200820_023955

Complete File: (Money_Model_Viz.py as per the tutorial)

from MoneyModel import *
from mesa.visualization.modules import CanvasGrid
from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.modules import ChartModule
import random
import numpy as np

main_grid = 133
display_grid = 400

from mesa.visualization.ModularVisualization import VisualizationElement

def agent_portrayal(agent):
    portrayal = {"Shape": "circle",
                "Filled": "true",
                "r": 0.5}

    if agent.wealth > 0:
        portrayal["Color"] = "red"
        portrayal["Layer"] = 0
    else:
        portrayal["Color"] = "grey"
        portrayal["Layer"] = 1
        portrayal["r"] = 0.2
    return portrayal

grid = CanvasGrid(agent_portrayal, 134, 134, 400, 400)
chart = ChartModule([{"Label": "Gini",
                    "Color": "Black"}],
                    data_collector_name='datacollector')

class HistogramModule(VisualizationElement):
    package_includes = ["Chart.min.js"]
    local_includes = ["HistogramModule.js"]

    def __init__(self, bins, canvas_height, canvas_width):
        self.canvas_height = canvas_height
        self.canvas_width = canvas_width
        self.bins = bins
        new_element = "new HistogramModule({}, {}, {})"
        new_element = new_element.format(bins,
                                        canvas_width,
                                        canvas_height)
        self.js_code = "elements.push(" + new_element + ");"

    def render(self, model):
        wealth_vals = [agent.wealth for agent in model.schedule.agents]
        hist = np.histogram(wealth_vals, bins=self.bins)[0]
        return [int(x) for x in hist]

histogram = HistogramModule(list(range(10)), 200, 500)
server = ModularServer(MoneyModel,
                    [grid, histogram, chart],
                    "Money Model",
                    {"N":1000, "width":134, "height":134})

server.port = 8521 # The default
server.launch()