Updated data generation code

This commit is contained in:
2025-09-01 14:46:34 -04:00
parent d998f6de4c
commit e018238935
14 changed files with 709 additions and 123 deletions

View File

@@ -8,7 +8,9 @@ import networkx as nx
import networkx.algorithms.community as nx_comm
from tqdm import tqdm
import sims
from dotenv import load_dotenv
load_dotenv()
def generate_connected_graph(rng: np.random.Generator, num_agents: int, graph_type: str) -> tuple[nx.Graph, str, np.ndarray]:
@@ -21,35 +23,34 @@ def generate_connected_graph(rng: np.random.Generator, num_agents: int, graph_ty
"""
G = None
if graph_type == "erdos_renyi":
# p = 2.0 * np.log(num_agents) / num_agents
p = 0.5
G = nx.erdos_renyi_graph(num_agents, p, seed=rng)
elif graph_type == "watts_strogatz":
k = 2
p = 0.1
k = 4
p = 0.5
G = nx.watts_strogatz_graph(num_agents, k, p, seed=rng)
elif graph_type == "barabasi_albert":
m = 2
if m >= num_agents: m = max(1, num_agents - 1)
G = nx.barabasi_albert_graph(num_agents, m, seed=rng)
G = nx.barabasi_albert_graph(num_agents, 2, seed=rng)
elif graph_type == "powerlaw_cluster":
m = 3 # Number of random edges to add for each new node
p = 0.1 # Probability of adding a triangle after adding a random edge
if m >= num_agents: m = max(1, num_agents - 1)
m = 2
p = 0.5
G = nx.powerlaw_cluster_graph(num_agents, m, p, seed=rng)
# Add self-loops, as they are often assumed in consensus algorithms
G.add_edges_from([(i, i) for i in range(num_agents)])
adj_matrix = nx.to_numpy_array(G, dtype=np.float32)
return G, graph_type, adj_matrix
graph_matrix = nx.adjacency_matrix(G).todense()
for i in range(num_agents):
graph_matrix[i,i]=1
G = nx.Graph(graph_matrix)
return G, graph_type, graph_matrix
def calculate_graph_metrics(G: nx.Graph) -> dict:
"""
@@ -91,30 +92,30 @@ def calculate_graph_metrics(G: nx.Graph) -> dict:
# --- Spectral & Community Metrics (Potentially Slow) ---
# These are also limited to smaller graphs.
if 1 < num_nodes < 500:
# Eigenvalues of the Laplacian matrix
try:
laplacian_eigenvalues = sorted(nx.laplacian_spectrum(G))
metrics["laplacian_eigenvalues"] = laplacian_eigenvalues
# The second-smallest eigenvalue of the Laplacian matrix
metrics["algebraic_connectivity"] = laplacian_eigenvalues[1]
except Exception:
metrics["laplacian_eigenvalues"] = None
metrics["algebraic_connectivity"] = None
# if 1 < num_nodes < 500:
# # Eigenvalues of the Laplacian matrix
# try:
# laplacian_eigenvalues = sorted(nx.laplacian_spectrum(G))
# metrics["laplacian_eigenvalues"] = laplacian_eigenvalues
# # The second-smallest eigenvalue of the Laplacian matrix
# metrics["algebraic_connectivity"] = laplacian_eigenvalues[1]
# except Exception:
# metrics["laplacian_eigenvalues"] = None
# metrics["algebraic_connectivity"] = None
# Modularity using the Louvain community detection algorithm
if num_edges > 0:
try:
communities = nx_comm.louvain_communities(G, seed=123)
metrics["modularity"] = nx_comm.modularity(G, communities)
except Exception:
metrics["modularity"] = None # Algorithm may fail on some graphs
else:
metrics["modularity"] = None
else:
metrics["laplacian_eigenvalues"] = None
metrics["algebraic_connectivity"] = None
metrics["modularity"] = None
# # Modularity using the Louvain community detection algorithm
# if num_edges > 0:
# try:
# communities = nx_comm.louvain_communities(G, seed=123)
# metrics["modularity"] = nx_comm.modularity(G, communities)
# except Exception:
# metrics["modularity"] = None # Algorithm may fail on some graphs
# else:
# metrics["modularity"] = None
# else:
# metrics["laplacian_eigenvalues"] = None
# metrics["algebraic_connectivity"] = None
# metrics["modularity"] = None
return metrics
@@ -132,9 +133,9 @@ def main():
# --- Configuration ---
GRAPH_GEN_ALGOS = ["erdos_renyi", "barabasi_albert", "powerlaw_cluster", "watts_strogatz"]
AGENT_COUNTS = range(5, 51, 5)# 5, 10, ..., 50 agents
GRAPHS_PER_AGENT_COUNT = 100
GRAPHS_PER_AGENT_COUNT = 80
GRAPHS_PER_GRAPH_ALGO = GRAPHS_PER_AGENT_COUNT // len(GRAPH_GEN_ALGOS)
SIMS_PER_GRAPH = 100
SIMS_PER_GRAPH = 400
OUTPUT_DIR = "datasets/consensus_dataset"
# --- Setup ---