Exemplo n.º 1
0
def run_many_times():
    """Run the colourings many times."""
    
    # Get CONSTANTS.
    # COLOURING_TO_SHOW and SHOW_LABELS are not needed
    # since we don't have an output figure.
    global GRAPH_SIZE, RANDOM_GRAPH_EDGE_NUMBER
    global ERDOS_RENYI_P, USE_ERDOS_RENYI
    global RUN_BRUTE_FORCE, TIMES_TO_RUN
    
    random_greedy = colouring_class.ColouringRepeated(
        "Random greedy", greedy_colouring, "randomGreedy",
        True, random_ordering)
    
    degree_greedy = colouring_class.ColouringRepeated(
        "Degree greedy", greedy_colouring, "degreeGreedy",
        True, degree_ordering)
    
    dsatur = colouring_class.ColouringRepeated(
        "DSatur", dsatur_colouring, "DSatur",
        preserve_capitalisation=True)

    if RUN_BRUTE_FORCE:
        product_brute_force = colouring_class.ColouringRepeated(
            "Product brute force",
            product_brute_force_colouring,
            "productBruteForce")

        custom_brute_force = colouring_class.ColouringRepeated(
            "Custom brute force",
            custom_brute_force_colouring,
            "customBruteForce")
    
    # Disable garbage collection so better comparison
    # (like in the timeit module).
    gc.disable()
    total_graph_time = 0
    for counter in range(TIMES_TO_RUN):
        
        # Create graph.  Time graph creation.
        pre_graph_time = time.time()
        if USE_ERDOS_RENYI:
            G = erdos_renyi(GRAPH_SIZE, ERDOS_RENYI_P)
        else:
            G = random_graph(GRAPH_SIZE, RANDOM_GRAPH_EDGE_NUMBER)
        post_graph_time = time.time()
        total_graph_time += post_graph_time - pre_graph_time
        
        # Run each colouring on G.
        for object_ in colouring_class.colouring_object_list:
            object_.main_colouring_function(G)
    gc.enable()
    # Print out info about graph creation times,
    # and running times and colours.
    print(f"The {TIMES_TO_RUN} graph creations " \
          f"took {total_graph_time:.6f} seconds.")
    for colouring_object in colouring_class.colouring_object_list:
        colouring_object.print_information(TIMES_TO_RUN)
Exemplo n.º 2
0
def main():

    ### reconstruction under 50%
    rg_graph_names = ['geometric_model', 'erdos_renyi', 'barabasi_albert_model', 'random_partition_model']
    ### which graphs do you want to consider?^
    parameter_mesh_size = 20 # number of sub rgs saught for a given rg model
    max_iterations = 1 # how long do we attempt to move towards a new rg
    averaging = 1 # number of times you sample to see which reconstruction was closest on average
    attempts = 1 # how many times we attemp to add a new random subset of edges to reconstruction per iteration
    empirical_graph = erdos_renyi(25, .5)
    observed_graph = remove_edges(empirical_graph, .1)
    print(find_closest_rg(observed_graph, rg_graph_names, parameter_mesh_size,
                        max_iterations, averaging, attempts))

    """
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
0
        'geometric_model', 'erdos_renyi', 'random_partition_model',
        'barabasi_albert_model'
    ]
    Averaging = 10
    percent_removed = linspace(0, 1, 10)
    plots = []
    simulations = 2

    # for p in percent_removed:
    #     counter = 0
    #     for i in range(simulations):
    #         Observered_G = remove_edges(barabasi_albert_model(10, 2), p)
    #         sim = edge_imputation_via_one_step_node_norm_minimization(
    #             Observered_G, Proposal_distributions, Averaging, 0)
    #         if sim[0] == 'barabasi_albert_model':
    #             counter += 1
    #             print(counter)
    #     percent_correct = counter/simulations
    #     plots.append(percent_correct)
    #
    # print(plots)
    # plt.plot(percent_removed,plots)
    # plt.ylabel('Simulation of Barabasi Albert Model')
    # plt.show()

    # Observered_G = remove_edges(barabasi_albert_model(20,2),.01)
    Observered_G = remove_edges(erdos_renyi(10, .999), .9)
    print(
        edge_imputation_via_one_step_node_norm_minimization(
            Observered_G, Proposal_distributions, Averaging, 0))
Exemplo n.º 6
0
def run_once():
    """Main function, does:
        graph creation;
        various colourings of the graph;
        output."""
    
    # CONSTANTS get
    # TIMES_TO_RUN not needed since it is known that
    # it is one if run_once() is called.
    global GRAPH_SIZE, RANDOM_GRAPH_EDGE_NUMBER
    global ERDOS_RENYI_P, COLOURING_TO_SHOW, SHOW_LABELS
    global RUN_BRUTE_FORCE, USE_ERDOS_RENYI
    
    # Create graph.  Time graph creation.
    pre_graph_time = time.time()
    if USE_ERDOS_RENYI:
        G = erdos_renyi(GRAPH_SIZE, ERDOS_RENYI_P)
    else:
        G = random_graph(GRAPH_SIZE, RANDOM_GRAPH_EDGE_NUMBER)
    post_graph_time = time.time()
    graph_time = post_graph_time - pre_graph_time

    # Print information about the graph.
    print("Vertices:")
    print(G.nodes())
    print("Edges:")
    print(G.edges())
    print(f"Creating the graph took {graph_time*1000:.3f} milliseconds.")

    # Instantiate instances of colouring_class.Colouring
    # for each colouring algorithm.
    
    random_greedy = colouring_class.Colouring(
        "Random greedy", greedy_colouring, "randomGreedy",
        True, random_ordering)

    degree_greedy = colouring_class.Colouring(
        "Degree greedy", greedy_colouring, "degreeGreedy",
        True, degree_ordering)

    dsatur = colouring_class.Colouring(
        "DSatur", dsatur_colouring, "DSatur",
        preserve_capitalisation=True)
    
    if RUN_BRUTE_FORCE:
        product_brute_force = colouring_class.Colouring(
            "Product brute force",
            product_brute_force_colouring,
            "productBruteForce")

        custom_brute_force = colouring_class.Colouring(
            "Custom brute force",
            custom_brute_force_colouring,
            "customBruteForce")

    # Default case for output_colouring is None
    # if COLOURING_TO_SHOW from graphs.config is invalid.
    output_colouring = None

    # Run each colouring on G and print out
    # the information about it.
    for colouring in colouring_class.colouring_object_list:
        colouring.main_colouring_function(G)
        
        # Assign the desired colouring to output_colouring.
        if colouring.config_name == COLOURING_TO_SHOW:
            output_colouring = colouring.colouring

    # Error message to say that there was an invalid COLOURING_TO_SHOW.
    if output_colouring == None and COLOURING_TO_SHOW != None:
        print("Invalid colouringToShow in graphs.config. " \
              "Using no colouring in output.")

    # Create and display output picture.
    # Try, except since errors can occur with large graphs.
    try:
        output_graph(G, output_colouring, SHOW_LABELS)
    except Exception as e:
        print(f"There was an exception in making the output graph (e).")
def erdos_renyi_generator(n=N, p=-1):
    if p < 0:
        p = np.random.rand()
    return random_graphs.erdos_renyi(n, p)
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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