-
Notifications
You must be signed in to change notification settings - Fork 4
/
agent.py
164 lines (130 loc) · 4.47 KB
/
agent.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from abc import abstractmethod
import random
import numpy as np
import pickle
from model import *
class Agent:
def __init__(self, tag, exploration_factor=1):
self.tag = tag
self.exp_factor = exploration_factor
self.prev_state = np.zeros((6, 7))
self.prev_move = -1
self.state = None
self.move = None
self.print_value = False
self.model = Model(self.tag)
self.memory = []
self.count_memory = 0
self.winner_flag = False
def choose_move(self, state, winner, learn):
self.load_to_memory(self.prev_state, self.prev_move, state, self.ava_moves(state), self.reward(winner))
if winner is not None:
self.count_memory += 1
self.prev_state = np.zeros((6, 7))
self.prev_move = -1
if learn is True and self.count_memory == 1000:
self.count_memory = 0
# Offline training
self.model.learn_batch(self.memory)
self.memory = []
# Online training
# self.learn(self.prev_state, self.prev_move, state, self.ava_moves(state), -1, self.reward(winner))
return None
p = random.uniform(0, 1)
if p < self.exp_factor:
idx = self.choose_optimal_move(state)
else:
ava_moves = self.ava_moves(state)
idx = random.choice(ava_moves)
self.prev_state = state
self.prev_move = idx
return idx
def choose_optimal_move(self, state):
ava_moves = self.ava_moves(state)
v = -float('Inf')
v_list = []
idx = []
for move in ava_moves:
value = self.model.calc_value(state, move)
v_list.append(round(float(value), 5))
if value > v:
v = value
idx = [move]
elif v == value:
idx.append(move)
idx = random.choice(idx)
return idx
def game_winner(self, state):
winner = None
for i in range(len(state[:,0])-3):
for j in range(len(state[0, :])-3):
winner = self.square_winner(state[i:i+4, j:j+4])
if winner is not None:
# print('winner is:', self.winner)
break
if winner is not None:
# print('winner is:', self.winner)
break
if np.min(np.abs(state[0, :])) != 0:
winner = 0
# print('no winner')
return winner
@staticmethod
def square_winner(square):
s = np.append([np.sum(square, axis=0), np.sum(square, axis=1).T],
[np.trace(square), np.flip(square,axis=1).trace()])
if np.max(s) == 4:
winner = 1
elif np.min(s) == -4:
winner = 2
else:
winner = None
return winner
@staticmethod
def make_state_from_move(state, move, player):
if move is None:
return state
state = np.array(state)
if player == 1:
tag = 1
else:
tag = -1
if len(np.where(state[:, move] == 0)[0]) == 0:
print(state)
idy = np.where(state[:, move] == 0)[0][-1]
state = np.array(state)
state[idy, move] = tag
return state
def reward(self, winner):
if winner is self.tag:
reward = 1
elif winner is None:
reward = 0
elif winner == 0:
reward = 0.5
else:
reward = -1
return reward
def learn(self, prev_state, prev_move, state, ava_moves, move, reward):
if prev_move != -1:
target = self.model.calc_target(prev_state, prev_move, state, ava_moves, reward)
# print(target)
self.model.train_model(prev_state, prev_move, target, 1)
@abstractmethod
def ava_moves(self, state):
pass
def load_to_memory(self, prev_state, prev_move, state, ava_moves, reward):
self.memory.append([prev_state, prev_move, state, ava_moves, reward])
def save_memory(self):
is_file_ = True
count = 1
s = ''
while is_file_:
s = 'data4/value_list_' + str(self.tag) + '_' + str(count) + '.pkl'
if Path(s).is_file():
is_file_ = True
count = count + 1
else:
is_file_ = False
with open(s, 'wb') as output:
pickle.dump(self.memory, output)