Spaces:
Runtime error
Runtime error
added mltiple networks
Browse files
app.py
CHANGED
@@ -10,10 +10,17 @@ Original file is located at
|
|
10 |
# Commented out IPython magic to ensure Python compatibility.
|
11 |
# %%capture
|
12 |
# !pip install gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
#
|
14 |
# !pip install cmocean
|
15 |
# !pip install mesa
|
16 |
-
|
|
|
17 |
|
18 |
import random
|
19 |
import pandas as pd
|
@@ -32,9 +39,14 @@ import cmocean
|
|
32 |
import tqdm
|
33 |
|
34 |
import scipy as sp
|
35 |
-
from scipy.stats import beta
|
36 |
|
|
|
|
|
|
|
37 |
|
|
|
|
|
|
|
38 |
|
39 |
import opinionated
|
40 |
import matplotlib.pyplot as plt
|
@@ -44,7 +56,6 @@ from opinionated.core import download_googlefont
|
|
44 |
download_googlefont('Quicksand', add_to_cache=True)
|
45 |
plt.rc('font', family='Quicksand')
|
46 |
|
47 |
-
|
48 |
experiences = {
|
49 |
'dissident_experiences': [1,0,0],
|
50 |
'supporter_experiences': [1,1,1],
|
@@ -533,97 +544,94 @@ class PoliticalModel(Model):
|
|
533 |
|
534 |
|
535 |
|
536 |
-
def run_simulation(n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=400, half_life=20):
|
537 |
-
|
538 |
-
|
539 |
-
# Complete graph
|
540 |
-
G = nx.complete_graph(n_agents)
|
541 |
|
542 |
-
|
543 |
-
|
544 |
-
"physical": {"network": G, "type": "physical", "positions": nx.circular_layout(G)}#kamada_kawai
|
545 |
-
}
|
546 |
|
547 |
-
|
548 |
-
|
|
|
|
|
549 |
|
550 |
-
|
551 |
-
|
552 |
-
social_learning_factor, half_life=half_life, print_agents=False, print_frequency=50, agent_reporters=True, intervention_list=intervention_list)
|
553 |
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
return model
|
558 |
|
559 |
-
#
|
|
|
|
|
|
|
560 |
|
561 |
-
|
562 |
-
model =run_simulation(n_agents=n_agents, share_regime_supporters=share_regime_supporters, threshold=threshold, social_learning_factor=social_learning_factor, simulation_steps=simulation_steps, half_life=half_life)
|
563 |
-
# Get data and reset index
|
564 |
-
agent_df = model.datacollector.get_agent_vars_dataframe().reset_index()
|
565 |
|
566 |
-
|
567 |
-
|
|
|
|
|
568 |
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
plt.plot(agent_df_pivot.index, agent_df_pivot[column], color='gray', alpha=0.1)
|
573 |
|
574 |
-
# Compute and plot the mean estimation
|
575 |
-
mean_estimation = agent_df_pivot.mean(axis=1)
|
576 |
-
plt.plot(mean_estimation.index, mean_estimation, color='black', linewidth=2)
|
577 |
|
578 |
-
|
579 |
-
plt.title('Agent Estimation Over Time', loc='right')
|
580 |
-
plt.xlabel('Time step')
|
581 |
-
plt.ylabel('Estimation')
|
582 |
-
return fig
|
583 |
|
584 |
|
585 |
-
|
|
|
586 |
|
587 |
-
import gradio as gr
|
588 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
589 |
|
|
|
|
|
|
|
|
|
|
|
590 |
|
591 |
-
|
592 |
-
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
593 |
-
with gr.Column():
|
594 |
-
gr.Markdown("""# Simulate Revolutions
|
595 |
-
Agents are placed on a fully connected graph. Vary the parameters below, and click 'Run Simulation' to run.
|
596 |
-
""")
|
597 |
-
with gr.Row():
|
598 |
-
with gr.Column():
|
599 |
-
|
600 |
-
|
601 |
-
# Sliders for each parameter
|
602 |
-
n_agents_slider = gr.Slider(minimum=100, maximum=500, step=10, label="Number of Agents", value=150)
|
603 |
-
share_regime_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Share of Regime Supporters", value=0.4)
|
604 |
-
threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Threshold", value=0.5)
|
605 |
-
social_learning_slider = gr.Slider(minimum=0.0, maximum=2.0, step=0.1, label="Social Learning Factor", value=1.0)
|
606 |
-
steps_slider = gr.Slider(minimum=10, maximum=100, step=5, label="Simulation Steps", value=40)
|
607 |
-
half_life_slider = gr.Slider(minimum=5, maximum=50, step=5, label="Half-Life", value=20)
|
608 |
-
|
609 |
-
with gr.Column():
|
610 |
-
# Button to trigger the simulation
|
611 |
-
button = gr.Button("Run Simulation")
|
612 |
-
plot_output = gr.Plot(label="Simulation Result")
|
613 |
-
|
614 |
-
# Function to call when button is clicked
|
615 |
-
def run_simulation_and_plot(*args):
|
616 |
-
fig = run_and_plot_simulation(*args)
|
617 |
-
return fig
|
618 |
-
|
619 |
-
# Setting up the button click event
|
620 |
-
button.click(
|
621 |
-
run_simulation_and_plot,
|
622 |
-
inputs=[n_agents_slider, share_regime_slider, threshold_slider, social_learning_slider, steps_slider, half_life_slider],
|
623 |
-
outputs=[plot_output]
|
624 |
-
)
|
625 |
-
|
626 |
-
# Launch the interface
|
627 |
-
if __name__ == "__main__":
|
628 |
-
demo.launch(debug=True)
|
629 |
|
|
|
10 |
# Commented out IPython magic to ensure Python compatibility.
|
11 |
# %%capture
|
12 |
# !pip install gradio
|
13 |
+
# # !pip install gradio==3.50.2
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
# Commented out IPython magic to ensure Python compatibility.
|
18 |
+
# %%capture
|
19 |
#
|
20 |
# !pip install cmocean
|
21 |
# !pip install mesa
|
22 |
+
#
|
23 |
+
# !pip install opinionated
|
24 |
|
25 |
import random
|
26 |
import pandas as pd
|
|
|
39 |
import tqdm
|
40 |
|
41 |
import scipy as sp
|
|
|
42 |
|
43 |
+
# from compress_pickle import dump, load
|
44 |
+
|
45 |
+
from scipy.stats import beta
|
46 |
|
47 |
+
# # %%capture
|
48 |
+
# !pip install git+https://github.com/MNoichl/opinionated.git#egg=opinionated
|
49 |
+
# # import opinionated
|
50 |
|
51 |
import opinionated
|
52 |
import matplotlib.pyplot as plt
|
|
|
56 |
download_googlefont('Quicksand', add_to_cache=True)
|
57 |
plt.rc('font', family='Quicksand')
|
58 |
|
|
|
59 |
experiences = {
|
60 |
'dissident_experiences': [1,0,0],
|
61 |
'supporter_experiences': [1,1,1],
|
|
|
544 |
|
545 |
|
546 |
|
547 |
+
# def run_simulation(n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=400, half_life=20):
|
548 |
+
# # Helper functions like graph_from_coordinates, ensure_neighbors should be defined outside this function
|
|
|
|
|
|
|
549 |
|
550 |
+
# # Complete graph
|
551 |
+
# G = nx.complete_graph(n_agents)
|
|
|
|
|
552 |
|
553 |
+
# # Networks dictionary
|
554 |
+
# networks = {
|
555 |
+
# "physical": {"network": G, "type": "physical", "positions": nx.circular_layout(G)}#kamada_kawai
|
556 |
+
# }
|
557 |
|
558 |
+
# # Intervention list
|
559 |
+
# intervention_list = [ ]
|
|
|
560 |
|
561 |
+
# # Initialize the model
|
562 |
+
# model = PoliticalModel(n_agents, networks, share_regime_supporters, threshold,
|
563 |
+
# social_learning_factor, half_life=half_life, print_agents=False, print_frequency=50, agent_reporters=True, intervention_list=intervention_list)
|
|
|
564 |
|
565 |
+
# # Run the model
|
566 |
+
# for _ in tqdm.tqdm_notebook(range(simulation_steps)): # Run for specified number of steps
|
567 |
+
# model.step()
|
568 |
+
# return model
|
569 |
|
570 |
+
# # Example usage
|
|
|
|
|
|
|
571 |
|
572 |
+
radius=.09
|
573 |
+
physical_graph_points = np.random.rand(100, 2)
|
574 |
+
physical_graph = graph_from_coordinates(physical_graph_points, radius)
|
575 |
+
physical_graph = nx.convert_node_labels_to_integers(ensure_neighbors(physical_graph))
|
576 |
|
577 |
+
# unconnected nodes: link or drop?
|
578 |
+
networks = {
|
579 |
+
"physical": {"network": physical_graph, "type": "physical", "positions": physical_graph_points, 'network_data_to_keep':{'radius':radius},'homophily':0. }}
|
|
|
580 |
|
|
|
|
|
|
|
581 |
|
582 |
+
model = PoliticalModel(100, networks, .5, .5,.5, half_life=20, print_agents=False, print_frequency=50, agent_reporters=True, intervention_list=[])
|
|
|
|
|
|
|
|
|
583 |
|
584 |
|
585 |
+
for _ in tqdm.tqdm_notebook(range(40)): # Run for specified number of steps
|
586 |
+
model.step()
|
587 |
|
|
|
588 |
import matplotlib.pyplot as plt
|
589 |
+
import pandas as pd
|
590 |
+
|
591 |
+
# Assuming 'model' is defined and has a datacollector with the necessary data
|
592 |
+
agent_df = model.datacollector.get_agent_vars_dataframe().reset_index()
|
593 |
+
|
594 |
+
# Pivot the dataframe for Estimation
|
595 |
+
agent_df_pivot = agent_df.pivot(index='Step', columns='AgentID', values='Estimation')
|
596 |
+
|
597 |
+
# Create the result plot
|
598 |
+
run_plot, ax = plt.subplots(figsize=(12, 8))
|
599 |
+
|
600 |
+
# Define colors for Dissident and Supporter
|
601 |
+
colors = {1: '#d6a44b', 0: '#1b4968'} # 1 for Dissident, 0 for Supporter
|
602 |
+
labels = {1: 'Dissident', 0: 'Supporter'}
|
603 |
+
legend_handles = []
|
604 |
+
|
605 |
+
# Plot each agent's data
|
606 |
+
for agent_id in agent_df_pivot.columns:
|
607 |
+
# Get the agent type (Dissident or Supporter)
|
608 |
+
agent_type = agent_df[agent_df['AgentID'] == agent_id]['Dissident'].iloc[0]
|
609 |
+
|
610 |
+
# Plot
|
611 |
+
line, = plt.plot(agent_df_pivot.index, agent_df_pivot[agent_id], color=colors[agent_type], alpha=0.1)
|
612 |
+
|
613 |
+
|
614 |
+
# Compute and plot the mean estimation for each group
|
615 |
+
for agent_type, color in colors.items():
|
616 |
+
mean_estimation = agent_df_pivot.loc[:, agent_df[agent_df['Dissident'] == agent_type]['AgentID']].mean(axis=1)
|
617 |
+
plt.plot(mean_estimation.index, mean_estimation, color=color, linewidth=2, label=f'{labels[agent_type]}')
|
618 |
+
|
619 |
+
# Set the plot title and labels
|
620 |
+
plt.title('Agent Estimation Over Time', loc='right')
|
621 |
+
plt.xlabel('Time step')
|
622 |
+
plt.ylabel('Estimation')
|
623 |
+
|
624 |
+
# Add legend
|
625 |
+
plt.legend(loc='lower right')
|
626 |
+
|
627 |
+
|
628 |
+
plt.show()
|
629 |
|
630 |
+
def run_and_plot_simulation(separate_agent_types=False,n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=40, half_life=20,
|
631 |
+
phys_network_radius=.06, powerlaw_exponent=3,physical_network_type='physical_network_type_fully_connected',
|
632 |
+
introduce_physical_homophily_true_false=False,physical_homophily=.5,
|
633 |
+
introduce_social_media_homophily_true_false=False,social_media_homophily=5,social_media_network_type_random_geometric_radius=.07,social_media_network_type_powerlaw_exponent=3,
|
634 |
+
social_media_network_type='Powerlaw',use_social_media_network=False):
|
635 |
|
636 |
+
print(physical_network_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
637 |
|