Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nivedkrishnans opened this issue Aug 19, 2020 · 0 comments
Open

Comments

@nivedkrishnans
Copy link

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()
@rht rht transferred this issue from projectmesa/mesa Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant