# 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!" # New Entrant vs Market Leader game # # Game Description: # A company whishes to invest in a new business line complementary to its # core business. The industry for this business line has low barriers to entry # but there is a market leader that can retaliate. # # Evaluated a scale of 0 to 5 the new business line has a base strategic value of 4, # for the company, since the from a financial standpoint the Net Present Value # of the investment is positive, and the business line has synergies with the # company's core business, helping to consolidate the company's position in its # own core business. The company operates on a highly competitive industry and the # new business line would lead to a vertical integration with a greater control # for the company's value chain. # # However, the strategic value will only be realized if favorable business conditions # are met, which depend upon the market leader's behavior if faced with a new entrant, # and the company's behavior with respect to the market leader. Namely, there are two ways # for the company to enter into the new market: # # - As a competitor (simple entry); # - Offering the leader a partnership agreement with respect to the company's core business # # # A simple entry leads to the following scenarios: # # - If the market leader chooses to fight when faced with a new entrant, the company # will be faced with a highly unfavorable strategic environment that would need a great # amount of resources to be diverted to support a competitive effort in order to keep the # cash flows high enough for the project to be financially viable, including synergies. # Furthermore the company would have to compete with the market leader for a position that # would be difficult to sustain. In this case the penalty on a scale of 0 to 5 is evaluated in 5, # which means that the strategic advantage would be lost with a negative impact of 1 on the core # business (due to the contamination effects of poor business performance and value chain linkages), # thus, in this case, the payoff for the company is 4 - 5. The effort involved in the fight also # leads to some loss for the market leader, since the leader, in this case, loses margin # and may show a fall in the company's results, the strategic penalty for the leader is 2, # so that for a simple entry the leader gets 5 - 2 of payoff. # # - If the leader does not fight, for a simple entry, then, the company (new entrant) gets the # strategic value of 4, but the leader is faced with a new entrant with some competitive ability # linked to synergies between its core business and its new business line, the loss for the # leader, in this case, is 3 (so that the leader gets 5 - 3. # # An entry offering partnership between the company and the market leader, if the # partnership is accepted by the market leader, consolidates the company's strategic advantage # with respect to its own core business due to the synergies which means that the company gets # an extra value of 1, obtaining a strategic value of 5. The leader loses a strategic value of 1 # with respect to the maximum 5, due to the fact that it has a new competitor, but the partnership # does not make its loss as high as the simple entry, so that the leader gets a payoff of 5 - 3. # # If the leader does not accept the partnership offer and fights, then the company loses a strategic advantage # with an extra penalty point of 1, due to the partnership negotiation efforts, which leads to # a loss of one extra point of 1 with respect to the simple entry and fight scenario, leading to a strategic # value of 4 - 6. The market leader still gets an advantage in terms of timing which leads to a strategic value # of 5 - 1.5 (higher than fighting in the simple entry scenario); # # If the leader does not accept the partnership and does not fight, the leader gets the same payoff as in # the simple entry do not fight alternative, but the new entrant gets a penalty of 1 due to the effort spent # on negotiating the partnerhsip. # # If the company does not enter, its payoff is 0. #Game structure: GameNewEntrant = ['New Entrant', ('Fight','Simple entry',4 - 5), ('Fight','Do not enter',0), ('Do not fight','Simple entry',4), ('Do not fight','Do not enter',0), ('Accept partnership','Enter offering partnership', 4 + 1), ('Accept partnership','Do not enter',0), ('Do not accept partnership and fight','Enter offering partnership', 4 - 6), ('Do not accept partnership and fight','Do not enter',0), ('Do not accept partnership and do not fight','Enter offering partnership', 4 - 1), ('Do not accept partnership and do not fight','Do not enter',0) ] GameLeader = ['Market Leader', ('Simple entry','Fight',5 - 2), ('Simple entry','Do not fight',5 - 3), ('Enter offering partnership','Accept partnership',5 - 1), ('Enter offering partnership','Do not accept partnership and fight',4 - 1.5), ('Enter offering partnership','Do not accept partnership and do not fight',5 - 3), ('Do not enter','No response',5)] #game(players,structure,plays,optimal) Game = game(('New Entrant','Market Leader'),[GameNewEntrant,GameLeader],None) #player(name,order,strategySpace,payoffs,choice,suboptimal,strategies,state,gameplay): NewEntrant = player('New Entrant',(1,0), [], [], None, [], None, [0,0], []) MarketLeader = player('Market Leader',(0,1), [], [], None, [], None, [0,0], []) Players = [NewEntrant, MarketLeader] 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