|
| 1 | +import mesa |
| 2 | + |
| 3 | +import streamlit as st |
| 4 | + |
| 5 | +import time |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | +import altair as alt |
| 10 | + |
| 11 | +import plotly.express as px |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +from conways_game_of_life.model import ConwaysGameOfLife |
| 15 | + |
| 16 | +import pandas as pd |
| 17 | + |
| 18 | + |
| 19 | +import random |
| 20 | + |
| 21 | +model = st.title("Boltzman Wealth Model") |
| 22 | +num_ticks = st.slider( |
| 23 | + "Select number of Simulation Runs", min_value=1, max_value=100, value=50 |
| 24 | +) |
| 25 | +height = st.slider("Select Grid Height", min_value=10, max_value=100, step=10, value=15) |
| 26 | +width = st.slider("Select Grid Width", min_value=10, max_value=100, step=10, value=20) |
| 27 | +model = ConwaysGameOfLife(height, width) |
| 28 | + |
| 29 | +col1, col2, col3 = st.columns(3) |
| 30 | +status_text = st.empty() |
| 31 | +# step_mode = st.checkbox('Run Step-by-Step') |
| 32 | +run = st.button("Run Simulation") |
| 33 | + |
| 34 | + |
| 35 | +if run: |
| 36 | + tick = time.time() |
| 37 | + step = 0 |
| 38 | + # init grid |
| 39 | + df_grid = pd.DataFrame() |
| 40 | + agent_counts = np.zeros((model.grid.width, model.grid.height)) |
| 41 | + for x in range(width): |
| 42 | + for y in range(height): |
| 43 | + df_grid = pd.concat( |
| 44 | + [df_grid, pd.DataFrame({"x": [x], "y": [y], "state": [0]})], |
| 45 | + ignore_index=True, |
| 46 | + ) |
| 47 | + |
| 48 | + heatmap = ( |
| 49 | + alt.Chart(df_grid) |
| 50 | + .mark_point(size=100) |
| 51 | + .encode(x="x", y="y", color=alt.Color("state")) |
| 52 | + .interactive() |
| 53 | + .properties(width=800, height=600) |
| 54 | + ) |
| 55 | + |
| 56 | + # init progress bar |
| 57 | + my_bar = st.progress(0, text="Simulation Progress") # progress |
| 58 | + placeholder = st.empty() |
| 59 | + st.subheader("Agent Grid") |
| 60 | + chart = st.altair_chart(heatmap, use_container_width=True) |
| 61 | + color_scale = alt.Scale(domain=[0, 1], range=["red", "yellow"]) |
| 62 | + for i in range(num_ticks): |
| 63 | + model.step() |
| 64 | + my_bar.progress((i / num_ticks), text="Simulation progress") |
| 65 | + placeholder.text("Step = %d" % i) |
| 66 | + for contents, x, y in model.grid.coord_iter(): |
| 67 | + # print('x:',x,'y:',y, 'state:',contents) |
| 68 | + selected_row = df_grid[(df_grid["x"] == x) & (df_grid["y"] == y)] |
| 69 | + df_grid.loc[ |
| 70 | + selected_row.index, "state" |
| 71 | + ] = contents.state # random.choice([1,2]) |
| 72 | + |
| 73 | + heatmap = ( |
| 74 | + alt.Chart(df_grid) |
| 75 | + .mark_circle(size=100) |
| 76 | + .encode(x="x", y="y", color=alt.Color("state", scale=color_scale)) |
| 77 | + .interactive() |
| 78 | + .properties(width=800, height=600) |
| 79 | + ) |
| 80 | + chart.altair_chart(heatmap) |
| 81 | + |
| 82 | + time.sleep(0.1) |
| 83 | + |
| 84 | + tock = time.time() |
| 85 | + st.success(f"Simulation completed in {tock - tick:.2f} secs") |
0 commit comments