def flockwork_P(N, P, t_run_total, rewiring_rate=1.0, initial_edges=None, seed=0, return_edge_changes_with_histograms=False): r""" Simulate a flockwork P-model where the disconnection rate per node is :math:`\gamma` and reconnection probability is :math:`P`. In order to start with an equilibrated initial state, use :func:`tacoma.flockwork.flockwork_P_equilibrium_configuration` or just pass ``initial_edges = None`` to this function. The total event rate is :math:`N\gamma` such that in one unit of time there's an expected number of :math:`N` events. Parameters ---------- N : int number of nodes P : float The reconnection probability. Has to be :math:`0\leq P\leq 1` t_run_total : float The total run time in units of :math:`\gamma^{-1}`. rewiring_rate : float, default : 1.0 Event rate :math:`\gamma` per node per unit of time. initial_edges : list of tuple of int, default : None The initial state of the network as an edge list. If `None` is provided, the initial state will be taken from an equilibrium configuration generated with :func:`tacoma.flockwork.flockwork_P_equilibrium_configuration` seed : int, default : 0 The random seed. return_edge_changes_with_histograms : bool, default : False Instead of the converted :class:`_tacoma.edge_changes`, return the original instance of :class:`_tacoma.edge_changes_with_histograms`. Returns ------- :class:`_tacoma.edge_changes` The simulated network. if return_edge_changes_with_histograms is `True`, returns an instance of :class:`_tacoma.edge_changes_with_histograms` instead. """ if initial_edges is None: initial_edges = flockwork_P_equilibrium_configuration(N, P) FW = tc.FlockworkPModel(initial_edges, N, rewiring_rate, P, save_temporal_network=True) FW.simulate(t_run_total) this_result = FW.edge_changes return this_result
print("simulation on edge_lists took", end-start,"seconds") pl.plot(SIS.time, SIS.I) mv_SIS = tc.MARKOV_SIS(N,t_run_total*2,infection_rate,recovery_rate,0.01,N//2,seed = seed) start = time.time() _tc.markov_SIS_on_edge_lists(fwP_el,mv_SIS,1.0) #print(mv_SIS.time, mv_SIS.I) end = time.time() print("Markov on edge_lists took", end-start,"seconds") pl.plot(mv_SIS.time, mv_SIS.I) FW = tc.FlockworkPModel(E, N, rewiring_rate[0][1], P[0]) mv_SIS = tc.MARKOV_SIS(N,t_run_total*2,infection_rate,recovery_rate,0.01,N//2,seed = seed) start = time.time() _tc.markov_SIS_on_FlockworkPModel(FW, mv_SIS,max_dt = 0.001) end = time.time() print("Markov on Model took", end-start,"seconds") pl.plot(mv_SIS.time, mv_SIS.I) pl.ylim([0,N]) pl.show()
def naive_varying_rate_flockwork_simulation(N, t, reconnection_rates, disconnection_rates, tmax): r""" Do a naive simulation of a Flockwork systems where the reconnection and disconnection rate vary over time as step functions. It is called `naive` because the rate change will not be considered when evaluating the inter-event time at time points when the rates change. I.e. at a rate change at time `t`, the new Flockwork model will be initiated as if the last event happened at time `t`. This error is neglibile if the upper bounded mean inter-event time at rate changes is :math:`\tau=(N(\alpha_{\mathrm{min}}+\beta_{\mathrm{min}})^{-1}\ll \Deltat_\mathrm{min}` where :math:`\Deltat_\mathrm{min}` is the minimal time between two rate changes. Parameters ---------- N : int number of nodes t : numpy.ndarray of float time points at which :math:`\alpha` and :math`\beta` change reconnection_rates : numpy.ndarray of float values of :math:`\alpha` for the corresponding time values in ``t``. disconnection_rates : numpy.ndarray of float values of :math:`\beta` for the corresponding time values in ``t``. tmax : float time at which the simulation is supposed to end. Returns ------- edge_changes : :class:`_tacoma.edge_changes` The simulated Flockwork instance. """ if len(t) != len(reconnection_rates) or len(reconnection_rates) != len( disconnection_rates): raise ValueError( 'The arrays `t`, `reconnection_rates` and `disconnection_rates` must have the same length' ) assert (t[-1] < tmax) t = np.append(t, tmax) all_networks = [] it = 0 result = None for a_, b_ in zip(reconnection_rates, disconnection_rates): dt = t[it + 1] - t[it] if a_ == 0.0 and b_ == 0.0: P = 0 else: P = a_ / (a_ + b_) g = (a_ + b_) if it == 0: initial_edges = flockwork_P_equilibrium_configuration(N, P) FW = tc.FlockworkPModel(initial_edges, N, g, P, save_temporal_network=True) FW.simulate(dt) this_result = FW.edge_changes if it < len(reconnection_rates) - 1: initial_edges = FW.get_current_edgelist() all_networks.append(this_result) it += 1 result = tc.concatenate(all_networks) return result
recovery_rate = 1 gamma = recovery_rate infection_rate = R0 * recovery_rate / k I0 = 1 tmax = 200 E = [(i, j) for i in range(N - 1) for j in range(i + 1, N)] E = [] print("initial edge list", E) FW = tc.FlockworkPModel( E, # initial edge list (list of tuple of ints) N, # number of nodes gamma, # rate with which anything happens P, # probability to reconnect save_temporal_network=True, save_aggregated_network=True, verbose=True, ) SIS = tc.SIS( N, #number of nodes tmax, # maximum time of the simulation infection_rate, recovery_rate, number_of_initially_infected=I0, # optional, default: 1 verbose=True, ) tc.gillespie_epidemics(FW, SIS, verbose=True)