Advertisement
Guest User

Anticipating Responsibility for Plan Selection

a guest
Jul 22nd, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.55 KB | Source Code | 0 0
  1. #To run different tests, uncomment the appropriate line
  2. # of code at the very end.
  3.  
  4. from random import randint
  5. from random import choice
  6.  
  7. def randomsubset(inputlist):
  8.     outputlist = []
  9.     for i in inputlist:
  10.         selection = randint(0, 1)
  11.         if selection == 1:
  12.             outputlist.append(i)
  13.     return outputlist
  14.  
  15. def randomsatisfaction(valuelist,num_actions):
  16.     satisfied = []
  17.     for i in range(num_actions):
  18.         satisfied.append([])
  19.         for j in range(num_actions):
  20.             satisfaction = randomsubset(valuelist)
  21.             satisfied[-1].append(satisfaction)
  22.     return satisfied
  23.  
  24. def calc_utility(satisfied,valuebase):
  25.     total = 0
  26.     for i in satisfied:
  27.         total += valuebase[i]
  28.     return total
  29.  
  30. def calc_regret_action(agentnum,act,satisfaction,valuebase,num_actions):
  31.     worstregret = 1000
  32.     for i in range(num_actions):
  33.         if agentnum == 0:
  34.             regret = calc_regret_outcome(agentnum,act,i,satisfaction,valuebase,num_actions)
  35.         else:
  36.             regret = calc_regret_outcome(agentnum,i,act,satisfaction,valuebase,num_actions)
  37.         if regret < worstregret:
  38.             worstregret = regret
  39.     return worstregret
  40.    
  41. def calc_regret_outcome(agentnum,act1,act2,satisfaction,valuebase,num_actions):
  42.     utility = calc_utility(satisfaction[act1][act2],valuebase)
  43.     worstregret = 1000
  44.     for i in range(num_actions):
  45.         if agentnum == 0:
  46.             utility2 = calc_utility(satisfaction[i][act2],valuebase)
  47.         else:
  48.             utility2 = calc_utility(satisfaction[act1][i],valuebase)
  49.         regret = utility - utility2
  50.         if regret < worstregret:
  51.             worstregret = regret
  52.     return worstregret
  53.  
  54. def resp_attribution(agentnum,act1,act2,satisfaction,valuebase,num_actions):
  55.     positiveresp = []
  56.     negativeresp = []
  57.     satisfied_real = satisfaction[act1][act2]
  58.     for i in range(num_actions):
  59.         if agentnum == 0:
  60.             satisfied = satisfaction[i][act2]
  61.         else:
  62.             satisfied = satisfaction[act1][i]
  63.         for j in satisfied_real:
  64.             if j not in positiveresp and j not in satisfied:
  65.                 positiveresp.append(j)
  66.         for j in satisfied:
  67.             if j not in negativeresp and j not in satisfied_real:
  68.                 negativeresp.append(j)
  69.     return [positiveresp,negativeresp]
  70.  
  71. def calc_resp_regret(agentnum,act,satisfaction,valuebase,num_actions):
  72.     worstscore = 1000
  73.     for i in range(num_actions):
  74.         if agentnum == 0:
  75.             result = resp_attribution(agentnum,act,i,satisfaction,valuebase,num_actions)
  76.         else:
  77.             result = resp_attribution(agentnum,i,act,satisfaction,valuebase,num_actions)
  78.         postiveresp = result[0]
  79.         negativeresp = result[1]
  80.         utilityplus = calc_utility(postiveresp, valuebase)
  81.         utilityminus = calc_utility(negativeresp, valuebase)
  82.         utility = utilityplus - utilityminus
  83.         if utility < worstscore:
  84.             worstscore = utility
  85.     # print("agent "+str(agentnum))
  86.     # print("act "+str(act))
  87.     # print("worstscore "+str(worstscore))
  88.     # print("")
  89.     return worstscore
  90.        
  91.    
  92.    
  93. def optimist_selection1(satisfaction,valuebase,num_actions):
  94.     bestutility = -1000
  95.     bestaction = []
  96.     for i in range(num_actions):
  97.         bestutility2 = 0
  98.         for j in satisfaction[i]:
  99.             utility = calc_utility(j,valuebase)
  100.             if utility > bestutility2:
  101.                 bestutility2 = utility
  102.         if bestutility2 > bestutility:
  103.             bestaction = [i]
  104.             bestutility = bestutility2
  105.         elif bestutility2 == bestutility:
  106.             bestaction.append(i)
  107.     return bestaction
  108.  
  109. def optimist_selection2(satisfaction,valuebase,num_actions):
  110.     bestutility = -1000
  111.     bestaction = []
  112.     for i in range(num_actions):
  113.         bestutility2 = 0
  114.         for j in range(num_actions):
  115.             satisfied = satisfaction[j][i]
  116.             utility = calc_utility(satisfied,valuebase)
  117.             if utility > bestutility2:
  118.                 bestutility2 = utility
  119.         if bestutility2 > bestutility:
  120.             bestaction = [i]
  121.             bestutility = bestutility2
  122.         elif bestutility2 == bestutility:
  123.             bestaction.append(i)
  124.     return bestaction
  125.  
  126. def pessimist_selection1(satisfaction,valuebase,num_actions):
  127.     bestutility = -1000
  128.     bestaction = []
  129.     for i in range(num_actions):
  130.         worstutility = 1000
  131.         for j in range(num_actions):
  132.             satisfied = satisfaction[i][j]
  133.             utility = calc_utility(satisfied,valuebase)
  134.             if utility < worstutility:
  135.                 worstutility = utility
  136.         if worstutility > bestutility:
  137.             bestaction = [i]
  138.             bestutility = worstutility
  139.         elif worstutility == bestutility:
  140.             bestaction.append(i)
  141.     return bestaction
  142.  
  143. def pessimist_selection2(satisfaction,valuebase,num_actions):
  144.     bestutility = -1000
  145.     bestaction = []
  146.     for i in range(num_actions):
  147.         worstutility = 1000
  148.         for j in range(num_actions):
  149.             satisfied = satisfaction[j][i]
  150.             utility = calc_utility(satisfied,valuebase)
  151.             if utility < worstutility:
  152.                 worstutility = utility
  153.         if worstutility > bestutility:
  154.             bestaction = [i]
  155.             bestutility = worstutility
  156.         elif worstutility == bestutility:
  157.             bestaction.append(i)
  158.     return bestaction
  159.  
  160. def regret_selection(agentnum,satisfaction,valuebase,num_actions):
  161.     bestregret = -1000
  162.     bestaction = []
  163.     for i in range(num_actions):
  164.         regret = calc_regret_action(agentnum, i, satisfaction, valuebase, num_actions)
  165.         if regret > bestregret:
  166.             bestaction = [i]
  167.             bestregret = regret
  168.         elif regret == bestregret:
  169.                 bestaction.append(i)
  170.     return bestaction
  171.  
  172. def resp_selection(agentnum,satisfaction,valuebase,num_actions):
  173.     bestscore = -1000
  174.     bestaction = []
  175.     for i in range(num_actions):
  176.         score = calc_resp_regret(agentnum,i,satisfaction,valuebase,num_actions)
  177.         if score > bestscore:
  178.             bestaction = [i]
  179.             bestscore = score
  180.         elif score == bestscore:
  181.             bestaction.append(i)
  182.     return bestaction
  183.  
  184. def joint_selection(satisfaction,valuebase1,valuebase2,num_actions):
  185.     bestactions = []
  186.     bestscore = -1000
  187.     for i in range(num_actions):
  188.         for j in range(num_actions):
  189.             satisfied = satisfaction[i][j]
  190.             score1 = calc_utility(satisfied, valuebase1)
  191.             score2 = calc_utility(satisfied, valuebase2)
  192.             score = min(score1,score2)
  193.             if score > bestscore:
  194.                 bestscore = score
  195.                 bestactions = [[i,j]]
  196.             elif score == bestscore:
  197.                 bestactions.append([i,j])
  198.     return bestactions
  199.                
  200.  
  201. def multitest(type1,type2,values,valuebase1,valuebase2,num_actions,num_iterations):
  202.     total_utility1 = 0
  203.     total_utility2 = 0
  204.     for i in range(num_iterations):
  205.         satisfaction = randomsatisfaction(values, num_actions)
  206.         if type1 == "opt":
  207.             choice1 = optimist_selection1(satisfaction,valuebase1,num_actions)
  208.         elif type1 == "pess":
  209.             choice1 = pessimist_selection1(satisfaction,valuebase1,num_actions)
  210.         elif type1 == "reg":
  211.             choice1 = regret_selection(0,satisfaction,valuebase1,num_actions)
  212.         elif type1 == "resp":
  213.             choice1 = resp_selection(0,satisfaction,valuebase1,num_actions)
  214.         else:
  215.             choice1 = range(num_actions)
  216.         if type2 == "opt":
  217.             choice2 = optimist_selection2(satisfaction,valuebase2,num_actions)
  218.         elif type2 == "pess":
  219.             choice2 = pessimist_selection2(satisfaction,valuebase2,num_actions)
  220.         elif type2 == "reg":
  221.             choice2 = regret_selection(1,satisfaction,valuebase2,num_actions)
  222.         elif type2 == "resp":
  223.             choice2 = resp_selection(1,satisfaction,valuebase2,num_actions)
  224.         else:
  225.             choice2 = range(num_actions)
  226.         choice1 = choice(choice1)
  227.         choice2 = choice(choice2)
  228.         utility1 = calc_utility(satisfaction[choice1][choice2],valuebase1)
  229.         utility2 = calc_utility(satisfaction[choice1][choice2],valuebase2)
  230.         total_utility1 += utility1
  231.         total_utility2 += utility2
  232.     return (total_utility1/num_iterations, total_utility2/num_iterations)
  233.              
  234.  
  235. def singletest(values,valuebase,num_actions):
  236.  #   satisfaction = randomsatisfaction(values, num_actions)
  237.     satisfaction = [[['a'],['c']],[['c'],['c','d']]]
  238.     for i in satisfaction:
  239.         print(i)
  240.     print("")  
  241.     opt1 = optimist_selection1(satisfaction,valuebase,num_actions)
  242.     print("The optimistic action for agent 1 is "+str(opt1))
  243.     opt2 = optimist_selection2(satisfaction,valuebase,num_actions)
  244.     print("The optimistic action for agent 2 is "+str(opt2))
  245.     print("")
  246.     pess1 = pessimist_selection1(satisfaction,valuebase,num_actions)
  247.     print("The pessimistic action for agent 1 is "+str(pess1))
  248.     pess2 = pessimist_selection2(satisfaction,valuebase,num_actions)
  249.     print("The pessimistic action for agent 2 is "+str(pess2))
  250.     print("")
  251.     reg1 = regret_selection(0,satisfaction,valuebase,num_actions)
  252.     print("The consistent-regret-minimising action for agent 1 is "+str(reg1))
  253.     reg2 = regret_selection(1,satisfaction,valuebase,num_actions)
  254.     print("The consistent-regret-minimising action for agent 2 is "+str(reg2))
  255.     print("")
  256.     resp1 = resp_selection(0,satisfaction,valuebase,num_actions)
  257.     print("The regret-minimising action for agent 1 is "+str(resp1))
  258.     resp2 = resp_selection(1,satisfaction,valuebase,num_actions)
  259.     print("The regret-minimising action for agent 2 is "+str(resp2))
  260.     print("")
  261.     opt1 = choice(opt1)
  262.     opt2 = choice(opt2)
  263.     pess1 = choice(pess1)
  264.     pess2 = choice(pess2)
  265.     reg1 = choice(reg1)
  266.     reg2 = choice(reg2)
  267.     resp1 = choice(resp1)
  268.     resp2 = choice(resp2)
  269.     utility1 = calc_utility(satisfaction[opt1][opt2],valuebase)
  270.     utility2 = calc_utility(satisfaction[pess1][pess2],valuebase)
  271.     utility3 = calc_utility(satisfaction[reg1][reg2],valuebase)
  272.     utility4 = calc_utility(satisfaction[resp1][resp2],valuebase)
  273.     print("The optimistic utility is "+str(utility1))
  274.     print("The pessimistic utility is "+str(utility2))
  275.     print("The regret-minimising utility is "+str(utility3))
  276.     print("The responsibility-minimising utility is "+str(utility4))
  277.    
  278.    
  279.    
  280. def fulltest(values,valuebase1,valuebase2,num_actions,num_iterations):
  281.     print("Valuebase 1: "+str(valuebase1))
  282.     print("Valuebase 2: "+str(valuebase2))
  283.     for type1 in ["opt","pess","reg","resp","rand"]:
  284.         for type2 in ["opt","pess","reg","resp","rand"]:
  285.             avg_utility = multitest(type1,type2,values,valuebase1,valuebase2,num_actions,num_iterations)
  286.             avg_utility1 = round(avg_utility[0],1)
  287.             avg_utility2 = round(avg_utility[1],1)
  288.             print ("Agent 1: "+str(type1)+" Agent 2: "+str(type2)+" Utility 1: "+str(avg_utility1)+" 2: "+str(avg_utility2)+" Total: "+str(round(avg_utility2+avg_utility1,1)))
  289.         print("")
  290.  
  291. def fulltest_joint(values,valuebase1,valuebase2,num_actions,num_iterations):
  292.     print("Valuebase 1: "+str(valuebase1))
  293.     print("Valuebase 2: "+str(valuebase2))
  294.     total_utility1 = 0
  295.     total_utility2 = 0
  296.     for i in range(num_iterations):
  297.         satisfaction = randomsatisfaction(values, num_actions)
  298.         acts = joint_selection(satisfaction, valuebase1, valuebase2, num_actions)
  299.         acts = choice(acts)
  300.         choice1 = acts[0]
  301.         choice2 = acts[1]
  302.         utility1 = calc_utility(satisfaction[choice1][choice2],valuebase1)
  303.         utility2 = calc_utility(satisfaction[choice1][choice2],valuebase2)
  304.         total_utility1 += utility1
  305.         total_utility2 += utility2
  306.         avg_utility1 = total_utility1/num_iterations
  307.         avg_utility2 = total_utility2/num_iterations
  308.         avg_utility1 = round(avg_utility1,1)
  309.         avg_utility2 = round(avg_utility2,1)
  310.     print ("Joint Planning Utility 1: "+str(avg_utility1)+" 2: "+str(avg_utility2)+" Total: "+str(round(avg_utility2+avg_utility1,1)))
  311.    
  312.    
  313. values = ["a","b","c","d"]
  314. valuebase1 = {'a':40,'b':10,'c':0,'d':-50}
  315. valuebase2 = {'a':40,'b':10,'c':0,'d':-50}
  316. num_actions = 4
  317. num_iterations = 100000
  318.            
  319. #fulltest(values,valuebase1,valuebase2,num_actions,num_iterations)
  320. #singletest(values,valuebase1,num_actions)
  321. fulltest_joint(values, valuebase1, valuebase2, num_actions, num_iterations)
  322.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement