iocane-antidote_v7c

This program has been disqualified.


Authorevolvingstuff
Submission date2011-06-19 07:50:37.435397
Rating8126
Matches played20
Win rate85.0

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):
            for k in range(markov_depth):
                predictions[i][j][k] = 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
                    tot_weight += weights[i][j][k]
    
    chosen_prediction = None
    #stochastically choose prediction (must ignore non-predicting predictors)
    if random() < weight_random / tot_weight:
        chosen_prediction = None
    else:
        tot_weight -= weight_random
        done = False
        for i in range(3):
            if done:
                break
            for j in range(3):
                if done:
                    break
                for k in range(markov_depth):
                    if predictions[i][j][k] != None:
                        if random() < weights[i][j][k]/tot_weight:
                            chosen_prediction = predictions[i][j][k]
                            done = True
                            break
                        else:
                            tot_weight -= weights[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]