Skip to content

Installing GTNash for use with Virtual environments

I found a workaround to use GTNash (in particular, Sage) with virtual environments. The problem is that Sage must be installed as a system package - the Pip packages sagemath is a stub that does not install anything. The workaround uses virtualenv, although venv and alternatives must have similar functionality.

Workaround:

  1. Install Sage as a system package.
$ (sudo) apt-get install sagemath
  1. Create a virtual environment using the --system-site-packages flag i.e.
$ virtualenv --system-site-pacakges <env_name>

I tested this with the basic examples on the tutorial (reproduced here), and this works. If it is useful, it can be added to the documentation.

import numpy as np
import gtnash
from sage.all import *
from gtnash.game.normalformgame import *
from gtnash.solver.wilsonalgorithm import *

players_actions = [[0, 1], [0, 1], [0, 1]]
utilities = [
    [6, 7, 0, 5, 1, 4, 2, 3],
    [0, 3, 7, 4, 6, 5, 1, 2],
    [4, 7, 4, 0, 3, 2, 6, 1],
]
current_nfg = NFG(players_actions, utilities)
print("Utilities of the game")
print(current_nfg)

equilibrium = wilson(current_nfg)
print(equilibrium)
print("Is this an equilibrium? ", current_nfg.is_equilibrium(equilibrium))

Alternative approaches tried:

I tried the method prescribed by the Sage installation guide here, but it doesn't work with Python 3.11 or 3.12. Most likely I'm doing something wrong, but in any case, the workaround should be simpler in any case.