Spaces:
Runtime error
Runtime error
# -*- coding: utf-8 -*- | |
"""revolutions_exploration.ipynb | |
Automatically generated by Colaboratory. | |
Original file is located at | |
https://colab.research.google.com/drive/1omNn2hrbDL_s1qwCOr7ViaIjrRW61YDt | |
""" | |
# !pip install gradio | |
# Commented out IPython magic to ensure Python compatibility. | |
# | |
# %%capture | |
# import multiprocessing | |
# | |
# multiprocessing.cpu_count() | |
# | |
# !pip install cmocean | |
# !pip install git+https://github.com/MNoichl/mesa | |
# | |
# !pip install compress-pickle --quiet | |
import random | |
import pandas as pd | |
from mesa import Agent, Model | |
from mesa.space import MultiGrid | |
import networkx as nx | |
from mesa.time import RandomActivation | |
from mesa.datacollection import DataCollector | |
import numpy as np | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
import matplotlib as mpl | |
import cmocean | |
import tqdm | |
import scipy as sp | |
# from compress_pickle import dump, load | |
from scipy.stats import beta | |
# Commented out IPython magic to ensure Python compatibility. | |
# %%capture | |
# !pip install git+https://github.com/MNoichl/opinionated.git#egg=opinionated | |
import opinionated | |
import matplotlib.pyplot as plt | |
plt.style.use("opinionated_rc") | |
from opinionated.core import download_googlefont | |
download_googlefont('Quicksand', add_to_cache=True) | |
plt.rc('font', family='Quicksand') | |
experiences = { | |
'dissident_experiences': [1,0,0], | |
'supporter_experiences': [1,1,1], | |
} | |
def apply_half_life_decay(data_list, half_life, decay_factors=None): | |
steps = len(data_list) | |
# Check if decay_factors are provided and are of the correct length | |
if decay_factors is None or len(decay_factors) < steps: | |
decay_factors = [0.5 ** (i / half_life) for i in range(steps)] | |
decayed_list = [data_list[i] * decay_factors[steps - 1 - i] for i in range(steps)] | |
return decayed_list | |
half_life=20 | |
decay_factors = [0.5 ** (i / half_life) for i in range(200)] | |
def get_beta_mean_from_experience_dict(experiences, half_life=20,decay_factors=None): #note: precomputed decay supersedes halflife! | |
eta = 1e-10 | |
return beta.mean(sum(apply_half_life_decay(experiences['dissident_experiences'], half_life,decay_factors))+eta, | |
sum(apply_half_life_decay(experiences['supporter_experiences'], half_life,decay_factors))+eta) | |
def get_beta_sample_from_experience_dict(experiences, half_life=20,decay_factors=None): | |
eta = 1e-10 | |
# print(sum(apply_half_life_decay(experiences['dissident_experiences'], half_life))) | |
# print(sum(apply_half_life_decay(experiences['supporter_experiences'], half_life))) | |
return beta.rvs(sum(apply_half_life_decay(experiences['dissident_experiences'], half_life,decay_factors))+eta, | |
sum(apply_half_life_decay(experiences['supporter_experiences'], half_life,decay_factors))+eta, size=1)[0] | |
print(get_beta_mean_from_experience_dict(experiences,half_life,decay_factors)) | |
print(get_beta_sample_from_experience_dict(experiences,half_life)) | |
#@title Load network functionality | |
def generate_community_points(num_communities, total_nodes, powerlaw_exponent=2.0, sigma=0.05, plot=False): | |
""" | |
This function generates points in 2D space, where points are grouped into communities. | |
Each community is represented by a Gaussian distribution. | |
Args: | |
num_communities (int): Number of communities (gaussian distributions). | |
total_nodes (int): Total number of points to be generated. | |
powerlaw_exponent (float): The power law exponent for the powerlaw sequence. | |
sigma (float): The standard deviation for the gaussian distributions. | |
plot (bool): If True, the function plots the generated points. | |
Returns: | |
numpy.ndarray: An array of generated points. | |
""" | |
# Sample from a powerlaw distribution | |
sequence = nx.utils.powerlaw_sequence(num_communities, powerlaw_exponent) | |
# Normalize sequence to represent probabilities | |
probabilities = sequence / np.sum(sequence) | |
# Assign nodes to communities based on probabilities | |
community_assignments = np.random.choice(num_communities, size=total_nodes, p=probabilities) | |
# Calculate community_sizes from community_assignments | |
community_sizes = np.bincount(community_assignments) | |
# Ensure community_sizes has length equal to num_communities | |
if len(community_sizes) < num_communities: | |
community_sizes = np.pad(community_sizes, (0, num_communities - len(community_sizes)), 'constant') | |
points = [] | |
community_centers = [] | |
# For each community | |
for i in range(num_communities): | |
# Create a random center for this community | |
center = np.random.rand(2) | |
community_centers.append(center) | |
# Sample from Gaussian distributions with the center and sigma | |
community_points = np.random.normal(center, sigma, (community_sizes[i], 2)) | |
points.append(community_points) | |
points = np.concatenate(points) | |
# Optional plotting | |
if plot: | |
plt.figure(figsize=(8,8)) | |
plt.scatter(points[:, 0], points[:, 1], alpha=0.5) | |
# for center in community_centers: | |
sns.kdeplot(x=points[:, 0], y=points[:, 1], levels=5, color="k", linewidths=1) | |
# plt.xlim(0, 1) | |
# plt.ylim(0, 1) | |
plt.show() | |
return points | |
def graph_from_coordinates(coords, radius): | |
""" | |
This function creates a random geometric graph from an array of coordinates. | |
Args: | |
coords (numpy.ndarray): An array of coordinates. | |
radius (float): A radius of circles or spheres. | |
Returns: | |
networkx.Graph: The created graph. | |
""" | |
# Create a KDTree for efficient query | |
kdtree = sp.spatial.cKDTree(coords) | |
edge_indexes = kdtree.query_pairs(radius) | |
g = nx.Graph() | |
g.add_nodes_from(list(range(len(coords)))) | |
g.add_edges_from(edge_indexes) | |
return g | |
def plot_graph(graph, positions): | |
""" | |
This function plots a graph with the given positions. | |
Args: | |
graph (networkx.Graph): The graph to be plotted. | |
positions (dict): A dictionary of positions for the nodes. | |
""" | |
plt.figure(figsize=(8,8)) | |
pos_dict = {i: positions[i] for i in range(len(positions))} | |
nx.draw_networkx_nodes(graph, pos_dict, node_size=30, node_color="#1a2340", alpha=0.7) | |
nx.draw_networkx_edges(graph, pos_dict, edge_color="grey", width=1, alpha=1) | |
plt.show() | |
def ensure_neighbors(graph): | |
""" | |
Ensure that all nodes in a NetworkX graph have at least one neighbor. | |
Parameters: | |
graph (networkx.Graph): The NetworkX graph to check. | |
Returns: | |
networkx.Graph: The updated NetworkX graph where all nodes have at least one neighbor. | |
""" | |
nodes = list(graph.nodes()) | |
for node in nodes: | |
if len(list(graph.neighbors(node))) == 0: | |
# The node has no neighbors, so select another node to connect it with | |
other_node = random.choice(nodes) | |
while other_node == node: # Make sure we don't connect the node to itself | |
other_node = random.choice(nodes) | |
graph.add_edge(node, other_node) | |
return graph | |
def compute_homophily(G,attr_name='attr'): | |
same_attribute_edges = sum(G.nodes[n1][attr_name] == G.nodes[n2][attr_name] for n1, n2 in G.edges()) | |
total_edges = G.number_of_edges() | |
return same_attribute_edges / total_edges if total_edges > 0 else 0 | |
def assign_initial_attributes(G, ratio,attr_name='attr'): | |
nodes = list(G.nodes) | |
random.shuffle(nodes) | |
attr_boundary = int(ratio * len(nodes)) | |
for i, node in enumerate(nodes): | |
G.nodes[node][attr_name] = 0 if i < attr_boundary else 1 | |
return G | |
def distribute_attributes(G, target_homophily, seed=None, max_iter=10000, cooling_factor=0.9995,attr_name='attr'): | |
random.seed(seed) | |
current_homophily = compute_homophily(G,attr_name) | |
temp = 1.0 | |
for i in range(max_iter): | |
# pick two random nodes with different attributes and swap their attributes | |
nodes = list(G.nodes) | |
random.shuffle(nodes) | |
for node1, node2 in zip(nodes[::2], nodes[1::2]): | |
if G.nodes[node1][attr_name] != G.nodes[node2][attr_name]: | |
G.nodes[node1][attr_name], G.nodes[node2][attr_name] = G.nodes[node2][attr_name], G.nodes[node1][attr_name] | |
break | |
new_homophily = compute_homophily(G,attr_name) | |
delta_homophily = new_homophily - current_homophily | |
dir_factor = np.sign(target_homophily - current_homophily) | |
# if the new homophily is closer to the target, or if the simulated annealing condition is met, accept the swap | |
if abs(new_homophily - target_homophily) < abs(current_homophily - target_homophily) or \ | |
(delta_homophily / temp < 700 and random.random() < np.exp(dir_factor * delta_homophily / temp)): | |
current_homophily = new_homophily | |
else: # else, undo the swap | |
G.nodes[node1][attr_name], G.nodes[node2][attr_name] = G.nodes[node2][attr_name], G.nodes[node1][attr_name] | |
temp *= cooling_factor # cool down | |
return G | |
def reindex_graph_to_match_attributes(G1, G2, attr_name): | |
# Get a sorted list of nodes in G1 based on the attribute | |
G1_sorted_nodes = sorted(G1.nodes(data=True), key=lambda x: x[1][attr_name]) | |
# Get a sorted list of nodes in G2 based on the attribute | |
G2_sorted_nodes = sorted(G2.nodes(data=True), key=lambda x: x[1][attr_name]) | |
# Create a mapping from the G2 node IDs to the G1 node IDs | |
mapping = {G2_node[0]: G1_node[0] for G2_node, G1_node in zip(G2_sorted_nodes, G1_sorted_nodes)} | |
# Generate the new graph with the updated nodes | |
G2_updated = nx.relabel_nodes(G2, mapping) | |
return G2_updated | |
########################## | |
def compute_mean(model): | |
agent_estimations = [agent.estimation for agent in model.schedule.agents] | |
return np.mean(agent_estimations) | |
def compute_median(model): | |
agent_estimations = [agent.estimation for agent in model.schedule.agents] | |
return np.median(agent_estimations) | |
def compute_std(model): | |
agent_estimations = [agent.estimation for agent in model.schedule.agents] | |
return np.std(agent_estimations) | |
class PoliticalAgent(Agent): | |
"""An agent in the political model. | |
Attributes: | |
estimation (float): Agent's current expectation of political change. | |
dissident (bool): True if the agent supports a regime change, False otherwise. | |
networks_estimations (dict): A dictionary storing the estimations of the agent for each network. | |
""" | |
def __init__(self, unique_id, model, dissident): | |
super().__init__(unique_id, model) | |
self.experiences = { | |
'dissident_experiences': [1], | |
'supporter_experiences': [1], | |
} | |
# self.estimation = estimation | |
self.estimations = [] | |
self.estimation = .5 #hardcoded_mean, will change in first step if agent interacts. | |
self.experiments = [] | |
self.dissident = dissident | |
# self.historical_estimations = [] | |
def update_estimation(self, network_id): | |
"""Update the agent's estimation for a given network.""" | |
# Get the neighbors from the network | |
potential_partners = [self.model.schedule.agents[n] for n in self.model.networks[network_id]['network'].neighbors(self.unique_id)] | |
current_estimate =get_beta_mean_from_experience_dict(self.experiences,half_life=self.model.half_life,decay_factors=self.model.decay_factors) | |
self.estimations.append(current_estimate) | |
self.estimation =current_estimate | |
current_experiment = get_beta_sample_from_experience_dict(self.experiences,half_life=self.model.half_life, decay_factors=self.model.decay_factors) | |
self.experiments.append(current_experiment) | |
if potential_partners: | |
partner = random.choice(potential_partners) | |
if self.model.networks[network_id]['type'] == 'physical': | |
if current_experiment >= self.model.threshold: | |
if partner.dissident: # removed division by 100? | |
self.experiences['dissident_experiences'].append(1) | |
self.experiences['supporter_experiences'].append(0) | |
else: | |
self.experiences['dissident_experiences'].append(0) | |
self.experiences['supporter_experiences'].append(1) | |
partner.experiences['dissident_experiences'].append(1 * self.model.social_learning_factor) | |
partner.experiences['supporter_experiences'].append(0) | |
else: | |
partner.experiences['dissident_experiences'].append(0) | |
partner.experiences['supporter_experiences'].append(1 * self.model.social_learning_factor) | |
# else: | |
# pass | |
# Only one network for the moment! | |
elif self.model.networks[network_id]['type'] == 'social_media': | |
if partner.dissident: # removed division by 100? | |
self.experiences['dissident_experiences'].append(1 * self.model.social_media_factor) | |
self.experiences['supporter_experiences'].append(0) | |
else: | |
self.experiences['dissident_experiences'].append(0) | |
self.experiences['supporter_experiences'].append(1 * self.model.social_media_factor) | |
# self.networks_estimations[network_id] = self.estimation | |
def combine_estimations(self): | |
# """Combine the estimations from all networks using a bounded confidence model.""" | |
values = [list(d.values())[0] for d in self.current_estimations] | |
if len(values) > 0: | |
# Filter the network estimations based on the bounded confidence range | |
within_range = [value for value in values if abs(self.estimation - value) <= self.model.bounded_confidence_range] | |
# If there are any estimations within the range, update the estimation | |
if len(within_range) > 0: | |
self.estimation = np.mean(within_range) | |
def step(self): | |
"""Agent step function which updates the estimation for each network and then combines the estimations.""" | |
if not hasattr(self, 'current_estimations'): # agents might already have this attribute because they were partnered up in the past. | |
self.current_estimations = [] | |
for network_id in self.model.networks.keys(): | |
self.update_estimation(network_id) | |
self.combine_estimations() | |
# self.historical_estimations.append(self.current_estimations) | |
del self.current_estimations | |
class PoliticalModel(Model): | |
"""A model of a political system with multiple interacting agents. | |
Attributes: | |
networks (dict): A dictionary of networks with network IDs as keys and NetworkX Graph objects as values. | |
""" | |
def __init__(self, n_agents, networks, share_regime_supporters, | |
# initial_expectation_of_change, | |
threshold, | |
social_learning_factor=1,social_media_factor=1, # one for equal learning, lower gets discounted | |
half_life=20, print_agents=False, print_frequency=30, | |
early_stopping_steps=20, early_stopping_range=0.01, agent_reporters=True,intervention_list=[],randomID=False): | |
self.num_agents = n_agents | |
self.threshold = threshold | |
self.social_learning_factor = social_learning_factor | |
self.social_media_factor = social_media_factor | |
self.print_agents_state = print_agents | |
self.half_life = half_life | |
self.intervention_list = intervention_list | |
self.model_id = randomID | |
self.print_frequency = print_frequency | |
self.early_stopping_steps = early_stopping_steps | |
self.early_stopping_range = early_stopping_range | |
self.mean_estimations = [] | |
self.decay_factors = [0.5 ** (i / self.half_life) for i in range(500)] # Nte this should be larger than | |
# we could use this for early stopping! | |
self.running = True | |
self.share_regime_supporters = share_regime_supporters | |
self.schedule = RandomActivation(self) | |
self.networks = networks | |
# Assign dissident as argument to networks, compute homophilies, and match up the networks so that the same id leads to the same atrribute | |
for i, this_network in enumerate(self.networks): | |
self.networks[this_network]["network"] = assign_initial_attributes(self.networks[this_network]["network"],self.share_regime_supporters,attr_name='dissident') | |
if 'homophily' in self.networks[this_network]: | |
self.networks[this_network]["network"] = distribute_attributes(self.networks[this_network]["network"], | |
self.networks[this_network]['homophily'], max_iter=5000, cooling_factor=0.995,attr_name='dissident') | |
self.networks[this_network]['network_data_to_keep']['actual_homophily'] = compute_homophily(self.networks[this_network]["network"],attr_name='dissident') | |
if i>0: | |
self.networks[this_network]["network"] = reindex_graph_to_match_attributes(self.networks[next(iter(self.networks))]["network"], self.networks[this_network]["network"], 'dissident') | |
# print(self.networks) | |
for i in range(self.num_agents): | |
# estimation = random.normalvariate(initial_expectation_of_change, 0.2) We set a flat prior now | |
agent = PoliticalAgent(i, self, self.networks[next(iter(self.networks))]["network"].nodes(data=True)[i]['dissident']) | |
self.schedule.add(agent) | |
# Should we update to the real share here?! | |
#################### | |
# Keep the attributes in the model and define model reporters | |
model_reporters = { | |
"Mean": compute_mean, | |
"Median": compute_median, | |
"STD": compute_std | |
} | |
for this_network in self.networks: | |
if 'network_data_to_keep' in self.networks[this_network]: | |
for key, value in self.networks[this_network]['network_data_to_keep'].items(): | |
attr_name = this_network + '_' + key | |
setattr(self, attr_name, value) | |
# Define a reporter function for this attribute | |
def reporter(model, attr_name=attr_name): | |
return getattr(model, attr_name) | |
# Add the reporter function to the dictionary | |
model_reporters[attr_name] = reporter | |
# Initialize DataCollector with the dynamic model reporters | |
if agent_reporters: | |
self.datacollector = DataCollector( | |
model_reporters=model_reporters, | |
agent_reporters={"Estimation": "estimation", "Dissident": "dissident"}#, "Historical Estimations": "historical_estimations"} | |
) | |
else: | |
self.datacollector = DataCollector( | |
model_reporters=model_reporters | |
) | |
def step(self): | |
"""Model step function which activates the step function of each agent.""" | |
self.datacollector.collect(self) # Collect data | |
# do interventions, if present: | |
for this_intervention in self.intervention_list: | |
# print(this_intervention) | |
if this_intervention['time'] == len(self.mean_estimations): | |
if this_intervention['type'] == 'threshold_adjustment': | |
self.threshold = max(0, min(1, self.threshold + this_intervention['strength'])) | |
if this_intervention['type'] == 'share_adjustment': | |
target_supporter_share = max(0, min(1, self.share_regime_supporters + this_intervention['strength'])) | |
agents = [self.schedule._agents[i] for i in self.schedule._agents] | |
current_supporters = sum(not agent.dissident for agent in agents) | |
total_agents = len(agents) | |
current_share = current_supporters / total_agents | |
# Calculate the number of agents to change | |
required_supporters = int(target_supporter_share * total_agents) | |
agents_to_change = abs(required_supporters - current_supporters) | |
if current_share < target_supporter_share: | |
# Not enough supporters, need to increase | |
dissidents = [agent for agent in agents if agent.dissident] | |
for agent in random.sample(dissidents, agents_to_change): | |
agent.dissident = False | |
elif current_share > target_supporter_share: | |
# Too many supporters, need to reduce | |
supporters = [agent for agent in agents if not agent.dissident] | |
for agent in random.sample(supporters, agents_to_change): | |
agent.dissident = True | |
# print(self.threshold) | |
if this_intervention['type'] == 'social_media_adjustment': | |
self.social_media_factor = max(0, min(1, self.social_media_factor + this_intervention['strength'])) | |
self.schedule.step() | |
current_mean_estimation = compute_mean(self) | |
self.mean_estimations.append(current_mean_estimation) | |
# Implement the early stopping criteria | |
if len(self.mean_estimations) >= self.early_stopping_steps: | |
recent_means = self.mean_estimations[-self.early_stopping_steps:] | |
if max(recent_means) - min(recent_means) < self.early_stopping_range: | |
# if self.print_agents_state: | |
# print('Early stopping at: ', self.schedule.steps) | |
# self.print_agents() | |
self.running = False | |
# if self.print_agents_state and (self.schedule.steps % self.print_frequency == 0 or self.schedule.steps == 1): | |
# print(self.schedule.steps) | |
# self.print_agents() | |
def run_simulation(n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=400, half_life=20): | |
# Helper functions like graph_from_coordinates, ensure_neighbors should be defined outside this function | |
# Complete graph | |
G = nx.complete_graph(n_agents) | |
# Networks dictionary | |
networks = { | |
"physical": {"network": G, "type": "physical", "positions": nx.circular_layout(G)}#kamada_kawai | |
} | |
# Intervention list | |
intervention_list = [ ] | |
# Initialize the model | |
model = PoliticalModel(n_agents, networks, share_regime_supporters, threshold, | |
social_learning_factor, half_life=half_life, print_agents=False, print_frequency=50, agent_reporters=True, intervention_list=intervention_list) | |
# Run the model | |
for _ in tqdm.tqdm_notebook(range(simulation_steps)): # Run for specified number of steps | |
model.step() | |
return model | |
# Example usage | |
def run_and_plot_simulation(n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=40, half_life=20): | |
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) | |
# Get data and reset index | |
agent_df = model.datacollector.get_agent_vars_dataframe().reset_index() | |
# Pivot the dataframe | |
agent_df_pivot = agent_df.pivot(index='Step', columns='AgentID', values='Estimation') | |
# Create the plot | |
fig, ax = plt.subplots(figsize=(12, 8)) | |
for column in agent_df_pivot.columns: | |
plt.plot(agent_df_pivot.index, agent_df_pivot[column], color='gray', alpha=0.1) | |
# Compute and plot the mean estimation | |
mean_estimation = agent_df_pivot.mean(axis=1) | |
plt.plot(mean_estimation.index, mean_estimation, color='black', linewidth=2) | |
# Set the plot title and labels | |
plt.title('Agent Estimation Over Time') | |
plt.xlabel('Time step') | |
plt.ylabel('Estimation') | |
return fig | |
# run_and_plot_simulation(n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=40, half_life=20) | |
import gradio as gr | |
import matplotlib.pyplot as plt | |
# Gradio interface | |
with gr.Blocks(theme=gr.themes.Monochrome()) as demo: | |
with gr.Column(): | |
gr.Markdown("# Simulation Visualization Interface") | |
with gr.Row(): | |
with gr.Column(): | |
# Sliders for each parameter | |
n_agents_slider = gr.Slider(minimum=100, maximum=500, step=10, label="Number of Agents", value=150) | |
share_regime_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Share of Regime Supporters", value=0.4) | |
threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Threshold", value=0.5) | |
social_learning_slider = gr.Slider(minimum=0.0, maximum=2.0, step=0.1, label="Social Learning Factor", value=1.0) | |
steps_slider = gr.Slider(minimum=10, maximum=100, step=5, label="Simulation Steps", value=40) | |
half_life_slider = gr.Slider(minimum=5, maximum=50, step=5, label="Half-Life", value=20) | |
with gr.Column(): | |
# Button to trigger the simulation | |
button = gr.Button("Run Simulation") | |
plot_output = gr.Plot(label="Simulation Result") | |
# Function to call when button is clicked | |
def run_simulation_and_plot(*args): | |
fig = run_and_plot_simulation(*args) | |
return fig | |
# Setting up the button click event | |
button.click( | |
run_simulation_and_plot, | |
inputs=[n_agents_slider, share_regime_slider, threshold_slider, social_learning_slider, steps_slider, half_life_slider], | |
outputs=[plot_output] | |
) | |
# Launch the interface | |
if __name__ == "__main__": | |
demo.launch(debug=True) | |