Update clientSubmitResults()
This commit is contained in:
1 parent
9311fb21cb
commit
2e8993a564
6 files changed
+128
-131
No files matched your search
+2
-3
@@ -525,10 +525,9 @@ def reportResults(arguments, data, wins, losses, draws, crashes, timelosses):
|
|||||||
'testid' : data['test']['id'], 'timeloss' : timelosses,
|
'testid' : data['test']['id'], 'timeloss' : timelosses,
|
||||||
}
|
}
|
||||||
|
|
||||||
url = pathjoin(arguments.server, 'submitResults')
|
url = pathjoin(arguments.server, 'clientSubmitResults')
|
||||||
try: return requests.post(url, data=data, timeout=HTTP_TIMEOUT).text
|
try: return requests.post(url, data=data, timeout=HTTP_TIMEOUT).text
|
||||||
except KeyboardInterrupt: sys.exit()
|
except: print('[NOTE] Unable To Reach Server'); return 'Unable'
|
||||||
except: print('[NOTE] Unable To Reach Server'); return "Unable"
|
|
||||||
|
|
||||||
|
|
||||||
def processCutechess(arguments, data, cutechess, concurrency):
|
def processCutechess(arguments, data, cutechess, concurrency):
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
# #
|
||||||
|
# OpenBench is a chess engine testing framework authored by Andrew Grant. #
|
||||||
|
# <https://github.com/AndyGrant/OpenBench> <andrew@grantnet.us> #
|
||||||
|
# #
|
||||||
|
# OpenBench is free software: you can redistribute it and/or modify #
|
||||||
|
# it under the terms of the GNU General Public License as published by #
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or #
|
||||||
|
# (at your option) any later version. #
|
||||||
|
# #
|
||||||
|
# OpenBench is distributed in the hope that it will be useful, #
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
||||||
|
# GNU General Public License for more details. #
|
||||||
|
# #
|
||||||
|
# You should have received a copy of the GNU General Public License #
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
||||||
|
# #
|
||||||
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def erf_inv(x):
|
||||||
|
a = 8*(math.pi-3)/(3*math.pi*(4-math.pi))
|
||||||
|
y = math.log(1-x*x)
|
||||||
|
z = 2/(math.pi*a) + y/2
|
||||||
|
return math.copysign(math.sqrt(math.sqrt(z*z - y/a) - z), x)
|
||||||
|
|
||||||
|
def phi_inv(p):
|
||||||
|
return math.sqrt(2)*erf_inv(2*p-1)
|
||||||
|
|
||||||
|
|
||||||
|
def bayeselo_to_proba(elo, drawelo):
|
||||||
|
pwin = 1.0 / (1.0 + math.pow(10.0, (-elo + drawelo) / 400.0))
|
||||||
|
ploss = 1.0 / (1.0 + math.pow(10.0, ( elo + drawelo) / 400.0))
|
||||||
|
pdraw = 1.0 - pwin - ploss
|
||||||
|
return pwin, pdraw, ploss
|
||||||
|
|
||||||
|
def proba_to_bayeselo(pwin, pdraw, ploss):
|
||||||
|
elo = 200 * math.log10(pwin/ploss * (1-ploss)/(1-pwin))
|
||||||
|
drawelo = 200 * math.log10((1-ploss)/ploss * (1-pwin)/pwin)
|
||||||
|
return elo, drawelo
|
||||||
|
|
||||||
|
|
||||||
|
def SPRT(wins, losses, draws, elo0, elo1):
|
||||||
|
|
||||||
|
# Estimate drawelo out of sample. Return LLR = 0.0 if there are not enough
|
||||||
|
# games played yet to compute an LLR. 0.0 will always be an active state
|
||||||
|
if wins > 0 and losses > 0 and draws > 0:
|
||||||
|
N = wins + losses + draws
|
||||||
|
elo, drawelo = proba_to_bayeselo(float(wins)/N, float(draws)/N, float(losses)/N)
|
||||||
|
else: return 0.00
|
||||||
|
|
||||||
|
# Probability laws under H0 and H1
|
||||||
|
p0win, p0draw, p0loss = bayeselo_to_proba(elo0, drawelo)
|
||||||
|
p1win, p1draw, p1loss = bayeselo_to_proba(elo1, drawelo)
|
||||||
|
|
||||||
|
# Log-Likelyhood Ratio
|
||||||
|
return wins * math.log(p1win / p0win) \
|
||||||
|
+ losses * math.log(p1loss / p0loss) \
|
||||||
|
+ draws * math.log(p1draw / p0draw)
|
||||||
|
|
||||||
|
def ELO(wins, losses, draws):
|
||||||
|
|
||||||
|
def _elo(x):
|
||||||
|
if x <= 0 or x >= 1: return 0.0
|
||||||
|
return -400*math.log10(1/x-1)
|
||||||
|
|
||||||
|
# win/loss/draw ratio
|
||||||
|
N = wins + losses + draws;
|
||||||
|
if N == 0: return (0, 0, 0)
|
||||||
|
w = float(wins) / N
|
||||||
|
l = float(losses)/ N
|
||||||
|
d = float(draws) / N
|
||||||
|
|
||||||
|
# mu is the empirical mean of the variables (Xi), assumed i.i.d.
|
||||||
|
mu = w + d/2
|
||||||
|
|
||||||
|
# stdev is the empirical standard deviation of the random variable (X1+...+X_N)/N
|
||||||
|
stdev = math.sqrt(w*(1-mu)**2 + l*(0-mu)**2 + d*(0.5-mu)**2) / math.sqrt(N)
|
||||||
|
|
||||||
|
# 95% confidence interval for mu
|
||||||
|
mu_min = mu + phi_inv(0.025) * stdev
|
||||||
|
mu_max = mu + phi_inv(0.975) * stdev
|
||||||
|
|
||||||
|
return (_elo(mu_min), _elo(mu), _elo(mu_max))
|
||||||
@@ -18,13 +18,9 @@
|
|||||||
# #
|
# #
|
||||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
|
||||||
import re
|
import re, django
|
||||||
import OpenBench.utils
|
import OpenBench.utils, OpenBench.stats, OpenBench.models
|
||||||
|
|
||||||
from django import template
|
|
||||||
|
|
||||||
from OpenBench.models import Test
|
|
||||||
from OpenBench.utils import ELO
|
|
||||||
|
|
||||||
def oneDigitPrecision(value):
|
def oneDigitPrecision(value):
|
||||||
try:
|
try:
|
||||||
@@ -50,7 +46,7 @@ def twoDigitPrecision(value):
|
|||||||
|
|
||||||
def gitDiffLink(test):
|
def gitDiffLink(test):
|
||||||
|
|
||||||
if type(test) != Test:
|
if type(test) != OpenBench.models.Test:
|
||||||
dev = test['dev']['source']
|
dev = test['dev']['source']
|
||||||
base = test['base']['source']
|
base = test['base']['source']
|
||||||
devsha = test['dev']['sha']
|
devsha = test['dev']['sha']
|
||||||
@@ -83,7 +79,7 @@ def longStatBlock(test):
|
|||||||
threads = tokens[0].split('=')[1]
|
threads = tokens[0].split('=')[1]
|
||||||
hash = tokens[1].split('=')[1]
|
hash = tokens[1].split('=')[1]
|
||||||
|
|
||||||
lower, elo, upper = ELO(test.wins, test.losses, test.draws)
|
lower, elo, upper = OpenBench.stats.ELO(test.wins, test.losses, test.draws)
|
||||||
error = max(upper - elo, elo - lower)
|
error = max(upper - elo, elo - lower)
|
||||||
|
|
||||||
elo = twoDigitPrecision(elo)
|
elo = twoDigitPrecision(elo)
|
||||||
@@ -120,7 +116,7 @@ def prettyName(name):
|
|||||||
return name
|
return name
|
||||||
|
|
||||||
|
|
||||||
register = template.Library()
|
register = django.template.Library()
|
||||||
register.filter('oneDigitPrecision', oneDigitPrecision)
|
register.filter('oneDigitPrecision', oneDigitPrecision)
|
||||||
register.filter('twoDigitPrecision', twoDigitPrecision)
|
register.filter('twoDigitPrecision', twoDigitPrecision)
|
||||||
register.filter('gitDiffLink', gitDiffLink)
|
register.filter('gitDiffLink', gitDiffLink)
|
||||||
|
|||||||
+1
-3
@@ -56,9 +56,7 @@ urlpatterns = [
|
|||||||
django.urls.path(r'clientWrongBench/', OpenBench.views.clientWrongBench),
|
django.urls.path(r'clientWrongBench/', OpenBench.views.clientWrongBench),
|
||||||
django.urls.path(r'clientSubmitNPS/', OpenBench.views.clientSubmitNPS),
|
django.urls.path(r'clientSubmitNPS/', OpenBench.views.clientSubmitNPS),
|
||||||
django.urls.path(r'clientSubmitError/', OpenBench.views.clientSubmitError),
|
django.urls.path(r'clientSubmitError/', OpenBench.views.clientSubmitError),
|
||||||
|
django.urls.path(r'clientSubmitResults/', OpenBench.views.clientSubmitResults),
|
||||||
# Links for the Client to work with the Server (DEAD)
|
|
||||||
django.urls.path(r'submitResults/', OpenBench.views.submitResults),
|
|
||||||
|
|
||||||
# Redirect anything else to the Index
|
# Redirect anything else to the Index
|
||||||
django.urls.path(r'', OpenBench.views.index),
|
django.urls.path(r'', OpenBench.views.index),
|
||||||
|
|||||||
+25
-107
@@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
import math, re, requests, random, datetime
|
import math, re, requests, random, datetime
|
||||||
|
|
||||||
import OpenBench.models
|
import OpenBench.models, OpenBench.stats
|
||||||
|
|
||||||
import django.utils.timezone
|
import django.utils.timezone
|
||||||
|
|
||||||
@@ -380,9 +380,9 @@ def workloadDictionary(test, result, machine):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def update(request, user):
|
def updateTest(request, user):
|
||||||
|
|
||||||
# Parse the data from the worker
|
# New results from the Worker
|
||||||
wins = int(request.POST['wins'])
|
wins = int(request.POST['wins'])
|
||||||
losses = int(request.POST['losses'])
|
losses = int(request.POST['losses'])
|
||||||
draws = int(request.POST['draws'])
|
draws = int(request.POST['draws'])
|
||||||
@@ -390,129 +390,47 @@ def update(request, user):
|
|||||||
timeloss = int(request.POST['timeloss'])
|
timeloss = int(request.POST['timeloss'])
|
||||||
games = wins + losses + draws
|
games = wins + losses + draws
|
||||||
|
|
||||||
# Parse the various IDs sent back
|
# Worker knows where to save the results
|
||||||
machineid = int(request.POST['machineid'])
|
machineid = int(request.POST['machineid'])
|
||||||
resultid = int(request.POST['resultid'])
|
resultid = int(request.POST['resultid'])
|
||||||
testid = int(request.POST['testid'])
|
testid = int(request.POST['testid'])
|
||||||
|
|
||||||
# Don't update an finished, deleted, or unappoved test
|
# Prevent updating a finished test
|
||||||
test = Test.objects.get(id=testid)
|
test = Test.objects.get(id=testid)
|
||||||
if test.finished or test.deleted or not test.approved:
|
if test.finished or test.deleted:
|
||||||
raise Exception()
|
return 'Stop'
|
||||||
|
|
||||||
# Updated stats for SPRT and ELO calculation
|
# Compute a new LLR for the updated results
|
||||||
swins = test.wins + wins
|
swins = test.wins + wins
|
||||||
slosses = test.losses + losses
|
slosses = test.losses + losses
|
||||||
sdraws = test.draws + draws
|
sdraws = test.draws + draws
|
||||||
|
WLD = (swins, slosses, sdraws)
|
||||||
|
sprt = OpenBench.stats.SPRT(*WLD, test.elolower, test.eloupper)
|
||||||
|
|
||||||
# New stats for the test
|
# Check for an H0 or H1 being accepted
|
||||||
sprt = SPRT(swins, slosses, sdraws, test.elolower, test.eloupper)
|
|
||||||
passed = sprt > test.upperllr
|
passed = sprt > test.upperllr
|
||||||
failed = sprt < test.lowerllr
|
failed = sprt < test.lowerllr
|
||||||
finished = passed or failed
|
finished = passed or failed
|
||||||
|
|
||||||
# Updating times manually since .update() won't invoke
|
# Update total games played by the Player
|
||||||
updated = timezone.now()
|
Profile.objects.filter(user=user).update(games=F('games') + games)
|
||||||
|
Profile.objects.get(user=user).save()
|
||||||
|
|
||||||
# Update total # of games played for the User
|
# Update the datetime in the Machine
|
||||||
Profile.objects.filter(user=user).update(
|
Machine.objects.get(id=machineid).save()
|
||||||
games=F('games') + games,
|
|
||||||
updated=updated
|
|
||||||
)
|
|
||||||
|
|
||||||
# Update last time we saw a result
|
# Update individual Result entry for the Player
|
||||||
Machine.objects.filter(id=machineid).update(
|
|
||||||
updated=updated
|
|
||||||
)
|
|
||||||
|
|
||||||
# Update for the new results
|
|
||||||
Result.objects.filter(id=resultid).update(
|
Result.objects.filter(id=resultid).update(
|
||||||
games=F('games') + games,
|
games = F('games') + games, wins = F('wins' ) + wins,
|
||||||
wins=F('wins') + wins,
|
losses = F('losses') + losses, draws = F('draws' ) + draws,
|
||||||
losses=F('losses') + losses,
|
crashes = F('crashes') + crashes, timeloss = F('timeloss') + timeloss,
|
||||||
draws=F('draws') + draws,
|
|
||||||
crashes=F('crashes') + crashes,
|
|
||||||
timeloss=F('timeloss') + timeloss,
|
|
||||||
updated=updated
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Finally, update test data and flags
|
# Update the overall test with the new data
|
||||||
Test.objects.filter(id=testid).update(
|
Test.objects.filter(id=testid).update(
|
||||||
games=F('games') + games,
|
games = F('games' ) + games, wins = F('wins' ) + wins,
|
||||||
wins=F('wins') + wins,
|
losses = F('losses') + losses, draws = F('draws') + draws,
|
||||||
losses=F('losses') + losses,
|
currentllr=sprt, passed=passed, failed=failed, finished=finished,
|
||||||
draws=F('draws') + draws,
|
|
||||||
currentllr=sprt,
|
|
||||||
passed=passed,
|
|
||||||
failed=failed,
|
|
||||||
finished=finished,
|
|
||||||
updated=updated
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Signal to stop completed tests
|
return ['None', 'Stop'][finished]
|
||||||
if finished: raise Exception()
|
|
||||||
|
|
||||||
def SPRT(wins, losses, draws, elo0, elo1):
|
|
||||||
|
|
||||||
# Estimate drawelo out of sample. Return LLR = 0.0 if there are not enough
|
|
||||||
# games played yet to compute an LLR. 0.0 will always be an active state
|
|
||||||
if wins > 0 and losses > 0 and draws > 0:
|
|
||||||
N = wins + losses + draws
|
|
||||||
elo, drawelo = proba_to_bayeselo(float(wins)/N, float(draws)/N, float(losses)/N)
|
|
||||||
else: return 0.00
|
|
||||||
|
|
||||||
# Probability laws under H0 and H1
|
|
||||||
p0win, p0draw, p0loss = bayeselo_to_proba(elo0, drawelo)
|
|
||||||
p1win, p1draw, p1loss = bayeselo_to_proba(elo1, drawelo)
|
|
||||||
|
|
||||||
# Log-Likelyhood Ratio
|
|
||||||
return wins * math.log(p1win / p0win) \
|
|
||||||
+ losses * math.log(p1loss / p0loss) \
|
|
||||||
+ draws * math.log(p1draw / p0draw)
|
|
||||||
|
|
||||||
def bayeselo_to_proba(elo, drawelo):
|
|
||||||
pwin = 1.0 / (1.0 + math.pow(10.0, (-elo + drawelo) / 400.0))
|
|
||||||
ploss = 1.0 / (1.0 + math.pow(10.0, ( elo + drawelo) / 400.0))
|
|
||||||
pdraw = 1.0 - pwin - ploss
|
|
||||||
return pwin, pdraw, ploss
|
|
||||||
|
|
||||||
def proba_to_bayeselo(pwin, pdraw, ploss):
|
|
||||||
elo = 200 * math.log10(pwin/ploss * (1-ploss)/(1-pwin))
|
|
||||||
drawelo = 200 * math.log10((1-ploss)/ploss * (1-pwin)/pwin)
|
|
||||||
return elo, drawelo
|
|
||||||
|
|
||||||
def erf_inv(x):
|
|
||||||
a = 8*(math.pi-3)/(3*math.pi*(4-math.pi))
|
|
||||||
y = math.log(1-x*x)
|
|
||||||
z = 2/(math.pi*a) + y/2
|
|
||||||
return math.copysign(math.sqrt(math.sqrt(z*z - y/a) - z), x)
|
|
||||||
|
|
||||||
def phi_inv(p):
|
|
||||||
# Quantile function for the standard Gaussian law: probability -> quantile
|
|
||||||
assert(0 <= p and p <= 1)
|
|
||||||
return math.sqrt(2)*erf_inv(2*p-1)
|
|
||||||
|
|
||||||
def ELO(wins, losses, draws):
|
|
||||||
|
|
||||||
def _elo(x):
|
|
||||||
if x <= 0 or x >= 1: return 0.0
|
|
||||||
return -400*math.log10(1/x-1)
|
|
||||||
|
|
||||||
# win/loss/draw ratio
|
|
||||||
N = wins + losses + draws;
|
|
||||||
if N == 0: return (0, 0, 0)
|
|
||||||
w = float(wins) / N
|
|
||||||
l = float(losses)/ N
|
|
||||||
d = float(draws) / N
|
|
||||||
|
|
||||||
# mu is the empirical mean of the variables (Xi), assumed i.i.d.
|
|
||||||
mu = w + d/2
|
|
||||||
|
|
||||||
# stdev is the empirical standard deviation of the random variable (X1+...+X_N)/N
|
|
||||||
stdev = math.sqrt(w*(1-mu)**2 + l*(0-mu)**2 + d*(0.5-mu)**2) / math.sqrt(N)
|
|
||||||
|
|
||||||
# 95% confidence interval for mu
|
|
||||||
mu_min = mu + phi_inv(0.025) * stdev
|
|
||||||
mu_max = mu + phi_inv(0.975) * stdev
|
|
||||||
|
|
||||||
return (_elo(mu_min), _elo(mu), _elo(mu_max))
|
|
||||||
+8
-9
@@ -569,20 +569,19 @@ def clientSubmitError(request):
|
|||||||
|
|
||||||
return HttpResponse('None')
|
return HttpResponse('None')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
@not_minified_response
|
@not_minified_response
|
||||||
def submitResults(request):
|
def clientSubmitResults(request):
|
||||||
|
|
||||||
# Verify that we got a valid login
|
# Verify the User's credentials
|
||||||
user = django.contrib.auth.authenticate(
|
user = django.contrib.auth.authenticate(
|
||||||
username=request.POST['username'],
|
username=request.POST['username'],
|
||||||
password=request.POST['password'])
|
password=request.POST['password'])
|
||||||
if user == None: return HttpResponse('Stop')
|
if user == None: return HttpResponse('Bad Credentials')
|
||||||
|
|
||||||
# Try to update each location
|
# Verify the Machine belongs to the User
|
||||||
try: OpenBench.utils.update(request, user)
|
machine = Machine.objects.get(id=int(request.POST['machineid']))
|
||||||
except: return HttpResponse('Stop')
|
if machine.owner != user.username: return HttpResponse('Bad Machine')
|
||||||
|
|
||||||
return HttpResponse('None')
|
# updateTest() will return 'None' or 'Stop'
|
||||||
|
return HttpResponse(OpenBench.utils.updateTest(request, user))
|
||||||
Reference in new issue
Block a user