コード例 #1
0
ファイル: max_3_cut_gw.py プロジェクト: c0n73x7/D1PL0MK4
            elif theta2 < angle and theta3 <= angle:
                angles.append(2*np.pi - theta3)
            elif theta1 < angle and theta2 <= angle:
                angles.append(angle + theta1)
            else:
                raise Exception(f'{theta1} ... {theta2} ... {theta3}')
        # labels
        labels = list()
        for angle in angles:
            label = int(((angle + psi) % 2*np.pi) / (2 * np.pi / 3))
            labels.append(label)
        labels = np.array(labels)
        # sum
        s = 0
        for l in range(3):
            for i in np.argwhere(labels == l).flatten():
                for j in np.argwhere(labels != l).flatten():
                    s += W[i][j]
        s = int(s/2)
        if best_sum < s:
            best_sum = s
    return best_sum


if __name__ == "__main__":
    W = generate_random_graph(30, 0.2)
    relax = solve_sdp_program(W)
    L = cholesky(relax)
    best_sum = find_partition(L, W)
    print(best_sum)
コード例 #2
0
import numpy as np
from max_k_cut_fj import solve_sdp_program as sdp_fj
from utils import generate_random_graph

if __name__ == "__main__":
    n, k = 30, 3
    for density in [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]:
        print(
            f'generate random graph with {n} vertices, and density {density}')
        W = generate_random_graph(n, density)
        print(f'solve frieze, and jerum relaxation')
        relax_fj = sdp_fj(W, k)
        path = f'./experiments/relax_fj_{n}_{density}_{k}'
        print(f'save result to "{path}"')
        np.save(path, relax_fj)
        print()
コード例 #3
0
def solver(algorithm, k_coloring, number_of_nodes, max_steps, number_of_runs):
    """Map coloring problem solver"""
    if k_coloring != 3 and k_coloring != 4:
        k_coloring = 4

    print("Solving for these parameters: ", "Algorithm=", algorithm,
          ", Number of colors=", k_coloring, ", Number of nodes=",
          number_of_nodes, "\nNumber of runs=", number_of_runs)
    if algorithm == "mc":
        print("Max Steps used by min-conflicts algorithm=", max_steps)

    # Variables used to gather statistics
    time_sum = 0
    solution_found_count = 0

    # Main loop for each run
    for i in range(number_of_runs):
        # Generate a random graph.
        graph, pos, edges = generate_random_graph(number_of_nodes)
        if number_of_runs == 1:
            # Plot the graph only if the number of runs is 1
            plot_graph(pos, edges, number_of_nodes, False, k_coloring, [])

        # Choose an algorithm to solve the graph
        # 1-Backtracking Algorithm
        if algorithm == "bt":
            start_time = time.monotonic()
            solution_exits, answer = backtracking(number_of_nodes, graph,
                                                  k_coloring)
            end_time = time.monotonic()
            time_sum += (end_time - start_time)
            if solution_exits:
                solution_found_count += 1
                if number_of_runs == 1:
                    print("Color Assignment", answer)
                    plot_graph(pos, edges, number_of_nodes, True, k_coloring,
                               answer)
            else:
                if number_of_runs == 1:
                    print("No Solution exists.")
        # 2-Min-conflicts Algorithm
        elif algorithm == "mc":
            start_time = time.monotonic()
            answer = min_conflicts(graph, number_of_nodes, k_coloring,
                                   max_steps)
            end_time = time.monotonic()
            time_sum += (end_time - start_time)
            if answer:
                solution_found_count += 1
                if number_of_runs == 1:
                    print("Color Assignment", answer)
                    plot_graph(pos, edges, number_of_nodes, True, k_coloring,
                               answer)
            else:
                if number_of_runs == 1:
                    print("No Solution exists.")
        # 3-Backtracking with forward checking
        elif algorithm == "bt-fc":
            start_time = time.monotonic()
            solution_exits, answer = backtracking_with_forward_checking(
                number_of_nodes, graph, k_coloring)
            end_time = time.monotonic()
            time_sum += (end_time - start_time)
            if solution_exits:
                solution_found_count += 1
                answer = modify_answer_format(answer, number_of_nodes)
                if number_of_runs == 1:
                    print("Color Assignment", answer)
                    plot_graph(pos, edges, number_of_nodes, True, k_coloring,
                               answer)
            else:
                if number_of_runs == 1:
                    print("No Solution exists.")
        # 4-Backtracking with mac
        elif algorithm == "bt-mac":
            start_time = time.monotonic()
            solution_exits, answer = backtracking_with_mac(
                number_of_nodes, graph, k_coloring)
            end_time = time.monotonic()
            time_sum += (end_time - start_time)
            if solution_exits:
                solution_found_count += 1
                answer = modify_answer_format(answer, number_of_nodes)
                if number_of_runs == 1:
                    print("Color Assignment", answer)
                    plot_graph(pos, edges, number_of_nodes, True, k_coloring,
                               answer)
            else:
                if number_of_runs == 1:
                    print("No Solution exists.")

    # Display statistics if number of runs is more than one
    if number_of_runs > 1:
        avg_runtime = time_sum / number_of_runs
        percentage_of_finding_a_solution = solution_found_count / number_of_runs
        print("Average Run Time = ", avg_runtime,
              ", Percentage of finding a solution = ",
              percentage_of_finding_a_solution * 100, "%")
    if number_of_runs == 1:
        print("Run Time = ", time_sum)