Exemple #1
0
def test_rbfgame_equality():
    """Test all branches of equality test"""
    # pylint: disable-msg=protected-access
    game = gamegen.sparse_game([2, 3], [3, 2], 10)
    regg = learning.rbfgame_train(game)
    copy = regg.restrict(np.ones(game.num_strats, bool))
    copy._alpha.setflags(write=True)
    copy._profiles.setflags(write=True)
    copy._lengths.setflags(write=True)
    copy._coefs.setflags(write=True)
    copy._offset.setflags(write=True)
    assert regg == copy

    for alph, prof in zip(np.split(copy._alpha, copy._size_starts[1:]),
                          np.split(copy._profiles, copy._size_starts[1:])):
        perm = np.random.permutation(alph.size)
        np.copyto(alph, alph[perm])
        np.copyto(prof, prof[perm])
    assert regg == copy

    copy._alpha.fill(0)
    assert regg != copy
    copy._profiles.fill(0)
    assert regg != copy
    copy._lengths.fill(0)
    assert regg != copy
    copy._coefs.fill(0)
    assert regg != copy
    copy._offset.fill(0)
    assert regg != copy
Exemple #2
0
def test_sparse_game(players, strategies, _):
    """Test sparse game"""
    game = gamegen.sparse_game(players, strategies, 1)
    assert not game.is_empty()
    assert game.num_profiles == 1
    assert np.all(players == game.num_role_players), \
        "didn't generate correct number of strategies"
    assert np.all(strategies == game.num_role_strats), \
        "didn't generate correct number of strategies"
def test_sparse_game(players, strategies, _):
    """Test sparse game"""
    game = gamegen.sparse_game(players, strategies, 1)
    assert not game.is_empty()
    assert game.num_profiles == 1
    assert np.all(players == game.num_role_players), \
        "didn't generate correct number of strategies"
    assert np.all(strategies == game.num_role_strats), \
        "didn't generate correct number of strategies"
Exemple #4
0
def test_nntrain_no_dropout():
    """Test no dropout"""
    game = gamegen.sparse_game([2, 3], [3, 2], 10)
    reggame = learning.nngame_train(game, dropout=0)
    assert np.all((reggame.profiles() > 0) | (reggame.payoffs() == 0))
    assert not np.any(np.isnan(reggame.get_payoffs(reggame.random_profile())))
    assert not np.any(
        np.isnan(
            reggame.get_dev_payoffs(reggame.random_role_deviation_profile())))
Exemple #5
0
def test_nntrain():
    """Test training neural networks"""
    game = gamegen.sparse_game([2, 3], [3, 2], 10)
    reggame = learning.nngame_train(game)
    assert np.all((reggame.profiles() > 0) | (reggame.payoffs() == 0))
    assert not np.any(np.isnan(reggame.get_payoffs(reggame.random_profile())))
    assert not np.any(
        np.isnan(
            reggame.get_dev_payoffs(reggame.random_role_deviation_profile())))
Exemple #6
0
def test_rbfgame_min_max_payoffs(players, strats):
    """Test min and max payoffs of rbf game"""
    game = gamegen.sparse_game(players, strats, 11)
    reggame = learning.rbfgame_train(game)
    full = paygame.game_copy(reggame)

    assert np.all(
        full.min_strat_payoffs() >= reggame.min_strat_payoffs() - 1e-4)
    assert np.all(
        full.max_strat_payoffs() <= reggame.max_strat_payoffs() + 1e-4)
Exemple #7
0
def test_skltrain():
    """Test scikit learn traing"""
    game = gamegen.sparse_game([2, 3], [3, 2], 10)
    model = gp.GaussianProcessRegressor(1.0 * gp.kernels.RBF(2, [1, 3]) +
                                        gp.kernels.WhiteKernel(1))
    reggame = learning.sklgame_train(game, model)
    assert np.all((reggame.profiles() > 0) | (reggame.payoffs() == 0))
    assert not np.any(np.isnan(reggame.get_payoffs(reggame.random_profile())))
    assert not np.any(
        np.isnan(
            reggame.get_dev_payoffs(reggame.random_role_deviation_profile())))

    with pytest.raises(ValueError):
        reggame.deviation_payoffs(game.random_mixture())
    assert game + reggame == reggame + game
Exemple #8
0
def test_neighbor():  # pylint: disable=too-many-locals
    """Test neighbor games"""
    game = gamegen.sparse_game([2, 3], [3, 2], 10)
    model = gp.GaussianProcessRegressor(1.0 * gp.kernels.RBF(2, [1, 3]) +
                                        gp.kernels.WhiteKernel(1),
                                        normalize_y=True)
    learn = learning.neighbor(learning.sklgame_train(game, model))
    full = paygame.game_copy(learn)

    errors = np.zeros(game.num_strats)
    for i, mix in enumerate(
            itertools.chain(game.random_mixtures(20),
                            game.random_sparse_mixtures(20)), 1):
        tdev = full.deviation_payoffs(mix)
        dev, _ = learn.deviation_payoffs(mix, jacobian=True)
        err = (tdev - dev)**2 / (np.abs(dev) + 1e-5)
        errors += (err - errors) / i
    assert np.all(errors < 5)

    submask = game.random_restriction()
    sublearn = learn.restrict(submask)
    subfull = full.restrict(submask)
    assert np.allclose(sublearn.get_payoffs(subfull.profiles()),
                       subfull.payoffs())

    norm = learn.normalize()
    assert np.allclose(norm.min_role_payoffs(), 0)
    assert np.allclose(norm.max_role_payoffs(), 1)

    assert learning.neighbor(learn, learn.num_neighbors) == learn

    learn = learning.neighbor(learning.rbfgame_train(game))
    jgame = json.dumps(learn.to_json())
    copy = learning.neighbor_json(json.loads(jgame))
    assert hash(copy) == hash(learn)
    assert copy == learn
    assert learn + copy == copy + learn

    empty = rsgame.empty_copy(learn)
    assert learn + empty == empty
Exemple #9
0
def test_point():  # pylint: disable=too-many-locals
    """Test point

    We increase player number so point is a more accurate estimator.
    """
    # pylint: disable-msg=protected-access
    game = gamegen.sparse_game(1000, 2, 10)
    model = learning.rbfgame_train(game)
    learn = learning.point(model)
    full = paygame.game_copy(learn)
    red = np.eye(game.num_roles).repeat(game.num_role_strats, 0)
    size = np.eye(game.num_strats).repeat(model._sizes, 0)

    def devpays(mix):
        """The deviation payoffs"""
        profile = learn._dev_players * mix
        dev_profiles = anp.dot(size, anp.dot(red, profile))
        vec = ((dev_profiles - model._profiles) /
               model._lengths.repeat(model._sizes, 0))
        rbf = anp.einsum('...ij,...ij->...i', vec, vec)
        exp = anp.exp(-rbf / 2) * model._alpha
        return model._offset + model._coefs * anp.dot(exp, size)

    devpays_jac = autograd.jacobian(devpays)  # pylint: disable=no-value-for-parameter

    errors = np.zeros(game.num_strats)
    for i, mix in enumerate(
            itertools.chain(game.random_mixtures(20),
                            game.random_sparse_mixtures(20)), 1):
        fdev = full.deviation_payoffs(mix)
        dev, jac = learn.deviation_payoffs(mix, jacobian=True)
        err = (fdev - dev)**2 / (np.abs(dev) + 1e-5)
        errors += (err - errors) / i
        tdev = devpays(mix)
        tjac = devpays_jac(mix)
        assert np.allclose(learn.deviation_payoffs(mix), dev)
        assert np.allclose(dev, tdev)
        assert np.allclose(jac, tjac)

    # Point is a very biased estimator, so errors are large
    assert np.all(errors < 10)

    submask = game.random_restriction()
    sublearn = learn.restrict(submask)
    subfull = full.restrict(submask)
    assert np.allclose(sublearn.get_payoffs(subfull.profiles()),
                       subfull.payoffs())

    norm = learn.normalize()
    assert np.allclose(norm.min_role_payoffs(), 0)
    assert np.allclose(norm.max_role_payoffs(), 1)

    assert learning.point(learn) == learn

    jgame = json.dumps(learn.to_json())
    copy = learning.point_json(json.loads(jgame))
    assert hash(copy) == hash(learn)
    assert copy == learn
    assert learn + copy == copy + learn

    empty = rsgame.empty_copy(learn)
    assert learn + empty == empty
Exemple #10
0
def test_sample():  # pylint: disable=too-many-locals
    """Test sample game"""
    # pylint: disable-msg=protected-access
    game = gamegen.sparse_game([2, 3], [3, 2], 10)
    model = learning.sklgame_train(
        game,
        gp.GaussianProcessRegressor(1.0 * gp.kernels.RBF(2, [1, 3]) +
                                    gp.kernels.WhiteKernel(1),
                                    normalize_y=True))
    learn = learning.sample(model)
    full = paygame.game_copy(learn)

    @autograd.primitive
    def sample_profs(mix):
        """Sample profiles"""
        return game.random_role_deviation_profiles(learn.num_samples,
                                                   mix).astype(float)

    @autograd.primitive
    def model_pays(profs):
        """Get pays from model"""
        return model.get_dev_payoffs(profs)

    @autograd.primitive
    def const_weights(profs, mix):
        """Get the weights"""
        return np.prod(mix**profs, 2).repeat(game.num_role_strats, 1)

    @autograd.primitive
    def rep(probs):
        """Repeat an array"""
        return probs.repeat(game.num_role_strats, 1)

    def rep_vjp(_repd, _probs):
        """The jacobian of repeat"""
        return lambda grad: np.add.reduceat(grad, game.role_starts, 1)

    autograd.extend.defvjp(sample_profs, None)
    autograd.extend.defvjp(model_pays, None)
    autograd.extend.defvjp(const_weights, None, None)
    autograd.extend.defvjp(rep, rep_vjp)  # This is wrong in autograd

    def devpays(mix):
        """Compute the dev pays"""
        profs = sample_profs(mix)
        payoffs = model_pays(profs)
        numer = rep(anp.prod(mix**profs, 2))
        denom = const_weights(profs, mix)
        weights = numer / denom / learn.num_samples
        return anp.einsum('ij,ij->j', weights, payoffs)

    devpays_jac = autograd.jacobian(devpays)  # pylint: disable=no-value-for-parameter

    errors = np.zeros(game.num_strats)
    samp_errors = np.zeros(game.num_strats)
    for i, mix in enumerate(
            itertools.chain(game.random_mixtures(20),
                            game.random_sparse_mixtures(20)), 1):
        seed = random.randint(0, 10**9)
        fdev = full.deviation_payoffs(mix)
        np.random.seed(seed)
        dev, jac = learn.deviation_payoffs(mix, jacobian=True)
        avg_err = (fdev - dev)**2 / (np.abs(fdev) + 1e-5)
        errors += (avg_err - errors) / i
        samp_err = ((learn.deviation_payoffs(mix) - dev)**2 /
                    (np.abs(dev) + 1e-5))
        samp_errors += (samp_err - samp_errors) / i

        np.random.seed(seed)
        tdev = devpays(mix)
        assert np.allclose(dev, tdev)
        np.random.seed(seed)
        tjac = devpays_jac(mix)
        assert np.allclose(jac, tjac)
    assert np.all(errors <= 200 * (samp_errors + 1e-5))

    submask = game.random_restriction()
    sublearn = learn.restrict(submask)
    subfull = full.restrict(submask)
    assert np.allclose(sublearn.get_payoffs(subfull.profiles()),
                       subfull.payoffs())

    norm = learn.normalize()
    assert np.allclose(norm.min_role_payoffs(), 0)
    assert np.allclose(norm.max_role_payoffs(), 1)

    assert learning.sample(learn, learn.num_samples) == learn

    learn = learning.sample(learning.rbfgame_train(game))
    jgame = json.dumps(learn.to_json())
    copy = learning.sample_json(json.loads(jgame))
    assert hash(copy) == hash(learn)
    assert copy == learn
    assert learn + copy == copy + learn

    empty = rsgame.empty_copy(learn)
    assert learn + empty == empty