def one_simulation(A, N1=50, N2=50, S1=10, S2=10, eps=0.05, p=0.33, t=1000, NODF=1, dir_name='50-50', rip=1, g=1, flag=False, re=1): from one_step_bi import one_step from random import randint from copy import deepcopy import time from grad_nestedness import NODF_calc start_time = time.time() name = repr(NODF) + '-' + repr(eps) + '-' + repr(p) + '-' + repr( rip) + '-' + repr(re) print('name simulation = ', name) NODF = NODF_calc(A) print('NODF = ', NODF) tau_max1 = int(t / N1) print('tau = ', tau_max1) v1 = [randint(0, S1 - 1) for x in range(0, N1)] n1 = [] Smax1 = max(v1) for i in range(0, Smax1 + 1): c1 = v1.count(i) n1.append(c1) v2 = [randint(0, S2 - 1) for x in range(0, N2)] n2 = [] Smax2 = max(v2) for i in range(0, Smax2 + 1): c2 = v2.count(i) n2.append(c2) import my_output as O for i in range(1, tau_max1): #qui il programma è un po' imparziale, perchè tratta la specie 1 come standard for k in range(0, N1): (v1, v2, n1, n2) = one_step(N1, N2, v1, v2, n1, n2, A, eps) #(v1, v2, n1, n2) = one_step(N1, N2, v1, v2, n1, n2, A, eps, rip, True, dir_name) n_1 = deepcopy(n1) S1 = len(n_1) - n_1.count(0) O.print_list_csv(S1, 'S1_' + name, dir_name, d=1) # O.print_list_csv(n_1, 'n1_'+name, dir_name, d = len(n_1)) n_2 = deepcopy(n2) S2 = len(n_2) - n_2.count(0) O.print_list_csv(S2, 'S2_' + name, dir_name, d=1) #O.print_list_csv(n_2, 'n2_'+name, dir_name, d = len(n_2)) simulation_t = round((time.time() - start_time) / 60, 2) print("Tempo simulazione: {}".format(simulation_t), 'min. \n')
def N_one_simulation(N1=50, N2=50, S1=10, S2=10, eps=0.05, p=0.33, t=1000, dir_name='50-50', rip=1, g=1, flag=False, re=1, option=0): from one_step_bi import one_step from random import randint from copy import deepcopy import time start_time = time.time() name = repr(eps) + '-' + repr(p) + '-' + repr(rip) + '-' + repr(re) from bipartite_graph_matrix import adjacency_matrix_nested, adjacency_matrix_nested2 if option == 0: #asymmetric nested / first type A = adjacency_matrix_nested(S1, S2, p, dir_name, re, g, flag) else: #balanced nested / second type A = adjacency_matrix_nested2(S1, S2, p, dir_name, re, g, flag) tau_max1 = int(t / N1) v1 = [randint(0, S1 - 1) for x in range(0, N1)] n1 = [] Smax1 = max(v1) for i in range(0, Smax1 + 1): c1 = v1.count(i) n1.append(c1) v2 = [randint(0, S2 - 1) for x in range(0, N2)] n2 = [] Smax2 = max(v2) for i in range(0, Smax2 + 1): c2 = v2.count(i) n2.append(c2) import my_output as O for i in range(1, tau_max1): #qui il programma è un po' imparziale, perchè tratta la specie 1 come standard for k in range(0, N1): (v1, v2, n1, n2) = one_step(N1, N2, v1, v2, n1, n2, A, eps) #(v1, v2, n1, n2) = one_step(N1, N2, v1, v2, n1, n2, A, eps, rip, True, dir_name) n_1 = deepcopy(n1) S1 = len(n_1) - n_1.count(0) O.print_list_csv(S1, 'N_S1_' + name, dir_name, d=1) O.print_list_csv(n_1, 'N_n1_' + name, dir_name, d=len(n_1)) n_2 = deepcopy(n2) S2 = len(n_2) - n_2.count(0) O.print_list_csv(S2, 'N_S2_' + name, dir_name, d=1) O.print_list_csv(n_2, 'N_n2_' + name, dir_name, d=len(n_2)) simulation_t = round((time.time() - start_time) / 60, 2) print("Tempo simulazione: {}".format(simulation_t), 'min. \n')
def adjacency_matrix_rnd(S1 = 10, S2 = 10, p = 0.33, dir_name = 'graph', index = 1, index2 = 1, flag = False): import networkx as nx from networkx.algorithms import bipartite import matplotlib.pyplot as plt import os from ensure_dir import ensure_dir import my_print as my import my_output as O curr_dir = os.getcwd() #print('dir_name = ', dir_name) file_path = dir_name+'/prova.txt' ensure_dir(file_path) directory = os.path.dirname(file_path) os.chdir(directory) G = bipartite.random_graph(S1, S2, p) deg = list(G.degree()) deg1 = deg[:S1] deg2 = deg[S1:] pos = {x:[0,x] for x in range(S1)} for j in range(S2): pos[S1+j] = [1,j] colors = ['r' for i in range(0,S1)] for j in range(S2): colors.append('b') fig, [ax1, ax] = plt.subplots(1,2, figsize = (9,4)) ax1.set_title('Interazioni mutualistiche casuali') ax1.set_axis_off() ax1.set_autoscale_on(True) nx.draw_networkx(G, pos = pos, node_color = colors, ax = ax1) A = nx.to_numpy_matrix(G) A2 = A.getA() A1 = [] for x in range(S1): A1.append(A2[x][S1:]) ax.grid() xtics = [x-0.5 for x in range(0,S2+1)] ytics = [x-0.5 for x in range(0,S1+1)] ax.set_yticks(ytics) ax.set_xticks(xtics) ax.set_ylabel('Impollinatori') ax.set_xlabel('Piante') ax.set_title('Configurazione casuale con C = '+format(p,'.2f')) ax.set_autoscale_on(True) plt.imshow(A1, cmap = 'Greys') if index == 1: fig.savefig('random_'+format(p,'.2f')+'.png') plt.close() if flag == False: if index == 1: my.print_tuple2(deg1, 'R_degree1-'+repr(index2), dir_name) my.print_tuple2(deg2, 'R_degree2-'+repr(index2), dir_name) else: O.print_list_csv(deg1, 'deg1_R-'+repr(index), dir_name) O.print_list_csv(deg2, 'deg2_R-'+repr(index), dir_name) file_path2 = "C:/Users/Utente/Anaconda3/UserScripts/Programmi cooperazione/" directory2 = os.path.dirname(file_path2) print('directory2 = ', directory2) print('curr_dir = ', curr_dir) os.chdir(directory2) return A1
def adjacency_matrix_nested2(S1=10, S2=10, p=0.33, dir_name='graph', index=1, index2=1, flag=False): #balanced nested import networkx as nx import matplotlib.pyplot as plt import os from ensure_dir import ensure_dir import my_print as my import my_output as O script_dir = os.getcwd() file_path = dir_name + '/prova.txt' ensure_dir(file_path) directory = os.path.dirname(file_path) os.chdir(directory) #attenzione, funziona bene solo con S1 = S2 G = nx.Graph() nodes = [x for x in range(S1 + S2)] G.add_nodes_from(nodes) #print(list(G.nodes())) edges = [] for i in range(S1): for j in range(S1, S1 + S2 - i): edges.append((i, j)) G.add_edges_from(edges) if p < 0.55: #this is where the actual edges are decided in most cases (C < 0.55) - different method G = adjust_edges1(G, S1, S2, p) if p > 0.55: G = adjust_edges2(G, S1, S2, p) deg = list(G.degree()) deg1 = deg[:S1] deg2 = deg[S1:] pos = {x: [0, x] for x in range(S1)} for j in range(S2): pos[S1 + j] = [1, j] colors = ['r' for i in range(0, S1)] for j in range(S2): colors.append('b') A = nx.to_numpy_matrix(G) A2 = A.getA() A1 = [] for x in range(S1): A1.append(A2[x][S1:]) #rimettere if index == 1 if index == 0: plt.style.use('seaborn') fig, [ax1, ax] = plt.subplots(1, 2, figsize=(10, 4)) ax1.set_title('Interazioni mutualistiche nidificate') ax1.set_axis_off() ax1.set_autoscale_on(True) nx.draw_networkx(G, pos=pos, node_color=colors, ax=ax1) ax.grid() xtics = [x - 0.5 for x in range(0, S2 + 1)] ytics = [x - 0.5 for x in range(0, S1 + 1)] ax.set_yticks(ytics) ax.set_xticks(xtics) ax.set_ylabel('Impollinatori') ax.set_xlabel('Piante') ax.set_title('Configurazione nidificata con C = ' + format(p, '.2f')) ax.set_autoscale_on(True) plt.setp(ax.get_xticklabels(), visible=False) plt.setp(ax.get_yticklabels(), visible=False) plt.imshow(A1, cmap='Greys') plt.tight_layout() fig.savefig('nested_' + format(p, '.2f') + '_II.png') if index == 1: fig = plt.figure() ax = fig.add_subplot(111) ax.grid() xtics = [x - 0.5 for x in range(0, S2 + 1)] ytics = [x - 0.5 for x in range(0, S1 + 1)] ax.set_yticks(ytics) ax.set_xticks(xtics) ax.set_ylabel('Impollinatori') ax.set_xlabel('Piante') ax.set_title('Nested II tipo con C = ' + format(p, '.3f')) ax.set_autoscale_on(True) plt.setp(ax.get_xticklabels(), visible=False) plt.setp(ax.get_yticklabels(), visible=False) plt.tight_layout() plt.imshow(A1, cmap='Greys') if index == 1: fig.savefig('nested_' + format(p, '.2f') + '_II.png') if flag == False: if index == 1: my.print_tuple2(deg1, 'N_degree1-' + repr(index2), dir_name) my.print_tuple2(deg2, 'N_degree2-' + repr(index2), dir_name) else: O.print_list_csv(deg1, 'deg1_N-' + repr(index), dir_name) O.print_list_csv(deg2, 'deg2_N-' + repr(index), dir_name) plt.close() os.chdir(script_dir) return A1
def adjacency_matrix_nested2(S1=10, S2=10, p=0.33, dir_name='graph', index=1, index2=1, flag=False): import networkx as nx import matplotlib.pyplot as plt import os from ensure_dir import ensure_dir import my_print as my import my_output as O file_path = dir_name + '/prova.txt' ensure_dir(file_path) directory = os.path.dirname(file_path) os.chdir(directory) #attenzione, funziona bene solo con S1 = S2 G = nx.Graph() nodes = [x for x in range(S1 + S2)] G.add_nodes_from(nodes) #print(list(G.nodes())) edges = [] for i in range(S1): for j in range(S1, S1 + S2 - i): edges.append((i, j)) G.add_edges_from(edges) if p < 0.55: G = adjust_edges1(G, S1, S2, p) #if p > 0.55: # G = adjust_edges2(G, S1, S2, p) deg = list(G.degree()) deg1 = deg[:S1] deg2 = deg[S1:] pos = {x: [0, x] for x in range(S1)} for j in range(S2): pos[S1 + j] = [1, j] colors = ['r' for i in range(0, S1)] for j in range(S2): colors.append('b') fig, [ax1, ax] = plt.subplots(1, 2, figsize=(9, 4)) ax1.set_title('Interazioni mutualistiche nested ') ax1.set_axis_off() ax1.set_autoscale_on(True) nx.draw_networkx(G, pos=pos, node_color=colors, ax=ax1) A = nx.to_numpy_matrix(G) A2 = A.getA() A1 = [] for x in range(S1): A1.append(A2[x][S1:]) ax.grid() xtics = [x - 0.5 for x in range(0, S2 + 1)] ytics = [x - 0.5 for x in range(0, S1 + 1)] ax.set_yticks(ytics) ax.set_xticks(xtics) ax.set_ylabel('Impollinatori') ax.set_xlabel('Piante') ax.set_title('Configurazione nested con C = ' + format(p, '.2f')) ax.set_autoscale_on(True) plt.imshow(A1, cmap='Greys') if index == 1: fig.savefig('nested_' + format(p, '.2f') + '.png') if flag == False: if index == 1: my.print_tuple2(deg1, 'N_degree1-' + repr(index2), dir_name) my.print_tuple2(deg2, 'N_degree2-' + repr(index2), dir_name) else: O.print_list_csv(deg1, 'deg1_N-' + repr(index), dir_name) O.print_list_csv(deg2, 'deg2_N-' + repr(index), dir_name) plt.close() file_path2 = "C:/Users/Utente/Anaconda3/UserScripts/Programmi cooperazione/" directory2 = os.path.dirname(file_path2) os.chdir(directory2) return A1