diff --git a/examples/forest_fire/app.py b/examples/forest_fire/app.py new file mode 100644 index 00000000..4dd7d544 --- /dev/null +++ b/examples/forest_fire/app.py @@ -0,0 +1,60 @@ +from forest_fire.model import ForestFire +from mesa.visualization import ( + SolaraViz, + make_plot_component, + make_space_component, +) +from mesa.visualization.user_param import ( + Slider, +) + +COLORS = {"Fine": "#00AA00", "On Fire": "#880000", "Burned Out": "#000000"} + + +def forest_fire_portrayal(tree): + if tree is None: + return + portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0} + (x, y) = (tree.cell.coordinate[i] for i in (0, 1)) + portrayal["x"] = x + portrayal["y"] = y + portrayal["color"] = COLORS[tree.condition] + return portrayal + + +def post_process_space(ax): + ax.set_aspect("equal") + ax.set_xticks([]) + ax.set_yticks([]) + + +def post_process_lines(ax): + ax.legend(loc="center left", bbox_to_anchor=(1, 0.9)) + + +space_component = make_space_component( + forest_fire_portrayal, + draw_grid=False, + post_process=post_process_space, +) +lineplot_component = make_plot_component( + COLORS, + post_process=post_process_lines, +) +# TODO: add back in pie chart component +# # no current pie chart equivalent in mesa>=3.0 +# pie_chart = mesa.visualization.PieChartModule( +# [{"Label": label, "Color": color} for (label, color) in COLORS.items()] +# ) +model = ForestFire() +model_params = { + "height": 100, + "width": 100, + "density": Slider("Tree density", 0.65, 0.01, 1.0, 0.01), +} +page = SolaraViz( + model, + components=[space_component, lineplot_component], + model_params=model_params, + name="Forest Fire", +) diff --git a/examples/forest_fire/forest_fire/server.py b/examples/forest_fire/forest_fire/server.py deleted file mode 100644 index 6d8f9fd3..00000000 --- a/examples/forest_fire/forest_fire/server.py +++ /dev/null @@ -1,36 +0,0 @@ -import mesa - -from .model import ForestFire - -COLORS = {"Fine": "#00AA00", "On Fire": "#880000", "Burned Out": "#000000"} - - -def forest_fire_portrayal(tree): - if tree is None: - return - portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0} - (x, y) = tree.pos - portrayal["x"] = x - portrayal["y"] = y - portrayal["Color"] = COLORS[tree.condition] - return portrayal - - -canvas_element = mesa.visualization.CanvasGrid( - forest_fire_portrayal, 100, 100, 500, 500 -) -tree_chart = mesa.visualization.ChartModule( - [{"Label": label, "Color": color} for (label, color) in COLORS.items()] -) -pie_chart = mesa.visualization.PieChartModule( - [{"Label": label, "Color": color} for (label, color) in COLORS.items()] -) - -model_params = { - "height": 100, - "width": 100, - "density": mesa.visualization.Slider("Tree density", 0.65, 0.01, 1.0, 0.01), -} -server = mesa.visualization.ModularServer( - ForestFire, [canvas_element, tree_chart, pie_chart], "Forest Fire", model_params -) diff --git a/examples/forest_fire/requirements.txt b/examples/forest_fire/requirements.txt index da2b9972..161a0312 100644 --- a/examples/forest_fire/requirements.txt +++ b/examples/forest_fire/requirements.txt @@ -1,3 +1,2 @@ jupyter -matplotlib -mesa~=2.0 +mesa[viz]>=3.0 diff --git a/examples/forest_fire/run.py b/examples/forest_fire/run.py deleted file mode 100644 index 1155b749..00000000 --- a/examples/forest_fire/run.py +++ /dev/null @@ -1,3 +0,0 @@ -from forest_fire.server import server - -server.launch(open_browser=True)