-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_play.py
323 lines (252 loc) · 9.52 KB
/
main_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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Andrea Galassi"
__copyright__ = "Copyright 2016-2017, Andrea Galassi"
__license__ = "MIT"
__version__ = "1.1.1"
__email__ = "[email protected]"
import numpy
import theano
import lasagne
import sys
import socket
from dataprocessing import (process_state_binary, add_CHOICE_binary_raw,
process_game_line)
from legality import get_legalities
from networks import load_net
illegalTO = 0
illegalFROM = 0
illegalREMOVE = 0
whiteport = 5802
blackport = 5803
def main(netname='TEST-TFR', team = "white"):
if (team == 'white'):
play(netname, whiteport)
elif (team == 'black'):
play(netname, blackport)
else:
play(netname)
sys.exit(0)
def init(name):
inputTO, TOnetwork, numTO, dataformat = load_net(name + "_TO")
inputFROM, FROMnetwork, numFROM, dataformat = load_net(name + "_FROM")
inputREMOVE, REMOVEnetwork, numREMOVE, dataformat = load_net(name + "_REMOVE")
TOpred = lasagne.layers.get_output(TOnetwork, deterministic=True)
TOpred_fn = theano.function(
[inputTO], TOpred, name="TO prediction function")
FROMpred = lasagne.layers.get_output(FROMnetwork, deterministic=True)
FROMpred_fn = theano.function(
[inputFROM], FROMpred, name="FROM prediction function")
REMOVEpred = lasagne.layers.get_output(REMOVEnetwork, deterministic=True)
REMOVEpred_fn = theano.function(
[inputREMOVE], REMOVEpred, name="REMOVE prediction function")
return ((TOpred_fn, numTO),
(FROMpred_fn, numFROM),
(REMOVEpred_fn, numREMOVE),
dataformat)
def choose(TOnet, FROMnet, REMOVEnet, state, data_format):
orderc = ['X', 'X', 'X']
orderc[TOnet[1]] = 'T'
orderc[FROMnet[1]] = 'F'
orderc[REMOVEnet[1]] = 'R'
order = "" + orderc[0] + orderc[1] + orderc[2]
print("\tOrder: " + order)
nets = ['X', 'X', 'X']
nets[TOnet[1]] = TOnet
nets[FROMnet[1]] = FROMnet
nets[REMOVEnet[1]] = REMOVEnet
FIRSTpred_fn = nets[0][0]
SECONDpred_fn = nets[1][0]
THIRDpred_fn =nets[2][0]
global illegalTO
global illegalFROM
global illegalREMOVE
illegal = ['X', 'X', 'X']
illegal[TOnet[1]] = illegalTO
illegal[FROMnet[1]] = illegalFROM
illegal[REMOVEnet[1]] = illegalREMOVE
print("Choosing best move: ")
print("\tchoosing FIRST position...")
# TO choice
states = [state]
FIRSTstate = process_state_binary(states, data_format)
FIRSTprob = FIRSTpred_fn(FIRSTstate)
correct = False
FIRSTchoice = 0
while not correct:
propchoice = numpy.argmax(FIRSTprob[0])
if FIRSTprob[0][propchoice] >= 0:
FIRSTchoice = [propchoice]
else:
print("No legal option aviable")
sys.exit()
print("\t\tTOchoice: " + str(FIRSTchoice[0]))
vec_4_leg = [FIRSTchoice, None, None]
legalities = get_legalities(vec_4_leg[TOnet[1]],
vec_4_leg[FROMnet[1]],
vec_4_leg[REMOVEnet[1]],
FIRSTstate)
if orderc[0] == 'T':
legalities = legalities[0]
elif orderc[0] == 'F':
legalities = legalities[1]
elif orderc[0] == 'R':
legalities = legalities[2]
if(legalities[0] == 0):
FIRSTprob[0][FIRSTchoice[0]] = -1
print("\t\t\tIllegal FIRST choice, retry")
illegal[0] += 1
else:
correct = True
print("\tchoosing SECOND position...")
# SECOND choice
SECONDstate = add_CHOICE_binary_raw(FIRSTstate, FIRSTchoice)
SECONDprob = SECONDpred_fn(SECONDstate)
correct = False
SECONDchoice = 0
while not correct:
propchoice = numpy.argmax(SECONDprob[0])
if SECONDprob[0][propchoice] >= 0:
SECONDchoice = [propchoice]
else:
print("No legal option aviable")
sys.exit()
print("\t\tSECONDchoice: " + str(SECONDchoice[0]))
vec_4_leg = [FIRSTchoice, SECONDchoice, None]
legalities = get_legalities(vec_4_leg[TOnet[1]],
vec_4_leg[FROMnet[1]],
vec_4_leg[REMOVEnet[1]],
SECONDstate)
if orderc[2] == 'T':
legal = legalities[9]
elif orderc[2] == 'F':
legal = legalities[8]
elif orderc[2] == 'R':
legal = legalities[5]
if(legal[0] == 0):
SECONDprob[0][SECONDchoice[0]] = -1
print("\t\t\tIllegal FROM choice, retry")
illegal[1] += 1
else:
correct = True
print("\tchoosing THIRD position...")
# REMOVE choice
THIRDstate = add_CHOICE_binary_raw(SECONDstate, SECONDchoice)
THIRDprob = THIRDpred_fn(THIRDstate)
correct = False
THIRDchoice = 0
while not correct:
propchoice = numpy.argmax(THIRDprob[0])
if THIRDprob[0][propchoice] >= 0:
THIRDchoice = [propchoice]
else:
print("No legal option aviable")
sys.exit()
print("\t\tTHIRDchoice: " + str(THIRDchoice[0]))
vec_4_leg = [FIRSTchoice, SECONDchoice, THIRDchoice]
legalities = get_legalities(vec_4_leg[TOnet[1]],
vec_4_leg[FROMnet[1]],
vec_4_leg[REMOVEnet[1]],
SECONDstate)[7]
if(legalities[0] == 0):
THIRDprob[0][THIRDchoice[0]] = -1
print("\t\t\tIllegal REMOVE choice, retry")
illegal[2] += 1
else:
correct = True
choices = [FIRSTchoice, SECONDchoice, THIRDchoice]
return (choices[TOnet[1]][0],
choices[FROMnet[1]][0],
choices[REMOVEnet[1]][0])
# color: W for white, B for black
def play(name, port=whiteport):
if (port == whiteport):
print("Hi, player White")
else:
print("Hi, player Black")
print("Loading networks")
TOnet, FROMnet, REMOVEnet, data_format = init(name)
print("\tNetworks loaded!")
sys.stdout.flush()
print("\tNetworks loaded")
print("Connecting to the game on port " + str(port))
sys.stdout.flush()
socket, connection = connect(port)
choices = 0
global illegalTO
global illegalFROM
global illegalREMOVE
print("\tConnected!")
sys.stdout.flush()
while(True):
# listen to the socket
print ("Waiting for message")
sys.stdout.flush()
data = receive(connection)
datastr = str(data)
print("String recieved: " + datastr)
# processing the state
state = process_game_line(datastr)
print("State: " + str(state))
sys.stdout.flush()
# answering
TOc, FROMc, REMOVEc = choose(TOnet, FROMnet, REMOVEnet,
state, data_format)
print("Choices: " + str(TOc) + " " + str(FROMc) + " " + str(REMOVEc))
choices += 1
meanIT = illegalTO * 1.0 / choices
meanIF = illegalFROM * 1.0 / choices
meanIR = illegalREMOVE * 1.0 / choices
print("\tIllegal TO: " + str(illegalTO) +
" (mean: " + str(meanIT) + ")")
print("\tIllegal FROM: " + str(illegalFROM) +
" (mean: " + str(meanIF) + ")")
print("\tIllegal REMOVE: " + str(illegalREMOVE) +
" (mean: " + str(meanIR) + ")")
connection.send("" + str(TOc) + "\n")
connection.send("" + str(FROMc) + "\n")
connection.send("" + str(REMOVEc) + "\n")
sys.stdout.flush()
def connect(port):
# try to connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind(('', port))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
exit(4)
print("\tSocket binding completed")
s.listen(100)
conn, addr = s.accept()
print("\tConnection established")
return s, conn
def receive(s):
MSGLEN = 30
chunks = []
bytes_recd = 0
while bytes_recd < MSGLEN:
chunk = s.recv(min(MSGLEN - bytes_recd, 4096))
print(str(chunk))
if chunk == '':
raise RuntimeError("socket connection broken")
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
data = ''.join(chunks)
return data
if __name__ == '__main__':
kwargs = {}
usage = ("Usage: %s netname team" % sys.argv[0])
if ('--help' in sys.argv) or ('-h' in sys.argv):
print("Neural Nine Men's Morris\n" +
"Playing clinet:\n" +
"Specify the name of the networks and the team " +
"color (because of the team port)")
if len(sys.argv) == 3:
kwargs['netname'] = sys.argv[1]
kwargs['team'] = sys.argv[2]
main(**kwargs)
elif len(sys.argv) == 1 :
main(**kwargs)
else:
print ("Wrong number of arguments: " + str(len(sys.argv))+"\n" + usage)