Ejemplo n.º 1
0
def benchmark_random_wp(n, start_lambda = 1, end_lambda = 10, step_lambda = 1, dir = True, v2 = False, iterations=3, step=10, ret = False, plot=False, path="", prt = True):
    """
    Benchmarks the window parity algorithms for strong parity games using the random game generator.
    Calls window parity partial solver on games generated using the random game generator function.
    Games of size 1 to 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 n due to construction).
    :param end_lambda: maximum value of lambda.
    :param start_lambda: starting value of lambda.
    :param step_lambda: step to be taken between the different value of lambda.
    :param dir: if True, uses DirFixWP if not, uses FixWP.
    :param v2: if True, uses partial_solver2. If False, uses partial_solver.
    :param iterations: number of times the algorithm is timed (default is 3).
    :param step: step to be taken in the generation.
    :param ret: if True, return the winning region of the games that were not completely solved.
    :param plot: if True, plots the data using matplotlib.
    :param path: path to the file in which to write the result.
    :param prt: True if the prints are activated.
    :return: Percentage of game solved, size of the games such that we return the time taken to resolve those games.
    """

    y = []  # list for the time recordings
    n_ = []  # list for the x values (1 to n)
    per_sol = [] # list for the percentage of the game solved

    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

    lam = start_lambda #current lambda used by the algorithme

    not_comp_solved = [] #store the game that are not completely solved

    # print first line of output
    if prt:
        print u"Generator".center(40) + "|" + u"Nodes (n)".center(12) + "|" + info.center(40) + "|" + "Percentage solved".center(19) + "|" + "\n" + \
            "-" * 115

    #for each value of lambda
    for lam in range(start_lambda, end_lambda + 1, step_lambda):
        #print current value of lambda
        if prt:
            print "lambda value : ".center(40) + str(lam) + "\n" + "-" * 115
        y_temp = [] # stores time recordings for the current value of lambda
        n_temp = [] # stores the size of the game for the current value of lambda 
        per_sol_temp = [] #stores the resolution percentage of the games for the current value of lambda
        nbr_generated = 0
        # games generated are size 1 to n
        for i in range(1, n + 1, step):
            temp = []  # temp list for #iterations recordings
            prio = randint(0, i)  # number of priorities
            min_out = randint(1, i) # minimum number of out edges
            max_out = randint(min_out, i) #maximum number of out edges
            g = generators.random(i, prio, min_out, max_out)  # generated game

            # #iterations calls to the solver are timed
            for j in range(iterations):
                with chrono:
                    if dir:
                        if v2:
                            (w0, w1) = dirfixwp.partial_solver2(g, lam)  # dirfixwp version 2 call
                        else:
                            (w0, w1) = dirfixwp.partial_solver(g, lam)  # dirfixwp call
                    else:
                        if v2:
                            (w0, w1) = fixwp.partial_solver2(g, lam)  # fixwp version 2 call
                        else:
                            (w0, w1) = fixwp.partial_solver(g, lam)  # fixwp call
                temp.append(chrono.interval)  # add time recording

            min_recording = min(temp)
            percent_solved = ((float(len(w0)) + len(w1)) /(i)) * 100
            #checking if completely solved
            if percent_solved != 100:
                not_comp_solved.append((g, w0, w1))

            y_temp.append(min_recording)  # get the minimum out of #iterations recordings
            n_temp.append(i) 
            per_sol_temp.append(percent_solved)
            total_time += min_recording
            

            if prt:
                print "Random graph".center(40) + "|" + str(i).center(12) + "|" \
                + str(y_temp[nbr_generated]).center(40) + "|" + str(percent_solved).center(19) + "|" + "\n" + "-" * 115

            nbr_generated += 1  # updating the number of generated mesures
        y.append(y_temp)
        n_.append(n_temp)
        per_sol.append(per_sol_temp)


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

    if plot:
        i = 0
        for lam in range(start_lambda, end_lambda + 1, step_lambda):
            fig, ax1 = plt.subplots()
            plt.grid(True)
            plt.title(u"Graphes aléatoires de taille 1 à " + str(n) + " avec lambda = " + str(lam))
            plt.xlabel(u'nombre de nœuds')
            # plt.yscale("log") allows logatithmic y-axis
            ax1.plot(n_[i], y[i], 'g.', label=u"Temps d'exécution", color='b')
            ax1.tick_params('y', colors='b')
            ax1.set_ylabel("Temps d'execution (s)", color = 'b')

            ax2 = ax1.twinx()
            ax2.plot(n_[i], per_sol[i], 'g.', label=u"Pourcentage résolu", color='r')
            ax2.set_ylim([0, 101])
            ax2.set_ylabel("Pourcentage du jeu resolu (%)", color = 'r')
            ax2.tick_params('y', colors='r')
            fig.tight_layout()

            plt.savefig(path+str(lam)+".png", bbox_inches='tight')
            plt.clf()
            plt.close()
            i = i + 1

    if ret:
        return (not_comp_solved, (n_, y))
Ejemplo n.º 2
0
def benchmark_random_wc(n, iterations=3, step=10, ret = False, plot=False, path="", prt = True):
    """
    Benchmarks the window parity algorithms for strong parity games using the random game generator.
    Calls window parity partial solver on games generated using the random game generator function.
    Games of size 1 to 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 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 ret: if True, return the winning region of the games that were not completely solved.
    :param plot: if True, plots the data using matplotlib.
    :param path: path to the file in which to write the result.
    :param prt: True if the prints are activated.
    """

    y = []  # list for the time recordings
    n_ = []  # list for the x values (1 to n)
    per_sol = [] # list for the percentage of the game solved

    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

    not_comp_solved = [] #store the game that are not completely solved

    # print first line of output
    if prt:
        print u"Generator".center(40) + "|" + u"Nodes (n)".center(12) + "|" + info.center(40) + "|" + "Percentage solved".center(19) + "|" + "\n" + \
            "-" * 115

    # games generated are size 1 to n
    for i in range(1, n + 1, step):
        temp = []  # temp list for #iterations recordings
        prio = randint(0, i)  # number of priorities
        min_out = randint(1, i) # minimum number of out edges
            max_out = randint(min_out, i) #maximum number of out edges
        g = generators.random(i, prio, min_out, max_out)  # generated game

        # #iterations calls to the solver are timed
        for j in range(iterations):
            with chrono:
                (w0, w1) = winningcore.partial_solver(g)  # winning core call
            temp.append(chrono.interval)  # add time recording

        min_recording = min(temp)
        percent_solved = ((float(len(w0)) + len(w1)) /(i)) * 100
        if percent_solved != 100:
            not_comp_solved.append((g, w0, w1))

        y.append(min_recording)  # get the minimum out of #iterations recordings
        n_.append(i)
        per_sol.append(percent_solved)
        total_time += min_recording
        

        if prt:
            print "Random graph".center(40) + "|" + str(i).center(12) + "|" \
            + str(y[nbr_generated]).center(40) + "|" + str(percent_solved).center(19) + "|" + "\n" + "-" * 115

        nbr_generated += 1  # updating the number of generated mesures
def benchmark_random_removed_optimization(n,
                                          iterations=3,
                                          step=10,
                                          plot=False,
                                          path=""):
    """
    Analyzes the influence of the removed list optimization on the recursive algorithm for random 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(2, n + 1, step):
        temp1 = []  # temp list for #iterations recordings
        g = generators.random(i, i, 1, (i / 2))  # 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(i)
        total_time += min_recording

        temp2 = []
        # #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
            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(i)

        print "Worst-case graph".center(40) + "|" + str(i).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 : random graphs of size 1 to "
            + str(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()
Ejemplo n.º 4
0
def gen(i):
    return generators.random(i, i, 1, (i / 2))
def benchmark_random_reduction(n, iterations=3, step=10, plot=False, path=""):
    """
    Benchmarks the reduction to safety algorithm for strong parity games using a random generator. Games of size 1 to 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.
    :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

    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"  # 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(5, n + 1, step):
        temp = []  # temp list for #iterations recordings
        g = generators.random(i, i, 1, (i / 2))  # generated game
        # #iterations calls to the solver are timed
        for j in range(iterations):
            with chrono:
                reduction_to_safety_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(i)
        total_time += min_recording

        print "Random graphs".center(40) + "|" + str(i).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 time".center(40) + "|" + "#".center(12) + "|" + \
          str(total_time).center(40) + "\n" + "-" * 108 + "\n"

    if plot:
        plt.grid(True)
        plt.title(u"Random graphs of size 1 to " + str(n))
        plt.xlabel(u'number of nodes')
        plt.ylabel(u'time (s)')
        points, = plt.plot(n_, y, 'g.', label=u"Execution time")
        plt.legend(loc='upper left', handles=[points])
        plt.savefig(path, bbox_inches='tight')
        plt.clf()
        plt.close()