-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytrueskill.cpp
142 lines (118 loc) · 5.07 KB
/
pytrueskill.cpp
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
#define NPY_NO_DEPRECATED_API NPY_1_10_API_VERSION
#include "Python.h"
#include "numpy/arrayobject.h"
#include "trueskill.h"
#include <sstream>
#include <vector>
template<typename T>
static bool validate_stride(PyArrayObject *x, const char *argname)
{
int ndims = PyArray_NDIM(x);
int stride = PyArray_STRIDE(x,ndims-1);
if (stride != sizeof(T))
{
std::ostringstream s;
s << "Expected PyArray '" << argname << "' with stride " << sizeof(T) << ", encountered stride " << stride;
PyErr_SetString(PyExc_ValueError, s.str().c_str());
Py_INCREF(PyExc_ValueError);
}
return stride == sizeof(T);
}
static PyObject* quality_1vs1(PyObject *self, PyObject *args)
{
PyArrayObject *env_obj, *r1_obj, *r2_obj;
if (!PyArg_ParseTuple(args, "OOO", &env_obj, &r1_obj, &r2_obj))
return NULL;
if (!validate_stride<double>(env_obj,"env") || !validate_stride<double>(r1_obj,"r1") || !validate_stride<double>(r2_obj,"r2"))
return NULL;
npy_intp *env_dims = PyArray_DIMS(env_obj);
npy_intp *r1_dims = PyArray_DIMS(r1_obj);
npy_intp *r2_dims = PyArray_DIMS(r2_obj);
PyArrayObject *q = (PyArrayObject*)PyArray_ZEROS(1, r1_dims, NPY_DOUBLE, 0);
for (int n=0; n<r1_dims[0]; ++n)
{
*(npy_double*)PyArray_GETPTR1(q,n) = trueskill::quality_1vs1(
(npy_double*)PyArray_GETPTR1(env_obj,0),
(npy_double*)PyArray_GETPTR1(r1_obj,n),
(npy_double*)PyArray_GETPTR1(r2_obj,n)
);
}
return Py_BuildValue("N",q);
}
static PyObject* likelihood_outcome_1vs1(PyObject *self, PyObject *args)
{
PyArrayObject *env_obj, *score12_obj, *r1_obj, *r2_obj;
if (!PyArg_ParseTuple(args, "OOOO", &env_obj, &score12_obj, &r1_obj, &r2_obj))
return NULL;
if (!validate_stride<double>(env_obj,"env") || !validate_stride<double>(r1_obj,"r1") || !validate_stride<double>(r2_obj,"r2"))
return NULL;
npy_intp *n_dims = PyArray_DIMS(score12_obj);
PyArrayObject *L = (PyArrayObject*)PyArray_ZEROS(1, n_dims, NPY_DOUBLE, 0);
for (int n=0; n<n_dims[0]; ++n)
{
*(npy_double*)PyArray_GETPTR1(L,n) = trueskill::likelihood_outcome_1vs1(
(npy_double*)PyArray_GETPTR1(env_obj,0),
*(npy_int*)PyArray_GETPTR1(score12_obj,n),
(npy_double*)PyArray_GETPTR1(r1_obj,n),
(npy_double*)PyArray_GETPTR1(r2_obj,n)
);
}
return Py_BuildValue("N",L);
}
static PyObject* rate_1vs1(PyObject *self, PyObject *args)
{
PyArrayObject *env_obj, *pid1_obj, *pid2_obj, *score12_obj, *ratings_by_pid_obj;
if (!PyArg_ParseTuple(args, "OOOOO", &env_obj, &pid1_obj, &pid2_obj, &score12_obj, &ratings_by_pid_obj))
return NULL;
if (!validate_stride<double>(env_obj,"env") || !validate_stride<double>(ratings_by_pid_obj,"ratings_by_pid"))
return NULL;
npy_intp *n_dims = PyArray_DIMS(pid1_obj);
npy_intp nx2_dims[] = { n_dims[0], 2 };
PyArrayObject *r1 = (PyArrayObject*)PyArray_ZEROS(2, nx2_dims, NPY_DOUBLE, 0);
PyArrayObject *r2 = (PyArrayObject*)PyArray_ZEROS(2, nx2_dims, NPY_DOUBLE, 0);
PyArrayObject *L = (PyArrayObject*)PyArray_ZEROS(1, n_dims, NPY_DOUBLE, 0);
PyArrayObject *nGames1 = (PyArrayObject*)PyArray_ZEROS(1, n_dims, NPY_INT, 0);
PyArrayObject *nGames2 = (PyArrayObject*)PyArray_ZEROS(1, n_dims, NPY_INT, 0);
std::vector<int> gameCount(n_dims[0],0);
for (int n=0; n<n_dims[0]; ++n)
{
int pid1 = *(npy_int*)PyArray_GETPTR1(pid1_obj,n);
int pid2 = *(npy_int*)PyArray_GETPTR1(pid2_obj,n);
int score12 = *(npy_int*)PyArray_GETPTR1(score12_obj,n);
// record prior rating
*(npy_double*)PyArray_GETPTR2(r1,n,trueskill::MU) = *(npy_double*)PyArray_GETPTR2(ratings_by_pid_obj,pid1,trueskill::MU);
*(npy_double*)PyArray_GETPTR2(r1,n,trueskill::SIGMA) = *(npy_double*)PyArray_GETPTR2(ratings_by_pid_obj,pid1,trueskill::SIGMA);
*(npy_double*)PyArray_GETPTR2(r2,n,trueskill::MU) = *(npy_double*)PyArray_GETPTR2(ratings_by_pid_obj,pid2,trueskill::MU);
*(npy_double*)PyArray_GETPTR2(r2,n,trueskill::SIGMA) = *(npy_double*)PyArray_GETPTR2(ratings_by_pid_obj,pid2,trueskill::SIGMA);
// update and record player game count
*(npy_int*)PyArray_GETPTR1(nGames1,n) = gameCount[pid1]++;
*(npy_int*)PyArray_GETPTR1(nGames2,n) = gameCount[pid2]++;
// record likelihood of outcome based on prior rating
*(npy_double*)PyArray_GETPTR1(L,n) = trueskill::likelihood_outcome_1vs1(
(npy_double*)PyArray_GETPTR1(env_obj,0),
score12,
(npy_double*)PyArray_GETPTR1(ratings_by_pid_obj, pid1),
(npy_double*)PyArray_GETPTR1(ratings_by_pid_obj, pid2)
);
// update rating
trueskill::rate_1vs1(
(npy_double*)PyArray_GETPTR1(env_obj,0),
score12,
(npy_double*)PyArray_GETPTR1(ratings_by_pid_obj, pid1),
(npy_double*)PyArray_GETPTR1(ratings_by_pid_obj, pid2)
);
}
return Py_BuildValue("NNNNN",r1,r2,nGames1,nGames2,L);
}
static PyMethodDef PyTrueSkillMethods[] = {
{"quality_1vs1", quality_1vs1, METH_VARARGS, "Calculate match quality for 1vs1 matches"},
{"likelihood_outcome_1vs1", likelihood_outcome_1vs1, METH_VARARGS, "Calculate likelihood of outcomes for 1vs1 matches"},
{"rate_1vs1", rate_1vs1, METH_VARARGS, "Updating ratings for a series of matches"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initpytrueskill(void)
{
(void) Py_InitModule("pytrueskill", PyTrueSkillMethods);
import_array();
}