return {'status_1': {node: node_update}} def update_2(): s2 = model.get_state('status_2') + 0.1 return {'status_2': s2} update_cfg = UpdateConfiguration({ 'arguments': {}, # No other arguments 'get_nodes': True, # We want the nodes as argument 'update_type': UpdateType.STATE }) u = Update(update_1, update_cfg ) # Create an Update object that contains the object function # Create a scheme with the correct sample function, parameters, bounds, and update function model.add_scheme( Scheme( sample_state_weighted, { 'args': { 'graph': model.graph }, 'lower_bound': 2, 'upper_bound': 3, 'updates': [u] })) # Add update 2 to the model, which will increase status_1 by 0.5 each iteration model.add_update(update_2) model.set_initial_state(initial_state) output = model.simulate(5) print(output)
model.set_states(['I', 'A', 'O']) update_cfg = UpdateConfiguration({ 'arguments': { 'constants': model.constants }, 'get_nodes': True }) up_I_A = Update(update_I_A, update_cfg) s_I = Update(shrink_I) s_A = Update(shrink_A) model.add_scheme( Scheme(sample_attention_weighted, { 'args': { 'graph': model.graph }, 'updates': [up_I_A] })) model.add_scheme( Scheme(lambda graph: graph.nodes, { 'args': { 'graph': model.graph }, 'lower_bound': 5000, 'updates': [s_I] })) model.add_scheme( Scheme(lambda graph: graph.nodes, { 'args': { 'graph': model.graph
class HIOM(Example): def __init__(self): n = 400 g = nx.watts_strogatz_graph(n, 2, 0.02) cfg = { 'utility': False, } self.model = Model(g, ModelConfiguration(cfg)) constants = { 'dt': 0.01, 'A_min': -0.5, 'A_star': 1, 's_O': 0.01, 's_I': 0, 'd_A': 0, 'p': 1, 'r_min': 0, 't_O': np.inf, 'N': n } def initial_I(constants): return np.random.normal(0, 0.3, constants['N']) def initial_O(constants): return np.random.normal(0, 0.2, constants['N']) initial_state = { 'I': initial_I, 'O': initial_O, 'A': 1 } def update_I_A(nodes, constants): node = nodes[0] nb = np.random.choice(self.model.get_neighbors(node)) if abs(self.model.get_node_state(node, 'O') - self.model.get_node_state(nb, 'O')) > constants['t_O']: return {'I': self.model.get_node_state(node, 'I')} else: # Update information r = constants['r_min'] + (1 - constants['r_min']) / (1 + np.exp(-1 * constants['p'] * (self.model.get_node_state(node, 'O') - self.model.get_node_state(nb, 'O')))) inf = r * self.model.get_node_state(node, 'I') + (1-r) * self.model.get_node_state(nb, 'I') + np.random.normal(0, constants['s_I']) # Update attention node_A = self.model.get_node_state(node, 'A') + constants['d_A'] * (2 * constants['A_star'] - self.model.get_node_state(node, 'A')) nb_A = self.model.get_node_state(nb, 'A') + constants['d_A'] * (2 * constants['A_star'] - self.model.get_node_state(nb, 'A')) return {'I': [inf], 'A': {node: node_A, nb: nb_A}} def update_A(constants): return {'A': self.model.get_state('A') - 2 * constants['d_A'] * self.model.get_state('A')/constants['N']} def update_O(constants): noise = np.random.normal(0, constants['s_O'], constants['N']) x = self.model.get_state('O') - constants['dt'] * (self.model.get_state('O')**3 - (self.model.get_state('A') + constants['A_min']) * self.model.get_state('O') - self.model.get_state('I')) + noise return {'O': x} def shrink_I(): return {'I': self.model.get_state('I') * 0.999} def shrink_A(): return {'A': self.model.get_state('A') * 0.999} def sample_attention_weighted(graph): probs = [] A = self.model.get_state('A') factor = 1.0/sum(A) for a in A: probs.append(a * factor) return np.random.choice(graph.nodes, size=1, replace=False, p=probs) # Model definition self.model.constants = constants self.model.set_states(['I', 'A', 'O']) update_cfg = UpdateConfiguration({ 'arguments': {'constants': self.model.constants}, 'get_nodes': True }) up_I_A = Update(update_I_A, update_cfg) s_I = Update(shrink_I) s_A = Update(shrink_A) self.model.add_scheme(Scheme(sample_attention_weighted, {'args': {'graph': self.model.graph}, 'updates': [up_I_A]})) self.model.add_scheme(Scheme(lambda graph: graph.nodes, {'args': {'graph': self.model.graph}, 'lower_bound': 5000, 'updates': [s_I]})) self.model.add_scheme(Scheme(lambda graph: graph.nodes, {'args': {'graph': self.model.graph}, 'lower_bound': 10000, 'updates': [s_A]})) self.model.add_update(update_A, {'constants': self.model.constants}) self.model.add_update(update_O, {'constants': self.model.constants}) self.model.set_initial_state(initial_state, {'constants': self.model.constants}) def simulate(self, n): return self.model.simulate(n) def visualize(self, iterations): visualization_config = { 'layout': 'fr', 'plot_interval': 100, 'plot_variable': 'O', 'variable_limits': { 'A': [0, 1], 'O': [-1, 1], 'I': [-1, 1] }, 'cmin': -1, 'cmax': 1, 'color_scale': 'RdBu', 'show_plot': True, # 'plot_output': '../animations/HIOM.gif', 'plot_title': 'HIERARCHICAL ISING OPINION MODEL', } self.model.configure_visualization(visualization_config, iterations) self.model.visualize('animation')
model.set_states(['I', 'A', 'O']) model.add_update(update, {'constants': model.constants}) update_network_cfg = UpdateConfiguration({ 'arguments': { 'constants': model.constants }, 'get_nodes': True, 'update_type': UpdateType.NETWORK }) update_network = Update(network_update, update_network_cfg) model.add_scheme( Scheme(sample_attention_weighted, { 'args': { 'graph': model.graph }, 'updates': [update_network] })) activism_update = Update(activism) model.add_scheme( Scheme( lambda graph: graph.nodes, { 'args': { 'graph': model.graph }, 'lower_bound': 300, 'upper_bound': 301, 'updates': [activism_update] }))