|
| 1 | +from forest_fire.model import ForestFire |
| 2 | +from mesa.visualization import ( |
| 3 | + SolaraViz, |
| 4 | + make_plot_component, |
| 5 | + make_space_component, |
| 6 | +) |
| 7 | +from mesa.visualization.user_param import ( |
| 8 | + Slider, |
| 9 | +) |
| 10 | + |
| 11 | +COLORS = {"Fine": "#00AA00", "On Fire": "#880000", "Burned Out": "#000000"} |
| 12 | + |
| 13 | + |
| 14 | +def forest_fire_portrayal(tree): |
| 15 | + if tree is None: |
| 16 | + return |
| 17 | + portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0} |
| 18 | + (x, y) = (tree.cell.coordinate[i] for i in (0, 1)) |
| 19 | + portrayal["x"] = x |
| 20 | + portrayal["y"] = y |
| 21 | + portrayal["color"] = COLORS[tree.condition] |
| 22 | + return portrayal |
| 23 | + |
| 24 | + |
| 25 | +def post_process_space(ax): |
| 26 | + ax.set_aspect("equal") |
| 27 | + ax.set_xticks([]) |
| 28 | + ax.set_yticks([]) |
| 29 | + |
| 30 | + |
| 31 | +def post_process_lines(ax): |
| 32 | + ax.legend(loc="center left", bbox_to_anchor=(1, 0.9)) |
| 33 | + |
| 34 | + |
| 35 | +space_component = make_space_component( |
| 36 | + forest_fire_portrayal, |
| 37 | + draw_grid=False, |
| 38 | + post_process=post_process_space, |
| 39 | +) |
| 40 | +lineplot_component = make_plot_component( |
| 41 | + COLORS, |
| 42 | + post_process=post_process_lines, |
| 43 | +) |
| 44 | +# TODO: add back in pie chart component |
| 45 | +# # no current pie chart equivalent in mesa>=3.0 |
| 46 | +# pie_chart = mesa.visualization.PieChartModule( |
| 47 | +# [{"Label": label, "Color": color} for (label, color) in COLORS.items()] |
| 48 | +# ) |
| 49 | +model = ForestFire() |
| 50 | +model_params = { |
| 51 | + "height": 100, |
| 52 | + "width": 100, |
| 53 | + "density": Slider("Tree density", 0.65, 0.01, 1.0, 0.01), |
| 54 | +} |
| 55 | +page = SolaraViz( |
| 56 | + model, |
| 57 | + components=[space_component, lineplot_component], |
| 58 | + model_params=model_params, |
| 59 | + name="Forest Fire", |
| 60 | +) |
0 commit comments