-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
14 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from mutable import Converter | ||
from .mutable import Converter | ||
|
||
import numpy as np | ||
import h5py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from converter import * | ||
from .converter import * | ||
from collections import namedtuple | ||
|
||
import numpy as np | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from server.serve import run | ||
|
||
run(port=8008) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import h5py | ||
import numpy as np | ||
|
||
from keras.models import load_model | ||
from keras_resnet import custom_objects | ||
|
||
from .results import order, rotate | ||
from munch.converter import convert_record | ||
|
||
network = 'nn/01_first_nn.h5' | ||
model = load_model(network) | ||
|
||
def predict(game_record): | ||
moves = game_record.split(';') | ||
nparr = np.array([convert_record(moves)]) | ||
predictions = order(model.predict(nparr)[0].tolist()) | ||
|
||
if len(moves) % 2 == 1: | ||
predictions = [rotate(p) for p in predictions] | ||
|
||
return predictions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
hp = list("abcdefghi") | ||
vp = list("987654321") | ||
|
||
hw = list("abcdefghx") | ||
vw = list("87654321x") | ||
|
||
|
||
available_moves = [] | ||
for v in vw: | ||
for h in hw: | ||
available_moves.append(h + v + "h") | ||
|
||
for v in vw: | ||
for h in hw: | ||
available_moves.append(h + v + "v") | ||
|
||
for v in vp: | ||
for h in hp: | ||
available_moves.append(h + v) | ||
|
||
|
||
p_rotations = {} | ||
for i, val in enumerate(hp): | ||
p_rotations[val] = hp[8 - i] | ||
for i, val in enumerate(vp): | ||
p_rotations[val] = vp[8 - i] | ||
|
||
w_rotations = {} | ||
for i, val in enumerate(hw[:8]): | ||
w_rotations[val] = hw[7 - i] | ||
for i, val in enumerate(vw[:8]): | ||
w_rotations[val] = vw[7 - i] | ||
|
||
|
||
def convert(pred): | ||
prob, move = pred | ||
return (move, int(prob * 1000)) | ||
|
||
def nonzero(pred): | ||
prob, move = pred | ||
return prob != 0 | ||
|
||
def order(predictions): | ||
preds = sorted(list(zip(predictions, available_moves)), reverse=True) | ||
filtered = filter(nonzero, map(convert, preds)) | ||
|
||
return list(filtered)[:20] | ||
|
||
|
||
def rotate(pred): | ||
move, prob = pred | ||
|
||
col = move[0] | ||
row = move[1] | ||
|
||
if len(move) == 3: | ||
newmove = w_rotations[col] + w_rotations[row] + move[2] | ||
elif len(move) == 2: | ||
newmove = p_rotations[col] + p_rotations[row] | ||
else: | ||
raise "oops" | ||
|
||
return (newmove, prob) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from .predictor import predict | ||
|
||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
from urllib.parse import urlparse, parse_qs | ||
import json | ||
|
||
class S(BaseHTTPRequestHandler): | ||
def _set_headers(self): | ||
self.send_response(200) | ||
self.send_header('Content-type', 'application/json') | ||
self.end_headers() | ||
|
||
def do_GET(self): | ||
self._set_headers() | ||
url = urlparse(self.path) | ||
game = url.query.replace("game=", "") | ||
|
||
encoded = json.dumps(predict(game)) | ||
self.wfile.write((encoded + "\n").encode('utf-8')) | ||
|
||
def run(server_class=HTTPServer, handler_class=S, port=80): | ||
server_address = ('', port) | ||
httpd = server_class(server_address, handler_class) | ||
print('Starting httpd...') | ||
httpd.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#curl "http://localhost:8008?game=" | ||
|
||
#curl "http://localhost:8008?game=d1;d9;d2;d8;d3;d7;a1h;d6;c1h;d5;d4v;d8v;d2v;f5h;c5h;h5h;d4;e4v;b6v;a8h;c8v;c5;c4;b5;c5;b4v" | ||
#curl "http://localhost:8008?game=d1;d9;d2;d8;d3;d7;a1h;d6;c1h;d5;d4v;d8v;d2v;f5h;c5h;h5h;d4;e4v;b6v;a8h;c8v;c5;c4;b5;c5" | ||
|
||
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4" | ||
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4;a5h" | ||
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4;a5h;e1v" | ||
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4;a5h;e1v;e7" | ||
curl "http://localhost:8008?game=e2;e8;e3;f8;e4;d4h;d3h;e3v;d4;b4h;b3h;f7;f6h;g7;c4;h6v;g6v;f7;b4;a5h;e1v;e7;a4" | ||
|
||
# clash in the middle | ||
# curl "http://localhost:8008?game=e2;e8;e3;e7;e4;e6;e5" |