-
Notifications
You must be signed in to change notification settings - Fork 0
/
Angel.py
105 lines (91 loc) · 3.49 KB
/
Angel.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
import numpy as np
from NeuralNetwork import NeuralNetwork
class Angel:
def __init__(self, sides, trained=False):
self.sides = sides
self.position = int(sides / 2) * sides + int(sides / 2)
self.moves = []
input_nodes = int(sides ** 2)
hidden_nodes = 140
output_nodes = 4
learning_rate = 0.1
if not trained:
weight_wih = np.random.randn(hidden_nodes, int(input_nodes)) / np.sqrt(input_nodes)
weight_who = np.random.randn(output_nodes, hidden_nodes) / np.sqrt(hidden_nodes)
else:
angel_who = open("Files/angel_who.csv", 'r')
angel_who_read = angel_who.readlines()
angel_who.close()
angel_wih = open("Files/angel_wih.csv", 'r')
angel_wih_read = angel_wih.readlines()
angel_wih.close()
weight_wih = np.asfarray([line.split(',') for line in angel_wih_read])
weight_who = np.asfarray([line.split(',') for line in angel_who_read])
self.consciousness = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who,
learning_rate)
def get_last_move(self):
if not len(self.moves) == 0:
return self.moves[len(self.moves) - 1]
def get_position(self):
return self.position
def reset(self):
self.position = int(self.sides / 2) * self.sides + int(self.sides / 2)
self.moves = []
self.consciousness.reset()
# only for AI mode
def angel_move(self, board):
turn = np.argmax(self.consciousness.query(board))
if turn == 0:
self.position += -self.sides
if turn == 1:
self.position += 1
if turn == 2:
self.position += self.sides
if turn == 3:
self.position += -1
self.moves.append(turn)
def smart_angel(self, board, blocks):
turns = self.consciousness.query(board)
dict_turn = {}
for i in range(4):
dict_turn[i] = turns[i][0]
for dict in sorted(dict_turn.items(), key=lambda x: -x[1]):
if dict[0] == 0 and not self.position - self.sides in blocks:
self.position += -self.sides
self.moves.append(dict[0])
return
if dict[0] == 1 and not self.position + 1 in blocks:
self.position += 1
self.moves.append(dict[0])
return
if dict[0] == 2 and not self.position + self.sides in blocks:
self.position += self.sides
self.moves.append(dict[0])
return
if dict[0] == 3 and not self.position - 1 in blocks:
self.position += -1
self.moves.append(dict[0])
return
self.position -= self.sides
self.moves.append(0)
# train angel
def train(self, has_won):
if has_won is "devil":
self.consciousness.train(False)
if has_won is "angel":
self.consciousness.train(True)
# only for player mode
def god_move(self, move):
if move == -self.sides:
self.moves.append(0)
if move == 1:
self.moves.append(1)
if move == self.sides:
self.moves.append(2)
if move == -1:
self.moves.append(3)
self.position += move
def get_wih(self):
return self.consciousness.wih
def get_who(self):
return self.consciousness.who