def generate_games(num): # pylint: disable=too-many-branches """Produce num random games per type""" np.random.seed(0) with open(path.join(_DIR, 'example_games', 'hard_nash.json')) as fil: yield 'hard', gamereader.load(fil).normalize() with open(path.join(_DIR, 'example_games', '2x2x2.nfg')) as fil: yield 'gambit', gamereader.load(fil).normalize() for _ in range(num): yield 'random', gamegen.game(*random_small()).normalize() for _ in range(num): strats = np.random.randint(2, 5, np.random.randint(2, 4)) yield 'covariant', gamegen.covariant_game(strats).normalize() for _ in range(num): strats = np.random.randint(2, 5, 2) yield 'zero sum', gamegen.two_player_zero_sum_game(strats).normalize() for _ in range(num): yield 'prisoners', gamegen.prisoners_dilemma().normalize() for _ in range(num): yield 'chicken', gamegen.sym_2p2s_game(0, 3, 1, 2).normalize() for _ in range(num): prob = np.random.random() yield 'mix', gamegen.sym_2p2s_known_eq(prob).normalize() for _ in range(num): strats = np.random.randint(2, 4) plays = np.random.randint(2, 4) yield 'polymatrix', gamegen.polymatrix_game(plays, strats).normalize() for _ in range(num): wins = np.random.random(3) + .5 loss = -np.random.random(3) - .5 yield 'roshambo', gamegen.rock_paper_scissors(wins, loss).normalize() yield 'shapley easy', gamegen.rock_paper_scissors(win=2).normalize() yield 'shapley normal', gamegen.rock_paper_scissors(win=1).normalize() yield 'shapley hard', gamegen.rock_paper_scissors(win=0.5).normalize() for _ in range(num): yield 'normagg small', gamegen.normal_aggfn(*random_agg_small()) for _ in range(num): yield 'polyagg small', gamegen.poly_aggfn(*random_agg_small()) for _ in range(num): yield 'sineagg small', gamegen.sine_aggfn(*random_agg_small()) for _ in range(num): facs = np.random.randint(2, 6) req = np.random.randint(1, facs) players = np.random.randint(2, 11) yield 'congestion', gamegen.congestion(players, facs, req) for _ in range(num): strats = np.random.randint(2, 6) players = np.random.randint(2, 11) yield 'local effect', gamegen.local_effect(players, strats) for _ in range(num): yield 'normagg large', gamegen.normal_aggfn(*random_agg_large()) for _ in range(num): yield 'polyagg large', gamegen.poly_aggfn(*random_agg_large()) for _ in range(num): yield 'sineagg large', gamegen.sine_aggfn(*random_agg_large()) for _ in range(num): agg = gamegen.sine_aggfn(*random_agg_small()) with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) yield 'rbf', learning.rbfgame_train(agg)
def test_two_player_zero_sum_game(strategies): game = gamegen.two_player_zero_sum_game(strategies) assert game.is_complete(), "didn't generate a full game" assert game.num_roles == 2, "not two player" assert game.is_asymmetric(), \ "didn't generate an asymmetric game" assert np.all(strategies == game.num_strategies), \ "didn't generate right number of strategies" assert game.is_constant_sum(), "game not constant sum"
def test_two_player_zero_sum_game(strategies): """Test arbitrary 2pzs""" game = gamegen.two_player_zero_sum_game(strategies) assert game.is_complete(), "didn't generate a full game" assert game.num_roles == 2, 'not two player' assert game.is_asymmetric(), \ "didn't generate an asymmetric game" assert np.all(strategies == game.num_role_strats), \ "didn't generate right number of strategies" assert game.is_constant_sum(), 'game not constant sum'
def test_constant_sum(): game = gamegen.two_player_zero_sum_game(2) assert game.is_constant_sum() payoffs = game.payoffs.copy() payoffs[game.profiles > 0] += 1 game2 = rsgame.game_copy(game, game.profiles, payoffs) assert game2.is_constant_sum() profiles = [ [1, 0, 1, 0], [1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 0, 1], ] payoffs = [ [1, 0, 2, 0], [3, 0, 0, 4], [0, 5, 6, 0], [0, 7, 0, 8], ] game3 = rsgame.game_copy(game, profiles, payoffs) assert not game3.is_constant_sum()
def test_two_player_zero_sum_mixed_wellfare(strategies): game = gamegen.two_player_zero_sum_game(strategies) for prof in game.random_mixtures(20): assert np.isclose(regret.mixed_social_welfare(game, prof), 0), \ "zero sum profile wasn't zero sum"
def test_two_player_zero_sum_pure_wellfare(strategies): game = gamegen.two_player_zero_sum_game(strategies) for prof in game.profiles: assert np.isclose(regret.pure_social_welfare(game, prof), 0), \ "zero sum profile wasn't zero sum"
def create(args): """Create zero sum game""" return gamegen.two_player_zero_sum_game(args.num_strats)
def create(args): game = gamegen.two_player_zero_sum_game(args.num_strats) serial = gamegen.game_serializer(game) return game, serial
def create(args): """Create zero sum game""" return gamegen.two_player_zero_sum_game(args.num_strats)
def test_two_player_zero_sum_mixture_wellfare(strategies): """test welfare in zero sum game""" game = gamegen.two_player_zero_sum_game(strategies) for prof in game.random_mixtures(20): assert np.isclose(regret.mixed_social_welfare(game, prof), 0), \ "zero sum profile wasn't zero sum"
def test_two_player_zero_sum_pure_wellfare(strategies): """Test pure welfare in zero sum games""" game = gamegen.two_player_zero_sum_game(strategies) for prof in game.profiles(): assert np.isclose(regret.pure_social_welfare(game, prof), 0), \ "zero sum profile wasn't zero sum"