This program has been disqualified.
Author | krussikas |
Submission date | 2016-03-23 14:00:56.024620 |
Rating | 2637 |
Matches played | 135 |
Win rate | 23.7 |
#!/usr/bin/python
import random, math, time
class Network:
def __init__(self, inp, hid, out):
self.mat = []
self.total = inp + hid + out
self.bias = [0.] * self.total
self.bcells = [0.] * self.total
self.cells = [0.] * self.total
self.inp = inp
self.hid = hid
self.out = out
for i in range(self.total):
self.bias[i] = random.gauss(0, 0.5)
variance = 1.0 / math.sqrt(self.total)
for y in range(self.total):
row = []
for x in range(self.total):
row.append(random.gauss(0, variance))
self.mat.append(row)
def learn(self, nout, speed, fade):
disp = self.inp + self.hid
for i in range(self.out):
di = disp + i
diff = nout[i] - self.cells[di]
der = speed / (1.0 + self.bcells[di] * self.bcells[di])
self.bias[di] += der
self.bias[di] -= fade * self.bias[di]
for j in range(self.inp + self.hid):
self.mat[di][j] += der * self.cells[j]
self.mat[di][j] -= fade * self.mat[di][j]
def calc(self, ninput):
for i in range(self.inp):
self.cells[i] = ninput[i]
self.bcells = [0.] * self.total
for i in range(self.inp, self.inp + self.hid):
self.bcells[i] = self.bias[i]
for j in range(self.inp + self.hid):
self.bcells[i] += self.cells[j] * self.mat[i][j]
for i in range(self.inp, self.inp + self.hid):
self.cells[i] = math.atan(self.bcells[i])
for i in range(self.inp + self.hid, self.total):
self.bcells[i] = self.bias[i]
for j in range(self.inp + self.hid):
self.bcells[i] += self.cells[j] * self.mat[i][j]
return self.bcells[-self.out:]
def prog():
speed = 0.1
fade = 0.05
global input, output
global ioutput
global netw, netwinput
letter2index = {"R": 0, "P": 1, "S": 2}
index2letter = ["R", "P", "S", "R"]
hidden = 30
if input == "" or type(input) != type(""):
netw = Network(6, hidden, 3)
netwinput = [0., 0., 0., 0., 0., 0.]
#fade = (time.time() - 1458741477.03602) * 0.001
print fade
#input = "R"
#ioutput = 0
#prog()
else:
netwinput[0] = netwinput[1] = netwinput[2] = 0.
netwinput[ioutput] = 1.0
netwinput[3] = netwinput[4] = netwinput[5] = 0.
netwinput[3 + letter2index[input]] = 1.0
netw.learn(netwinput[3:6], speed, fade)
netwoutput = netw.calc(netwinput)
maxoutput = -100
for o in range(len(netwoutput)):
if netwoutput[o] > maxoutput:
maxoutput = netwoutput[o]
maxindex = o
ioutput = maxindex + 1
output = index2letter[ioutput]
prog()