iocane-antidote_v8

This program has been disqualified.


Authorevolvingstuff
Submission date2011-06-19 08:32:07.618171
Rating7514
Matches played242
Win rate73.55

Source code:

from random import *

def findMatchMe(history_me, history_yu, depth):
    prefix = history_me[-depth:]
    i = history_me[:-1].rfind(prefix)
    if i > -1:
        return history_yu[i+depth]
    else:
        return None

def findMatchYu(history_yu, depth):
    prefix = history_yu[-depth:]
    i = history_yu[:-1].rfind(prefix)
    if i > -1:
        return history_yu[i+depth]
    else:
        return None

def findMatchUs(history_us, depth):
    prefix = history_us[-depth*2:]
    i = history_us[:-2].rfind(prefix)
    if i > -1:
        return history_us[i+depth*2].upper()
    else:
        return None

if input == "":
    shift = {None:None, 'R':'P', 'P':'S', 'S':'R'}
    initial_random_moves = 10
    decay = 0.6
    history_me, history_yu, history_us = '', '', ''
    markov_depth = 13
    weight_random = 1
    weights = [[[1 for k in range(markov_depth)] for j in range(3)] for i in range(3)]
    predictions = [[[None for k in range(markov_depth)] for j in range(3)] for i in range(3)]
    chosen_prediction = None
    output = choice(['R','P','S'])
    chosen_i, chosen_j, chosen_k = -1, -1, -1
else:
    #store histories
    history_me += output
    history_yu += input
    history_us += input.lower() + output
    
    #decay models and update weights based on input observation
    weight_random *= decay
    if input != chosen_prediction:
        weight_random += 1
    for i in range(3):
        for j in range(3):
            for k in range(markov_depth):
                if chosen_prediction != None:
                    weights[i][j][k] *= decay
                    if input == predictions[i][j][k]:
                        weights[i][j][k] += 1
                
    #make new predictions
    tot_weight = 0
    tot_weight += weight_random
    prediction = None
    for i in range(3):
        for j in range(3):
            longest_prediction = None
            for k in range(markov_depth-1):
                predictions[i][j][k] = None
                prediction = None
                if i == 0:
                    prediction = findMatchMe(history_me, history_yu, k+1)
                elif i == 1:
                    prediction = findMatchYu(history_yu, k+1)
                else:
                    prediction = findMatchUs(history_us, k+1)
                for j2 in range(j): #iocane-shift
                    prediction = shift[shift[prediction]]
                if prediction != None:
                    predictions[i][j][k] = prediction
                    longest_prediction = prediction
                    tot_weight += weights[i][j][k]
            if longest_prediction != None:
                predictions[i][j][markov_depth-1] = longest_prediction
                tot_weight += weights[i][j][markov_depth-1]
    
    chosen_prediction = None
    #choose highest weight
    mx = 0
    for i in range(3):
        for j in range(3):
            for k in range(markov_depth):
                if predictions[i][j][k] != None and weights[i][j][k] > mx:
                    mx = weights[i][j][k]
                    chosen_prediction = predictions[i][j][k]

    #choose action
    if len(history_me) < initial_random_moves or chosen_prediction == None:
        output = choice(['R','P','S'])
    else:
        output = shift[chosen_prediction]