# 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 two-person Prisoner Dilemma Game # # 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!" #2 Person Prisoner Dilemma # This version of the prisoner dilemma is based on a cooperation game between two 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. #Game structure: GameA = ['A', # Player A ('C','C',3), # When B cooperates: if A cooperates, A receives 3 units of political gains ('C','D',5), # When B cooperates: if A defects, A receives 5 units of political gains ('D','C',0), # When B defects: if A cooperates, A receives 0 units of political gains ('D','D',1)] # When B defects: if A defects, A receives 1 unit of political gains GameB = ['B', # Player B ('C','C',3), # When A cooperates: if B cooperates, B receives 3 units of political gains ('C','D',5), # When A cooperates: if B defects, B receives 5 units of political gains ('D','C',0), # When A defects: if B cooperates, B receives 0 units of political gains ('D','D',1)] # When A defects: if B defects, B receives 1 unit of political gains #game(players,structure,plays,optimal) Game = game(('A','B'),[GameA,GameB],None) #player(self,name,order,strategySpace,payoffs,choice,suboptimal,strategies,state,gameplay): PlayerA = player('A',(1,0), [], [], None, [], None, [0,0], []) PlayerB = player('B',(0,1), [], [], None, [], None, [0,0], []) Players = [PlayerA, PlayerB] 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