network2

This program has been disqualified.


Authorkrussikas
Submission date2016-03-27 12:50:02.047012
Rating4412
Matches played115
Win rate42.61

Source code:

#!/usr/bin/python

import random, math, time

class Network:
	def __init__(self, inp, hid, out, stddev, fade):
		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
		self.fade = fade

		for i in range(self.total):
			self.bias[i] = random.gauss(0, 0.5)

		variance = stddev / 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):
		disp = self.inp + self.hid
		for i in range(self.out):
			di = disp + i
			diff = nout[i] - self.cells[di]
			der = diff / (1.0 + self.bcells[di] * self.bcells[di])
			self.bias[di] += der
			self.bias[di] -= self.fade * self.bias[di]
			for j in range(self.inp + self.hid):
				self.mat[di][j] += der * self.cells[j]
				self.mat[di][j] -= self.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 frange(x, end, step):
	lst = []
	while x <= end:
		lst.append(x)
		x += step
	return lst


def prog():
	speed = 1.0
	fade = 0.15
	
	global input, output
	global ioutput, total
	global bots, botoutputs, stats, netwinput, maxindex
	letter2index = {"R": 0, "P": 1, "S": 2}
	index2letter = ["R", "P", "S", "R"]

	hidden = 30
	if input == "" or type(input) != type(""):
		bots = []
		for hid in [hidden]:
			for stddev in [0.5]:
				for forget in [0.35]:
					bots.append(Network(6, hid, 3, stddev, forget))

		stats = [0] * len(bots)
		botoutputs = [-1] * len(bots)
		netwinput = [0.] * 6
		total = 0
		#print fade
	else:
		netwinput[0] = netwinput[1] = netwinput[2] = 0.
		netwinput[3] = netwinput[4] = netwinput[5] = 0.
		netwinput[3 + letter2index[input]] = 1.0
		total += 1

		for i in range(len(stats)):
			if maxindex[i] == letter2index[input]:
				stats[i] += 1
		
		for i in range(len(bots)):
			bots[i].learn(netwinput[3:6])

	best = -1
	maxstats = -1
	netwoutput = []
	maxindex = [-1] * len(bots)
	for i in range(len(bots)):
		if stats[i] > maxstats:
			maxstats = stats[i]
			best = i

		netwoutput.append(bots[i].calc(netwinput))
		maxoutput = -100
		for o in range(len(netwoutput[i])):
			if netwoutput[i][o] > maxoutput:
				maxoutput = netwoutput[i][o]
				maxindex[i] = o

	#print str(total) + ":", stats
	ioutput = maxindex[best] + 1
	output = index2letter[ioutput]


prog()