Iterating generator with equilibria seems very slow
Closed this issue · 2 comments
Hi,
I am using both Support and Vertex Enumeration to compute the equilibria of a zero-sum game. This is done as follows (U the payoff matrix):
import nashpy
game = nashpy.Game(U)
equilibria = game.support_enumeration()
for i, eq in enumerate(equilibria):
print('Solution ', i)
print('Row player strategy: ', eq[0])
print('Column player strategy: ', eq[1])
The above code seems to work fine when each player has a small number of strategies available (e.g 2-5). However, when I try to solve slightly larger games (e.g 10 strategies) iterating over the results for Support Enumeration becomes extremely slow. This also applies to Vertex Enumeration when the problem size increases a bit more (e.g. 50-100 strategies).
Is this normal/expected? If so is there any workaround this?
That's expected behaviour. The python generator construct is wonderful:
equilibria = game.support_enumeration()
that line creates the generator which just initiates all the rules that are required, it does not actually step through the algorithm yet.
The iteration then does indeed step through it. So that is "slow" because it's actually where the algorithm is taking place.
This is referred to as a lazy generator. This post might be helpful: https://realpython.com/introduction-to-python-generators/
If so is there any workaround this?
If you did want to change this you could expend the generator as you create it:
equilibria = list(game.support_enumeration())
But this just changes where time is spent and not how much time.
Closing this as it's expected behaviour.
Thanks a lot!