Beispiel #1
0
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')
Beispiel #2
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 #3
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)