def optimality(characters): # Generate a prism file to represent SMG of game between both teams sys.stdout = open("smg.prism", "w") smgPrefix.run(characters) free_strat.run(characters, 1) free_strat.run(characters, 2) suffix.run(characters, False) sys.stdout = sys.__stdout__ # run prism-games with lots of memory, hardcoded prism-games location on SAND os.system( "../../../../../../usr/local/prism-games-2.0.beta3-linux64/bin/prism -cuddmaxmem 100g -javamaxmem 100g smg.prism smg_props.props -prop 4 -s > log.txt" ) p1_opt = find_prev_result() os.system( "../../../../../../usr/local/prism-games-2.0.beta3-linux64/bin/prism -cuddmaxmem 100g -javamaxmem 100g smg.prism smg_props.props -prop 5 -s > log.txt" ) p2_opt = find_prev_result() return p1_opt, p2_opt
def optimality(characters): print "Calculating optimal values..." sys.stdout = open("smg.prism", "w") smgPrefix.run(characters) free_strat.run(characters, 1) free_strat.run(characters, 2) suffix.run(characters, False) sys.stdout = sys.__stdout__ os.system( "~/../../usr/prism-games/prism-games-2.0.beta3-linux64/bin/prism -cuddmaxmem 4g smg.prism smg_props.props -prop 4 -s > log.txt" ) p1_opt = find_prev_result() print "Optimal strategy for player one guarentees:", p1_opt os.system( "~/../../usr/prism-games/prism-games-2.0.beta3-linux64/bin/prism -cuddmaxmem 4g smg.prism smg_props.props -prop 5 -s > log.txt" ) p2_opt = find_prev_result() print "Optimal strategy for player two guarentees:", p2_opt return p1_opt, p2_opt
def flip_and_run(it, opponent): print opponent, "is opponent team in iteration:", str(it) best_pair = None best_score = 0.0 # build model for each opponent for i in range(len(possible_pairs)): pair_A = possible_pairs[i] pair_B = [possible_pairs[i][1]] + [possible_pairs[i][0]] pair_result_a = 0.0 pair_result_b = 0.0 # Calculate it forwards sys.stdout = open( "it" + str(it) + "vs" + possible_pairs[i][0] + possible_pairs[i][1] + ".prism", "w") if it % 2 == 1: matchup = possible_pairs[i] + opponent prefix.run(matchup, "mdp", False) # False as |I| = 1 free_strat.run(matchup, 1) sys.stdout = sys.__stdout__ os.system("cat adversarial_strategy_" + str(it - 1) + ".txt >> it" + str(it) + "vs" + possible_pairs[i][0] + possible_pairs[i][1] + ".prism") sys.stdout = open( "it" + str(it) + "vs" + possible_pairs[i][0] + possible_pairs[i][1] + ".prism", "a") else: matchup = opponent + possible_pairs[i] prefix.run(matchup, "mdp", False) sys.stdout = sys.__stdout__ os.system("cat adversarial_strategy_" + str(it - 1) + ".txt >> it" + str(it) + "vs" + possible_pairs[i][0] + possible_pairs[i][1] + ".prism") sys.stdout = open( "it" + str(it) + "vs" + possible_pairs[i][0] + possible_pairs[i][1] + ".prism", "a") free_strat.run(matchup, 2) suffix.run(matchup, False) # False as |I| = 1 sys.stdout = sys.__stdout__ os.system("prism -javamaxmem 100g -s it" + str(it) + "vs" + possible_pairs[i][0] + possible_pairs[i][1] + ".prism props.props -prop " + str(2 - it % 2) + " > log.txt") pair_result_a = find_prev_result() print "ProbAdv_" + str(2 - (it % 2)) + "(" + str( matchup) + ") = " + str(pair_result_a) # calculate it backwards sys.stdout = open( "it" + str(it) + "vs" + possible_pairs[i][1] + possible_pairs[i][0] + ".prism", "w") if it % 2 == 1: matchup = [possible_pairs[i][1]] + [possible_pairs[i][0] ] + opponent prefix.run(matchup, "mdp", False) # False as |I| = 1 free_strat.run(matchup, 1) sys.stdout = sys.__stdout__ os.system("cat adversarial_strategy_" + str(it - 1) + ".txt >> it" + str(it) + "vs" + possible_pairs[i][1] + possible_pairs[i][0] + ".prism") sys.stdout = open( "it" + str(it) + "vs" + possible_pairs[i][1] + possible_pairs[i][0] + ".prism", "a") else: matchup = opponent + [possible_pairs[i][1] ] + [possible_pairs[i][0]] prefix.run(matchup, "mdp", False) sys.stdout = sys.__stdout__ os.system("cat adversarial_strategy_" + str(it - 1) + ".txt >> it" + str(it) + "vs" + possible_pairs[i][1] + possible_pairs[i][0] + ".prism") sys.stdout = open( "it" + str(it) + "vs" + possible_pairs[i][1] + possible_pairs[i][0] + ".prism", "a") free_strat.run(matchup, 2) suffix.run(matchup, False) # False as |I| = 1 sys.stdout = sys.__stdout__ os.system("prism -javamaxmem 100g -s it" + str(it) + "vs" + possible_pairs[i][1] + possible_pairs[i][0] + ".prism props.props -prop " + str(2 - it % 2) + " > log.txt") pair_result_b = find_prev_result() print "ProbAdv_" + str(2 - (it % 2)) + "(" + str( matchup) + ") = " + str(pair_result_b) # forwards or backwards? pair_result = pair_result_a possible_pair = pair_A if pair_result_b < pair_result_a: pair_result = pair_result_b possible_pair = pair_B # find max if pair_result > best_score: best_score = pair_result best_pair = possible_pair print best_pair, "found as adversarial team, generating strategy..." # Write old_adv VS best_opp to file with multiple i in I for adversary calculation sys.stdout = open("it" + str(it) + "_adv.prism", "w") if it % 2 == 1: matchup = best_pair + opponent prefix.run(matchup, "mdp", True) # True as |I| > 1 free_strat.run(matchup, 1) sys.stdout = sys.__stdout__ os.system("cat adversarial_strategy_" + str(it - 1) + ".txt >> it" + str(it) + "_adv.prism") sys.stdout = open("it" + str(it) + "_adv.prism", "a") suffix.run(matchup, True) # True as |I| > 1 else: matchup = opponent + best_pair prefix.run(matchup, "mdp", True) sys.stdout = sys.__stdout__ os.system("cat adversarial_strategy_" + str(it - 1) + ".txt >> it" + str(it) + "_adv.prism") sys.stdout = open("it" + str(it) + "_adv.prism", "a") free_strat.run(matchup, 2) suffix.run(matchup, True) sys.stdout = sys.__stdout__ os.system("prism -s -javamaxmem 100g it" + str(it) + "_adv.prism props.props -prop " + str(2 - it % 2) + " -s -exportadvmdp tmp.tra -exportstates tmp.sta > log.txt") sys.stdout = open("adversarial_strategy_" + str(it) + ".txt", "w") # Write adversary to file (adversarial_strategy_it) educate_strat.run(matchup, "tmp", 2 - (it % 2)) sys.stdout = sys.__stdout__ return best_pair, best_score
print chosen_seed_team, "chosen as the seed, calculating adversaries..." # Find adversary of seed strategy: # generate file: seed_v_free for all opponents # calculate probAdv_2 and find_max() for greatest probAdv and 'best' opponent pair sys.stdout = open("random_seed.txt", "w") random_seed_strat.run(chosen_seed_team + ["K", "W"], 1, "none") sys.stdout = sys.__stdout__ for i in range(len(possible_pairs)): sys.stdout = open("seed" + str(i) + ".prism", "w") matchup = chosen_seed_team + possible_pairs[i] prefix.run(matchup, "mdp", False) sys.stdout = sys.__stdout__ os.system("cat random_seed.txt >> seed" + str(i) + ".prism") sys.stdout = open("seed" + str(i) + ".prism", "a") free_strat.run(matchup, 2) suffix.run(matchup, False) sys.stdout = sys.__stdout__ os.system("prism -s -javamaxmem 100g seed" + str(i) + ".prism props.props -prop 2 > log.txt") pair_result = find_prev_result() print "ProbAdv_2(" + str(matchup) + ") = " + str(pair_result) if pair_result > best_score: best_score = pair_result best_pair = possible_pairs[i] # Generate first adversary (adversarial_strategy_0.txt) # Create model with multiple i in I # write adversary to file using tmp files generated by PRISM calculation print best_pair, "found as adversarial team, generating strategy..." matchup = chosen_seed_team + best_pair
for i in range(len(f)-1,0,-1): if "Result:" in f[i]: return f[i].split("ult: ")[1].split(" (value")[0] config = sys.argv[1] output_destination = sys.argv[2] on_sand = sys.argv[3] pairs = ["KA","KW","AW"] # All material choices for a player for pair in pairs: # For each pair print("Pair: " + pair + " generating optimal strategy, this can take up to 5 minutes ...") characters = pair+pair # Generate the optimal strategy for a symmetric setup from the full initial set sys.stdout = open(output_destination + "/" + characters + "_smg_mul.prism", "w+") prefix.run(characters, 1, 1, config) free_strat.run(characters, 1) free_strat.run(characters, 2) suffix.run(characters, 1) sys.stdout = sys.__stdout__ if on_sand == "1": # Usually takes 2-3 seconds to build the model and 2.5 minutes to calculate the adversary # "on sand" os.system("/scratch/gethin/prism-william/prism/bin/prism " + output_destination + "/" + characters + "_smg_mul.prism \ properties/smg.props -prop 1 -exportstates " + output_destination + "/tmp.sta -exportadvmdp " + output_destination + "/tmp.tra -javamaxmem 400g \ > " + output_destination + "/log.txt") else: # "not on sand" os.system("prism " + output_destination + "/" + characters + "_smg_mul.prism properties/smg.props -prop 1 -javamaxmem 4g -exportstates " + output_destination + "/tmp.sta -exportadvmdp " + output_destination + "/tmp.tra > " + output_destination + "/log.txt") print("Strategy generated, calculating adversaries..") # Strategy is encoded in tmp.sta and tmp.tra files. sys.stdout = open(output_destination + "/" + pair + "_optimal_strategy.txt", "w+")