-1

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]?

  • Not very clear what you need. Do you want to, individually, generate all combinations (permutations?) between [0,0,0,0,...] and [n,n,n,n,...], each with a length of `n`? Or do you just need an array of shape (n,n,n) to sore stuff in? Can you show us a minimal example of the desired for two players? Is there a dimension for number of possible decisions? – wwii Oct 27 '17 at 18:13
  • @wwii I want to generate all permutations between [0,0,0,...,0] and [8,8,8,...,8] where the dimension (length?) of the indices is n. I have to do this based on some value of n dynamically. – Shashank Gargeshwari Oct 27 '17 at 18:29
  • 1
    How big is `n` likely to be? Unless `n` is quite small, the total number of combinations will chew up a lot of RAM. BTW, that kind of combination is called the cartesian product. In plain Python you'd generate it with `itertools.product`. There are various recipes for making cartesian products in Numpy. – PM 2Ring Oct 27 '17 at 18:37

1 Answers1

0

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.

user2699
  • 2,927
  • 14
  • 31