pythonnumpymultidimensional-arraygame-theorygambit

Generating all the possible values in a ndarray in numpy?


I am using gambit in python to simulate a world in a game theoretic manner. One construct of gambit is to save the "outcomes" for a set of decisions each player involved takes. This is of the form:

game[d1,d2,d3,...,dn][n] = payoff

where d1 is the index of decision made by player 1, d2 is the index of decision made by player 2, and so on and n is the index of the player for whom the payoff is being stored.

Now, there may be variable number of players, so the dimension of the index passed into the game object may change

how do I generate the series from [0,0,...,0] through [8,8,...,8] (where dimension = number of players = n) so that I can store them into [d1,d2,d3,...,dn]?


Solution

  • Take a look at python's itertools module. It sounds like the product function will do what you want.

    Example:

    import itertools as it
     list(it.product(*[range(2)]*3))
    

    Gives all lists of length three with two elements

    [(0, 0, 0),
    (0, 0, 1),
    (0, 1, 0),
    (0, 1, 1),
    (1, 0, 0),
    (1, 0, 1),
    (1, 1, 0),
    (1, 1, 1)]
    

    There's lots of other possibilities with itertools, in particular it also provides permutations and combinations.