def fixwp_example6(): g = io.load_from_file("assets/strong parity/example_6.txt") (w0, w1) = fixwp.partial_solver(g, 1) #no solutions with lambda = 1 (w0_2, w1_2) = fixwp.partial_solver(g, 2) #no solutions with lambda = 2 (w0_3, w1_3) = fixwp.partial_solver(g, 3) #no solutions with lambda = 3 (w0_4, w1_4) = fixwp.partial_solver(g, 4) #full solve with lambda >= 4 return ops.are_lists_equal(w0, []) and ops.are_lists_equal( w1, []) and ops.are_lists_equal(w0_2, []) and ops.are_lists_equal( w1_2, []) and ops.are_lists_equal(w0_3, []) and ops.are_lists_equal( w1_3, []) and ops.are_lists_equal( w0_4, []) and ops.are_lists_equal(w1_4, [1, 2, 3, 4, 5])
def fixwp_example8(): #Figure 3.2.7 g = io.load_from_file("assets/strong parity/example_8.txt") (w0, w1) = fixwp.partial_solver( g, 10 ) #no solutions with any lambda because the winner is player 0 but player 1 can manage to loop to prevent window from closing return ops.are_lists_equal(w0, []) and ops.are_lists_equal(w1, [])
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))
def benchmark_ladder_wp(n, start_lambda = 1, end_lambda = 10, step_lambda = 1, dir = True, v2 = False, iterations=3, step=10, plot=False, path="", save_res = False, path_res = "", 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 plot: if True, plots the data using matplotlib. :param path: path to the file in which to write the result. :param save_res: if True, save the results on a file. :param path_res: 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 g = generators.ladder(i) # 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 = ((len(w0) + len(w1)) /(2*i)) * 100 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 "Ladder 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 Ladder 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 #save the percent solve for each player for each lambda for each size of game in a txt if save_res: i = 0 for lam in range(start_lambda, end_lambda + 1, step_lambda): #computing the percent solved for each player for the games not solved completely part_solv = 0 comp_solv = 0 percent_solv = [] for (g, w0, w1) in not_comp_solved: #one more game not solved completely part_solv += 1 #checking is the solutions are included to the true solutions. Computing the true sols with Zielonka algorithm (sp_w0, sp_w1) = sp(g) a = all(s in sp_w1 for s in w1) #cheking if included b = all(s in sp_w0 for s in w0) #cheking if included #if not included stop the algorithm if not a or not b: print "Error the solutions found are not included to the true solutions" return -1 #total percentage solved percent_solved_total = ((float(len(w0)) + len(w1)) /(len(sp_w0) + len(sp_w1))) * 100 #percentage solved for player 0 if len(sp_w0) > 0: percent_solved_0 = (float(len(w0)) /len(sp_w0)) * 100 else: percent_solved_0 = 100 #percentage solved for player 1 if len(sp_w1) > 0: percent_solved_1 = (float(len(w1)) /len(sp_w1)) * 100 else: percent_solved_1 = 100 #adding the percentages computed to the list + the size of the game percent_solv.append((len(g.get_nodes()), percent_solved_total, percent_solved_0, percent_solved_1)) comp_solv += n/step - len(not_comp_solved) io.write_benchmark_partial_solver(comp_solv, part_solv, percent_solv, path_res+"_"+str(lam)+".txt", n_[i], y[i], n, 1, lam, False, "")
def fixwp_example9(): g = io.load_from_file("assets/strong parity/example_9.txt") (w0, w1) = fixwp.partial_solver(g, 1) #full solve with lambda >= 1 return ops.are_lists_equal(w0, [1, 2, 3]) and ops.are_lists_equal(w1, [])
def solver(): """ Takes appropriate actions according to the chosen options (using command_line_handler() output). """ # Parsing the command line arguments args = command_line_handler() if args.mode == "solve": """ ----- Solving mode ----- """ if args.gp: g = tools.load_generalized_from_file( args.inputFile) # we have a generalized parity game arena else: g = tools.load_from_file( args.inputFile) # loading game from the input file player = 0 # default player is 0, so solution comes as (W_0,sigma_0), (W_1,sigma_1) or (W_0, W_1) # Reachability (target and player is set) if args.target is not None: player = int(args.target[0] ) # getting player (as int), replacing default player target = map( int, args.target[1].split(",") ) # getting node ids in target (transforming them into int) solution = reachability.reachability_solver( g, target, player) # calling reachability solver ops.print_solution(solution, player) # printing the solution # Safety (safe set provided) if args.safe is not None: player = 1 # the player with the reachability objective (to write the solution later) safe_set = map( int, args.safe[0].split(",") ) # getting node ids in safe set (transforming them into int) target_set = [] # adds every node not in the safe set to the target set for node in g.get_nodes(): if not (node in safe_set): target_set.append(node) # the solution comes out as (W_1,sigma_1), (W_0,sigma_0) solution = reachability.reachability_solver( g, target_set, 1 ) # calling reachability solver with target set for player 1 (2) ops.print_solution( solution, 1 ) # printing the solution, player 1 (2) has the reachability objective # Weak parity elif args.wp: solution = weakparity.weak_parity_solver( g) # calling weak parity solver on the game ops.print_solution(solution, player) # printing the solution # Strong parity (an algorithm is chosen) elif args.parity_algorithm is not None: if (args.parity_algorithm == 'recursive'): solution = strongparity.strong_parity_solver( g) # calling recursive algorithm for parity games ops.print_solution( solution, player) # printing the solution (with strategy) elif (args.parity_algorithm == 'safety'): solution = strongparity.reduction_to_safety_parity_solver( g) # calling reduction to safety algorithm ops.print_winning_regions( solution[0], solution[1]) # printing the solution (without strategy) elif (args.parity_algorithm == 'antichain'): # calling antichain-based algorithm, assumes indexes start with 1 solution = strongparity.strong_parity_antichain_based(g, 1) ops.print_winning_regions( solution[0], solution[1]) # printing the solution (without strategy) else: # this should not happen solution = None # Generalized parity elif args.gp: solution = generalizedparity.generalized_parity_solver( g) # calling classical algorithm for generalized parity games ops.print_winning_regions( solution[0], solution[1]) # printing the solution (without strategy) #Window parity partial solvers elif args.windowparity is not None: if args.windowparity[0] == 'dirfixwp': lam = int(args.windowparity[1]) v2 = bool(args.windowparity[2]) if v2: solution = dirfixwp.partial_solver2( g, lam) #calling the dirfixwp v2 algorithm ops.print_winning_regions( solution[0], solution[1] ) #printing the solutions (without strategy) else: solution = dirfixwp.partial_solver( g, lam) #calling the dirfixwp algorithm ops.print_winning_regions( solution[0], solution[1] ) #printing the solutions (without strategy) elif args.windowparity[0] == 'fixwp': lam = int(args.windowparity[1]) v2 = bool(args.windowparity[2]) if v2: solution = fixwp.partial_solver2( g, lam) #calling the dirfixwp v2 algorithm ops.print_winning_regions( solution[0], solution[1] ) #printing the solutions (without strategy) else: solution = fixwp.partial_solver( g, lam) #calling the dirfixwp algorithm ops.print_winning_regions( solution[0], solution[1] ) #printing the solutions (without strategy) #Winning core partial solver elif args.winningcore: solution = winningcore.partial_solver( g) #calling the dirfixwp v2 algorithm ops.print_winning_regions( solution[0], solution[1]) #printing the solutions (without strategy) # If output option is chosen and the algorithm is the classical algo for generalized parity games, use special # function dedicated to writing solution of generalized parity games (need to consider several priorities) if (args.outputFile is not None) and args.gp: tools.write_generalized_solution_to_file(g, solution[0], solution[1], args.outputFile) # If output option is chosen and the algorithm is the reduction to safety algorithm for parity games or the # antichain-based algorithm for parity games then the output is only the winning regions, not the strategies elif (args.outputFile is not None) and (args.parity_algorithm == 'safety' or args.parity_algorithm == 'antichain'): tools.write_solution_to_file_no_strategies(g, solution[0], solution[1], args.outputFile) # If output option is chosen and the algorithm is a partial solver for parity games then the output is only the winning regions, not the strategies. elif (args.outputFile is not None) and (args.winningcore is not None or args.windowparity is not None): tools.write_solution_to_file_no_strategies_solvpart( g, solution[0], solution[1], args.outputFile) # Else the regular regions + strategies are output elif args.outputFile is not None: tools.write_solution_to_file(g, solution, player, args.outputFile) elif args.mode == "bench": """ ----- Benchmark mode ----- """ max = args.max step = args.step rep = args.repetitions plot = args.outputPlot is not None # Reachability if args.reachability_type is not None: if args.reachability_type == 'complete0': r_bench.benchmark(max, generators.complete_graph, [1], 0, iterations=rep, step=step, plot=plot, regression=True, order=2, path=args.outputPlot, title=u"Graphes complets de taille 1 à " + str(max)) elif args.reachability_type == 'complete1': r_bench.benchmark(max, generators.complete_graph, [1], 1, iterations=rep, step=step, plot=plot, regression=True, order=2, path=args.outputPlot, title=u"Graphes complets de taille 1 à " + str(max)) elif args.reachability_type == 'worstcase': r_bench.benchmark(max, generators.reachability_worst_case, [1], 0, iterations=rep, step=step, plot=plot, regression=True, order=2, path=args.outputPlot, title=u"Graphes 'pire cas' de taille 1 à " + str(max)) # Weak parity elif args.weakparity_type is not None: if args.weakparity_type == 'complete': wp_bench.benchmark(max, generators.complete_graph_weakparity, iterations=rep, step=step, plot=plot, regression=True, order=2, path=args.outputPlot, title=u"Graphes complets de taille 1 à " + str(max)) elif args.weakparity_type == 'worstcase': wp_bench.benchmark(max, generators.weak_parity_worst_case, iterations=rep, step=step, plot=plot, regression=True, order=3, path=args.outputPlot, title=u"Graphes 'pire cas' de taille 1 à " + str(max)) # parity elif args.parity_type is not None: if args.parity_type == 'recursive-random': sp_bench.benchmark_random(max, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.parity_type == 'safety-random': sp_bench.benchmark_random_reduction(max, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.parity_type == 'antichain-random': sp_bench.benchmark_random_antichain_based(max, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.parity_type == 'recursive-worstcase': sp_bench.benchmark_worst_case(max, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.parity_type == 'safety-worstcase': sp_bench.benchmark_worst_case_reduction(max, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.parity_type == 'antichain-worstcase': sp_bench.benchmark_worst_case_antichain_based( max, iterations=rep, step=step, plot=plot, path=args.outputPlot) # generalized parity elif args.genparity_type: gp_bench.benchmark_random_k_functions(max, 3, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.winningcore_type is not None: if args.winningcore_type == 'random': ps_bench.benchmark_random_wc(max, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.winningcore_type == 'ladder': ps_bench.benchmark_ladder_wc(max, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.winningcore_type == 'worstcase': ps_bench.benchmark_worst_case_wc(max, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.windowparity_type is not None: min_lambda = int(args.windowparity_type[2]) max_lambda = int(args.windowparity_type[3]) step_lambda = int(args.windowparity_type[4]) v2 = bool(args.windowparity_type[5]) if args.windowparity_type[0] == 'dirfixwp': if args.windowparity_type[1] == 'random': ps_bench.benchmark_random_wp(max, start_lambda=min_lambda, end_lambda=max_lambda, step_lambda=step_lambda, dir=True, v2=v2, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.windowparity_type[1] == 'ladder': ps_bench.benchmark_ladder_wp(max, start_lambda=min_lambda, end_lambda=max_lambda, step_lambda=step_lambda, dir=True, v2=v2, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.windowparity_type[1] == 'worstcase': ps_bench.benchmark_worst_case_wp(max, start_lambda=min_lambda, end_lambda=max_lambda, step_lambda=step_lambda, dir=True, v2=v2, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.windowparity_type[0] == 'fixwp': if args.windowparity_type[1] == 'random': ps_bench.benchmark_random_wp(max, start_lambda=min_lambda, end_lambda=max_lambda, step_lambda=step_lambda, dir=False, v2=v2, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.windowparity_type[1] == 'ladder': ps_bench.benchmark_ladder_wp(max, start_lambda=min_lambda, end_lambda=max_lambda, step_lambda=step_lambda, dir=False, v2=v2, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.windowparity_type[1] == 'worstcase': ps_bench.benchmark_worst_case_wp(max, start_lambda=min_lambda, end_lambda=max_lambda, step_lambda=step_lambda, dir=False, v2=v2, iterations=rep, step=step, plot=plot, path=args.outputPlot) elif args.mode == "test": sp_test_result = sp_test.launch_tests() wp_test_result = wp_test.launch_tests() r_test_result = r_test.launch_tests() gp_test_result = gp_test.launch_tests() sp_test_result = sp_test.launch_tests() buchi_test_result = buchi_test.launch_tests() if (sp_test_result and wp_test_result and r_test_result and gp_test_result and sp_test_result and buchi_test_result): print "All tests passed with success" else: print "Some tests failed"