Skip to content

Commit

Permalink
Game Theory all algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth Nahar committed May 3, 2020
0 parents commit e94cd29
Show file tree
Hide file tree
Showing 22 changed files with 863 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Problem1/Implementation_details.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Data Structures Used :

1. Utilities are stored in array of array's. Basically stored it
in a matrix of n-dimension.
2. Ordering of axis is same for all players. (s_1, s_2, s_3, .......)

------------------------------------------------------------------------------------------------

Algorithm Used :

-- All Algorithms are implemented generically.
Number of players and strategies can be anything.
-- s1, s2, s3 = Total numbers of strategies of Players

1. Strongly Dominant Strategy

a. For each s_(-i) get max s_i.(If multiple s_i are there for
same s_(-i) no startegy can be SDS.)
b. If all values of above matrix are same that means it is SDS.

Time Complexity :

Get max values : O(s1*s2*s3*......)
Repeat this for each player.

T = O(N*s1*s2*s3*.........)

2. Weakly Dominant Strategy

a. Use similar strategy as SDS. For each s_(-i) get max s_i. (If multiple s_i
Store it in vector.)
b. If all values contain in common only one value. Then WDS exists. This is of order(N)

Time complexity :

T = O(N*s1*s2*s3*.........)

3. SDSE : If all players have SDS then SDSE exists.
4. WDSE : If all players have WDS then WDSE exists.

5. Nash Equilibrium

a. For all S_(-i) get max vals and corresponding index.(max, s_i).
b. Create tuple(s_i, s_(-i)). For each player.
c. Check if all N matrices, have any tuple in common if yes then we have nash equilibrium.

Time Complexity :

a. First two steps will take O(N*s1*s2*s3....)
b. Third Step stores indices in sorted order so O(s_(-i))

T = O(N*s1*s2*s3.......)

6. Maxmin Values

a. Get minval for all s_(-i). That is tuples of form (s_i , min val) will be formed.
b. Get max out of this.

Time Complexity :

T = O(N*s1*s2*s3......)

6. Minmax Values

a. Get maxval for all s_i. That is tuples of form (s_(-i), max_val) will be formed.
b. Get min out of this.

Time Complexity :

T = O(N*s1*s2*s3........)
12 changes: 12 additions & 0 deletions Problem1/Test_Cases/strategic_info1.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file contains structure of number of players and their strategies
# It also contains utilities for all players indivisually.
[Strategies]
N = 2
Player1 = ['NC', 'C']
Player2 = ['NC', 'C']

[Utilities]
# Player utilities are in order [[(NC, NC), (NC,C)],[(C,NC),(C,C)]] for all.
# Ordering of strategies must be same as Defining strategies.
Player1 = [[-2, -10], [-1, -5]]
Player2 = [[-2, -10], [-1, -5]]
14 changes: 14 additions & 0 deletions Problem1/Test_Cases/strategic_info2.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file contains structure of number of players and their strategies
# It also contains utilities for all players indivisually.
[Strategies]
N = 3
Player1 = ['A1', 'A2']
Player2 = ['B1', 'B2']
Player3 = ['C1', 'C2']

[Utilities]
# Player utilities are in order of startegies product P1 : [(B1, c1), (B1, C2), (B2,C1), (B2, C2)]
# Ordering of strategies must be same as Defining strategies.
Player1 = [[1, 1, 1, -1], [2, 0, 0 ,0]]
Player2 = [[1, 1, 1, -1], [2, 0, 0, 0]]
Player3 = [[1, 1, 1, -1], [2, 0, 0, 0]]
12 changes: 12 additions & 0 deletions Problem1/Test_Cases/strategic_info3.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file contains structure of number of players and their strategies
# It also contains utilities for all players indivisually.
[Strategies]
N = 2
Player1 = ['A1', 'A2', 'A3', 'A4']
Player2 = ['B1', 'B2', 'B3']

[Utilities]
# Player utilities are in order of startegies product P1.
# Ordering of strategies must be same as Defining strategies.
Player1 = [[1, 2, 1], [2, 2, 2], [1, 1, 3], [1, 3, 2]]
Player2 = [[3, 3, 1, 2], [2, 3, 2, 1], [2, 1, 2, 3]]
180 changes: 180 additions & 0 deletions Problem1/analyse_equilibrium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
## This file contains calculate basic equilibriums
import numpy as np

## This Prints all dominant startegies for each player
def strongly_dominant_strategies(strategies, utilites) :

sds = list()
## Iterate for each player to calculate SDSE
for index in range(len(strategies)) :

# Get max index of si for all s_(-i)
max_index = np.argmax(utilites[index], axis=index)
max_val = np.max(max_index)
## If all index values are same then its a possible candidate
if np.all(max_index == max_val) :
## To check if s_i > s_i' for all s_(-i)
values = np.max(utilites[index], axis=index)
values = np.expand_dims(values, axis=index)
values = np.repeat(values, len(strategies[index]), axis=index)
sub_mat = values - utilites[index]
sub_mat = np.delete(sub_mat, max_val, axis=index)
if np.all(sub_mat > 0) :
sds.append(max_val)
else :
sds.append(-1)
else :
sds.append(-1)

# Enumerate SDS for all players
print("Strongly Dominant Startegies are : ")
isSDSE = True
for idx, index in enumerate(sds) :
if index == -1 :
print("Player", idx+1, " : No Dominant Startegy")
isSDSE = False
else :
print("Player", idx+1, " : ",strategies[idx][index], " is a Strongly Dominant Startegy")
print("------------------------------------------")
return sds, isSDSE

## Prints all Weakly dominant startegies
def weakly_dominant_startegies(strategies, utilites) :

wds = list()
## Iterate through indivisual strategy for wds
for index in range(len(strategies)) :
## As Weakly dominant startegies follows >= rule we need to get each and every
## index where max value occurs
max_vals = np.max(utilites[index], axis=index)
max_vals = np.expand_dims(max_vals, axis=index)
all_indices = np.argwhere(utilites[index] == max_vals)
## All indices n*Total_Players
axis_indices = np.take(all_indices, indices=index, axis=1)
idx, counts = np.unique(axis_indices, return_counts=True)
## Check if max val counts are equal to number of s_i
if np.max(counts) == np.prod(np.shape(max_vals)) :
## Check if there are no two such strategies
if len(np.argwhere(counts == np.max(counts))) > 1 :
wds.append(-1)
else :
wds.append(idx[np.argmax(counts)])
else :
wds.append(-1)

## Enumerate all WDS for all Players
print("Weakly Dominant Startegies are : ")
isWDSE = True
for idx, index in enumerate(wds) :
if index == -1 :
print("Player", idx+1, " : No Dominant Startegy")
isWDSE = False
else :
print("Player", idx+1, " : ",strategies[idx][index], " is a Weakly Dominant Startegy")
print("------------------------------------------")
return wds, isWDSE

## Prints SDSE and WDSE
def print_equilibrium(strategies, idx_list) :

equil_strats = "{"
for idx, index in enumerate(idx_list) :
equil_strats += strategies[idx][index]
equil_strats += ", "

equil_strats = equil_strats[:-2] + "}"
return equil_strats

## Print Nash Equilibriums
def nash_equilibrium(strategies, utilites) :

indices_max = list()
## Get max s_i for all S_-i for all i's. If that array intersect each other we have PSNE.
for index in range(len(strategies)) :
max_vals = np.max(utilites[index], axis=index)
max_vals = np.expand_dims(max_vals, axis=index)
all_indices = np.array(np.argwhere(utilites[index] == max_vals).tolist())
dtypes = 'int64, '*all_indices.shape[1]
all_indices = all_indices.view(dtypes[:-2])
indices_max.append(all_indices)

## Get intersection of arrays
intersect_f = indices_max[0]
for index in range(1, len(indices_max)) :
intersect_f = np.intersect1d(intersect_f, indices_max[index])

print("Nash Equilbriums are : ")
for index, row in enumerate(intersect_f) :
print(index+1, ". ", print_equilibrium(strategies, row))

if len(intersect_f) == 0 :
print("No nash equilibriums.............")

print("-----------------------------------------")

## Maxmin values and their Strategies
def maxmin(strategies, utilites) :

axis_list = list(range(len(strategies)))
for index in range(len(strategies)) :
# Get min across all axis and its max val
axis_tuple = tuple(axis_list[:index] + axis_list[index+1:])
min_vals = np.amin(utilites[index], axis=axis_tuple)
maxminval = np.max(min_vals)
maxminstrats = np.argwhere(min_vals == maxminval).flatten()
print("Maxmin Value for Player",index+1," : ", maxminval)
starts_list = "{"
for idx in maxminstrats :
starts_list += strategies[index][idx] + ","
starts_list = starts_list[:-1] + "}"
print("MaxMin Strategies for Player",index+1," are : ",starts_list)

print("-----------------------------------------")

## Minimax Value and Startegies of other player
def minmax(strategies, utilites) :

for index in range(len(strategies)) :
# Get max across s_i and min across rest of it.
max_vals = np.amax(utilites[index], axis=index)
minmaxval = np.min(max_vals)
minmaxstrats = np.argwhere(max_vals == minmaxval)
print("Minmax Value for Player",index+1," : ",minmaxval)
starts_list = "{"
for row in minmaxstrats:
indivisual_list="("
forloopCount = 0
for idx in range(len(strategies)) :
if idx == index :
continue
indivisual_list += strategies[idx][row[forloopCount]] + ","
forloopCount += 1
indivisual_list = indivisual_list[:-1] + ")"
starts_list += indivisual_list + ","

starts_list = starts_list[:-1] + "}"
print("MinMax Startegies against Player",index+1," are : ",starts_list)

print("-----------------------------------------")

def analyse(strategies, utilites) :

sds, isSDSE = strongly_dominant_strategies(strategies, utilites)
wds, isWDSE = weakly_dominant_startegies(strategies, utilites)
if isSDSE :
print("Strongly Dominant Strategy Equilibrium is : ", print_equilibrium(strategies, sds))
else :
print("No SDSE")

print("-----------------------------------------")

if isWDSE :
print("Weakly Dominant Strategy equilibrium is : ", print_equilibrium(strategies, wds))
else :
print("No WDSE")

print("-----------------------------------------")

nash_equilibrium(strategies, utilites)
maxmin(strategies, utilites)
minmax(strategies, utilites)
32 changes: 32 additions & 0 deletions Problem2/Implementation_details.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Data Structure Used :

1. As it's for two players, Utility is stored in matrix of two dimension.
2. Ordering of axis is same (s1, s2).

----------------------------------------------------------------------------------------------

Algorithm :

1. I have used Lo exist are Linear programming for solving msne.

Two conditions for MSNE to exist are :

1. u(s_i, sigma_(-i)*) = u(s_i' , sigma_(-i)*) for all s_i, s_i' in delta_(s1).
2. u(s_i, sigma_(-i)*) >= u(s_i' , sigma_(-i)*) for all s_i in delta_(s1), s_i' not in delta.

2. LP formulation for this is :

Max u(s_i, sigma_(-i)*).

such that satisfying above two conditions and sum(sigma_(-i)) = 1.


Clearly, utilising conditions for player1 will give sigma2 and for player2 will
give sigma1.If both converges then we have solution.

3. Repeat above process for each possible delta_Set.


Time Complexity :

T = (2^s1 - 1)*(2^s2 - 1)*LP_Solver_Time.
12 changes: 12 additions & 0 deletions Problem2/Test_Cases/strategic_info1.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file contains structure of number of players and their strategies
# It also contains utilities for all players indivisually.
[Strategies]
N = 2
Player1 = ['A', 'B']
Player2 = ['A', 'B']

[Utilities]
# Player utilities are in order [[(NC, NC), (NC,C)],[(C,NC),(C,C)]] for all.
# Ordering of strategies must be same as Defining strategies.
Player1 = [[10, 0], [0, 1]]
Player2 = [[10, 0], [0, 1]]
12 changes: 12 additions & 0 deletions Problem2/Test_Cases/strategic_info2.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file contains structure of number of players and their strategies
# It also contains utilities for all players indivisually.
[Strategies]
N = 2
Player1 = ['A', 'B']
Player2 = ['A', 'B']

[Utilities]
# Player utilities are in order [[(NC, NC), (NC,C)],[(C,NC),(C,C)]] for all.
# Ordering of strategies must be same as Defining strategies.
Player1 = [[2, 0], [0, 1]]
Player2 = [[1, 0], [0, 2]]
Loading

0 comments on commit e94cd29

Please sign in to comment.