Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #To run different tests, uncomment the appropriate line
- # of code at the very end.
- from random import randint
- from random import choice
- def randomsubset(inputlist):
- outputlist = []
- for i in inputlist:
- selection = randint(0, 1)
- if selection == 1:
- outputlist.append(i)
- return outputlist
- def randomsatisfaction(valuelist,num_actions):
- satisfied = []
- for i in range(num_actions):
- satisfied.append([])
- for j in range(num_actions):
- satisfaction = randomsubset(valuelist)
- satisfied[-1].append(satisfaction)
- return satisfied
- def calc_utility(satisfied,valuebase):
- total = 0
- for i in satisfied:
- total += valuebase[i]
- return total
- def calc_regret_action(agentnum,act,satisfaction,valuebase,num_actions):
- worstregret = 1000
- for i in range(num_actions):
- if agentnum == 0:
- regret = calc_regret_outcome(agentnum,act,i,satisfaction,valuebase,num_actions)
- else:
- regret = calc_regret_outcome(agentnum,i,act,satisfaction,valuebase,num_actions)
- if regret < worstregret:
- worstregret = regret
- return worstregret
- def calc_regret_outcome(agentnum,act1,act2,satisfaction,valuebase,num_actions):
- utility = calc_utility(satisfaction[act1][act2],valuebase)
- worstregret = 1000
- for i in range(num_actions):
- if agentnum == 0:
- utility2 = calc_utility(satisfaction[i][act2],valuebase)
- else:
- utility2 = calc_utility(satisfaction[act1][i],valuebase)
- regret = utility - utility2
- if regret < worstregret:
- worstregret = regret
- return worstregret
- def resp_attribution(agentnum,act1,act2,satisfaction,valuebase,num_actions):
- positiveresp = []
- negativeresp = []
- satisfied_real = satisfaction[act1][act2]
- for i in range(num_actions):
- if agentnum == 0:
- satisfied = satisfaction[i][act2]
- else:
- satisfied = satisfaction[act1][i]
- for j in satisfied_real:
- if j not in positiveresp and j not in satisfied:
- positiveresp.append(j)
- for j in satisfied:
- if j not in negativeresp and j not in satisfied_real:
- negativeresp.append(j)
- return [positiveresp,negativeresp]
- def calc_resp_regret(agentnum,act,satisfaction,valuebase,num_actions):
- worstscore = 1000
- for i in range(num_actions):
- if agentnum == 0:
- result = resp_attribution(agentnum,act,i,satisfaction,valuebase,num_actions)
- else:
- result = resp_attribution(agentnum,i,act,satisfaction,valuebase,num_actions)
- postiveresp = result[0]
- negativeresp = result[1]
- utilityplus = calc_utility(postiveresp, valuebase)
- utilityminus = calc_utility(negativeresp, valuebase)
- utility = utilityplus - utilityminus
- if utility < worstscore:
- worstscore = utility
- # print("agent "+str(agentnum))
- # print("act "+str(act))
- # print("worstscore "+str(worstscore))
- # print("")
- return worstscore
- def optimist_selection1(satisfaction,valuebase,num_actions):
- bestutility = -1000
- bestaction = []
- for i in range(num_actions):
- bestutility2 = 0
- for j in satisfaction[i]:
- utility = calc_utility(j,valuebase)
- if utility > bestutility2:
- bestutility2 = utility
- if bestutility2 > bestutility:
- bestaction = [i]
- bestutility = bestutility2
- elif bestutility2 == bestutility:
- bestaction.append(i)
- return bestaction
- def optimist_selection2(satisfaction,valuebase,num_actions):
- bestutility = -1000
- bestaction = []
- for i in range(num_actions):
- bestutility2 = 0
- for j in range(num_actions):
- satisfied = satisfaction[j][i]
- utility = calc_utility(satisfied,valuebase)
- if utility > bestutility2:
- bestutility2 = utility
- if bestutility2 > bestutility:
- bestaction = [i]
- bestutility = bestutility2
- elif bestutility2 == bestutility:
- bestaction.append(i)
- return bestaction
- def pessimist_selection1(satisfaction,valuebase,num_actions):
- bestutility = -1000
- bestaction = []
- for i in range(num_actions):
- worstutility = 1000
- for j in range(num_actions):
- satisfied = satisfaction[i][j]
- utility = calc_utility(satisfied,valuebase)
- if utility < worstutility:
- worstutility = utility
- if worstutility > bestutility:
- bestaction = [i]
- bestutility = worstutility
- elif worstutility == bestutility:
- bestaction.append(i)
- return bestaction
- def pessimist_selection2(satisfaction,valuebase,num_actions):
- bestutility = -1000
- bestaction = []
- for i in range(num_actions):
- worstutility = 1000
- for j in range(num_actions):
- satisfied = satisfaction[j][i]
- utility = calc_utility(satisfied,valuebase)
- if utility < worstutility:
- worstutility = utility
- if worstutility > bestutility:
- bestaction = [i]
- bestutility = worstutility
- elif worstutility == bestutility:
- bestaction.append(i)
- return bestaction
- def regret_selection(agentnum,satisfaction,valuebase,num_actions):
- bestregret = -1000
- bestaction = []
- for i in range(num_actions):
- regret = calc_regret_action(agentnum, i, satisfaction, valuebase, num_actions)
- if regret > bestregret:
- bestaction = [i]
- bestregret = regret
- elif regret == bestregret:
- bestaction.append(i)
- return bestaction
- def resp_selection(agentnum,satisfaction,valuebase,num_actions):
- bestscore = -1000
- bestaction = []
- for i in range(num_actions):
- score = calc_resp_regret(agentnum,i,satisfaction,valuebase,num_actions)
- if score > bestscore:
- bestaction = [i]
- bestscore = score
- elif score == bestscore:
- bestaction.append(i)
- return bestaction
- def joint_selection(satisfaction,valuebase1,valuebase2,num_actions):
- bestactions = []
- bestscore = -1000
- for i in range(num_actions):
- for j in range(num_actions):
- satisfied = satisfaction[i][j]
- score1 = calc_utility(satisfied, valuebase1)
- score2 = calc_utility(satisfied, valuebase2)
- score = min(score1,score2)
- if score > bestscore:
- bestscore = score
- bestactions = [[i,j]]
- elif score == bestscore:
- bestactions.append([i,j])
- return bestactions
- def multitest(type1,type2,values,valuebase1,valuebase2,num_actions,num_iterations):
- total_utility1 = 0
- total_utility2 = 0
- for i in range(num_iterations):
- satisfaction = randomsatisfaction(values, num_actions)
- if type1 == "opt":
- choice1 = optimist_selection1(satisfaction,valuebase1,num_actions)
- elif type1 == "pess":
- choice1 = pessimist_selection1(satisfaction,valuebase1,num_actions)
- elif type1 == "reg":
- choice1 = regret_selection(0,satisfaction,valuebase1,num_actions)
- elif type1 == "resp":
- choice1 = resp_selection(0,satisfaction,valuebase1,num_actions)
- else:
- choice1 = range(num_actions)
- if type2 == "opt":
- choice2 = optimist_selection2(satisfaction,valuebase2,num_actions)
- elif type2 == "pess":
- choice2 = pessimist_selection2(satisfaction,valuebase2,num_actions)
- elif type2 == "reg":
- choice2 = regret_selection(1,satisfaction,valuebase2,num_actions)
- elif type2 == "resp":
- choice2 = resp_selection(1,satisfaction,valuebase2,num_actions)
- else:
- choice2 = range(num_actions)
- choice1 = choice(choice1)
- choice2 = choice(choice2)
- utility1 = calc_utility(satisfaction[choice1][choice2],valuebase1)
- utility2 = calc_utility(satisfaction[choice1][choice2],valuebase2)
- total_utility1 += utility1
- total_utility2 += utility2
- return (total_utility1/num_iterations, total_utility2/num_iterations)
- def singletest(values,valuebase,num_actions):
- # satisfaction = randomsatisfaction(values, num_actions)
- satisfaction = [[['a'],['c']],[['c'],['c','d']]]
- for i in satisfaction:
- print(i)
- print("")
- opt1 = optimist_selection1(satisfaction,valuebase,num_actions)
- print("The optimistic action for agent 1 is "+str(opt1))
- opt2 = optimist_selection2(satisfaction,valuebase,num_actions)
- print("The optimistic action for agent 2 is "+str(opt2))
- print("")
- pess1 = pessimist_selection1(satisfaction,valuebase,num_actions)
- print("The pessimistic action for agent 1 is "+str(pess1))
- pess2 = pessimist_selection2(satisfaction,valuebase,num_actions)
- print("The pessimistic action for agent 2 is "+str(pess2))
- print("")
- reg1 = regret_selection(0,satisfaction,valuebase,num_actions)
- print("The consistent-regret-minimising action for agent 1 is "+str(reg1))
- reg2 = regret_selection(1,satisfaction,valuebase,num_actions)
- print("The consistent-regret-minimising action for agent 2 is "+str(reg2))
- print("")
- resp1 = resp_selection(0,satisfaction,valuebase,num_actions)
- print("The regret-minimising action for agent 1 is "+str(resp1))
- resp2 = resp_selection(1,satisfaction,valuebase,num_actions)
- print("The regret-minimising action for agent 2 is "+str(resp2))
- print("")
- opt1 = choice(opt1)
- opt2 = choice(opt2)
- pess1 = choice(pess1)
- pess2 = choice(pess2)
- reg1 = choice(reg1)
- reg2 = choice(reg2)
- resp1 = choice(resp1)
- resp2 = choice(resp2)
- utility1 = calc_utility(satisfaction[opt1][opt2],valuebase)
- utility2 = calc_utility(satisfaction[pess1][pess2],valuebase)
- utility3 = calc_utility(satisfaction[reg1][reg2],valuebase)
- utility4 = calc_utility(satisfaction[resp1][resp2],valuebase)
- print("The optimistic utility is "+str(utility1))
- print("The pessimistic utility is "+str(utility2))
- print("The regret-minimising utility is "+str(utility3))
- print("The responsibility-minimising utility is "+str(utility4))
- def fulltest(values,valuebase1,valuebase2,num_actions,num_iterations):
- print("Valuebase 1: "+str(valuebase1))
- print("Valuebase 2: "+str(valuebase2))
- for type1 in ["opt","pess","reg","resp","rand"]:
- for type2 in ["opt","pess","reg","resp","rand"]:
- avg_utility = multitest(type1,type2,values,valuebase1,valuebase2,num_actions,num_iterations)
- avg_utility1 = round(avg_utility[0],1)
- avg_utility2 = round(avg_utility[1],1)
- 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)))
- print("")
- def fulltest_joint(values,valuebase1,valuebase2,num_actions,num_iterations):
- print("Valuebase 1: "+str(valuebase1))
- print("Valuebase 2: "+str(valuebase2))
- total_utility1 = 0
- total_utility2 = 0
- for i in range(num_iterations):
- satisfaction = randomsatisfaction(values, num_actions)
- acts = joint_selection(satisfaction, valuebase1, valuebase2, num_actions)
- acts = choice(acts)
- choice1 = acts[0]
- choice2 = acts[1]
- utility1 = calc_utility(satisfaction[choice1][choice2],valuebase1)
- utility2 = calc_utility(satisfaction[choice1][choice2],valuebase2)
- total_utility1 += utility1
- total_utility2 += utility2
- avg_utility1 = total_utility1/num_iterations
- avg_utility2 = total_utility2/num_iterations
- avg_utility1 = round(avg_utility1,1)
- avg_utility2 = round(avg_utility2,1)
- print ("Joint Planning Utility 1: "+str(avg_utility1)+" 2: "+str(avg_utility2)+" Total: "+str(round(avg_utility2+avg_utility1,1)))
- values = ["a","b","c","d"]
- valuebase1 = {'a':40,'b':10,'c':0,'d':-50}
- valuebase2 = {'a':40,'b':10,'c':0,'d':-50}
- num_actions = 4
- num_iterations = 100000
- #fulltest(values,valuebase1,valuebase2,num_actions,num_iterations)
- #singletest(values,valuebase1,num_actions)
- fulltest_joint(values, valuebase1, valuebase2, num_actions, num_iterations)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement