def test(): import numpy as np from numpy import diag import numpy.random as rand from numpy.linalg import norm, inv import models from viz import plotNetwork from util import gnp, barabasi_albert, from_edgelist, rowStochastic rand.seed(1233) #A, N = from_edgelist('./networks/facebook_combined.txt') N = 2000 A = gnp(N,0.02) A = rowStochastic(A) s = rand.rand(N) max_rounds = 1000 #A = gnp(N, 0.12, rand_weights=True) B = diag(rand.rand(N)) * 0.5 #models.deGroot(A, s, max_rounds) models.friedkinJohnsen(A, s, max_rounds, conv_stop=False)
def ga(A, B, s, max_rounds, eps=1e-6, plot=False, conv_stop=True, save=False, **kwargs): '''Simulates the Generalized Asymmetric Coevolutionary Game. This model does nto require an adjacency matrix. Connections between nodes are calculated depending on the proximity of their opinions. Args: A (NxN numpy array): Adjacency matrix (its diagonal is the stubborness) B (NxN numpy array): The stubborness of each node s (1xN numpy array): Initial opinions (intrinsic beliefs) vector op_eps: ε parameter of the model max_rounds (int): Maximum number of rounds to simulate eps (double): Maximum difference between rounds before we assume that the model has converged (default: 1e-6) plot (bool): Plot preference (default: False) conv_stop (bool): Stop the simulation if the model has converged (default: True) save (bool): Save the simulation data into text files **kargs: Arguments c, eps, and p for dynamic_weights function (eps and p need to be specified only if c='pow') (default: c='linear') Returns: A txN vector of the opinions of the nodes over time ''' # Check if c function was specified if kwargs: c = kwargs['c'] # Extra parameters for pow function eps_c = kwargs.get('eps', 0.1) p_c = kwargs.get('eps', 2) else: # Otherwise use linear as default c = 'linear' eps_c = None p_c = None N, z, max_rounds = preprocessArgs(s, max_rounds) opinions = np.zeros((max_rounds, N)) opinions[0, :] = s for t in trange(1, max_rounds): Q = dynamic_weights(A, s, z, c, eps_c, p_c) + B Q = rowStochastic(Q) B_temp = np.diag(np.diag(Q)) Q = Q - B_temp z = np.dot(Q, z) + np.dot(B_temp, s) opinions[t, :] = z if conv_stop and \ norm(opinions[t - 1, :] - opinions[t, :], np.inf) < eps: print('G-A converged after {t} rounds'.format(t=t)) break if plot: plotOpinions(opinions[0:t + 1, :], 'Hegselmann-Krause', dcolor=True) if save: timeStr = datetime.now().strftime("%m%d%H%M") simid = 'ga' + timeStr saveModelData(simid, N=N, max_rounds=max_rounds, eps=eps, rounds_run=t + 1, A=A, s=s, B=B, c=c, eps_c=eps_c, p_c=p_c, opinions=opinions[0:t + 1, :]) return opinions[0:t + 1, :]
def ga(A, B, s, max_rounds, eps=1e-6, plot=False, conv_stop=True, save=False, **kwargs): '''Simulates the Generalized Asymmetric Coevolutionary Game. This model does nto require an adjacency matrix. Connections between nodes are calculated depending on the proximity of their opinions. Args: A (NxN numpy array): Adjacency matrix (its diagonal is the stubborness) B (NxN numpy array): The stubborness of each node s (1xN numpy array): Initial opinions (intrinsic beliefs) vector op_eps: ε parameter of the model max_rounds (int): Maximum number of rounds to simulate eps (double): Maximum difference between rounds before we assume that the model has converged (default: 1e-6) plot (bool): Plot preference (default: False) conv_stop (bool): Stop the simulation if the model has converged (default: True) save (bool): Save the simulation data into text files **kargs: Arguments c, eps, and p for dynamic_weights function (eps and p need to be specified only if c='pow') (default: c='linear') Returns: A txN vector of the opinions of the nodes over time ''' # Check if c function was specified if kwargs: c = kwargs['c'] # Extra parameters for pow function eps_c = kwargs.get('eps', 0.1) p_c = kwargs.get('eps', 2) else: # Otherwise use linear as default c = 'linear' eps_c = None p_c = None N, z, max_rounds = preprocessArgs(s, max_rounds) opinions = np.zeros((max_rounds, N)) opinions[0, :] = s for t in trange(1, max_rounds): Q = dynamic_weights(A, s, z, c, eps_c, p_c) + B Q = rowStochastic(Q) B_temp = np.diag(np.diag(Q)) Q = Q - B_temp z = np.dot(Q, z) + np.dot(B_temp, s) opinions[t, :] = z if conv_stop and \ norm(opinions[t - 1, :] - opinions[t, :], np.inf) < eps: print('G-A converged after {t} rounds'.format(t=t)) break if plot: plotOpinions(opinions[0:t+1, :], 'Hegselmann-Krause', dcolor=True) if save: timeStr = datetime.now().strftime("%m%d%H%M") simid = 'ga' + timeStr saveModelData(simid, N=N, max_rounds=max_rounds, eps=eps, rounds_run=t+1, A=A, s=s, B=B, c=c, eps_c=eps_c, p_c=p_c, opinions=opinions[0:t+1, :]) return opinions[0:t+1, :]