hist match 2

This program has been disqualified.


Authorrspk
Submission date2013-04-26 23:13:45.227001
Rating6863
Matches played36
Win rate72.22

Source code:

#simple history matcher
#uses P.0,1,and 2 from http://www.ofb.net/~egnor/iocaine.html and 3 history matching predictors
#licatj @ no-spam-pls rpi.edu

import random

wins = ['RS','SP','PR']
		


numMeta = 3 #num of meta strategies for each predictor

if input=='': #first round; initialize everything
	myLastMove = 'X'
	currSet = 0
	score = 0
	allMoves = [] #history of all moves made
	myMoves = []
	hisMoves = []
	output = random.choice(['R','P','S'])
	#initialize strategies
	def hist(allMoves,type):
		if len(allMoves) < 2:
			#print "random"
			return random.choice('RPS')
		maxDepth = 400
		maxLength = 20
		#search backwards
		candStart = len(allMoves)-2 #starting point of the current candidate being considered
		currCand = candStart
		currStr = len(allMoves)-1
		bestStart = candStart
		bestLen = 0
		matchFound = False
		while not matchFound: #search backwards
			if currCand < 0:
				break
			#print "comparing %s and %s" % (allMoves[currCand],allMoves[currStr])
			if allMoves[currCand] == allMoves[currStr]:
				#print "same!"
				currCand -= 1
				currStr -= 1
				if candStart-currCand > bestLen:
					bestLen = candStart-currCand
					bestStart = candStart
					if bestLen >= maxLength:
						matchFound = True
			else:
				#restart search
				currStr = len(allMoves)-1
				candStart -= 1
				currCand = candStart
		#print "best len was " + str(bestLen)
		#print bestStart
		#print "returning " + allMoves[bestStart+1]
		#what we return depends on the format of the history provided
		if type==0:
			return allMoves[bestStart+1][0]
		elif type==1:
			return allMoves[bestStart+1][1]
		else:
			return allMoves[bestStart+1]
	
	def histAll():
		global allMoves
		return hist(allMoves,1)
		
	def histHis(): #only uses opponent's history
		global hisMoves
		return hist(hisMoves,2)
		
	def histMines(): 
		global myMoves
		return hist(myMoves,2)
	
		
	allStrats = [histAll,histMines] #all should return [a,b] where a is in [0,1] and b is 'R','P',or 'S'
	allScores = []
	allPredictions = []
	for i in range(len(allStrats)*numMeta):
		allScores.append(0)
		allPredictions.append('X')
else:
	allMoves.append(myLastMove + input)
	myMoves.append(myLastMove)
	hisMoves.append(input)
	scoreChange = 0
	if (myLastMove+input) in wins:
		score += 1
		scoreChange = 1
	elif (input+myLastMove) in wins:
		score -= 1
		scoreChange = -1
		
	if currSet >= 500 and score < 0:
		output = random.choice(['R','P','S']) #resort to random strategy
	else: #actually try something
		output = 'R'
		for i in range(len(allStrats)):
			for j in range(i, i+numMeta):
				if allPredictions[j] == input:
					allScores[j] += 1
			predBase = allStrats[i]() #update predictions:
			jumpUp = {'R':'S', 'P':'R', 'S':'P'}
			allPredictions[numMeta*i] = predBase #P.0 : assume they'll follow the move exactly predicted
			allPredictions[numMeta*i+1] = jumpUp[predBase] #P.1 - assume they predict we'll try to beat predBase
			allPredictions[numMeta*i+2] = jumpUp[jumpUp[predBase]] #P.2 - assume they predict we'll try to beat jumpUp[predBase]
			#allPredictions[6*i+3] = #P'.0
		#select the best one so far
		i = allScores.index(max(allScores))
		output = {'R':'P', 'P':'S', 'S':'R'}[allPredictions[i]]
		#print "best strat is " + str(i)
	#print allMoves
	#print allScores
	#print "next you'll choose " + allPredictions[i]
	#output = 'R'
		
currSet += 1
myLastMove = output