This repository has been archived by the owner on Sep 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sarsa_play.py
99 lines (86 loc) · 4.02 KB
/
sarsa_play.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
"""
Amca: The RL-Based Backgammon Agent
https://github.com/ardabbour/amca/
Abdul Rahman Dabbour, Omid Khorsand Kazemy, Yusuf Izmirlioglu
Cognitive Robotics Laboratory
Faculty of Engineering and Natural Sciences
Sabanci University
This script allows us to play against a SARSA Backgammon agent.
"""
import argparse
import pickle
from amca.game import SarsaGame
if __name__ == "__main__":
PARSER = argparse.ArgumentParser(description='Train an agent using RL')
PARSER.add_argument('--name', '-n',
help='Name of the agent to play againts.',
default='amca/models/sarsa-vs_random-1M.pkl',
type=str)
PARSER.add_argument('--games', '-g',
help='Number of games to play.',
default=1)
ARGS = PARSER.parse_args()
filename = ARGS.name
infile = open(filename, 'rb')
agent = pickle.load(infile)
infile.close()
# TODO Make human player 1
opponent = 'human'
gamei = SarsaGame(agent, opponent)
num_move = 0
for _ in range(int(ARGS.games)):
while (not gamei.is_over()):
gamei.roll_dice()
curstate = gamei.get_state3(gamei.get_dice(0))
possible_actions, their_rewards = gamei.get_actions(
agent, gamei.get_dice(0))
curaction = agent.playAction(curstate, possible_actions)
gamei.update_board(agent, curaction)
print("Computer Turn, dices: " + str(gamei.get_dice(0)) +
" " + str(gamei.get_dice(1)))
print("Computer played: ")
print(curaction)
if not gamei.is_over():
nextstate = gamei.get_state3(gamei.get_dice(1))
possible_actions, their_rewards = gamei.get_actions(
agent, gamei.get_dice(1))
nextaction = agent.playAction(curstate, possible_actions)
gamei.update_board(agent, nextaction)
print("Computer played: ")
print(nextaction)
if not gamei.is_over():
gamei.roll_dice()
print("Human Turn, dices: " + str(gamei.get_dice(0)) +
" " + str(gamei.get_dice(1)))
gamei.render()
move_type = input(
'input your action type from {move,hit,reenter,reenter_hit,bearoff}')
if move_type == 'move' or 'hit':
source = int(input('input your source from {0,1,..,23}'))
target = int(input('input your target from {0,1,..,23}'))
human_action = (move_type, source, target)
elif move_type == 'bearoff':
source = int(input('input your source from {0,1,..,23}'))
human_action = (move_type, source)
elif move_type == 'reenter' or 'reenter_hit':
target = int(input('input your target from {0,1,..,23}'))
human_action = (move_type, target)
gamei.update_board(opponent, human_action)
if not gamei.is_over():
gamei.render()
move_type = input(
'input your action type from {move,hit,reenter,reenter_hit,bearoff}')
if move_type == 'move' or 'hit':
source = int(input('input your source from {0,1,..,23}'))
target = int(input('input your target from {0,1,..,23}'))
human_action = (move_type, source, target)
elif move_type == 'bearoff':
source = int(input('input your source from {0,1,..,23}'))
human_action = (move_type, source)
elif move_type == 'reenter' or 'reenter_hit':
target = int(input('input your target from {0,1,..,23}'))
human_action = (move_type, target)
gamei.update_board(opponent, human_action)
num_move = num_move+1