Closed Holzhauer closed 6 days 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.
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.
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()
.
Do you have a sample repo that I can build on to do some experimentation?
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.
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=.
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?
@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.
Ah, I meant the latter, in which case I'll check out solara -- I've only used tornado in the past. Thanks.
Somewhat related: https://github.com/projectmesa/mesa/issues/2176
with the current solara front end this is largely resolved.
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.