Beispiel #1
0
def precision_simul(N, S1, S2, C, eps, rip, t, work_dir):
    import time
    from one_simulation import one_simulation
    import my_output as O
    from grad_nestedness import NODF_calc
    N1 = N
    N2 = N
    start = time.time()
    parent_dir = work_dir + '/precision/'
    dir_name1 = parent_dir + '/nuova/'
    dir_name2 = parent_dir + '/vecchia/'

    from bipartite_graph_matrix import adjacency_matrix_nested2, adjacency_matrix_rnd2

    print('Eseguo matrice nested nuova.')
    A1 = adjacency_matrix_nested2(S1, S2, C, dir_name1, 1)
    NODF1 = NODF_calc(A1)

    for i in range(rip):
        one_simulation(A1,
                       N1,
                       N2,
                       S1,
                       S2,
                       eps,
                       C,
                       t,
                       NODF1,
                       dir_name1,
                       rip,
                       re=i)
    O.NODF_print_info_csv(NODF1, eps, C, rip, dir_name1, parent_dir)
    O.info_precision(N, rip, [NODF1], eps, C, t, work_dir, parent_dir)

    print('Eseguo matrice random.')
    A2 = adjacency_matrix_rnd2(S1, S2, C, dir_name2, 1)
    NODF2 = NODF_calc(A2)

    for i in range(rip):
        one_simulation(A2,
                       N1,
                       N2,
                       S1,
                       S2,
                       eps,
                       C,
                       t,
                       NODF2,
                       dir_name2,
                       rip,
                       re=i)
    O.NODF_print_info_csv(NODF2, eps, C, rip, dir_name2, parent_dir)
    O.info_precision(N, rip, [NODF2], eps, C, t, work_dir, parent_dir)

    end = time.time()
    elapsed = round((end - start) / 60, 2)
    print('NODF_launcher eseguito in {} min \n'.format(elapsed))
Beispiel #2
0
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')
Beispiel #3
0
def NODF_swap3(S1, S2, C, step, P, dir_name):
    import matplotlib.pyplot as plt
    import numpy as np
    from grad_nestedness import swap_matrix_element, NODF_calc
    from bipartite_graph_matrix import adjacency_matrix_nested2, adjacency_matrix_rnd2
    import my_output as O
    NODFs = []
    matrix_dir = dir_name + '/matrix'

    print('Eseguo matrice nested nuova.')
    A = adjacency_matrix_nested2(S1, S2, C, dir_name, 0)
    NODF = NODF_calc(A)
    #print('NODFstart = ', NODFstart)
    O.print_matrix2(A, S1, S2, C, NODF, 0, matrix_dir)
    NODFs.append(round(NODF, 3))
    As = []
    As.append(A)
    for i in range(100):
        A = adjacency_matrix_rnd2(S1, S2, C)
        NODF = NODF_calc(A)
        NODFs.append(round(NODF, 3))
        As.append(A)
        O.print_matrix2(As[-1], S1, S2, C, NODF, i, matrix_dir)
    return (As, NODFs)
Beispiel #4
0
def NODF_swap(S1, S2, C, step, P, dir_name):
    from grad_nestedness import swap_matrix_element, NODF_calc
    from bipartite_graph_matrix import adjacency_matrix_nested2
    import my_output as O
    NODFs = []
    matrix_dir = dir_name + '/matrix_step'
    A = adjacency_matrix_nested2(S1, S2, C, dir_name, 0)
    NODF = NODF_calc(A)

    O.print_matrix2(A, S1, S2, C, NODF, 0, matrix_dir)

    NODFs.append(NODF)
    for i in range(step):
        (A, NODF) = swap_matrix_element(A, P)
        O.print_matrix2(A, S1, S2, C, NODF, i + 1, matrix_dir)
        NODFs.append(NODF)

    O.print_NODF_step(NODFs, C, P, dir_name)
Beispiel #5
0
def NODF_launcher(N, S1, S2, C, eps, step, P, rip, t, work_dir, option):
    import time
    from one_simulation import one_simulation
    from bipartite_graph_matrix import adjacency_matrix_nested2, adjacency_matrix_nested
    import my_output as O
    #from NODF_swap import swap_matrix_element, NODF_swap2
    from grad_nestedness import swap_matrix_element, NODF_calc
    import os
    N1 = N
    N2 = N
    start = time.time()
    if option == 0:
        parent_dir = work_dir+'/NODF/op0/'
    else:
        parent_dir = work_dir+'/NODF/op1/'
    dir_name = parent_dir+repr(C)+'-'+repr(eps)+'-'+repr(rip)
    
    matrix_dir = dir_name+'/matrix'
    if option == 0:
        print('Eseguo matrice nested nuova.')
        A = adjacency_matrix_nested2(S1, S2, C, dir_name, 0)
    else:
        print('Eseguo matrice nested vecchia.')
        A = adjacency_matrix_nested(S1, S2, C, dir_name, 0)
        
    NODF = NODF_calc(A)
    A0 = A
    NODFs = []
    NODFs.append(NODF)
    #(As, NODFs) = NODF_swap2(S1, S2, C, step, P, dir_name, option)
    #from NODF_swap import NODF_swap3
    #(As, NODFs) = NODF_swap3(S1, S2, C, step, P, dir_name)
    file_path2 = "C:/Users/Utente/Anaconda3/UserScripts/Programmi cooperazione/"
    directory2 = os.path.dirname(file_path2)
    os.chdir(directory2)
    T = True
    for i in range(10):
        if i == 0:
            O.print_matrix2(A0, S1, S2, C, NODF, 0, matrix_dir)
            for j in range(rip):
                print('Eseguo simulazione numero {} su {} con NODF = {}'.format(j+1,rip, NODF))
                one_simulation(A0, N1, N2, S1, S2, eps, C, t, NODF, dir_name, rip, re = j)
            O.NODF_print_info_csv(NODFs[i], eps, C, rip, dir_name, parent_dir)
       
        else:
            (A, NODF) = swap_matrix_element(A0,P*i)
            tentativo = 1
            while NODF > NODFs[-1] - 0.04 :
                (A, NODF) = swap_matrix_element(A0,P*i)
                tentativo = tentativo + 1
                if tentativo > 10000:
                    T = False
                    break
            if T == True:
                print('Matrice selezionata al tentativo numero ', tentativo)
                NODFs.append(NODF)
                O.print_matrix2(A, S1, S2, C, NODF, i, matrix_dir)
                for j in range(rip):
                    print('Eseguo simulazione numero {} su {} con NODF = {}'.format(j+1,rip, NODF))
                    one_simulation(A, N1, N2, S1, S2, eps, C, t, NODF, dir_name, rip, re = j)
                O.NODF_print_info_csv(NODFs[i], eps, C, rip, dir_name, parent_dir)
    O.info_NODF(N, rip, NODFs, eps, C, t, work_dir, parent_dir)
    
    end = time.time()
    elapsed = round((end-start)/60,2)
    print('NODF_launcher eseguito in {} min \n'.format(elapsed))
Beispiel #6
0
def NODF_swap2(S1, S2, C, step, P, dir_name, option):
    #import matplotlib.pyplot as plt
    import numpy as np
    from grad_nestedness import swap_matrix_element, NODF_calc
    from bipartite_graph_matrix import adjacency_matrix_nested2, adjacency_matrix_nested
    import my_output as O
    NODFs = []
    matrix_dir = dir_name + '/matrix'
    if option == 0:
        print('Eseguo matrice nested nuova.')
        A = adjacency_matrix_nested2(S1, S2, C, dir_name, 0)
    else:
        print('Eseguo matrice nested vecchia.')
        A = adjacency_matrix_nested(S1, S2, C, dir_name, 0)

    NODF = NODF_calc(A)
    A0 = A
    #print('Astart = ', Astart)
    #NODFstart = NODF
    #print('NODFstart = ', NODFstart)
    O.print_matrix2(A, S1, S2, C, NODF, 0, matrix_dir)
    NODFs.append(round(NODF, 3))
    A1, A2, A3, A4, A5, A6, A7, A8 = [], [], [], [], [], [], [], []
    As = [A0, A1, A2, A3, A4, A5, A6, A7, A8]
    M = NODF + 0.001
    m = 0.25
    selection = np.linspace(m, M, 10)
    #print('selection = ', selection)
    for i in range(len(selection)):
        print('selection[-{}] = '.format(i + 1), selection[-i - 1])
    counts = 1
    tentativo = 1
    m2 = min([selection[-counts], NODFs[-1]])
    while counts < len(selection) - 1:

        (A, NODF) = swap_matrix_element(A0, P)

        if NODF < m2 and NODF > selection[-counts - 2]:

            NODFs.append(round(NODF, 3))
            As[counts] = A
            O.print_matrix2(A, S1, S2, C, NODF, counts, matrix_dir)
            counts = counts + 1
            print(
                'Matrice numero {} selezionata al tentativo {} con NODF = {}'.
                format(counts, tentativo, NODFs[-1]))
            tentativo = 1
            m2 = min([selection[-counts], NODFs[-1]])
            if counts < len(selection) - 1:
                print('Cerco {} < NODF < {}: \n'.format(
                    round(selection[-counts - 2], 3), round(m2, 3)))

        else:
            print('NODF trovata = ', NODF)
            print('NODF richiesta tra {} e {}'.format(
                selection[-counts - 2], min([selection[-counts], NODFs[-1]])))
            tentativo = tentativo + 1

    #As = [A0, A1, A2, A3, A4, A5, A6, A7, A8, A9]
    #questo è da cambiare
    O.print_NODF_step2(NODFs, C, P, dir_name)

    print('len(As) = ', len(As))
    print('len(NODFs) = ', len(NODFs))
    for i in range(len(As)):
        print(As[i], '\n')
    return ([A0, A1, A2, A3, A4, A5, A6, A7, A8], NODFs)
def adjacency_matrix_rnd(S1=10,
                         S2=10,
                         p=0.35,
                         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
    from grad_nestedness import NODF_calc
    plt.style.use('seaborn')
    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)
    k = int(p * S1 * S2)
    G = bipartite.gnmk_random_graph(S1, S2, k)
    G1 = ordina_grafo_1(G, S1, S2)
    G1 = ordina_grafo_2(G1, S1, S2)

    deg = list(G1.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:])

    NODF = NODF_calc(A1)
    print('NODF prima del riordino = ', NODF, '\n')

    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('Configurazione casuale con C = ' + format(p, '.2f'))
    plt.setp(ax.get_xticklabels(), visible=False)
    plt.setp(ax.get_yticklabels(), visible=False)
    plt.imshow(A1, cmap='Greys')
    plt.tight_layout()

    fig.savefig('random_' + format(p, '.2f') + '.png')
    plt.close()

    A = nx.to_numpy_matrix(G1)
    A2 = A.getA()
    A1 = []
    for x in range(S1):
        A1.append(A2[x][S1:])

    #NODF = NODF_calc(A1)
    #print('NODF dopo il riordino = ', NODF, '\n')

    #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('Configurazione casuale con C = '+format(p,'.2f'))
    #plt.setp(ax.get_xticklabels(), visible=False)
    #plt.setp(ax.get_yticklabels(), visible=False)
    #plt.imshow(A1, cmap = 'Greys')
    #plt.tight_layout()

    #fig.savefig('random_'+format(p,'.2f')+'_ordinata.png')
    #plt.close()

    fig, [ax1, ax] = plt.subplots(1, 2, figsize=(10, 4))
    ax1.set_title('Interazioni mutualistiche casuali')
    ax1.set_axis_off()
    ax1.set_autoscale_on(True)
    nx.draw_networkx(G1, 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 casuale 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('random_' + format(p, '.2f') + '_ordinata.png')
    plt.close()

    print('curr_dir = ', curr_dir)
    os.chdir(curr_dir)
    return A1