Forever winner

 Author pepenacho Submission date 2016-04-07 19:17:48.898815 Rating 1627 Matches played 422 Win rate 17.06

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

Source code:

``````#!/usr/bin/env python
#no uso RPS sino WLD (win-lose-draw),
#asumo que el otro programa esta respondiendo a mi input
#WLD se calcula relativo a su input inmediato anterior
import random
import math
def myResult(myInput, hisInput):
if(myInput == "R"):
if(hisInput == "R"):
return "D"
if(hisInput == "P"):
return "L"
if(hisInput == "S"):
return "W"
if(myInput == "P"):
if(hisInput == "R"):
return "W"
if(hisInput == "P"):
return "D"
if(hisInput == "S"):
return "L"
if(myInput == "S"):
if(hisInput == "R"):
return "L"
if(hisInput == "P"):
return "W"
if(hisInput == "S"):
return "D"

def whatDoes(action,to):
if(action == "W"):
if(to == "R"):
return "P"
if(to == "P"):
return "S"
if(to == "S"):
return "R"
if(action == "L"):
if(to == "R"):
return "S"
if(to == "P"):
return "R"
if(to == "S"):
return "P"
if(action =="D"):

class Modelo:
self.contextos = {}
self.contextoActual=""
#agrega char al contexto actual
def agregarChar(self, c):
self.contextoActual+=c

if( len(self.contextoActual) > self.cantidadCaracts):
self.contextoActual = self.contextoActual[pos:]

if not (self.contextoActual in self.contextos):
self.contextos[self.contextoActual] = {}
if c in self.contextos[self.contextoActual]:
self.contextos[self.contextoActual][c] += 1
else:
self.contextos[self.contextoActual][c] = 1

def mostrar(self):
for contexto,tabla in self.contextos.iteritems():
for label,cantidad in tabla.iteritems():
print "en ctx "+contexto+" N("+label+")="+str(cantidad)
print "--------------------------"

def valido(self):
return self.contextoActual in self.contextos
#escupe la posibilidad del char segun el contexto actual

if not self.contextoActual in self.contextos:
return 0;

total = 0
for letra, veces in self.contextos[self.contextoActual].iteritems():
total += veces
if(c in self.contextos[self.contextoActual]):
return self.contextos[self.contextoActual][c]/total
else:
return 0

#escupe el char mas posible segun el contexto actualizado
def proximoMasProbable(self):
if not self.contextoActual in self.contextos:
return "H";

masprobable="W"
for label in self.contextos[self.contextoActual]:
masprobable=label
#print "soy "+str(self.cantidadCaracts)+" y creo que va a salir "+masprobable
#print "contexto: "+self.contextoActual
#self.mostrar()

return masprobable

class MultiModel:
def __init__(self, max_model, simbolos):
self.models=[]
for i in range(0,max_model):
self.models.append(Modelo(i+1))
self.weights=[max_model-i+1 for i in range(max_model)]
self.simbolos=simbolos

for i in range(len(self.models)):
modelo=self.models[i]
previsto = modelo.proximoMasProbable()
self.weights[i]+=1
else:
self.weights[i]-=1

if(self.weights[i]<1):
self.weights[i]=1
#if previsto==whatDoes("

def agregarChar(self,c):
for m in self.models:
m.agregarChar(c)

def mostrar(self):
mejor=0
print "-----------------------"
for i in range(len(self.weights)):

if self.weights[i]>self.weights[mejor]:
mejor = i
print str(self.weights[i])+" "+str(i)
#print "el que mas me gusta es "+ str(mejor)

def probabilidad(self, c):#es un promedio
p=0
total = 0
for i in range(len(self.models)):
m = self.models[i]
a=len(self.models)
w = self.weights[i]
if m.valido():
total +=1*w
"""

w = 1#self.weights[i]
if m.valido():
total += w
p+=dp
"""

if total == 0:
return 0
return p/total

def proximoMasProbable(self):
posibles=self.simbolos#SOLO ANDA CON RPS!
masprobable="R"
for label in posibles:
masprobable = label

return masprobable
"""
class Neuron:
def __init__(self, inputs):
self.weights=[random() for i in range(inputs)]

def weight(self, inputno):
total = sum(self.weights)
return self.weights[inputno]/total

def positiveStimuli(self):
for i in range(len(self.weights)):
self.weights[i]=math.exp(self.weights[i])

def negativeStimuli(self):
for i in range(len(self.weights)):
self.weights[i]=self.weights[i]**-0.1#hmmmmmm busco algo que no impacte muy duo en las bajitas y le de un saque a las altas!

"""

initialize = 1
if(input==""):
prevInput="caca"
prevoutput="R"
initialize = 0
multiModelo=MultiModel(6,["R","P","S"])
multiModeloRes=MultiModel(6,["W","L","D"])

if(input !=""):
inputReal = input#myResult(input, prevInput)
#input = whatDoes(inputReal, prevInput)
multiModelo.aprenderRPS(inputReal)
multiModelo.agregarChar(inputReal)

#modelo1.agregarChar(inputReal)
#modelo2.agregarChar(inputReal)

#modelo1.mostrar()
masprobable = multiModelo.proximoMasProbable()#modelo1.proximoMasProbable()
multiModeloRes.agregarChar(myResult(prevoutput,inputReal))
accionMasProbable=multiModeloRes.proximoMasProbable()
recontraProbable=whatDoes(accionMasProbable,masprobable)
#output = whatDoes("L", recontraProbable)
output =  recontraProbable
multiModelo.mostrar()
print "malo dice" + input +" bueno dice "+prevoutput
prevoutput = output

if(input==""):
modelo1 = Modelo(1)
prevInput ="R"#asumo que el anterior fue roca porque es mejor
output="R"
else:
prevInput = input``````