qnResult

Authorkera
Submission date2017-07-19 10:45:20.658050
Rating7322
Matches played327
Win rate72.78

Use rpsrunner.py to play unranked matches on your computer.

Source code:

def calcOutput(hist):
  q = [0 for k in range(3)]
  for i in range(0,3):
    q[i] = 1*w[i][c*9];
    for k in range(0,c*9):
      q[i] = q[i] + hist[k] * w[i][k]
  return q

def cvtHand(a):
  if a == 0:
    return 'R'
  if a == 1:
    return 'P'
  if a == 2:
    return 'S'

def cvtNum(a):
  if a == 'R':
    return 0
  if a == 'P':
    return 1
  if a == 'S':
    return 2

def calcHand(q):
  max = -99999;
  maxId = -1;
  for i in range(0,3):
    if max < q[i]:
      max = q[i]
      maxId = i
  return cvtHand(maxId)

def calcNextHist(op):
  nextHist = []
  nextHist.extend(hist)
  del nextHist[0:9]
  oph = [0 for k in range(3)]
  oph[cvtNum(op)] = 1
  nextHist = nextHist+oph+[0 for k in range(6)]
  return nextHist

def maxA(tmp):
  ret = tmp[0]
  for i in range(1,len(tmp)):
    if ret < tmp[i]:
      ret = tmp[i]
  return ret

def calcTeacher(op):
  teacher = [0 for k in range(3)]
  nHist = calcNextHist(op)
  winHandNum = (cvtNum(op)+1)%3
  loseHandNum = (cvtNum(op)+2)%3
  for i in range(0,3):
    nHist[c*9-6+i] = 1
    if winHandNum == i:
      nHist[c*9-3+0]=1
    elif loseHandNum == i:
      nHist[c*9-3+1]=1
    else:
      nHist[c*9-3+2]=1
    teacher[i] = g*maxA(calcOutput(nHist))
    nHist[c*9-6+i] = nHist[c*9-3+0] = nHist[c*9-3+1] = nHist[c*9-3+2]=0

    if winHandNum == i:
      teacher[i] += 1
    if loseHandNum == i:
      teacher[i] -= 1
  return teacher

def learn(op):
  dw = 0.000001
  idw = 1.0 / dw
  teacher = calcTeacher(op)
  out = calcOutput(hist)
  for i in range(0,3):
    oriError = pow(teacher[i] - out[i],2)/2
    error = pow(teacher[i] - out[i]+dw,2)/2
    w[i][c*9] += (error-oriError) * idw * r
    for j in range(0, c*9):
        w[i][j] += hist[j] * (error-oriError) * idw * r


def updateHist(op, my, h):
  oph = [0 for k in range(3)]
  myh = [0 for k in range(3)]
  reh = [0 for k in range(3)]
  del h[0:9]
  oph[cvtNum(op)] = 1
  myh[cvtNum(my)] = 1
  if (cvtNum(op)+1)%3 == cvtNum(my):
    reh[0] = 1
  elif (cvtNum(op)+2)%3 == cvtNum(my):
    reh[1] = 1
  else:
    reh[2] = 1
  for i in range(0,3):
    h.append(oph[i])
  for i in range(0,3):
    h.append(myh[i])
  for i in range(0,3):
    h.append(reh[i])


if input == '':
  c = 7
  g = 0.5
  r = 1.0/(c*3+1) * 2.0
  w = [[0 for k in range(c*9+1)]for j in range(3)]
  hist = [0 for k in range(9*c)]
else:
  learn(input)
  updateHist(input, oldOutput, hist)

output = calcHand(calcOutput(hist))
oldOutput = output