-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.py
152 lines (138 loc) · 5.53 KB
/
Player.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
import random
import numpy as np
import theano.tensor.nnet as Tann
import theano.tensor as T
import ANN
import TrainingSetReader as reader
class ANNPlayer:
"""
Defines a player who makes move by training an Artificial Neural Network
"""
def __init__(self, layer_sizes=[16, 2000, 4], lr=.1, activation=ANN.relu, max_iterations=10, rand_limit_min=-.02, rand_limit_max=.02,
learningSet = [], learningSet_answ = [], testSet = [], testSet_answ = []):
# Converting data...
print("Converting data...")
learningSet = self.convert_input_divide_relative_to_max(learningSet)
learningSet_answ = self.convert_answers(learningSet_answ)
testSet = self.convert_input_divide_relative_to_max(testSet)
# Builds the network
self.neuralNet = ANN.neuralnetwork(layer_sizes=layer_sizes, lr=lr, activation=activation, max_iterations=max_iterations, rand_limit_min=rand_limit_min,
rand_limit_max=rand_limit_max, learningSet=learningSet, learningSet_answ=learningSet_answ, testSet=testSet, testSet_answ=testSet_answ)
def convert_input_divide_relative_to_max(self, input):
"""
Devides the input by the max value in the input
:param input: array of 16 values (exponents of 2)
:return: converted array
"""
assert len(input[0]) == 16
arr = []
for i in range(len(input)):
innerArr = input[i]
innerArr = np.array(innerArr)
maxValue = innerArr.max()
innerArr /= maxValue
arr.append(innerArr)
return np.array(arr)
def convert_input_divide_by_2048(self, input):
"""
Devides the input by 11 (2^11 = 2048)
:param input: array of 16 values (exponents of 2)
:return: converted array
"""
assert len(input[0]) == 16
arr = []
maxValue = 11.0
for i in range(len(input)):
innerArr = input[i]
innerArr = np.array(innerArr)
innerArr /= maxValue
arr.append(innerArr)
return np.array(arr)
def convert_input_divide_relative_to_max_pow_2(self, input):
"""
Makes power of 2's and then devides the input by the max value
:param input: array of 16 values (exponents of 2)
:return: converted array
"""
assert len(input[0]) == 16
arr = []
for i in range(len(input)):
innerArr = input[i]
innerArr = np.array(innerArr)
for j in range(len(innerArr)):
if not innerArr[j]==0:
innerArr[j] = pow(2, innerArr[j])
maxValue = innerArr.max()
innerArr /= maxValue
arr.append(innerArr)
return np.array(arr)
def convert_answers(self, answers):
"""
Converts the answer to an array of 0 and 1
:param array of integers
:return double array of arrays containing 0 and 1
"""
arr = []
for i in range(len(answers)):
innerArr = np.zeros(4)
innerArr[answers[i]] = 1.0
arr.append(innerArr)
return arr
def getMove(self, boardValues):
"""
Converts the predicted array and returns the indexes in order [0.1, 0.6, 0.05, 0.25] --> [2, 4, 1, 3]
:param boardValues: a board state
:return: an array of moves in prioritized order
"""
arr = self.convert_input_divide_relative_to_max([boardValues])
result = self.neuralNet.predictMovePriority(arr)[0]
movePriority = []
for k in range(len(result)):
max_value = -1.0
max_index = -1.0
for i in range(len(result)):
if result[i] > max_value:
max_index = i
max_value = result[i]
result[max_index] = -1
movePriority.append(max_index+1)
return movePriority
class RandomPlayer:
"""
A random player
"""
def getMove(self, boardValues):
"""
:param boardValues: a board state
:return: a random move priority
"""
movePriority = []
while len(movePriority) < 4:
value = random.choice([1,2,3,4])
if value not in movePriority:
movePriority.append(value)
return movePriority
class Player:
"""
Common class for a player
Could be a random player of an ANN player.
Can be expanded to human player as well
"""
def __init__(self, randomPlayer = False, layer_sizes=[16, 250, 4], lr=.1, activation=ANN.relu, max_iterations=1000,
rand_limit_min=-.02, rand_limit_max=.02, learning_set="humantest2", test_set="humantest1"):
if randomPlayer:
self.player = RandomPlayer()
else:
print("Reading data...")
trainingSet, trainingSetAnsw = reader.readSet(filename=learning_set)
testSet, testSetAnsw = reader.readSet(filename=test_set)
self.player = ANNPlayer(layer_sizes=layer_sizes, lr=lr, activation=activation, max_iterations=max_iterations,
rand_limit_min=rand_limit_min, rand_limit_max=rand_limit_max, learningSet=trainingSet,
learningSet_answ=trainingSetAnsw, testSet=testSet, testSet_answ=testSetAnsw)
def move(self, boardValues):
"""
Common move method
:param boardValues: a board state
:return: a prioritized move array
"""
return self.player.getMove(boardValues=boardValues)