projectmesa / mesa

Mesa is an open-source Python library for agent-based modeling, ideal for simulating complex systems and exploring emergent behaviors.
https://mesa.readthedocs.io
Apache License 2.0
2.45k stars 874 forks source link

Model properties and/or model parameters as arguments for VisualisationElements #1216

Closed Holzhauer closed 6 days ago

Holzhauer commented 2 years ago

Currently, visualisation element parameters such as CanvasGrid's width and heights parameters need to be hard-coded more or less, and it seems not possible to initialise the grid with other dimensions than initially defined, when resetting the model.

It would be great to allow functions as arguments which are called with the model or model parameter objects.

rht commented 2 years ago

This is possible if this part of ModularServer's __init__

        self.js_code = []
        for element in self.visualization_elements:
            for include_file in element.package_includes:
                self.package_includes.add(include_file)
            for include_file in element.local_includes:
                self.local_includes.add(include_file)
            self.js_code.append(element.js_code)

is moved to be inside reset_model() instead. Where the self.js_code of the CanvasGrid is modified to the dimensions you need before the explicit reset.

Would be great if you can describe the use case you're facing, so that I can understand the context better.

rht commented 2 years ago

This part of the code: https://github.com/projectmesa/mesa/blob/2228b3c11a480c150f262d890ca0cd9e016e684c/mesa/visualization/ModularVisualization.py#L268-L274. I should have linked it from GH instead of copy-pasting it from my local file.

Holzhauer commented 2 years ago

Thanks @rht! Use case: Analysing impact of grid size (here: fire evacuation model) via GUI slider. Therefore, the grid extent is changed on reset. However, currently the canvas does not adapt: for smaller extents than initially, the grid covers only part of the canvas, for higher extents only part of the grid is displayed. I copied the code above to ModularServer.reset_model(). I added

        canvas.new_element = "new CanvasModule({}, {}, {}, {})".format(
            canvas.canvas_width, canvas.canvas_height, floor_size, floor_size
        )
        canvas.js_code = "elements.push(" + canvas.new_element + ");"
        canvas.render(self)

to Model.__init__() after MultiGrid was instantiated. The only issue left is, I still need to reload the webpage to update the canvas extent. I haven't found a solution yet to render the PageHandler in ModularServer.reset_model().

rht commented 2 years ago

Do you have a sample repo that I can build on to do some experimentation?

Holzhauer commented 2 years ago

The model I used to test grid resize is https://github.com/UniK-INES/MesaSimpleEvacuation. Modifications to mesa have been done in https://github.com/UniK-INES/mesa/tree/ines_edu.

rht commented 2 years ago

OK, thank you! It looks like you have also improved the boid_flockers example. Looking forward to the PR. And also, I think you can also help review the discrepancy in the Mesa/Agents.jl/MASON/NetLogo implementation of the boid_flockers, in this thread https://github.com/JuliaDynamics/Agents.jl/pull/394#issuecomment-770368876=.

stephenfmann commented 5 months ago

This would be extremely useful, but it looks like it has been quiet for 2 years. I assume we aren't going to get a pull request from @Holzhauer on this specific issue? If so, would a PR from me be considered instead, if I replicated the changes in a new fork?

Holzhauer commented 5 months ago

@stephenfmann is your comment about the boid_flockers example or about the grid size update? The former I can push next week after minor reviews, the latter I think is not relevant anymore as the new solara framework supports resizing.

stephenfmann commented 5 months ago

Ah, I meant the latter, in which case I'll check out solara -- I've only used tornado in the past. Thanks.

EwoutH commented 3 months ago

Somewhat related: https://github.com/projectmesa/mesa/issues/2176

quaquel commented 6 days ago

with the current solara front end this is largely resolved.