Пример #1
0
def runmatch(x, o, n):
    """Run a match between two policies.

    Parameters
    ----------
    x : Policy
        Policy that plays X.
    o : Policy
        Policy that plays O.
    n : int
        Number of games in the match.

    Returns
    -------
    xwin, owin : tuple of float
        Wins percentages (1 - xwin - owin = number of draws).

    """
    xwins = owins = 0
    for i in range(n):
        log = field.rungame(x, o)
        winner = field.winner(log[-1])
        if winner == field.X:
            xwins += 1
        elif winner == field.O:
            owins += 1

    return 1.0 * xwins / n, 1.0 * owins / n
Пример #2
0
            xwins += 1
        elif winner == field.O:
            owins += 1

    return 1.0 * xwins / n, 1.0 * owins / n


x = vtable.VTablePolicy(field.X, 0.2, 0.2)
o = hardcoded.BlockAttackPolicy(field.O)

# Train
games_in_round = 10
rounds = 10
curiosity_multiplier = 0.5

for i in range(rounds):
    wins, losses = runmatch(x, o, games_in_round)
    print('Round {} (of {} games): curiosity = {:5f}  '
          'WIN: {:4.0%}, LOSE: {:4.0%}'
          .format(i, games_in_round, x.curiosity, wins, losses))
    x.curiosity *= curiosity_multiplier

# Demo!
print('')
print('Demo:')
log = field.rungame(x, o)
fields = [field.posno_to_field(p) for p in log]
print('|'.join(f[:3] for f in fields))
print('|'.join(f[3:6] for f in fields))
print('|'.join(f[6:] for f in fields))
Пример #3
0
def test_rungame_cc():
    """Run game between two instances of CenterCornersPolicy."""
    x = hc.CenterCornersPolicy()
    o = hc.CenterCornersPolicy()
    assert ttf.rungame(x, o) == [0, 81, 83, 92, 1550, 8111,
                                 8117, 8144, 8630, 10817]
Пример #4
0
def test_rungame_ba():
    """Run a game between two instances of BlockAttackPolicy."""
    x = hc.BlockAttackPolicy(ttf.X)
    o = hc.BlockAttackPolicy(ttf.O)
    expect = [0, 81, 13203, 13932, 13950, 14193, 14247, 14250, 18624, 18625]
    assert ttf.rungame(x, o) == expect
Пример #5
0
def test_rungame_tl():
    """Run game between two instances of TopLeftPolicy."""
    x = hc.TopLeftPolicy()
    o = hc.TopLeftPolicy()
    assert ttf.rungame(x, o) == [0, 1, 7, 16, 70, 151, 637, 1366]