def create(args): assert len(args.pands) % 2 == 0, \ 'Must specify matching sets of players and strategies' game = gamegen.role_symmetric_game(args.pands[::2], args.pands[1::2]) serial = gamegen.game_serializer(game) return game, serial
def test_base_game_to_str(): game = rsgame.BaseGame([2, 1], [1, 2]) serial = gamegen.game_serializer(game) expected = ('BaseGame:\n Roles: r0, r1\n Players:\n 2x r0\n' ' 1x r1\n Strategies:\n r0:\n s0\n' ' r1:\n s0\n s1') assert game.to_str(serial) == expected
def test_game_to_str(): game = gamegen.role_symmetric_game([2, 1], [1, 2]) serial = gamegen.game_serializer(game) expected = ('Game:\n Roles: r0, r1\n Players:\n 2x r0\n' ' 1x r1\n Strategies:\n r0:\n s0\n' ' r1:\n s0\n s1\n' 'payoff data for 2 out of 2 profiles') assert game.to_str(serial) == expected
def test_learning(): with tempfile.NamedTemporaryFile('w') as game: sgame = gamegen.add_noise(gamegen.role_symmetric_game([2, 2], [3, 3]), 10) serial = gamegen.game_serializer(sgame) json.dump(sgame.to_json(serial), game) game.flush() assert not subprocess.run([GA, 'learning', '-i', game.name, '-o/dev/null', '-p1', '--dist-thresh', '1e-3', '-r1e-3', '-t1e-3', '--rand-restarts', '0', '-m10000', '-c1e-8']).returncode game.seek(0) assert not subprocess.run([GA, 'learning'], stdin=game).returncode
def test_subgame(): game = rsgame.BaseGame([3, 4], [3, 2]) subg = np.asarray([1, 0, 1, 0, 1], bool) devs = subgame.deviation_profiles(game, subg) assert devs.shape[0] == 7, \ "didn't generate the right number of deviating profiles" adds = subgame.additional_strategy_profiles(game, subg, 1).shape[0] assert adds == 6, \ "didn't generate the right number of additional profiles" subg2 = subg.copy() subg2[1] = True assert (subgame.subgame(game, subg2).num_all_profiles == adds + subgame.subgame(game, subg).num_all_profiles), \ "additional profiles didn't return the proper amount" serial = gamegen.game_serializer(game) sub_serial = subgame.subserializer(serial, subg) assert (subgame.subgame(game, subg).num_role_strats == sub_serial.num_role_strats)
def test_sgboot(): with tempfile.NamedTemporaryFile('w') as mixed, \ tempfile.NamedTemporaryFile('w') as game: sgame = gamegen.add_noise(gamegen.role_symmetric_game([2, 3], [4, 3]), 20) serial = gamegen.game_serializer(sgame) json.dump(sgame.to_json(serial), game) game.flush() profs = [serial.to_prof_json(sgame.uniform_mixture())] json.dump(profs, mixed) mixed.flush() assert not subprocess.run([GA, 'sgboot', '-i', game.name, mixed.name, '-o/dev/null']).returncode game.seek(0) assert not subprocess.run([GA, 'sgboot', mixed.name, '-tsurplus', '--processes', '1', '-n21', '-p', '5', '95', '-m'], stdin=game).returncode
def test_sample_game_to_str(): base = gamegen.role_symmetric_game([2, 1], [1, 2]) serial = gamegen.game_serializer(base) game = gamegen.add_noise(base, 3) expected = ('SampleGame:\n Roles: r0, r1\n Players:\n 2x r0\n' ' 1x r1\n Strategies:\n r0:\n s0\n' ' r1:\n s0\n s1\n' 'payoff data for 2 out of 2 profiles\n' '3 observations per profile') assert game.to_str(serial) == expected game = rsgame.SampleGame(base) expected = ('SampleGame:\n Roles: r0, r1\n Players:\n 2x r0\n' ' 1x r1\n Strategies:\n r0:\n s0\n' ' r1:\n s0\n s1\n' 'payoff data for 2 out of 2 profiles\n' '1 observation per profile') assert game.to_str(serial) == expected game = rsgame.SampleGame(rsgame.BaseGame(base)) expected = ('SampleGame:\n Roles: r0, r1\n Players:\n 2x r0\n' ' 1x r1\n Strategies:\n r0:\n s0\n' ' r1:\n s0\n s1\n' 'payoff data for 0 out of 2 profiles\n' 'no observations') assert game.to_str(serial) == expected profiles = [[2, 1, 0], [2, 0, 1]] spayoffs = [ [[[1], [2], [0]]], [[[3, 4], [0] * 2, [5, 6]]], ] game = rsgame.SampleGame(base, profiles, spayoffs) expected = ('SampleGame:\n Roles: r0, r1\n Players:\n 2x r0\n' ' 1x r1\n Strategies:\n r0:\n s0\n' ' r1:\n s0\n s1\n' 'payoff data for 2 out of 2 profiles\n' '1 to 2 observations per profile') assert game.to_str(serial) == expected
def test_nash(): with open(GAME) as f: assert not subprocess.run([GA, 'nash'], stdin=f).returncode assert not subprocess.run([ GA, 'nash', '-i', GAME, '-o/dev/null', '-r1e-2', '-d1e-2', '-c1e-7', '-x100', '-s1e-2', '-m5', '-n', '-p1']).returncode assert not subprocess.run([GA, 'nash', '-tpure', '-i', GAME]).returncode assert not subprocess.run([GA, 'nash', '-tmin-reg-prof', '-i', GAME]).returncode assert not subprocess.run([GA, 'nash', '-tmin-reg-grid', '-i', GAME]).returncode assert not subprocess.run([GA, 'nash', '-tmin-reg-rand', '-m10', '-i', GAME]).returncode assert not subprocess.run([GA, 'nash', '-trand', '-m10', '-i', GAME]).returncode assert subprocess.run([GA, 'nash', '-tfail', '-i', GAME]).returncode with tempfile.NamedTemporaryFile('w') as game: sgame = gamegen.rock_paper_scissors() serial = gamegen.game_serializer(sgame) json.dump(sgame.to_json(serial), game) game.flush() assert not subprocess.run([GA, 'nash', '-tpure', '--one', '-i', game.name]).returncode
def create(args): game = gamegen.two_player_zero_sum_game(args.num_strats) serial = gamegen.game_serializer(game) return game, serial
def test_json_copy_sample_game(game_size, samples): base = gamegen.role_symmetric_game(*game_size) game1 = gamegen.add_noise(base, 1, samples) serial = gamegen.game_serializer(game1) game2, _ = gameio.read_sample_game(game1.to_json(serial)) assert game1 == game2
def test_json_copy_game(players, strategies): game1 = gamegen.role_symmetric_game(players, strategies) serial = gamegen.game_serializer(game1) game2, _ = gameio.read_game(game1.to_json(serial)) assert game1 == game2
def test_json_copy_base_game(players, strategies): game1 = rsgame.BaseGame(players, strategies) serial = gamegen.game_serializer(game1) game2, _ = gameio.read_base_game(game1.to_json(serial)) assert game1 == game2