update pubeval to take tuples of proper length as input

This commit is contained in:
Christoffer Müller Madsen 2018-03-14 12:47:27 +01:00
parent 57367329ec
commit fcc373c0d8
5 changed files with 22 additions and 7 deletions

View File

@ -33,7 +33,7 @@ class Board:
negatives = [x if x < 0 else 0 for x in board]
board.append(15 - sum(positives))
board.append(-15 - sum(negatives))
return board
return tuple(board)

2
bot.py
View File

@ -52,7 +52,7 @@ class Bot:
# TODO: Test this, the score results should be deterministic
def make_pubeval_move(self, board, sym, roll):
legal_moves = Board.calculate_legal_states(tuple(board), sym, roll)
moves_and_scores = [(board, pubeval.eval(False, board)) for board in legal_moves]
moves_and_scores = [(board, pubeval.eval(False, Board.board_features_to_pubeval(board, sym))) for board in legal_moves]
scores = [ x[1] for x in moves_and_scores ]
best_move_pair = moves_and_scores[np.array(scores).argmax()]
return best_move_pair

View File

@ -184,7 +184,7 @@ class Game:
roll = self.roll()
self.board = (self.p1.make_move(self.board, self.p1.get_sym(), roll))[0]
roll = self.roll()
self.board = Board.flip(self.p2.make_pubeval_move(Board.board_features_to_pubeval(self.board, self.p2.get_sym()), self.p2.get_sym(), roll)[0][0:26])
self.board = Board.flip(self.p2.make_pubeval_move(self.board, self.p2.get_sym(), roll)[0][0:26])
sys.stderr.write("\t outcome {}".format(Board.outcome(self.board)[1]))
outcomes.append(Board.outcome(self.board)[1])
sys.stderr.write("\n")

View File

@ -1,5 +1,7 @@
#include <Python.h>
static PyObject* PubevalError;
static float x[122];
static const float wc[122] = {
@ -140,7 +142,11 @@ pubeval_eval(PyObject *self, PyObject *args) {
numValues = PyTuple_Size(tuple_obj);
if (numValues < 0) return NULL;
if (numValues != 28) {
PyErr_SetString(PubevalError, "Tuple must have 28 entries");
return NULL;
}
// Iterate over tuple to retreive positions
for (int i=0; i<numValues; i++) {
val_obj = PyTuple_GetItem(tuple_obj, i);
@ -168,6 +174,15 @@ static struct PyModuleDef pubeval_definition = {
};
PyMODINIT_FUNC PyInit_pubeval(void) {
//Py_Initialize();
return PyModule_Create(&pubeval_definition);
PyObject* module;
module = PyModule_Create(&pubeval_definition);
if (module == NULL)
return NULL;
PubevalError = PyErr_NewException("pubeval.error", NULL, NULL);
Py_INCREF(PubevalError);
PyModule_AddObject(module, "error", PubevalError);
return module;
}

View File

@ -4,6 +4,6 @@ pubeval = Extension('pubeval',
sources = ['pubeval.c'])
setup (name = 'pubeval',
version = '0.2',
version = '0.3',
description = 'Pubeval for Python',
ext_modules = [pubeval])