Skip to content

Instantly share code, notes, and snippets.

@WinstonCampeau
Last active August 28, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WinstonCampeau/f27d1d68a3898f5b1b8d3e1bc156f90a to your computer and use it in GitHub Desktop.
Save WinstonCampeau/f27d1d68a3898f5b1b8d3e1bc156f90a to your computer and use it in GitHub Desktop.
SageMath - Shapley Value of Tricycle Game
#Three traders F1, F2, and F3 each have a front wheel of a tricycle and three traders R1, R2, and R3 each have one rear wheel of a tricycle.
#To construct a tricycle, two rear wheels and one front wheel are required. The value of a coalition is the number of tricycles it can construct.
# E.g. v(FRR) = 1, v(RR) = 0 v(FR) = 0.
#Let A be the front wheels and B be the rear wheels
A={1,2,3}
B={4,5,6}
subset_dictionary = {}
#This code generated all the possible coalitions of rear and front wheels.
#Then it checks if there are at least two rear wheels and one front wheel.
#If yes, then a value of one is assigned to the coalition, else zero.
for subset_size in range(0,7):
for subset in itertools.combinations([1,2,3,4,5,6], subset_size):
if len(A.intersection(subset))>=1 and len(B.intersection(subset))>=2:
subset_dictionary.update({(subset):1})
else:
subset_dictionary.update({(subset):0})
integer_game = CooperativeGame(subset_dictionary)
integer_game.shapley_value()
##########################OUTPUT#################################
#subset_dictionary = {(3, 4, 6): 1, (2, 3, 5): 0, (1, 3): 0, (1, 2, 4, 6): 1, (2, 5, 6): 1,
(1, 2, 3, 4, 6): 1, (5, 6): 0, (1, 4, 5): 1, (1, 6): 0, (3, 4, 5, 6): 1,
(2, 5): 0, (4,): 0, (1, 3, 4, 5): 1, (1, 2, 3, 4, 5, 6): 1, (1, 3, 6):
0, (1, 2): 0, (2, 3, 5, 6): 1, (2, 3, 4, 5): 1, (1, 3, 5, 6): 1, (1,):
0, (1, 5): 0, (3,): 0, (1, 2, 5): 0, (1, 3, 4, 5, 6): 1, (2, 3, 6): 0,
(5,): 0, (2, 3, 4, 5, 6): 1, (1, 2, 3, 5, 6): 1, (2, 4, 5): 1, (1, 2, 3,
6): 0, (1, 2, 3, 5): 0, (2, 4, 5, 6): 1, (3, 5, 6): 1, (2, 3, 4, 6): 1,
(2, 6): 0, (3, 6): 0, (4, 5): 0, (1, 2, 3, 4, 5): 1, (2, 4, 6): 1, (1,
4): 0, (1, 2, 3): 0, (1, 2, 5, 6): 1, (2, 3): 0, (1, 2, 4): 0, (4, 5,
6): 0, (2, 3, 4): 0, (3, 5): 0, (1, 2, 6): 0, (4, 6): 0, (1, 2, 4, 5,
6): 1, (2,): 0, (1, 2, 3, 4): 0, (1, 5, 6): 1, (1, 3, 5): 0, (1, 3, 4,
6): 1, (): 0, (6,): 0, (1, 3, 4): 0, (3, 4, 5): 1, (1, 4, 6): 1, (1, 4,
5, 6): 1, (3, 4): 0, (2, 4): 0, (1, 2, 4, 5): 1}
#solution = {1: 1/15, 2: 1/15, 3: 1/15, 4: 4/15, 5: 4/15, 6: 4/15}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment