Exemple #1
0
def simulate_random_graph(graph_name, number_of_nodes):

    if graph_name == "random_partition_model":
        p_in = uniform()
        p_out = uniform()
        community_sizes = uniform_partition_gen(2, number_of_nodes, choice(floor(number_of_nodes / 2)))
        return random_partition_model(community_sizes, p_in, p_out)

    elif graph_name == "erdos_renyi":
        p = uniform()
        return erdos_renyi(number_of_nodes,p)

    elif graph_name == "geometric_model":
        r = floor(sqrt(choice(number_of_nodes))+1)
        return geometric_model(number_of_nodes,r)

    elif graph_name == "barabasi_albert_model":
        c = choice(floor(number_of_nodes/2))+1
        return barabasi_albert_model(number_of_nodes,c)

    else:
        print('please provide a valid graphical model')
        return None
Exemple #2
0
def simulate_random_graph(graph_name,
                          number_of_nodes,
                          additional_parameters=None):

    if additional_parameters is not None:
        keys = additional_parameters.keys()

    if graph_name == "random_partition_model":
        if additional_parameters is not None:

            if 'p_in' in keys:
                p_in = additional_parameters['p_in']
            else:
                p_in = uniform()
            if 'p_out' in keys:
                p_out = additional_parameters['p_out']
            else:
                p_out = uniform()
            if 'max_community_sizes' in keys:
                max_community_sizes = additional_parameters[
                    'min_community_sizes']
            else:
                max_community_sizes = choice(floor(number_of_nodes / 2))
            if 'min_community_sizes' in keys:
                min_community_sizes = additional_parameters[
                    'min_community_sizes']
            else:
                min_community_sizes = 1

            community_sizes = uniform_partition_gen(min_community_sizes,
                                                    number_of_nodes,
                                                    max_community_sizes)
        else:
            p_out = uniform()
            p_in = uniform()
            community_sizes = uniform_partition_gen(
                1, number_of_nodes, choice(floor(number_of_nodes / 2)))

        return random_partition_model(community_sizes, p_in, p_out)

    elif graph_name == "erdos_renyi":
        if additional_parameters is not None:
            if 'p' in keys:
                p = additional_parameters['p']
            else:
                p = uniform()
        else:
            p = uniform()
        return erdos_renyi(number_of_nodes, p)

    elif graph_name == "geometric_model":
        if additional_parameters is not None:
            if 'r' in keys:
                r = additional_parameters['r']
            else:
                r = floor(sqrt(choice(number_of_nodes)) + 1)
        else:
            r = floor(sqrt(choice(number_of_nodes)) + 1)
        return geometric_model(number_of_nodes, r)

    elif graph_name == "barabasi_albert_model":
        if additional_parameters is not None:
            if 'c' in keys:
                c = additional_parameters['c']
            else:
                c = choice(floor(number_of_nodes / 2)) + 1
        else:
            c = choice(floor(number_of_nodes / 2)) + 1

        return barabasi_albert_model(number_of_nodes, c)

    else:
        print('please provide a valid graphical model')
        return None
Exemple #3
0
# G_2 = get_dd_planted_partition(n, q, c, epsilon)
# norm_name = 'fro'
# print(Norm_Difference(G_1,G_2, norm_name))
# simulate_random_graph("random_partition_model", 20).degree()
# print(generate_proposal_JDDs(20, ['random_partition_model'], 10))
# print(generate_proposal_JDDs(20, ['erdos_renyi'], 10))
# print(generate_proposal_JDDs(20, ['geometric_model'], 10))
# print(generate_proposal_JDDs(20, ['barabasi_albert_model'], 10))

if __name__ == "__main__":
    Proposal_distributions = [
        'geometric_model', 'erdos_renyi', 'random_partition_model',
        'barabasi_albert_model'
    ]
    Averaging = 100
    Observered_G = barabasi_albert_model(10, 2)
    print(
        edge_imputation_via_one_step_node_norm_minimization(
            Observered_G, Proposal_distributions, Averaging, 0))

    JDD_rp = generate_proposal_JDDs(10, ['random_partition_model'], 1000)
    JDD_er = generate_proposal_JDDs(10, ['erdos_renyi'], 1000)
    JDD_gm = generate_proposal_JDDs(10, ['geometric_model'], 1000)
    JDD_ba = generate_proposal_JDDs(10, ['barabasi_albert_model'], 1000)

    print(norm_difference(JDD_rp, JDD_rp, 'fro'))
    print(norm_difference(JDD_rp, JDD_er, 'fro'))
    print(norm_difference(JDD_rp, JDD_gm, 'fro'))
    print(norm_difference(JDD_rp, JDD_ba, 'fro'))

    print(norm_difference(JDD_er, JDD_er, 'fro'))
def barabasi_albert_generator(n=N, c=-1):
    if c < 0:
        c = np.random.randint(1, 50)
    #print c
    return random_graphs.barabasi_albert_model(n, c)
def simulate_random_graph(graph_name, number_of_nodes, additional_parameters = None):

    random_graphs = []

    if additional_parameters is not None:
        keys = additional_parameters.keys()

    if graph_name == "random_partition_model":

        if additional_parameters is not None:

            if 'p_in' in keys:
                p_in = additional_parameters['p_in']
            else:
                p_in = uniform()
            if 'p_out' in keys:
                p_out = additional_parameters['p_out']
            else:
                p_out = uniform()
            if 'max_community_sizes' in keys:
                max_community_sizes = additional_parameters['max_community_sizes']
            else:
                max_community_sizes = choice(floor(number_of_nodes / 2))
            for p_in_ in p_in:
                for p_out_ in p_out:
                    for max_community_sizes_ in max_community_sizes:

                        ### generate random graph
                        community_sizes = uniform_partition_gen(2,
                                    number_of_nodes, int(max_community_sizes_))

                        if community_sizes is not None:
                            new_graph = random_partition_model(community_sizes, p_in_, p_out_)
                        else:
                            new_graph = nx.Graph()

                        if len(new_graph.nodes()) == 0:
                            community_sizes = uniform_partition_gen(int(2),
                                number_of_nodes, int(max_community_sizes_))
                            if community_sizes is not None:
                                new_graph = random_partition_model(community_sizes, p_in_, p_out_)
                                random_graphs.append((new_graph,
                                {"graph": graph_name, "max_community_sizes": max_community_sizes_,
                                 "min_community_sizes_": 2, "p_in": p_in_,
                                 "p_out": p_out_}))

            return random_graphs

        else:
            p_out = uniform()
            p_in = uniform()
            max_community_sizes = choice(floor(number_of_nodes)-2)+1
            min_community_sizes = 2
            community_sizes = uniform_partition_gen(min_community_sizes, number_of_nodes, max_community_sizes)
            new_graph = random_partition_model(community_sizes, p_in, p_out)
            if new_graph.nodes():
                random_graphs.append((new_graph,
                {"graph": graph_name, "max_community_sizes": max_community_sizes,
                 "min_community_sizes_": min_community_sizes, "p_in": p_in,
                 "p_out": p_out}))
            else:
                random_graphs = None

            return random_graphs

    elif graph_name == "erdos_renyi":
        if additional_parameters is not None:
            if 'p' in keys:
                p = additional_parameters['p']
                for p_ in p:
                    random_graphs.append((erdos_renyi(number_of_nodes, p_),
                                          {"graph": graph_name, "p": p_}))
            else:
                p = uniform()
                random_graphs.append((erdos_renyi(number_of_nodes, p),
                                      {"graph": graph_name, "p": p}))
        else:
            p = uniform()
            random_graphs.append((erdos_renyi(number_of_nodes,p),
                                {"graph": graph_name, "p": p}))
        return random_graphs

    elif graph_name == "geometric_model":
        if additional_parameters is not None:
            if 'r' in keys:
                r = additional_parameters['r']
                for r_ in r:
                    random_graphs.append((geometric_model(number_of_nodes,r_),
                                {"graph": graph_name, "r": r_}))
            else:
                r = floor(sqrt(choice(number_of_nodes)) + 1)
                random_graphs.append((geometric_model(number_of_nodes,r),
                                {"graph": graph_name, "r": r}))
        else:
            r = floor(sqrt(choice(number_of_nodes))+1)
            random_graphs.append((geometric_model(number_of_nodes,r),
                                {"graph": graph_name, "r": r}))
        return random_graphs

    elif graph_name == "barabasi_albert_model":

        if additional_parameters is not None:
            if 'c' in keys:
                c = additional_parameters['c']
                for c_ in c:
                    random_graphs.append((barabasi_albert_model(number_of_nodes,int(c_)),
                                         {"graph": graph_name, "c": int(c_)}))
            else:
                c = choice(floor(number_of_nodes / 2)) + 1
                random_graphs.append((barabasi_albert_model(number_of_nodes,c),
                                         {"graph": graph_name, "c": c}))
        else:
            c = choice(floor(number_of_nodes/2))+1
            random_graphs.append((barabasi_albert_model(number_of_nodes,c),
                                         {"graph": graph_name, "c": c}))

        return random_graphs

    else:
        print('please provide a valid graphical model')
        return None
def simulate_single_rg(graph_name, number_of_nodes, additional_parameters = None):

    random_graphs = []

    if additional_parameters is not None:
        keys = additional_parameters.keys()

    if graph_name == "random_partition_model":

        if additional_parameters is not None:

            if 'p_in' in keys:
                p_in = additional_parameters['p_in']
            else:
                p_in = uniform()
            if 'p_out' in keys:
                p_out = additional_parameters['p_out']
            else:
                p_out = uniform()
            if 'max_community_sizes' in keys:
                max_community_sizes = int(additional_parameters['min_community_sizes'])
            else:
                max_community_sizes = choice(floor(number_of_nodes / 2))
            if 'min_community_sizes' in keys:
                min_community_sizes = int(additional_parameters['min_community_sizes'])
            else:
                min_community_sizes = 1
            ### check the parameters make sense
            if max_community_sizes < min_community_sizes:
                temp = max_community_sizes
                max_community_sizes = min_community_sizes
                min_community_sizes = temp

            ### generate random graph
            community_sizes = uniform_partition_gen(min_community_sizes,
                        number_of_nodes, max_community_sizes)
            new_graph = random_partition_model(community_sizes, p_in, p_out)
            while len(new_graph.nodes()) == 0:
                community_sizes = uniform_partition_gen(min_community_sizes,
                                                        number_of_nodes, max_community_sizes)
                new_graph = random_partition_model(community_sizes, p_in, p_out)

            random_graphs.append((new_graph,
                    {"graph": graph_name, "max_community_sizes": int(max_community_sizes),
                     "min_community_sizes_": int(min_community_sizes), "p_in": p_in,
                     "p_out": p_out}))
            return random_graphs

        else:
            p_out = uniform()
            p_in = uniform()
            max_community_sizes = choice(floor(number_of_nodes / 2))
            min_community_sizes = 1
            community_sizes = uniform_partition_gen(min_community_sizes, number_of_nodes, max_community_sizes)
            new_graph = random_partition_model(community_sizes, p_in, p_out)
            if new_graph.nodes():
                random_graphs.append((new_graph,
                {"graph": graph_name, "max_community_sizes": max_community_sizes,
                 "min_community_sizes_": min_community_sizes, "p_in": p_in,
                 "p_out": p_out}))
            else:
                random_graphs = None

            return random_graphs

    elif graph_name == "erdos_renyi":
        if additional_parameters is not None:
            if 'p' in keys:
                p = additional_parameters['p']
                random_graphs.append((erdos_renyi(number_of_nodes, p),
                                          {"graph": graph_name, "p": p}))
            else:
                p = uniform()
                random_graphs.append((erdos_renyi(number_of_nodes, p),
                                      {"graph": graph_name, "p": p}))
        else:
            p = uniform()
            random_graphs.append((erdos_renyi(number_of_nodes,p),
                                {"graph": graph_name, "p": p}))
        return random_graphs

    elif graph_name == "geometric_model":
        if additional_parameters is not None:
            if 'r' in keys:
                r = additional_parameters['r']
                random_graphs.append((geometric_model(number_of_nodes,r),
                                {"graph": graph_name, "r": r}))
            else:
                r = floor(sqrt(choice(number_of_nodes)) + 1)
                random_graphs.append((geometric_model(number_of_nodes,r),
                                {"graph": graph_name, "r": r}))
        else:
            r = floor(sqrt(choice(number_of_nodes))+1)
            random_graphs.append((geometric_model(number_of_nodes,r),
                                {"graph": graph_name, "r": r}))
        return random_graphs

    elif graph_name == "barabasi_albert_model":

        if additional_parameters is not None:
            if 'c' in keys:
                c = additional_parameters['c']
                random_graphs.append((barabasi_albert_model(number_of_nodes,int(c)),
                                         {"graph": graph_name, "c": c}))
            else:
                c = int(choice(floor(number_of_nodes))-2)
                random_graphs.append((barabasi_albert_model(number_of_nodes,c),
                                         {"graph": graph_name, "c": c}))
        else:
            c = int(choice(floor(number_of_nodes))-2)
            random_graphs.append((barabasi_albert_model(number_of_nodes,c),
                                         {"graph": graph_name, "c": c}))

        return random_graphs

    else:
        print('please provide a valid graphical model')
        return None