|
| 1 | +from bank_reserves.agents import Person |
| 2 | +from bank_reserves.model import BankReservesModel |
| 3 | +from mesa.visualization import ( |
| 4 | + SolaraViz, |
| 5 | + make_plot_component, |
| 6 | + make_space_component, |
| 7 | +) |
| 8 | +from mesa.visualization.user_param import ( |
| 9 | + Slider, |
| 10 | +) |
| 11 | + |
| 12 | +# The colors here are taken from Matplotlib's tab10 palette |
| 13 | +# Green |
| 14 | +RICH_COLOR = "#2ca02c" |
| 15 | +# Red |
| 16 | +POOR_COLOR = "#d62728" |
| 17 | +# Blue |
| 18 | +MID_COLOR = "#1f77b4" |
| 19 | + |
| 20 | + |
| 21 | +def person_portrayal(agent): |
| 22 | + if agent is None: |
| 23 | + return |
| 24 | + |
| 25 | + portrayal = {} |
| 26 | + |
| 27 | + # update portrayal characteristics for each Person object |
| 28 | + if isinstance(agent, Person): |
| 29 | + portrayal["Shape"] = "circle" |
| 30 | + portrayal["r"] = 0.5 |
| 31 | + portrayal["Layer"] = 0 |
| 32 | + portrayal["Filled"] = "true" |
| 33 | + |
| 34 | + color = MID_COLOR |
| 35 | + |
| 36 | + # set agent color based on savings and loans |
| 37 | + if agent.savings > agent.model.rich_threshold: |
| 38 | + color = RICH_COLOR |
| 39 | + if agent.savings < 10 and agent.loans < 10: |
| 40 | + color = MID_COLOR |
| 41 | + if agent.loans > 10: |
| 42 | + color = POOR_COLOR |
| 43 | + |
| 44 | + portrayal["color"] = color |
| 45 | + |
| 46 | + return portrayal |
| 47 | + |
| 48 | + |
| 49 | +def post_process_space(ax): |
| 50 | + ax.set_aspect("equal") |
| 51 | + ax.set_xticks([]) |
| 52 | + ax.set_yticks([]) |
| 53 | + |
| 54 | + |
| 55 | +def post_process_lines(ax): |
| 56 | + ax.legend(loc="center left", bbox_to_anchor=(1, 0.9)) |
| 57 | + |
| 58 | + |
| 59 | +# dictionary of user settable parameters - these map to the model __init__ parameters |
| 60 | +model_params = { |
| 61 | + "init_people": Slider( |
| 62 | + "People", |
| 63 | + 25, |
| 64 | + 1, |
| 65 | + 200, |
| 66 | + # description="Initial Number of People" |
| 67 | + ), |
| 68 | + "rich_threshold": Slider( |
| 69 | + "Rich Threshold", |
| 70 | + 10, |
| 71 | + 1, |
| 72 | + 20, |
| 73 | + # description="Upper End of Random Initial Wallet Amount", |
| 74 | + ), |
| 75 | + "reserve_percent": Slider( |
| 76 | + "Reserves", |
| 77 | + 50, |
| 78 | + 1, |
| 79 | + 100, |
| 80 | + # description="Percent of deposits the bank has to hold in reserve", |
| 81 | + ), |
| 82 | +} |
| 83 | + |
| 84 | +space_component = make_space_component( |
| 85 | + person_portrayal, |
| 86 | + draw_grid=False, |
| 87 | + post_process=post_process_space, |
| 88 | +) |
| 89 | +lineplot_component = make_plot_component( |
| 90 | + {"Rich": RICH_COLOR, "Poor": POOR_COLOR, "Middle Class": MID_COLOR}, |
| 91 | + post_process=post_process_lines, |
| 92 | +) |
| 93 | +model = BankReservesModel() |
| 94 | + |
| 95 | +page = SolaraViz( |
| 96 | + model, |
| 97 | + components=[space_component, lineplot_component], |
| 98 | + model_params=model_params, |
| 99 | + name="Bank Reserves Model", |
| 100 | +) |
| 101 | +page # noqa |
0 commit comments