-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·80 lines (64 loc) · 1.95 KB
/
test.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
#!/usr/bin/python
from game import MinesGame
from time import strftime
from net import Net
import torch
import numpy as np
WIDTH = 8
GOAL_STEPS = 500
scores = []
choices = []
wins = []
print_rounds = False
def test(net, show_games):
print("START")
date = strftime("%Y-%m-%d-%H:%M:%S")
print(date)
for each_game in range(100):
game_memory = []
game = MinesGame(8, 8)
observations = game.game_board
score = 0
for _ in range(GOAL_STEPS):
board = observations
board = torch.Tensor(board)
board = board.reshape([1, 1, 8, 8])
action = net(board)
prediction = torch.max(action, 1)
if show_games:
print(action)
action = torch.max(action).item()
action = prediction[1].item()
# action = int(round(action, 0))
if show_games:
game.print_board()
choices.append(action)
observations, done, reward, won = game.enter_input(action)
game_memory.append([observations, action])
score += reward
if done:
break
print(f"------------------ won: {won} score: {score} -----------------")
scores.append(score)
if won:
wins.append(1)
else:
wins.append(0)
average_score = sum(scores) / len(scores)
average_win = sum(wins) / len(scores)
print("Average score: ", average_score)
print("Average win: ", average_win)
date = strftime("%Y-%m-%d-%H:%M:%S")
print(date)
if __name__ == "__main__":
net = Net()
net.load_state_dict(torch.load("models/value.pth"))
import argparse
parser = argparse.ArgumentParser(description="Main file")
parser.add_argument("--print", type=bool)
args = parser.parse_args()
if args.print:
show_games = True
elif args.print is None:
show_games = False
test(net, show_games)