# Carlos Pedro Gonçalves (2015) # Instituto Superior de Ciências Sociais e Políticas (ISCSP), University of Lisbon # cgoncalves@iscsp.ulisboa.pt # # Subject Matter: Lecture Materials on Artificial Intelligence for Decision Making # # Application of the game theory classes introduced in # # Game Theory classes (http://pythonfiddle.com/game-theory-classes/): # - Class player # - Class game # # The player class introduces an artificial agent that implements the decision # process of game theory based on Nash decision making process); # # The game class (defines the structure of the game and calculates the pure strategies Nash Equilibrium of a game). # # The current example applies the above classes to solve the three-person Prisoner Dilemma Game # (For the two-person example see (http://pythonfiddle.com/prisoner-dilemma/) # # For more details and lecture materials (in Portuguese) see: # https://sites.google.com/site/autonomouscomputingsystems/lectures-in-portuguese/decision-support-methods class player: def __init__(self,name,order,strategySpace,payoffs,choice,suboptimal,strategies,state,gameplay): self.name = name self.order = order self.strategySpace = strategySpace self.payoffs = payoffs self.choice = choice self.suboptimal = suboptimal self.strategies = strategies self.state = state self.gameplay = gameplay def processGame(self,G): for i in range(0,len(G)): X = G[i] if X[0] == self.name: for j in range(1,len(X)): Branch = X[j] Alternative = list(Branch) del Alternative[len(Alternative) - 1] self.strategySpace = self.strategySpace + [tuple(Alternative)] self.payoffs = self.payoffs + [Branch[len(Branch) - 1]] def evaluate(self): X = [] for i in range(0,len(self.strategySpace)): Alternative1 = self.strategySpace[i] for j in range(0,len(self.strategySpace)): Alternative2 = self.strategySpace[j] if Alternative1 != Alternative2: if len(Alternative1) == len(Alternative2): Compare = 0 for k in range(0,len(Alternative1) - 1): if Alternative1[k] == Alternative2[k]: Compare = Compare + 0 else: Compare = Compare + 1 if Compare == 0: PayoffCompare = [self.payoffs[i],self.payoffs[j]] M = max(PayoffCompare) if self.payoffs[i] == M: self.choice = Alternative1 X = X + [self.choice] else: self.suboptimal = self.suboptimal + [Alternative1] if self.payoffs[j] == M: self.choice = Alternative2 X = X + [self.choice] else: self.suboptimal = self.suboptimal + [Alternative2] X = set(X) self.suboptimal = set(self.suboptimal) self.strategies = list(X - self.suboptimal) print "\nStrategies selected by ", self.name,":" print self.strategies for l in range(0,len(self.strategies)): strategy = self.strategies[l] for m in range(0,len(strategy)): O = self.order[m] self.state[O] = strategy[m] self.gameplay = self.gameplay + [tuple(self.state)] class game: def __init__(self,players,structure,optimal): self.players = players self.structure = structure self.optimal = optimal def Nash(self,GP): Y = set(GP[0]) for i in range(0,len(GP)): X = set(GP[i]) Y = Y & X self.optimal = list(Y) if len(self.optimal) != 0: print "\nThe pure strategies Nash equilibria are:" for k in range(0,len(self.optimal)): print self.optimal[k] else: print "\nThis game has no pure strategies Nash equilibria!" #3 Person Prisoner Dilemma # This version of the prisoner dilemma is based on a cooperation game between three political # agents, such that each agent can either cooperate forming a partnership or defect, # in which case no partnership is formed. The payoffs are measured in units of political # gains by each agent. The political gains are being measured on a scale of 0 to 5. # # The payoffs are defined as follows: # - If all agents cooperate each one gets 3 unis of political gains # - If two agents cooperate and one of them defects, the one that defects gets 5 units of political gains # but that agent is punished by the other two and loses 1.5 units for each cooperative agent (total gain: # 5 - 2 * 1.5), the two agents that cooperate obtain a gain of 2 plus, as a result, from their joint political # action are able to assure an extra gain of 2 (each player receives 2 + 2 units) # - If two agents defects and one of them cooperates, the two that defect get 3.5 units of political gains # and the one that cooperates gets 1 unit of political gains # - If the three agents defect, each of them gets 1 unit of political gains #Game structure: #3 Person Prisoner's Dilemma #Game structure: GameA = ['A', # Game for player A ('C','C','C',3), ('C','C','D',5 - 2 * 1.5), ('C','D','C',2 + 2), ('C','D','D',3.5), ('D','C','C',2 + 2), ('D','C','D',3.5), ('D','D','C',1), ('D','D','D',1)] GameB = ['B', # Game for player B ('C','C','C',3), ('C','C','D',5 - 2 * 1.5), ('C','D','C',2 + 2), ('C','D','D',3.5), ('D','C','C',2 + 2), ('D','C','D',3.5), ('D','D','C',1), ('D','D','D',1)] GameC = ['C', # Game for player C ('C','C','C',3), ('C','C','D',5 - 2 * 1.5), ('C','D','C',2 + 2), ('C','D','D',3.5), ('D','C','C',2 + 2), ('D','C','D',3.5), ('D','D','C',1), ('D','D','D',1)] #game(players,structure,plays,optimal) Game = game(('A','B','C'),[GameA,GameB,GameC],None) #player(self,name,order,strategySpace,payoffs,choice,suboptimal,strategies,state,gameplay): PlayerA = player('A',(1,2,0), [], [], None, [], None, [0,0,0], []) PlayerB = player('B',(0,2,1), [], [], None, [], None, [0,0,0], []) PlayerC = player('C',(0,1,2), [], [], None, [], None, [0,0,0], []) Players = [PlayerA, PlayerB, PlayerC] for i in range(0,len(Players)): Players[i].processGame(Game.structure) Players[i].evaluate() GP = [] for i in range(0,len(Players)): X = Players[i].gameplay GP = GP + [X] Game.Nash(GP)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question