meta-iocane_v1

This program has been disqualified.


Authorevolvingstuff
Submission date2011-06-20 06:55:35.813080
Rating7594
Matches played432
Win rate72.92

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
    meta_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)]
    meta_prediction = [None for j in range(3)]
    meta_weight = [1 for j in range(3)]
    chosen_prediction = None
    meta_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 chosen_prediction != None:
        if input != chosen_prediction:
            weight_random += 1
        for i in range(3):
            for j in range(3):
                for k in range(markov_depth):
                    weights[i][j][k] *= decay
                    if input == predictions[i][j][k]:
                        weights[i][j][k] += 1

    #decay meta model
    if meta_chosen_prediction != None:
        for i in range(3):
            meta_weight[i] *= meta_decay
            if meta_chosen_prediction == meta_prediction[i]:
                meta_weight[i] += 1
                
    #make new predictions
    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
    
    #choose highest weight (possibly random)
    chosen_prediction = None
    mx = weight_random
    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]

    #meta-iocane
    meta_chosen_prediction = None
    mx = 0
    for i in range(3):
        meta_prediction[i] = chosen_prediction
        for i2 in range(i):
            meta_prediction[i] = shift[shift[meta_prediction[i]]]
        if meta_weight[i] > mx:
            mx = meta_weight[i]
            meta_chosen_prediction = meta_prediction[i]

    #choose action
    if meta_chosen_prediction == None:
        output = choice(['R','P','S'])
    else:
        output = shift[meta_chosen_prediction]