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_normal_game(players, strategies, functions, inp, weight): """Test normal aggfn""" aggfn_verify(gamegen.normal_aggfn(players, strategies, functions)) aggfn_verify( gamegen.normal_aggfn(players, strategies, functions, input_prob=inp, weight_prob=weight))
def test_random_canongame(strats): """Test random canon games""" strats = np.array(strats) if np.all(strats == 1): return # not a game game = gamegen.normal_aggfn(2, strats, strats.sum()) cgame = canongame.canon(game) paygame.game_copy(cgame)
async def test_basic_profile_aggfn(): """Test using an action graph game""" agame = gamegen.normal_aggfn([4, 3], [3, 4], 5) profs = agame.random_profiles(20) sched = gamesched.gamesched(agame) paylist = await asyncio.gather(*[sched.sample_payoffs(p) for p in profs]) pays = np.stack(paylist) assert np.allclose(pays[profs == 0], 0)
def test_canon(): """Test basic canon game""" profs = [[2, 0, 0, 3], [1, 1, 0, 3]] pays = [[1, 0, 0, 1], [2, 3, 0, np.nan]] game = paygame.game([2, 3], [3, 1], profs, pays) cgame = canongame.canon(game) assert cgame.num_profiles == 2 assert cgame.num_complete_profiles == 1 pay = cgame.get_payoffs([2, 0, 0]) assert np.allclose(pay, [1, 0, 0]) expected = [[2, 0, 0], [1, 1, 0]] assert np.all(cgame.profiles() == expected) expected = [[1, 0, 0], [2, 3, 0]] assert np.allclose(cgame.payoffs(), expected) assert np.allclose(cgame.deviation_payoffs([1, 0, 0]), [1, 3, np.nan], equal_nan=True) dev, jac = cgame.deviation_payoffs([0.5, 0.5, 0], jacobian=True) assert dev.shape == (3,) assert jac.shape == (3, 3) assert np.allclose(cgame.min_strat_payoffs(), [1, 3, np.nan], equal_nan=True) assert np.allclose(cgame.max_strat_payoffs(), [2, 3, np.nan], equal_nan=True) ngame = cgame.normalize() expected = [[0, 0, 0], [0.5, 1, 0]] assert utils.allclose_perm(ngame.payoffs(), expected) rgame = cgame.restrict([True, True, False]) expected = [[1, 0], [2, 3]] assert utils.allclose_perm(rgame.payoffs(), expected) copy_str = json.dumps(cgame.to_json()) copy = canongame.canon_json(json.loads(copy_str)) assert hash(cgame) == hash(copy) assert cgame == copy assert [2, 0, 0] in cgame assert [0, 2, 0] not in cgame assert repr(cgame) == 'CanonGame([2], [3], 2 / 6)' other = canongame.canon(gamegen.normal_aggfn([2, 2, 3], [3, 1, 1], 2)) assert other + cgame == cgame + other
def test_canon(): """Test basic canon game""" profs = [[2, 0, 0, 3], [1, 1, 0, 3]] pays = [[1, 0, 0, 1], [2, 3, 0, np.nan]] game = paygame.game([2, 3], [3, 1], profs, pays) cgame = canongame.canon(game) assert cgame.num_profiles == 2 assert cgame.num_complete_profiles == 1 pay = cgame.get_payoffs([2, 0, 0]) assert np.allclose(pay, [1, 0, 0]) expected = [[2, 0, 0], [1, 1, 0]] assert np.all(cgame.profiles() == expected) expected = [[1, 0, 0], [2, 3, 0]] assert np.allclose(cgame.payoffs(), expected) assert np.allclose(cgame.deviation_payoffs([1, 0, 0]), [1, 3, np.nan], equal_nan=True) dev, jac = cgame.deviation_payoffs([0.5, 0.5, 0], jacobian=True) assert dev.shape == (3, ) assert jac.shape == (3, 3) assert np.allclose(cgame.min_strat_payoffs(), [1, 3, np.nan], equal_nan=True) assert np.allclose(cgame.max_strat_payoffs(), [2, 3, np.nan], equal_nan=True) ngame = cgame.normalize() expected = [[0, 0, 0], [0.5, 1, 0]] assert utils.allclose_perm(ngame.payoffs(), expected) rgame = cgame.restrict([True, True, False]) expected = [[1, 0], [2, 3]] assert utils.allclose_perm(rgame.payoffs(), expected) copy_str = json.dumps(cgame.to_json()) copy = canongame.canon_json(json.loads(copy_str)) assert hash(cgame) == hash(copy) assert cgame == copy assert [2, 0, 0] in cgame assert [0, 2, 0] not in cgame assert repr(cgame) == 'CanonGame([2], [3], 2 / 6)' other = canongame.canon(gamegen.normal_aggfn([2, 2, 3], [3, 1, 1], 2)) assert other + cgame == cgame + other
def agg(): """Action graph game""" return gamegen.normal_aggfn([3, 4], [4, 3], 10)
def test_normal_game(players, strategies, functions, inp, weight): """Test normal aggfn""" aggfn_verify(gamegen.normal_aggfn(players, strategies, functions)) aggfn_verify(gamegen.normal_aggfn( players, strategies, functions, input_prob=inp, weight_prob=weight))