def iteration(g, v): """ Performs a single iteration of the simulation. :param g: Graph `G = (M, C)`. :param v: Set of votes `V` (excluding 0). """ # Activation ratio of a channel `x`. def a(x): return g.edges[x]['a'] c = {c for c in g.edges if trial(a(c))} # Active channels. i = {m: neighbours(m, c) for m in g.nodes} # Active neighbours. w = {(m, n): valuation_density(g, m, n) for m in g.nodes for n in i[m]} for m, data in g.nodes(data=True): if len(i[m]) == 0: # Skip members that did not have active # neighbours to communicate with. continue # Optimisation to prevent multiple evaluations. cached = functools.lru_cache(maxsize=len(v)) data['w'] = cached(average_density(m, i, w)) data['d'] = decision(v, data['w'], data['ro'])
def nodes(g, v, ro): """ Initialises attributes of the nodes of the graph `g`: - 'w' for preference density function; - 'd' for initial decision of a member; - 'ro' for impulsiveness indicator. :param g: Graph. :param v: Set of votes `V` (excluding 0). :param ro: Function that returns impulsiveness indicator. Examples: nodes(g, v, ro=lambda: 25) nodes(g, v, ro=lambda: uniform(20, 30)) """ for _, data in g.nodes(data=True): w = {v: int(uniform() * 10000) for v in v} w = {v: w[v] / sum(w.values()) for v in v} w = function(w) data['w'] = w # Preference density function. data['ro'] = ro() # Impulsiveness indicator. data['d'] = decision(v, data['w'], data['ro']) # Initial decision. assert sum(w(v) for v in v) == 1 assert data['ro'] > 0 assert data['d'] == 0 or data['d'] in v
def leader(g, v, m, j, a=lambda: 1.0): """ Makes a leader out of the member. :param g: Graph. :param v: Set of votes `V` (excluding 0). :param m: The node that will become a leader. :param j: Vote of the leader. :param a: Channel activation function. """ w = {v: uniform(0.001, 0.002) for v in v} w[j] = 1 - sum(w.values()) + w[j] w = function(w) data = g.nodes[m] data['w'] = w # Preference density function. data['d'] = decision(v, data['w'], data['rho']) # Initial decision. for n in g.neighbors(m): r = uniform(0, 0.5) data = g.edges[m, n] data['a'] = a() # Activation ratio. data['d'] = { # Dialogue matrix. (0, 0): r, (0, 1): 1 - r, (1, 0): 0, (1, 1): 0, }