コード例 #1
0
def worstcase1_removed_optimization():
    """
    Solves a worst case graph G_n for n = 1.
    """
    g = io.load_from_file("assets/strong parity/worstcase_1.txt")
    removed = bitarray([False] + ([False] * len(g.nodes)))
    (a, b), (c, d) = sp.strong_parity_solver_non_removed(g, removed)
    return a == [1, 3, 4, 2, 0] and b == {1: 2, 3: 1} and c == [] and d == {}
コード例 #2
0
def example_1_removed_optimization():
    """
    Solves a simple example.
    """
    g = io.load_from_file("assets/strong parity/example_1.txt")
    removed = bitarray([False] + ([False] * len(g.nodes)))
    (a, b), (c, d) = sp.strong_parity_solver_non_removed(g, removed)
    return (a == [1, 3, 2]) and b == {1: 1, 3: 3} and c == [] and d == {}
コード例 #3
0
def figure56_removed_optimization():
    """
    Solves the strong parity game from figure 5.6.
    """
    fig56_graph = io.load_from_file("assets/strong parity/figure56.txt")
    removed = bitarray([False] + ([False] * len(fig56_graph.nodes)))
    (a, b), (c, d) = sp.strong_parity_solver_non_removed(fig56_graph, removed)
    return (a == [2, 4, 1, 6]) and b == {
        2: 2,
        4: 1
    } and c == [5, 3] and d == {
        5: 5
    }
コード例 #4
0
def worstcase2_removed_optimization():
    """
    Solves a worst case graph G_n for n = 2.
    """
    g = io.load_from_file("assets/strong parity/worstcase_2.txt")
    removed = bitarray([False] + ([False] * len(g.nodes)))
    (a, b), (c, d) = sp.strong_parity_solver_non_removed(g, removed)
    return a == [] and b == {} and c == [6, 8, 9, 7, 5, 4, 0, 2, 1, 3
                                         ] and d == {
                                             0: 4,
                                             2: 4,
                                             4: 5,
                                             6: 7,
                                             8: 6
                                         }
コード例 #5
0
def example_5_removed_optimization():
    """
    Solves a simple example.
    """
    g = io.load_from_file("assets/strong parity/example_5.txt")
    removed = bitarray([False] + ([False] * len(g.nodes)))
    (a, b), (c, d) = sp.strong_parity_solver_non_removed(g, removed)
    return a == [2, 1, 5] and b == {
        1: 2,
        2: 2,
        5: 5
    } and c == [7, 6, 3, 4] and d == {
        3: 6,
        4: 3,
        6: 6
    }
コード例 #6
0
def example_3_removed_optimization():
    """
    Solves a simple example.
    """
    g = io.load_from_file("assets/strong parity/example_3.txt")
    removed = bitarray([False] + ([False] * len(g.nodes)))
    (a, b), (c, d) = sp.strong_parity_solver_non_removed(g, removed)
    return (a == [2, 1, 3, 4]) and b == {
        4: 4,
        2: 4,
        1: 2
    } and c == [6, 7, 5] and d == {
        7: 6,
        6: 6,
        5: 6
    }
コード例 #7
0
def ladder(n, iterations=3, step=10, plot=False, path=""):
    """
    Benchmarks the recursive algorithm for strong parity games using the worst case generator which yields an
    exponential complexity. Calls strong parity solver on games generated using the worst case generator function.
    Games of size 5 to 5*n are solved and a timer records the time taken to get the solution.The solver can be timed
    several times and the minimum value is selected using optional parameter iterations (to avoid recording time
    spikes and delays due to system load). The result can be plotted using matplotlib.
    :param n: number of nodes in generated graph (nodes is 5*n due to construction).
    :param iterations: number of times the algorithm is timed (default is 3).
    :param step: step to be taken in the generation.
    :param plot: if True, plots the data using matplotlib.
    :param path: path to the file in which to write the result.
    """

    y = []  # list for the time recordings
    n_ = []  # list for the x values (5 to 5n)
    y2 = []  # list for the time recordings
    n_2 = []  # list for the x values (5 to 5n)
    total_time = 0  # accumulator to record total time

    nbr_generated = 0  # conserving the number of generated mesures (used to get the index of a mesure)

    chrono = timer.Timer(verbose=False)  # Timer object

    info = "Time to solve (s)"  # info about the current benchmark

    # print first line of output
    print u"Generator".center(40) + "|" + u"Nodes (n)".center(12) + "|" + info.center(40) + "\n" + \
          "-" * 108

    # games generated are size 1 to n
    for i in range(1, n + 1, step):
        temp1 = []  # temp list for #iterations recordings
        g = generators.ladder(i)  # generated game

        # #iterations calls to the solver are timed
        for j in range(iterations):
            with chrono:
                strong_parity_solver(g)  # solver call
            temp1.append(chrono.interval)  # add time recording

        min_recording = min(temp1)
        y.append(
            min_recording)  # get the minimum out of #iterations recordings
        n_.append(2 * i - 1)
        total_time += min_recording

        temp2 = []
        # #iterations calls to the solver are timed
        removed = bitarray([False] + ([False] * len(g.nodes)))
        for j in range(iterations):
            with chrono:
                strong_parity_solver_non_removed(g, removed)  # solver call
            temp2.append(chrono.interval)  # add time recording

        min_recording = min(temp2)
        y2.append(
            min_recording)  # get the minimum out of #iterations recordings
        n_2.append(2 * i - 1)


        print "Worst-case graph".center(40) + "|" + str(i * 5).center(12) + "|" \
              + str(y[nbr_generated]).center(40) + "\n" + "-" * 108

        nbr_generated += 1  # updating the number of generated mesures

        # at the end, print total time
    print "-" * 108 + "\n" + "Total (s)".center(40) + "|" + "#".center(12) + "|" + \
          str(total_time).center(40) + "\n" + "-" * 108 + "\n"

    if plot:
        plt.grid(True)
        plt.title(u"Graphes 'pire cas' de taille 5 à " + str(2 * n - 1))
        plt.xlabel(u'nombre de nœuds')
        plt.ylabel(u'temps (s)')

        # plt.yscale("log") allows logatithmic y-axis
        points, = plt.plot(n_, y, 'g.', label=u"Temps d'exécution")
        points2, = plt.plot(n_2, y2, 'r.', label=u"Temps d'exécution 2")

        plt.legend(loc='upper left', handles=[points, points2])
        plt.savefig(path, bbox_inches='tight')
        plt.clf()
        plt.close()
コード例 #8
0
def benchmark_worst_case_removed_optimization(n,
                                              iterations=3,
                                              step=10,
                                              plot=False,
                                              path=""):
    """
        Analyzes the influence of the removed list optimization on the recursive algorithm for worst-case graphs.

        :param n: number of nodes in generated graph (nodes is 5*n due to construction).
        :param iterations: number of times the algorithm is timed (default is 3).
        :param step: step to be taken in the generation.
        :param plot: if True, plots the data using matplotlib.
        :param path: path to the file in which to write the result.
        """

    y = []  # list for the time recordings
    n_ = []  # list for the x values (5 to 5n)
    y2 = []  # list for the time recordings
    n_2 = []  # list for the x values (5 to 5n)
    total_time = 0  # accumulator to record total time

    nbr_generated = 0  # conserving the number of generated mesures (used to get the index of a mesure)

    chrono = timer.Timer(verbose=False)  # Timer object

    info = "Time to solve (s)"  # info about the current benchmark

    # print first line of output
    print u"Generator".center(40) + "|" + u"Nodes (n)".center(12) + "|" + info.center(40) + "\n" + \
          "-" * 108

    # games generated are size 1 to n
    for i in range(1, n + 1, step):
        temp = []  # temp list for #iterations recordings
        g = generators.strong_parity_worst_case(i)  # generated game

        # #iterations calls to the solver are timed
        for j in range(iterations):
            with chrono:
                strong_parity_solver(g)  # solver call
            temp.append(chrono.interval)  # add time recording

        min_recording = min(temp)
        y.append(
            min_recording)  # get the minimum out of #iterations recordings
        n_.append(5 * i)
        total_time += min_recording

        temp = []
        # #iterations calls to the solver are timed
        for j in range(iterations):
            removed = bitarray([False] + ([False] * len(g.nodes)))
            with chrono:
                strong_parity_solver_non_removed(g, removed)  # solver call
            temp.append(chrono.interval)  # add time recording

        min_recording = min(temp)
        y2.append(
            min_recording)  # get the minimum out of #iterations recordings
        n_2.append(5 * i)

        min_recording = min(temp)

        print "Worst-case graph".center(40) + "|" + str(i * 5).center(12) + "|" \
              + str(y[nbr_generated]).center(40) + "\n" + "-" * 108

        nbr_generated += 1  # updating the number of generated mesures

        # at the end, print total time
    print "-" * 108 + "\n" + "Total (s)".center(40) + "|" + "#".center(12) + "|" + \
          str(total_time).center(40) + "\n" + "-" * 108 + "\n"
    if plot:
        plt.grid(True)
        plt.title(
            u"Recursive algorithm runtime comparison : worst-case graphs of size 1 to "
            + str(5 * n))
        plt.xlabel(u'number of nodes')
        plt.ylabel(u'time (s)')

        # plt.yscale("log") allows logatithmic y-axis
        points, = plt.plot(n_, y, 'g.', label=u"Regular")
        points2, = plt.plot(n_2,
                            y2,
                            'r.',
                            label=u"Without sub-game construction")

        plt.legend(loc='upper left', handles=[points, points2])
        plt.savefig(path, bbox_inches='tight')
        plt.clf()
        plt.close()