I’ve implemented the dominant strategy and solved the problem of prisoners dilemma using it.

In this example, there’re two players A and B. If only one of them confesses while the other doesn’t, he or she is set free while the other has to serve ten yrs. If both of them use the right of silence, both of them will serve 6 months. If both of them confess, both of them have to serve two yrs.

In this implementaion, I’ve assume the one and the only one existence of equilibrium, and no duplicate values in the profit matrix.

This code is written in Ruby and released as GNU GPL. If I think this is fun, then I’ll keep improving it; like better interfaces, supporting 3 players, implementing repeated games or sub games.

Suggestions and comments are welcome.

class DominantStrategy

# profit_matrix : 2×2 profit matrix

# player B

# +———

# player A |

# |

def findStrategyForPlayerA(profit_matrix)

m = [ [[0,0], [0,0]],

[[0,0], [0,0]] ]

# Player A’s perspective

0.upto(1) do |column|

if profit_matrix[0][column][0] < profit_matrix[1][column][0]
best_row = 1
else # I assume that the profits are differents
best_row = 0
end
m[best_row][column][0] = 1
end
# Player B's perspective
0.upto(1) do |row|
if profit_matrix[row][0][1] < profit_matrix[row][1][1]
best_column = 1
else # I assume that the profits are differents
best_column = 0
end
m[row][best_column][1] = 1
end
# I assume exactly one equilibrium
0.upto(1) do |i|
0.upto(1) do |j|
if m[i][j] == [1, 1]
return i
end
end
end
return nil
end
end
if __FILE__ == $0
profit_matrix = [ [[-0.5, -0.5], [-10, 0]],
[[0, -10], [-2, -2]] ]
s = DominantStrategy.new
puts s.findStrategyForPlayerA(profit_matrix)
end
[/code]
Result:
[code lang="cpp"]
$ ruby dominant_strategy.rb
1
[/code]
So the player A must confess. (Ditto for B)
p.s. Hats off to John von Neumann.

## Post a Comment